-
Notifications
You must be signed in to change notification settings - Fork 8
/
m-userdata.tmpl
87 lines (67 loc) · 2.71 KB
/
m-userdata.tmpl
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
#! /bin/bash
# Note: terraform will attempt to replace $ variables with variables passed to this.
# Thus don't use shell $ variables!
# Let the first master node setup mysql and k3s; other master nodes wait before setup
[ ${inst-id} -ne 0 ] && sleep 60
# aws cli v1 install; region setup, etc
DEBIAN_FRONTEND=noninteractive apt update && apt -y install zip unzip python3 python3-venv python-is-python3
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
rm -rf awscli-bundle
export AWS_REGION=`curl --silent http://169.254.169.254/latest/meta-data/placement/region`
export AWS_DEFAULT_REGION=`curl --silent http://169.254.169.254/latest/meta-data/placement/region`
# Prepare kubectl for root
sudo mkdir ~root/.kube
sudo chmod 750 ~root/.kube
# Basic install of k3s.
curl -sfL https://get.k3s.io | sh -s - server \
--datastore-endpoint="mysql://admin:${pwd}@tcp(${host}:3306)/k3s" \
--disable traefik \
--token ${token}
sudo snap install helm --classic
# Wait for k3s.yaml to appear
while ! sudo [ -e /etc/rancher/k3s/k3s.yaml ]
do
sleep 5
done
# Allow root to have superuser over the cluster:
sudo cp /etc/rancher/k3s/k3s.yaml ~root/.kube/config
# Wait for all kube-system deployments to roll out
for d in $(sudo kubectl get deploy -n kube-system --no-headers -o name)
do
sudo kubectl -n kube-system rollout status $d
done
# Display kubeconfig to console.
[ ${inst-id} -eq 0 -a "${kubeconfig-console}" = "true" ] && {
ss=/etc/cron.daily/kube-config-to-console.sh
cat > $ss << EOF
#!/bin/bash
kc=~root/.kube/config
[ -r \$kc ] && {
echo ===================================================================== > /dev/console
echo Cluster kubeconfig: > /dev/console
echo ===================================================================== > /dev/console
cat \$kc > /dev/console
echo ===================================================================== > /dev/console
echo End cluster kubeconfig > /dev/console
}
EOF
chmod 755 $ss
$ss
}
# Save kubeconfig as an ssm parameter.
[ ${inst-id} -eq 0 -a "${kubeconfig-ssm}" = "true" ] && {
aws ssm put-parameter --name ${prefix}-kubeconfig --value "`cat ~root/.kube/config`" --type String
}
# nginx-ingress install. To replace traefik which broke (was deployed as a daemonset but overtime changed to deployment).
sudo helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
sudo helm repo update
# Force ingress-nginx install as a daemonset
sudo helm install ingress-nginx ingress-nginx/ingress-nginx \
-n ingress-nginx \
--create-namespace \
--set kind=DaemonSet
# finally how does k3s look?
sudo kubectl get nodes
sudo kubectl get all -A