-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes_provision.sh
285 lines (229 loc) · 9.13 KB
/
kubernetes_provision.sh
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
# set -e
master_node_ports=('6443/tcp' '2379:2380/tcp' '10250/tcp' '10251/tcp' '10252/tcp' '10255/tcp' 'ssh')
worker_node_ports=('10250/tcp' '10251/tcp' '10255/tcp' 'ssh')
log_message(){
if [[ $? -eq 0 ]];then
printf "\e[0;32m \u2713 Success: $1 \e[0m \n"
else
printf "\e[0;31m \u2713 Failed: $1 \e[0m \n"
exit 1
fi
}
setup_firewall(){
for port in ${SELECTED_UFW_NODE[@]}
do
sudo ufw allow $port
done
log_message "UFW setup for kubernetes network"
sudo ufw enable
log_message "UFW enabled"
sudo ufw reload
log_message "UFW reload"
}
request_node_hostname(){
read -p "Enter node hostname: " node
while [[ -z $node ]];do
read -p "Enter node hostname: " node
done
}
install_node_options(){
echo "Choose node target mode"
install_app_options=( "master" "worker" "quit")
select opt in "${install_app_options[@]}"
do
case $opt in
1|master)
request_node_hostname
export NODE_MODE=$node
export SELECTED_UFW_NODE=${master_node_ports[@]}
break
;;
2|worker)
request_node_hostname
export NODE_MODE=$node
export SELECTED_UFW_NODE=${worker_node_ports[@]}
break
;;
3|quit)
exit
break
;;
*) echo "invalid option $REPLY";;
esac
done
}
setup_docker_systemd(){
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf > /dev/null 2>&1
overlay
br_netfilter
EOF
log_message "containerd config add"
sudo modprobe overlay > /dev/null 2>&1
sudo modprobe br_netfilter > /dev/null 2>&1
log_message "containerd config apply"
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf > /dev/null 2>&1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
log_message "CRI conf add"
# refresh system without boot
sudo sysctl --system > /dev/null 2>&1
log_message "system reset"
# install supportive packages
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg2 -y > /dev/null 2>&1
log_message "Install ca-certificates curl software-properties-common gnupg2"
# # Add docker
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key --keyring /etc/apt/trusted.gpg.d/docker.gpg add - > /dev/null 2>&1
# sudo add-apt-repository \
# "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
# $(lsb_release -cs) \
# stable" -y > /dev/null 2>&1
# log_message "Install docker"
# install containerd
sudo apt update && sudo apt install containerd -y > /dev/null 2>&1
log_message "install containerd.io"
sudo mkdir -p /etc/containerd > /dev/null 2>&1
sudo containerd config default > /etc/containerd/config.toml
log_message "containered config copied to /etc/containerd/config.toml"
sudo systemctl restart containerd > /dev/null 2>&1
log_message "restart containerd"
sudo systemctl enable containerd > /dev/null 2>&1
log_message "enable containerd"
# ADDED CGROUPS TO /etc/containerd/config.toml
## To use the systemd cgroup driver in /etc/containerd/config.toml with runc, set
# sed -i'' -e '/containerd.runtimes.runc\]/{
# N;N;N;N;a\
# [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]\
# SystemdCgroup = true
# }' /etc/containerd/config.toml
# sed -i 's/\[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options\]/&\n\t systemd_cgroup=true/' /etc/containerd/config.toml
sed -i -e 's/SystemdCgroup.*/SystemdCgroup=true/' /etc/containerd/config.toml
# sudo nano /etc/containerd/config.toml
log_message '
Add the systemd cgroup driver in /etc/containerd/config.toml with runc
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
...
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
'
sudo modprobe overlay > /dev/null 2>&1
sudo modprobe br_netfilter > /dev/null 2>&1
log_message "containerd config reapply"
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf > /dev/null 2>&1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
log_message "Add CRI cof to /etc/sysctl.d/99-kubernetes-cri.conf"
sudo sysctl --system > /dev/null 2>&1
log_message "system reset"
#cri version
VERSION=1.22
OS=xUbuntu_20.10
cat <<EOF | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list > /dev/null 2>&1
deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /
EOF
log_message "add kubectl to apt repo"
cat <<EOF | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.list > /dev/null 2>&1
deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/ /
EOF
log_message "add cri to apt repo"
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | sudo apt-key --keyring /etc/apt/trusted.gpg.d/libcontainers.gpg add - > /dev/null 2>&1
log_message "download kube"
wait $!
curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/Release.key | sudo apt-key --keyring /etc/apt/trusted.gpg.d/libcontainers-cri-o.gpg add - > /dev/null 2>&1
log_message "download cri"
sudo apt update > /dev/null 2>&1
log_message "system update"
sudo apt install -y jq cri-o-runc
curl https://raw.githubusercontent.com/cri-o/cri-o/main/scripts/get | bash > /dev/null 2>&1
log_message "Install cri-o and cri-o-runc"
sudo systemctl daemon-reload > /dev/null 2>&1
log_message "system daemon reload"
sudo systemctl start crio > /dev/null 2>&1
log_message "start crio"
sudo systemctl enable crio > /dev/null 2>&1
log_message "enable crio"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key --keyring /etc/apt/trusted.gpg.d/docker.gpg add - > /dev/null 2>&1
log_message "adding docker apt keys"
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable" -y > /dev/null 2>&1
log_message "downloading docker..."
sudo apt update && sudo apt install docker-ce docker-ce-cli -y > /dev/null 2>&1
log_message "install docker-ce docker-ce-cli"
sudo cat <<EOF | sudo tee /etc/docker/daemon.json > /dev/null 2>&1
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
log_message "docker conf add to /etc/docker/daemon.json"
sudo mkdir -p /etc/systemd/system/docker.service.d > /dev/null 2>&1
log_message "dir create /etc/systemd/system/docker.service.d"
sudo cp -r /lib/systemd/system/docker.service /etc/systemd/system/docker.service.d > /dev/null 2>&1
log_message "docker conf copy from /lib/systemd/system/docker.service"
sudo systemctl daemon-reload > /dev/null 2>&1
log_message "system daemon reload"
sudo systemctl restart docker > /dev/null 2>&1
log_message "docker restart"
sudo systemctl enable docker > /dev/null 2>&1
log_message "docker enable"
}
setup_kubernetes(){
sudo apt update > /dev/null 2>&1
log_message "System update"
sudo swapoff --all
log_message "Swap off"
sudo sed -i '/ swap / s/^/#/' /etc/fstab
log_message "Swap off persisted"
#sudo hostnamectl set-hostname master-node > /dev/null 2&>1
sudo hostnamectl set-hostname $NODE_MODE
log_message "set hostname $NODE_MODE"
# INSTALL KUBERNETES
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add > /dev/null 2>&1
log_message "add kubernetes apt key"
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" -y > /dev/null 2>&1
log_message "add kubernetes repo"
sudo apt install kubeadm kubelet kubectl -y > /dev/null 2>&1
log_message "download kubeadm kubelet kubectl"
sudo apt-mark hold kubeadm kubelet kubectl > /dev/null 2>&1
log_message "hold updates kubeadm kubelet kubectl"
}
init(){
echo -e "\e[106m this kubernetes metal server setup script is designed for Ubuntu_18.04 VM that runs with systemd \e[0m"
echo -e "\e[208m IT MUST RUN WITH SUDO PRIVILGES \e[0m"
echo -e "\e[1;35m *** if you are not in root mode please exit and run [sudo -i] first *** \e[0m"
install_node_options
wait $!
setup_docker_systemd
setup_kubernetes
setup_firewall
}
init
echo -e " \e[44m ALL GOOD \e[0m
TO initialize a cluster plane NOW RUN:\n
\e[38;5;44m
$ sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --ignore-preflight-errors=all --v=5 --cri-socket /var/run/dockershim.sock \
--apiserver-advertise-address=master-machine-ip
\e[0m \n
THEN INSTALL NETWROKING FLANNEL --pod-network-cidr=10.244.0.0/16 :\n
\e[38;5;44m $ sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml \e[0m \n
OR CALICO:\n
\e[38;5;44m
# one liner
$ sudo kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml \n
# or docs version
$ sudo kubectl create -f https://docs.projectcalico.org/manifests/tigera-operator.yaml \n
$ sudo kubectl create -f https://docs.projectcalico.org/manifests/custom-resources.yaml \n
\e[0m
IF YOU WANT TO JOIN YOUR WORKER NODES:\n
\e[38;5;44m $ kubeadm join --discovery-token .... --discovery-token-ca-cert-hash ... \e[0m
"