This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmaster.tf
110 lines (95 loc) · 3.96 KB
/
master.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
resource "scaleway_ip" "k8s_master_ip" {
count = 1
}
resource "scaleway_server" "k8s_master" {
count = 1
name = "${terraform.workspace}-master-${count.index + 1}"
image = "${data.scaleway_image.ubuntu.id}"
type = "${var.server_type}"
public_ip = "${element(scaleway_ip.k8s_master_ip.*.ip, count.index)}"
security_group = "${scaleway_security_group.master_security_group.id}"
connection {
type = "ssh"
user = "root"
private_key = "${file(var.private_key)}"
}
provisioner "file" {
source = "scripts/"
destination = "/tmp"
}
provisioner "file" {
source = "addons/"
destination = "/tmp"
}
provisioner "file" {
source = "kubeadm"
destination = "/tmp/"
}
provisioner "remote-exec" {
inline = [
<<EOT
#!/bin/bash
set -e
chmod +x /tmp/docker-install.sh
chmod +x /tmp/kubeadm-install.sh
chmod g+w -R /tmp/kubeadm/
export ubuntu_version=$(echo -n ${var.ubuntu_version} | cut -d " " -f 2 | awk '{print tolower($0)}')
/tmp/docker-install.sh $${ubuntu_version} ${var.arch} ${var.docker_version} && \
/tmp/kubeadm-install.sh ${var.k8s_version} && \
modify_kube_apiserver_config(){
while [[ ! -e /etc/kubernetes/manifests/kube-apiserver.yaml ]]; do
sleep 0.5s;
done && \
sed -i 's/failureThreshold: [0-9]/failureThreshold: 18/g' /etc/kubernetes/manifests/kube-apiserver.yaml && \
sed -i 's/timeoutSeconds: [0-9][0-9]/timeoutSeconds: 20/g' /etc/kubernetes/manifests/kube-apiserver.yaml && \
sed -i 's/initialDelaySeconds: [0-9][0-9]/initialDelaySeconds: 240/g' /etc/kubernetes/manifests/kube-apiserver.yaml
}
# ref https://github.com/kubernetes/kubeadm/issues/413 (initialDelaySeconds is too eager)
if [[ ${var.arch} == "arm" ]]; then modify_kube_apiserver_config & fi
export KUBEADM_VERSION=$(apt-cache madison kubeadm | grep $(echo ${var.k8s_version} | cut -c8-) | \
awk 'NR==1 {print $3}' | rev | cut -c4- | rev)
dpkg --compare-versions "$${KUBEADM_VERSION}" lt 1.13 && \
export KUBEADM_CONFIG_FILE=/tmp/kubeadm/v1alpha3-config.yaml || \
export KUBEADM_CONFIG_FILE=/tmp/kubeadm/v1beta1-config.yaml
dpkg --compare-versions "$${KUBEADM_VERSION}" lt 1.12 && \
export KUBEADM_CONFIG_FILE=""
dpkg --compare-versions "$${KUBEADM_VERSION}" lt 1.11 && \
export VERBOSITY_EXTRA_ARGS='' || \
export VERBOSITY_EXTRA_ARGS='--v ${var.kubeadm_verbosity}'
if [[ -z "$${KUBEADM_CONFIG_FILE}" ]]; then
kubeadm init \
--apiserver-advertise-address=${self.private_ip} \
--apiserver-cert-extra-sans=${self.public_ip} \
--kubernetes-version=${var.k8s_version} \
--ignore-preflight-errors=KubeletVersion \
$${VERBOSITY_EXTRA_ARGS};
else
sed -i 's/CONFIG_CLUSTER_PUBLIC_IP/${self.public_ip}/g' $${KUBEADM_CONFIG_FILE} && \
sed -i 's/CONFIG_CLUSTER_PRIVATE_IP/${self.private_ip}/g' $${KUBEADM_CONFIG_FILE} && \
sed -i "s/CONFIG_KUBERNETES_VERSION/v$${KUBEADM_VERSION}/g" $${KUBEADM_CONFIG_FILE} && \
sed -i "s/CONFIG_CONTAINER_LOG_MAX_SIZE/${var.container_log_max_size}/" $${KUBEADM_CONFIG_FILE}
kubeadm init \
--ignore-preflight-errors=KubeletVersion \
--config=$${KUBEADM_CONFIG_FILE} \
$${VERBOSITY_EXTRA_ARGS};
fi && \
mkdir -p $HOME/.kube && cp -i /etc/kubernetes/admin.conf $HOME/.kube/config && \
kubectl create secret -n kube-system generic weave-passwd --from-literal=weave-passwd=${var.weave_passwd} && \
kubectl apply -f "https://cloud.weave.works/k8s/net?password-secret=weave-passwd&k8s-version=$(kubectl version | base64 | tr -d '\n')" && \
chmod +x /tmp/monitoring-install.sh && /tmp/monitoring-install.sh ${var.arch}
EOT
]
}
provisioner "local-exec" {
command = "./scripts/kubectl-conf.sh ${terraform.workspace} ${self.public_ip} ${self.private_ip} ${var.private_key}"
on_failure = "continue"
}
}
data "external" "kubeadm_join" {
program = ["./scripts/kubeadm-token.sh"]
query = {
host = "${scaleway_ip.k8s_master_ip.0.ip}"
key = "${var.private_key}"
}
depends_on = ["scaleway_server.k8s_master"]
}