-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathk3s-multi-node-installer.sh.tpl
122 lines (95 loc) · 2.97 KB
/
k3s-multi-node-installer.sh.tpl
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
#!/bin/bash
set -euo pipefail
declare -A hosts
{{- range .nodes }}
hosts[{{ .Hostname }}]={{ .Type }}
{{- end }}
HOSTNAME=$(cat /etc/hostname)
if [ ! "$HOSTNAME" ]; then
HOSTNAME=$(cat /proc/sys/kernel/hostname)
if [ ! "$HOSTNAME" ] || [ "$HOSTNAME" = "localhost.localdomain" ]; then
echo "ERROR: Could not identify whether the host is a k3s server or agent due to missing hostname"
exit 1
fi
fi
NODETYPE="${hosts[$HOSTNAME]:-none}"
if [ "$NODETYPE" = "none" ]; then
echo "ERROR: Could not identify whether host '$HOSTNAME' is a k3s server or agent"
exit 1
fi
mount /var
mkdir -p /var/lib/rancher/k3s/agent/images/
cp {{ .imagesPath }}/* /var/lib/rancher/k3s/agent/images/
umount /var
CONFIGFILE={{ .configFilePath }}/$NODETYPE.yaml
if [ "$HOSTNAME" = {{ .initialiser }} ]; then
CONFIGFILE={{ .configFilePath }}/{{ .initialiserConfigFile }}
{{ if .manifestsPath }}
mkdir -p /opt/eib-k8s/manifests
cp {{ .manifestsPath }}/* /opt/eib-k8s/manifests/
cat <<- 'EOF' > /opt/eib-k8s/create_manifests.sh
#!/bin/bash
failed=false
for file in /opt/eib-k8s/manifests/*; do
output=$(/opt/bin/kubectl create -f "$file" --kubeconfig=/etc/rancher/k3s/k3s.yaml 2>&1)
if [ $? != 0 ]; then
while IFS= read -r line; do
if [[ "$line" != *"AlreadyExists"* ]]; then
failed=true
fi
done <<< "$output"
fi
echo "$output"
done
if [ $failed = "true" ]; then
exit 1
fi
EOF
chmod +x /opt/eib-k8s/create_manifests.sh
cat <<- EOF > /etc/systemd/system/kubernetes-resources-install.service
[Unit]
Description=Kubernetes Resources Install
Requires=k3s.service
After=k3s.service
ConditionPathExists=/opt/bin/kubectl
ConditionPathExists=/etc/rancher/k3s/k3s.yaml
[Install]
WantedBy=multi-user.target
[Service]
Type=oneshot
Restart=on-failure
RestartSec=60
ExecStartPre=/bin/sh -c 'until [ "\$(systemctl show -p SubState --value k3s.service)" = "running" ]; do sleep 10; done'
ExecStart=/opt/eib-k8s/create_manifests.sh
# Disable the service and clean up
ExecStartPost=/bin/sh -c "systemctl disable kubernetes-resources-install.service"
ExecStartPost=rm -f /etc/systemd/system/kubernetes-resources-install.service
ExecStartPost=rm -rf /opt/eib-k8s
EOF
systemctl enable kubernetes-resources-install.service
{{- end }}
fi
{{- if and .apiVIP4 .apiHost }}
echo "{{ .apiVIP4 }} {{ .apiHost }}" >> /etc/hosts
{{- end }}
{{- if and .apiVIP6 .apiHost }}
echo "{{ .apiVIP6 }} {{ .apiHost }}" >> /etc/hosts
{{- end }}
mkdir -p /etc/rancher/k3s/
cp $CONFIGFILE /etc/rancher/k3s/config.yaml
if [ "$NODETYPE" = "server" ]; then
{{- if .setNodeIPScript }}
sh {{ .setNodeIPScript }}
{{- end }}
fi
if [ -f {{ .registryMirrors }} ]; then
cp {{ .registryMirrors }} /etc/rancher/k3s/registries.yaml
fi
export INSTALL_K3S_EXEC=$NODETYPE
export INSTALL_K3S_SKIP_DOWNLOAD=true
export INSTALL_K3S_SKIP_START=true
export INSTALL_K3S_BIN_DIR=/opt/bin
mkdir -p $INSTALL_K3S_BIN_DIR
cp {{ .binaryPath }} $INSTALL_K3S_BIN_DIR/k3s
chmod +x $INSTALL_K3S_BIN_DIR/k3s
sh {{ .installScript }}