-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
192 lines (159 loc) · 4.36 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
resource "random_string" "kube-encryption" {
length = 32
special = false
}
data "template_file" "kube-encryption" {
template = file("./templates/kube-encryption.yml.tpl")
vars = {
encryption_key = base64encode(random_string.kube-encryption.result)
}
}
resource "local_file" "kube-encryption" {
content = data.template_file.kube-encryption.rendered
filename = "certs/encryption-config.yaml"
}
resource "google_compute_address" "ip_address" {
name = "k8s-hard-way"
}
resource "tls_private_key" "ssh-key" {
algorithm = "RSA"
rsa_bits = 2048
}
resource "local_file" "ssh-key" {
content = tls_private_key.ssh-key.private_key_pem
file_permission = 0600
filename = "./ssh-key.pem"
}
resource "google_compute_instance" "controller" {
count = 3
name = "controller-${count.index}"
machine_type = "e2-medium"
zone = "europe-west1-b"
can_ip_forward = "true"
boot_disk {
initialize_params {
image = "ubuntu-2004-lts"
}
}
network_interface {
network = "k8s-hard-way"
subnetwork = "k8s-hard-way"
network_ip = "10.120.0.1${count.index}"
access_config {
// Ephemeral IP
}
}
metadata = {
ssh-keys = "terraform:${tls_private_key.ssh-key.public_key_openssh}"
}
}
resource "null_resource" "cluster" {
count = 3
# Changes to any instance of the cluster requires re-provisioning
triggers = {
cluster_instance_ids = join(",", google_compute_instance.controller.*.id)
}
# Bootstrap script can run on any instance of the cluster
# So we just choose the first in this case
connection {
type = "ssh"
user = "terraform"
private_key = tls_private_key.ssh-key.private_key_pem
host = element(google_compute_instance.controller.*.network_interface.0.access_config.0.nat_ip, count.index)
}
provisioner "file" {
source = "certs/"
destination = "/tmp/"
}
provisioner "file" {
source = "bin/"
destination = "/tmp/"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/etcd_bootstrap.sh",
"/tmp/etcd_bootstrap.sh"
]
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/control_plane_bootstrap.sh",
"/tmp/control_plane_bootstrap.sh"
]
}
}
resource "null_resource" "rbac" {
# Changes to any instance of the cluster requires re-provisioning
triggers = {
cluster_instance_ids = join(",", google_compute_instance.controller.*.id)
}
# Bootstrap script can run on any instance of the cluster
# So we just choose the first in this case
connection {
type = "ssh"
user = "terraform"
private_key = tls_private_key.ssh-key.private_key_pem
host = element(google_compute_instance.controller.*.network_interface.0.access_config.0.nat_ip, 0)
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/rbac.sh",
"/tmp/rbac.sh"
]
}
depends_on = [
null_resource.cluster
]
}
resource "google_compute_instance" "worker" {
count = 3
name = "worker-${count.index}"
machine_type = "e2-medium"
zone = "europe-west1-b"
can_ip_forward = "true"
boot_disk {
initialize_params {
image = "ubuntu-2004-lts"
}
}
network_interface {
network = "k8s-hard-way"
subnetwork = "k8s-hard-way"
access_config {
// Ephemeral IP
}
}
metadata = {
"ssh-keys" = "terraform:${tls_private_key.ssh-key.public_key_openssh}"
"pod-cidr" = "10.200.${count.index}.0/24"
}
}
resource "null_resource" "workers" {
count = 3
# Changes to any instance of the cluster requires re-provisioning
triggers = {
cluster_instance_ids = join(",", google_compute_instance.worker.*.id)
}
# Bootstrap script can run on any instance of the cluster
# So we just choose the first in this case
connection {
type = "ssh"
user = "terraform"
private_key = tls_private_key.ssh-key.private_key_pem
host = element(google_compute_instance.worker.*.network_interface.0.access_config.0.nat_ip, count.index)
}
provisioner "file" {
source = "certs/"
destination = "/tmp/"
}
provisioner "file" {
source = "bin/"
destination = "/tmp/"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/worker_bootstrap.sh",
"/tmp/worker_bootstrap.sh"
]
}
}