-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathuser-data.tftpl
72 lines (64 loc) · 2.92 KB
/
user-data.tftpl
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
#!/bin/bash
# Networking requirements
# https://kubernetes.io/docs/setup/production-environment/container-runtimes/#install-and-configure-prerequisites
cat <<EOF >/etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF >/etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
# Install containerd
# https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
# https://github.com/containerd/containerd/blob/main/docs/getting-started.md#option-2-from-apt-get-or-dnf
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" >/etc/apt/sources.list.d/docker.list
apt-get update
apt-get install -y containerd.io
# Configure containerd to use the systemd cgroup driver
# https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd-systemd
mkdir -p /etc/containerd
containerd config default >/etc/containerd/config.toml
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
systemctl restart containerd
# Install Kubernetes tools
# https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#installing-kubeadm-kubelet-and-kubectl
apt-get install -y apt-transport-https
curl -fsSLo /etc/apt/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" >/etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubeadm kubelet kubectl
apt-mark hold kubelet kubeadm kubectl
%{ if node == "master" ~}
#------------------------------------------------------------------------------#
# Master: initialise cluster
#------------------------------------------------------------------------------#
kubeadm init \
--token "${token}" \
--token-ttl 15m \
--apiserver-cert-extra-sans "${master_public_ip}" \
%{~ if cidr != null ~}
--pod-network-cidr "${cidr}" \
%{~ endif ~}
--node-name master
# Prepare kubeconfig file for downloading
cp /etc/kubernetes/admin.conf /home/ubuntu
chown ubuntu:ubuntu /home/ubuntu/admin.conf
kubectl --kubeconfig /home/ubuntu/admin.conf config set-cluster kubernetes --server "https://${master_public_ip}:6443"
%{~ endif ~}
%{~ if node == "worker" ~}
#------------------------------------------------------------------------------#
# Worker: join cluster
#------------------------------------------------------------------------------#
kubeadm join "${master_private_ip}:6443" \
--token "${token}" \
--discovery-token-unsafe-skip-ca-verification \
--node-name worker-${worker_index}
%{~ endif }
# Indicate completion
touch /home/ubuntu/done