forked from jpetazzo/ampernetacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudinit.tf
164 lines (154 loc) · 4.22 KB
/
cloudinit.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
locals {
packages = [
"apt-transport-https",
"build-essential",
"ca-certificates",
"curl",
"docker.io",
"jq",
"kubeadm",
"kubelet",
"lsb-release",
"make",
"prometheus-node-exporter",
"python3-pip",
"software-properties-common",
"tmux",
"tree",
"unzip",
]
}
data "cloudinit_config" "_" {
for_each = local.nodes
part {
filename = "cloud-config.cfg"
content_type = "text/cloud-config"
content = <<-EOF
hostname: ${each.value.node_name}
package_update: true
package_upgrade: false
packages:
${yamlencode(local.packages)}
apt:
sources:
kubernetes.list:
source: "deb https://apt.kubernetes.io/ kubernetes-xenial main"
key: |
${indent(8, data.http.apt_repo_key.body)}
users:
- default
- name: k8s
primary_group: k8s
groups: docker
home: /home/k8s
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ${tls_private_key.ssh.public_key_openssh}
write_files:
- path: /etc/kubeadm_token
owner: "root:root"
permissions: "0600"
content: ${local.kubeadm_token}
- path: /etc/kubeadm_config.yaml
owner: "root:root"
permissions: "0600"
content: |
kind: InitConfiguration
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- token: ${local.kubeadm_token}
---
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
cgroupDriver: cgroupfs
---
kind: ClusterConfiguration
apiVersion: kubeadm.k8s.io/v1beta2
apiServer:
certSANs:
- @@PUBLIC_IP_ADDRESS@@
- path: /home/k8s/.ssh/id_rsa
defer: true
owner: "k8s:k8s"
permissions: "0600"
content: |
${indent(4, tls_private_key.ssh.private_key_pem)}
- path: /home/k8s/.ssh/id_rsa.pub
defer: true
owner: "k8s:k8s"
permissions: "0600"
content: |
${indent(4, tls_private_key.ssh.public_key_openssh)}
EOF
}
# By default, all inbound traffic is blocked
# (except SSH) so we need to change that.
part {
filename = "allow-inbound-traffic.sh"
content_type = "text/x-shellscript"
content = <<-EOF
#!/bin/sh
sed -i "s/-A INPUT -j REJECT --reject-with icmp-host-prohibited//" /etc/iptables/rules.v4
netfilter-persistent start
EOF
}
dynamic "part" {
for_each = each.value.role == "controlplane" ? ["yes"] : []
content {
filename = "kubeadm-init.sh"
content_type = "text/x-shellscript"
content = <<-EOF
#!/bin/sh
PUBLIC_IP_ADDRESS=$(curl https://icanhazip.com/)
sed -i s/@@PUBLIC_IP_ADDRESS@@/$PUBLIC_IP_ADDRESS/ /etc/kubeadm_config.yaml
kubeadm init --config=/etc/kubeadm_config.yaml --ignore-preflight-errors=NumCPU
export KUBECONFIG=/etc/kubernetes/admin.conf
kubever=$(kubectl version | base64 | tr -d '\n')
kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$kubever
mkdir -p /home/k8s/.kube
cp $KUBECONFIG /home/k8s/.kube/config
chown -R k8s:k8s /home/k8s/.kube
EOF
}
}
dynamic "part" {
for_each = each.value.role == "worker" ? ["yes"] : []
content {
filename = "kubeadm-join.sh"
content_type = "text/x-shellscript"
content = <<-EOF
#!/bin/sh
kubeadm join --discovery-token-unsafe-skip-ca-verification --token ${local.kubeadm_token} ${local.nodes[1].ip_address}:6443
EOF
}
}
}
data "http" "apt_repo_key" {
url = "https://packages.cloud.google.com/apt/doc/apt-key.gpg.asc"
}
# The kubeadm token must follow a specific format:
# - 6 letters/numbers
# - a dot
# - 16 letters/numbers
resource "random_string" "token1" {
length = 6
number = true
lower = true
special = false
upper = false
}
resource "random_string" "token2" {
length = 16
number = true
lower = true
special = false
upper = false
}
locals {
kubeadm_token = format(
"%s.%s",
random_string.token1.result,
random_string.token2.result
)
}