forked from kelseyhightower/kubestack
-
Notifications
You must be signed in to change notification settings - Fork 10
/
kubernetes.tf
217 lines (206 loc) · 9.79 KB
/
kubernetes.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
resource "null_resource" "discovery_url_template" {
provisioner "local-exec" {
command = "curl -s 'https://discovery.etcd.io/new?size=1' > templates/discovery_url"
}
}
resource "null_resource" "generate_ssl" {
count = "${var.generate_ssl}"
provisioner "local-exec" {
command = "bash files/ssl/generate-ssl.sh"
}
}
resource "template_file" "discovery_url" {
template = "templates/discovery_url"
depends_on = [
"null_resource.discovery_url_template"
]
}
resource "template_file" "controller_cloud_init" {
template = "templates/cloud-init"
vars {
flannel_network = "${var.flannel_network}"
flannel_backend = "${var.flannel_backend}"
etcd_servers = "http://127.0.0.1:2379"
cluster_token = "${var.cluster_name}"
discovery_url = "${template_file.discovery_url.rendered}"
}
}
resource "template_file" "compute_cloud_init" {
template = "templates/cloud-init"
vars {
flannel_network = "${var.flannel_network}"
flannel_backend = "${var.flannel_backend}"
etcd_servers = "${join(",", "${formatlist("http://%s:2379", openstack_compute_instance_v2.controller.*.network.0.fixed_ip_v4)}")}"
cluster_token = "${var.cluster_name}"
discovery_url = "${template_file.discovery_url.rendered}"
}
}
resource "openstack_networking_floatingip_v2" "controller" {
count = "1"
pool = "${var.floatingip_pool}"
}
resource "openstack_networking_floatingip_v2" "compute" {
count = "${var.compute_count}"
pool = "${var.floatingip_pool}"
}
resource "openstack_compute_keypair_v2" "kubernetes" {
name = "${var.project}"
public_key = "${file(var.public_key_path)}"
}
resource "openstack_compute_instance_v2" "controller" {
name = "${var.cluster_name}-controller${count.index}"
count = "1"
image_name = "${var.kubernetes_image}"
flavor_name = "${var.kubernetes_flavor}"
key_pair = "${openstack_compute_keypair_v2.kubernetes.name}"
network {
name = "${var.network_name}"
}
security_groups = [
"${openstack_compute_secgroup_v2.kubernetes_base.name}",
"${openstack_compute_secgroup_v2.kubernetes_controller.name}"
]
floating_ip = "${element(openstack_networking_floatingip_v2.controller.*.address, count.index)}"
user_data = "${template_file.controller_cloud_init.rendered}"
provisioner "file" {
source = "files"
destination = "/tmp/stage"
connection {
user = "core"
}
}
provisioner "remote-exec" {
inline = [
"sudo mkdir -p /opt/bin",
"sudo wget -q -O /opt/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/${var.kubectl_version}/bin/linux/amd64/kubectl",
"sudo chmod 0755 /opt/bin/kubectl",
"sudo mkdir -p /etc/kubernetes/ssl",
"cd /tmp/stage/ssl",
"echo \"subjectAltName = @alt_names\n[alt_names]\" >> openssl.cnf",
"echo DNS.1 = kubernetes >> openssl.cnf",
"echo DNS.2 = kubernetes.local >> openssl.cnf",
"echo DNS.3 = ${element(openstack_networking_floatingip_v2.controller.*.address, count.index)}.xip.io >> openssl.cnf",
"echo 'IP.1 = ${element(openstack_networking_floatingip_v2.controller.*.address, count.index)}' >> openssl.cnf",
"echo 'IP.3 = ${cidrhost(var.portal_net, count.index + 1)}' >> openssl.cnf",
"echo 'IP.2 = ${self.network.0.fixed_ip_v4}' >> openssl.cnf",
"openssl genrsa -out controller-key.pem 2048",
"openssl req -new -key controller-key.pem -out controller.csr -subj '/CN=kubernetes-controller' -config openssl.cnf",
"openssl x509 -req -in controller.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out controller.pem -days 365 -extensions v3_req -extfile openssl.cnf",
"sudo mv controller*.pem /etc/kubernetes/ssl",
"sudo mv admin*.pem /etc/kubernetes/ssl",
"sudo mv ca.pem /etc/kubernetes/ssl",
"sudo chown root:core /etc/kubernetes/ssl/*; sudo chmod 0640 /etc/kubernetes/ssl/*-key.pem",
"sed -i 's/MY_IP/${self.network.0.fixed_ip_v4}/' /tmp/stage/*/*",
"sed -i 's/ADVERTISE_IP/${element(openstack_networking_floatingip_v2.controller.*.address, count.index)}/' /tmp/stage/*/*",
"sed -i 's|PORTAL_NET|${var.portal_net}|' /tmp/stage/*/*",
"sed -i 's|CLUSTER_DNS|${cidrhost(var.portal_net, 200)}|' /tmp/stage/*/*",
"sed -i 's|HYPERKUBE_VERSION|${var.hyperkube_version}|' /tmp/stage/*/*",
"sudo mkdir -p /etc/kubernetes/manifests",
"sudo mv /tmp/stage/controller/*.yaml /etc/kubernetes/manifests/",
"sudo mv /tmp/stage/controller/*.service /etc/systemd/system/",
"sudo mv /tmp/stage/addons /etc/kubernetes/addons",
"#rm -rf /tmp/stage",
"sudo systemctl daemon-reload",
"sudo systemctl restart docker",
"sudo systemctl enable kube-kubelet",
"sudo systemctl start kube-kubelet",
"echo Wait until API comes online...",
"while ! curl http://127.0.0.1:8080/version; do sleep 60; done",
"curl -XPOST -d'{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"name\":\"kube-system\"}}' \"http://127.0.0.1:8080/api/v1/namespaces\"",
]
connection {
user = "core"
}
}
depends_on = [
"template_file.controller_cloud_init",
"null_resource.generate_ssl",
]
}
resource "openstack_compute_instance_v2" "compute" {
name = "${var.cluster_name}-compute${count.index}"
count = "${var.compute_count}"
image_name = "${var.kubernetes_image}"
flavor_name = "${var.kubernetes_flavor}"
floating_ip = "${element(openstack_networking_floatingip_v2.compute.*.address, count.index)}"
key_pair = "${openstack_compute_keypair_v2.kubernetes.name}"
network {
name = "${var.network_name}"
}
security_groups = [
"${openstack_compute_secgroup_v2.kubernetes_base.name}",
"${openstack_compute_secgroup_v2.kubernetes_compute.name}"
]
user_data = "${template_file.compute_cloud_init.rendered}"
provisioner "file" {
source = "files"
destination = "/tmp/stage"
connection {
user = "core"
}
}
provisioner "remote-exec" {
inline = [
"sudo mkdir -p /opt/bin",
"sudo wget -q -O /opt/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/${var.kubectl_version}/bin/linux/amd64/kubectl",
"sudo chmod 0755 /opt/bin/kubectl",
"sudo mkdir -p /etc/kubernetes/ssl",
"cd /tmp/stage/ssl",
"echo \"subjectAltName = @alt_names\n[alt_names]\" >> openssl.cnf",
"echo 'IP.1 = ${self.network.0.fixed_ip_v4}' >> openssl.cnf",
"openssl genrsa -out compute-key.pem 2048",
"openssl req -new -key compute-key.pem -out compute.csr -subj '/CN=kubernetes-compute' -config openssl.cnf",
"openssl x509 -req -in compute.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out compute.pem -days 365 -extensions v3_req -extfile openssl.cnf",
"sudo mv compute-key.pem /etc/kubernetes/ssl",
"sudo mv compute.pem /etc/kubernetes/ssl",
"sudo mv ca.pem /etc/kubernetes/ssl",
"sudo chown root:core /etc/kubernetes/ssl/*; sudo chmod 0640 /etc/kubernetes/ssl/*-key.pem",
"sed -i 's/MY_IP/${self.network.0.fixed_ip_v4}/' /tmp/stage/*/*",
"sed -i 's/ADVERTISE_IP/${element(openstack_networking_floatingip_v2.compute.*.address, count.index)}/' /tmp/stage/*/*",
"sed -i 's/CONTROLLER_HOST/${openstack_compute_instance_v2.controller.0.network.0.fixed_ip_v4}/' /tmp/stage/*/*",
"sed -i 's|PORTAL_NET|${var.portal_net}|' /tmp/stage/*/*",
"sed -i 's|CLUSTER_DNS|${cidrhost(var.portal_net, 200)}|' /tmp/stage/*/*",
"sudo mkdir -p /etc/kubernetes/manifests",
"sudo mv /tmp/stage/compute/*.yaml /etc/kubernetes/manifests/",
"sudo mv /tmp/stage/compute/*.service /etc/systemd/system/",
"sudo mv /tmp/stage/compute/compute-kubeconfig.yaml.config /etc/kubernetes/compute-kubeconfig.yaml",
"#rm -rf /tmp/stage",
"sudo systemctl daemon-reload",
"sudo systemctl restart docker",
"sudo systemctl enable kube-kubelet",
"sudo systemctl start kube-kubelet",
]
connection {
user = "core"
}
}
depends_on = [
"template_file.compute_cloud_init",
"openstack_compute_instance_v2.controller"
]
}
resource "null_resource" "controller" {
provisioner "remote-exec" {
inline = [
"/opt/bin/kubectl config set-cluster ${var.cluster_name} --certificate-authority=/etc/kubernetes/ssl/ca.pem \\",
" --server=https://${openstack_compute_instance_v2.controller.0.network.0.fixed_ip_v4}:443",
"/opt/bin/kubectl config set-credentials ${var.kubernetes_user} \\",
" --certificate-authority=/etc/kubernetes/ssl/ca.pem \\",
" --client-key=/etc/kubernetes/ssl/admin-key.pem \\",
" --client-certificate=/etc/kubernetes/ssl/admin.pem",
"/opt/bin/kubectl config set-context ${var.kubernetes_user} --cluster=${var.cluster_name} --user=${var.kubernetes_user}",
"/opt/bin/kubectl config set-context kubernetes --cluster=${var.cluster_name} --user=${var.kubernetes_user}",
]
connection {
user = "core"
host = "${openstack_networking_floatingip_v2.controller.0.address}"
}
}
depends_on = [
"openstack_compute_instance_v2.controller",
"openstack_compute_instance_v2.compute",
]
}
output "kubernetes-controller" {
value = "$ ssh -A core@${openstack_networking_floatingip_v2.controller.0.address}"
}