-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon
532 lines (446 loc) · 18.8 KB
/
common
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#! /bin/bash
# =============================================================================
# NAME: display_section
# DESC: display a section / title on the output
# =============================================================================
# $1 message to display
# =============================================================================
function display_section() {
# echo -e "\033[0;1;4m$1\033[0;0m"
printf "\033[0;1;4m%s\033[0;0m\n" "$1"
}
# =============================================================================
# NAME: display_message
# DESC: display a message / title on the output
# =============================================================================
# $1 message
# $2 status
# =============================================================================
function display_message() {
case "${2^^}" in
"OK") COLOR="\033[0;32m";;
"KO") COLOR="\033[0;31m";;
"ERROR") COLOR="\033[0;31m";;
"WARN") COLOR="\033[0;33m";;
"INFO") COLOR="\033[1;34m";;
esac
printf "$(date '+%Y-%m-%d %H:%M:%S') [$COLOR${2}\033[0;0m] - $(hostname): $1\n"
}
# =============================================================================
# NAME: display_message_result
# DESC: display a message / title on the output using the last command result
# as OK / KO status flag
# =============================================================================
# $1 message
# =============================================================================
function display_message_result() {
if [ "$?" -eq 0 ];
then
display_message "$1" "OK"
else
display_message "$1" "KO"
exit 1
fi
}
# =============================================================================
# NAME: common_install
# DESC: install the kubernetes commands like kubeadm and kubectl
# =============================================================================
function common_install() {
display_section "Configure repositories & install packages"
apt update > /dev/null 2>&1
display_message_result "Updating package repository"
apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates > /dev/null 2>&1
display_message_result "Installing base needed packages"
mkdir -p /etc/apt/keyrings
rm -f /etc/apt/trusted.gpg.d/docker.gpg
curl -fsSL "https://download.docker.com/linux/$(lsb_release -a 2> /dev/null|grep 'Distributor ID:'|cut -d$'\t' -f2|tr '[:upper:]' '[:lower:]')/gpg" | gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
display_message_result "Adding docker package repository signature"
add-apt-repository --yes "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$(lsb_release -a 2> /dev/null|grep "Distributor ID:"|cut -d$'\t' -f2|tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable"
display_message_result "Adding docker package repository"
rm -f /etc/apt/keyrings/kubernetes-apt-keyring.gpg
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key -a 2> /dev/null| gpg --dearmour -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
display_message_result "Adding google package repository signature"
chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | tee /etc/apt/sources.list.d/kubernetes.list > /dev/null
display_message_result "Adding google package repository"
chmod 644 /etc/apt/sources.list.d/kubernetes.list
apt update > /dev/null 2>&1
display_message_result "Updating package repository"
apt install -y kubectl > /dev/null 2>&1
display_message_result "Installing kubectl"
apt-mark hold kubectl > /dev/null 2>&1
display_message_result "Freezing kubernetes tools version"
}
# =============================================================================
# NAME: common_config
# DESC: install the kubernetes commands like kubeadm and kubectl, then configure
# host to support kubernetes node requierments
# =============================================================================
function common_config() {
common_install
display_section "Configure system"
swapoff -a
display_message_result "Disabling swap in this session"
sed -i '/swap/d' /etc/fstab
display_message_result "Disabling swap in this file /etc/fstab"
modprobe overlay
display_message_result "Enabling module overlay"
modprobe br_netfilter
display_message_result "Enabling module br_netfilter"
cat >>/etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
display_message_result "Enabling module load for containerd"
apt install -y containerd.io kubelet kubeadm kubectl > /dev/null 2>&1
display_message_result "Installing containerd & kubernetes tools"
apt-mark hold kubelet kubeadm kubectl > /dev/null 2>&1
display_message_result "Freezing kubernetes tools version"
containerd config default > /etc/containerd/config.toml
display_message_result "Configuring containerd 1#2"
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
display_message_result "Configuring containerd 2#2"
systemctl restart containerd
display_message_result "Restarting containerd"
systemctl enable containerd
display_message_result "Enabling containerd"
cat >>/etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
display_message_result "Configuring network for kubernetes"
sysctl --system > /dev/null 2>&1
display_message_result "Applying system configuration"
swapoff -a
}
# =============================================================================
# NAME: configure_master
# DESC: configure a master node
# =============================================================================
# $1 : mode (alone or multi or single)
# =============================================================================
function configure_master() {
display_section "Configure kubernetes"
kube_status=""
if [ -f /etc/kubernetes/admin.conf ];
then
kube_status=$(kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes 2> /dev/null|grep "$(hostname)")
fi
if [ -n "${kube_status}" ];
then
display_message "Master node already exists" "OK"
else
if [ "${KUBE_FIRST_MASTER_FLAG}" == "1" ];
then
cat << EOF > ./kubeadm-config.yaml
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
networking:
podSubnet: "${KUBE_NETWORK_CIDR}"
EOF
if [ "$1" == "multi" ];
then
cat << EOF >> ./kubeadm-config.yaml
kubernetesVersion: stable
apiServer:
certSANs:
- ${KUBE_ADDRESS}
controlPlaneEndpoint: "${KUBE_ADDRESS}"
EOF
fi
systemctl restart kubelet
kubeadm init --config=./kubeadm-config.yaml >> "$LOG_FILE" 2>&1
display_message_result "Starting master node"
rm -f kubeadm-config.yaml
mkdir -p "$HOME/.kube"
cp -i /etc/kubernetes/admin.conf "$HOME/.kube/config"
chown "$(id -u):$(id -g)" "$HOME/.kube/config"
display_message_result "Writting kubernetes config file to $HOME/.kube/config"
NET_URL=""
case "${KUBE_NETWORK_TYPE}" in
"flannel") NET_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml";;
*) display_message "Unsupported network type - switching to default (flannel)" "WARN";
NET_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml";;
esac
if [ -n "${NET_URL}" ];
then
kubectl apply -f "${NET_URL}" >> "$LOG_FILE" 2>&1
display_message_result "Loading network configuration into cluster"
fi
if [ "$1" == "alone" ];
then
kubectl taint node $(hostname) node-role.kubernetes.io/control-plane:NoSchedule- > /dev/null 2>&1
display_message_result "Allowing pods on master because of standalone node"
# waiting for master Ready
display_message "Waiting for node $(hostname) condition=Ready during timeout=600s" "INFO"
kubectl wait --for=condition=Ready node $(hostname) --all --timeout=600s > /dev/null
# check if node is Ready
is_node_ready "$(hostname)"
if [ "$?" -eq 0 ]; then
display_message "Your cluster is Ready" "OK"
else
display_message "Your cluster is not Ready" "KO"
fi
# ensure default account created
ensure_default_account_created
# run a pod test
test_pod_helloworld
fi
generate_config_archive
else
display_message "Next master" "INFO"
load_config_archive
kubeadm join "${KUBE_API_URL}" --token "${KUBE_MASTER_TOKEN}" --discovery-token-ca-cert-hash "${KUBE_DISCOVERY_HASH}" --control-plane > /dev/null 2>&1
display_message_result "Master joining cluster"
mkdir -p "$HOME/.kube"
cp -i /etc/kubernetes/admin.conf "$HOME/.kube/config"
chown "$(id -u):$(id -g)" "$HOME/.kube/config"
display_message_result "Writting kubernetes config file to $HOME/.kube/config"
fi
fi
}
# =============================================================================
# NAME: status_master
# DESC: read master status
# =============================================================================
function status_master() {
node_status=$(kubectl get nodes 2> /dev/null|grep "$(hostname)")
if [ -n "${node_status}" ];
then
echo "installed"
else
echo "not installed"
fi
}
# =============================================================================
# NAME: test_pod_helloworld
# DESC: run pod pod-helloworld.yml as test file
# =============================================================================
function test_pod_helloworld() {
# create hello yaml file
cat >pod-helloworld.yml <<EOF
apiVersion: v1
kind: Pod
metadata:
name: pod-helloworld
spec:
containers:
- name: container-helloworld
image: alpine
command: ["/bin/sh"]
args: ["-c", "echo hello world; sleep infinity"]
restartPolicy: OnFailure
EOF
# create pod helloworld
kubectl delete --grace-period=1 -f pod-helloworld.yml > /dev/null 2>/dev/null
kubectl create -f pod-helloworld.yml > /dev/null
display_message_result "create pod-helloworld"
# todo check if Ready=false is correct
# try --for=condition=Completed has failed
#
# debug command
# kubectl describe pod/pod-helloworld |grep Condition -A 5
# Conditions:
# Type Status
# Initialized True
# Ready False
# ContainersReady False
# PodScheduled True
#
# wait for main lifecycle
# from https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/
# wait for PodScheduled
#kubectl wait --for=condition=PodScheduled pod/pod-helloworld
#kubectl wait --for=condition=Initialized pod/pod-helloworld
#kubectl wait --for=condition=Ready=False pod/pod-helloworld
kubectl wait --for=condition=Ready pod/pod-helloworld > /dev/null
hello=$(kubectl logs --pod-running-timeout=20s pod/pod-helloworld |grep "hello")
display_message_result "pod-helloworld says $hello"
kubectl delete --grace-period=1 -f pod-helloworld.yml > /dev/null
display_message_result "delete pod-helloworld"
}
function ensure_default_account_created() {
# The default account is known to take a while to appear; see
# https://github.com/kubernetes/kubernetes/issues/66689
i="0"
while [ $i -lt 10 ]
do
kubectl -n default get serviceaccount default -o name > /dev/null
if [ "$?" -eq 0 ];
then
display_message "default account service account is created" "OK"
break
else
i=$[$i+1]
display_message " retry $i/10 default account service account is net yet created, sleeping for 5s" "INFO"
sleep 5
fi
done
}
# =============================================================================
# NAME: is_node_ready
# DESC: return 0 if $1 node name is Ready
# =============================================================================
# $1 name of the node
# =============================================================================
function is_node_ready() {
# filter to all nodes where name $1 .items[?(@.metadata.name==\"$1\")]
# read .status.conditions[] where type=Ready and return status value
# if status is True then node is Ready
is_ready=$(kubectl get node -o=jsonpath="{range .items[?(@.metadata.name==\""$1"\")]}{.status.conditions[?(@.type==\"Ready\")].status}")
if [ "$is_ready" = "True" ]; then
return 0
else
return 1
fi
}
# =============================================================================
# NAME: decode_km_token
# DESC: extract content from km_token into context
# =============================================================================
# $1 : km_token
# =============================================================================
function decode_km_token() {
KUBE_KM_TOKEN="$1"
if [ -z "${KUBE_KM_TOKEN}" ];
then
display_message "--km-token is blank" "ERROR"
exit 1
fi
decoded_km_token=$(echo "${KUBE_KM_TOKEN}"|base64 -d 2> /dev/null)
display_message_result "Decoding km-token"
KUBE_ADDRESS=$(echo "${decoded_km_token}"|cut -d ',' -f 1)
KUBE_API_URL="${KUBE_ADDRESS}"
display_message_result "Extracting API url and port"
if [ -z "${KUBE_ADDRESS}" ];
then
display_message "--km-token not well formed: api address not present" "ERROR"
exit 1
fi
KUBE_MASTER_TOKEN=$(echo "${decoded_km_token}"|cut -d ',' -f 2)
display_message_result "Extracting join token"
if [ -z "${KUBE_MASTER_TOKEN}" ];
then
display_message "--km-token not well formed: join token not present" "ERROR"
exit 1
fi
KUBE_DISCOVERY_HASH=$(echo "${decoded_km_token}"|cut -d ',' -f 3)
display_message_result "Extracting discovery hash"
if [ -z "${KUBE_DISCOVERY_HASH}" ];
then
display_message "--km-token not well formed: discover hash not present" "ERROR"
exit 1
fi
}
# =============================================================================
# NAME: install_nginx
# DESC: install nginx and configure it as load balancer
# =============================================================================
# $1 : master list
# $2 : worker list
# =============================================================================
function install_nginx_instance() {
display_section "Installing Nginx >> $(hostname)"
apt update > /dev/null 2>&1
display_message_result "Updating package repository"
apt install -y nginx ufw > /dev/null 2>&1
display_message_result "Installing base needed packages"
ufw allow 'Nginx Full' > /dev/null 2>&1
display_message_result "Allow port 'Nginx Full' in firewall"
ufw allow 'Nginx HTTP' > /dev/null 2>&1
display_message_result "Allow port 'Nginx HTTP' in firewall"
ufw allow 'Nginx HTTPS' > /dev/null 2>&1
display_message_result "Allow port 'Nginx HTTPS' in firewall"
mkdir -p /etc/nginx/tcpconf.d
cat << EOF > /etc/nginx/tcpconf.d/kubernetes.conf
stream {
upstream kubernetes {
$(echo -e $1)
}
upstream kubernetes_https {
$(echo -e $2)
}
server {
listen 6443;
proxy_pass kubernetes;
}
server {
listen 443;
proxy_pass kubernetes_https;
}
}
EOF
display_message_result "Creating Nginx LB configuration file"
if ! grep -q tcpconf.d /etc/nginx/nginx.conf;
then
echo 'include /etc/nginx/tcpconf.d/*;' >> /etc/nginx/nginx.conf
display_message_result "Loading Nginx LB configuration file in default configuration"
fi
systemctl stop haproxy 2> /dev/null||true
systemctl restart nginx
display_message_result "Restarting Nginx and loading LB configuration"
}
# =============================================================================
# NAME: install_nginx_servers
# DESC: install nginx servers and configure them as load balancer
# =============================================================================
# $1 : nginx server list
# $2 : master nodes list
# $3 : worker nodes list
# =============================================================================
function install_nginx_servers() {
display_section "Installing Nginx servers list"
NODE_LIST_ARG=""
while IFS= read -r CURRENT_MASTER;
do
NODE_LIST_ARG="${NODE_LIST_ARG} --master ${CURRENT_MASTER}:${KUBE_PORT}"
done <<< "$2"
while IFS= read -r CURRENT_WORKER;
do
NODE_LIST_ARG="${NODE_LIST_ARG} --worker ${CURRENT_WORKER}:443"
done <<< "$3"
while IFS= read -r CURRENT_NGINX; do
upload_tools "${CURRENT_MASTER}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Pushing commands"
ssh_command "${CURRENT_NGINX}" "sudo ./klb --apply --type nginx ${NODE_LIST_ARG}" "${SSH_USER}" "${SSH_PASS}" "${SSH_KEY}"
display_message_result "Deploying nginx load-balancer"
done <<< "$1"
}
# =============================================================================
# NAME: load_config_archive
# DESC: load archive containing masters certificats & others files needs
# =============================================================================
function load_config_archive() {
display_section "Configuration archive >> $(hostname)"
if [ -f ./kube-config.tgz ];
then
mkdir -p /etc/kubernetes/pki/etcd
display_message_result "Creating directory structure"
tar -xvzf ./kube-config.tgz -C / > /dev/null
display_message_result "Extracting configuration archive"
else
display_message "Can't find archive file in the current directory" "KO"
exit 1
fi
}
# =============================================================================
# NAME: generate_config_archive
# DESC: generate archive containing masters certificats & others files needs
# =============================================================================
function generate_config_archive() {
display_section "Configuration archive"
rm -f ./kube-config.tgz
tar -cvzf ./kube-config.tgz /etc/kubernetes/pki/ca.crt \
/etc/kubernetes/pki/ca.key \
/etc/kubernetes/pki/sa.key \
/etc/kubernetes/pki/sa.pub \
/etc/kubernetes/pki/front-proxy-ca.crt \
/etc/kubernetes/pki/front-proxy-ca.key \
/etc/kubernetes/pki/etcd/ca.crt \
/etc/kubernetes/pki/etcd/ca.key \
/etc/kubernetes/admin.conf > /dev/null 2>&1
display_message_result "Generating config archive"
}