-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·1287 lines (1022 loc) · 36.9 KB
/
run.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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
usage()
{
cat >&2 <<-EOF
Usage:
$0 install
$0 uninstall
$0 add worker_node_ip
$0 remove worker_node_ip
Options:
install: install kuberneters cluster
uninstall: uninstall kubernetes cluster
add: add one worker node to kubernetes cluster
remove: remove one worker node from kubernetes cluster
Examples:
$0 install
$0 uninstall
$0 add 192.168.50.24
$0 remove 192.168.50.25
EOF
exit 1
}
log_info()
{
echo >&2
echo -e "\\x1b[1;36m[$(date +%F:%T) INFO]: $*\\x1b[0m" >&2
}
log_warn()
{
echo >&2
echo -e "\\x1b[1;33m[$(date +%F:%T) WARNING]: $*\\x1b[0m" >&2
}
log_error()
{
echo >&2
echo -e "\\x1b[1;31m[$(date +%F:%T) ERROR]: $* \\x1b[0m" >&2
}
check_files_exist()
{
local files="$@"
for file in $files; do
[[ -s $file ]] || {
log_error "cannot find file: $file"
return 1
}
done
}
check_directories_exist()
{
local directories="$@"
for directory in $directories; do
[[ -d "$directory" ]] || {
log_error "cannot find a directory: $directory"
return 1
}
[[ $(ls -A "$directory") ]] || {
log_error "cannot find valid files in $directory, it's null"
return 1
}
done
}
check_command()
{
local cmd="$1"
command -v "$cmd" &>/dev/null || {
log_error "please install $cmd command"
return 1
}
}
check_ssh_port()
{
local ip="$1"
nmap -sn -n $ip | grep 'Host is up' > /dev/null || {
log_error "cannot access node $ip! Please fix this and try again"
return 1
}
if nmap -p22 -n $ip | grep "22/tcp" | grep -q "closed"; then
log_error "SSH service of IP $ip is closed! Please fix this and try again"
return 1
fi
}
check_ssh_login()
{
local ip="$1"
# must be set --foreground option, or cannot work well on CentOS7.2
if timeout --help | grep -q "\--foreground"; then
timeout --foreground 10 $SSH $ip "echo &>/dev/null" || {
log_error "failed to ssh node $ip without password"
return 1
}
else
timeout 10 $SSH $ip "echo &>/dev/null" || {
log_error "failed to ssh node $ip without password"
return 1
}
fi
}
check_selinux_config()
{
local ip="$1"
if $SSH $ip "[[ $(getenforce) = \"Enforcing\" ]]"; then
log_error "SELinux should be disabled in $ip"
return 1
fi
}
check_docker_status()
{
local ip="$1"
$SSH $ip "[[ \"$(systemctl is-active docker)\" = \"active\" ]]" || {
log_error "docker is not running in $ip"
return 1
}
}
parse_deploy_config()
{
log_info "parsing config file ..."
local deploy_config_file="$K8S_SRC/deploy.conf"
check_files_exist "$deploy_config_file" || return
while read -r line; do
echo $line | grep -q -e "^#" -e "^$" -e "^\[" && continue
# here not use local to define, since will use these var in other function
var=$(echo $line | cut -f1 -d"=")
value=$(echo $line | cut -f2 -d"=")
export $var=$value
done < $deploy_config_file
}
check_deploy_config()
{
log_info "checking config file ..."
case $ACTION in
install)
local deploy_config_file="$K8S_SRC/deploy.conf"
check_files_exist "$deploy_config_file" || return
K8S_IP_MASTERS=$(grep -v -e "^#" $deploy_config_file | grep K8S_IP_MASTER | cut -f2 -d"=")
K8S_IP_WORKERS=$(grep -v -e "^#" $deploy_config_file | grep K8S_IP_WORKER | cut -f2 -d"=")
if [[ $HA_DEPLOYMENT = "True" ]]; then
(( $(echo "$K8S_IP_MASTERS" | wc -l) == 3 )) || {
log_error "only support three master nodes for High Available Deployment"
return 1
}
else
(( $(echo "$K8S_IP_MASTERS" | wc -l) == 1 )) || {
log_error "only support one master node for Non-High Available Deployment"
return 1
}
fi
;;
add)
local installed_ip="$K8S_SRC/.installed_ip"
check_files_exist "$installed_ip" || return
grep -q -w $ADD_IP_WORKER $installed_ip && {
log_error "added ip $ADD_IP_WORKER have been in current kubernetes cluster"
return 1
}
K8S_IP_MASTERS=$(grep "MASTER" $installed_ip | awk '{print $2}')
K8S_IP_WORKERS=$ADD_IP_WORKER
;;
remove)
local installed_ip="$K8S_SRC/.installed_ip"
check_files_exist "$installed_ip" || return
grep -q -w $REMOVE_IP_WORKER $installed_ip || {
log_error "removed ip $REMOVE_IP_WORKER is not in current kubernetes cluster"
return 1
}
K8S_IP_MASTERS=$(grep "MASTER" $installed_ip | awk '{print $2}')
K8S_IP_WORKERS=$REMOVE_IP_WORKER
;;
uninstall)
local installed_ip="$K8S_SRC/.installed_ip"
if check_files_exist "$installed_ip"; then
K8S_IP_MASTERS=$(grep "MASTER" $installed_ip | awk '{print $2}')
K8S_IP_WORKERS=$(grep "WORKER" $installed_ip | awk '{print $2}')
else
log_warn "cannot find $K8S_SRC/.installed_ip"
local deploy_config_file="$K8S_SRC/deploy.conf"
check_files_exist "$deploy_config_file" || return
K8S_IP_MASTERS=$(grep -v -e "^#" $deploy_config_file | grep K8S_IP_MASTER | cut -f2 -d"=")
K8S_IP_WORKERS=$(grep -v -e "^#" $deploy_config_file | grep K8S_IP_WORKER | cut -f2 -d"=")
fi
;;
esac
}
check_nodes_status()
{
log_info "checking nodes status ..."
local ips="$1"
for ip in $ips; do
check_ssh_port $ip || return
check_ssh_login $ip || return
check_selinux_config $ip || return
check_docker_status $ip || return
done
}
yum_install_pkg()
{
log_info "installing pkg ..."
local pkg="$1"
local ips="$2"
local ips_num=$(echo "$ips" | wc -w)
$PSSH -H "$ips" -p $ips_num "yum install -y $pkg" || {
log_error "failed to install $pkg package"
return 1
}
}
yum_remove_pkg()
{
log_info "removing pkg ..."
local pkg="$1"
local ips="$2"
local ips_num=$(echo "$ips" | wc -w)
$PSSH -H "$ips" -p $ips_num "yum remove -y $pkg" || {
log_error "failed to remove $pkg package"
return 1
}
}
install_k8s_pkg()
{
log_info "installing k8s pkgs ..."
check_directories_exist "$K8S_SRC/yum" || return
local ips="$1"
local ips_num=$(echo "$ips" | wc -w)
$PSSH -H "$ips" -p $ips_num "mkdir -p /tmp/k8s_pkg/" &>/dev/null || return
$PSCP -H "$ips" -p $ips_num -r $K8S_SRC/yum/* /tmp/k8s_pkg/ &>/dev/null || return
$PSSH -H "$ips" -p $ips_num "yum install -y /tmp/k8s_pkg/kube*" || {
log_error "failed to install kubeadm pkgs"
return 1
}
}
remove_k8s_pkg()
{
log_info "removing k8s pkgs ..."
local ips="$1"
local ips_num=$(echo "$ips" | wc -w)
$PSSH -H "$ips" -p $ips_num "yum remove -y \"kube*\"" || return
}
stop_disable_service()
{
local service="$1"
local ips="$2"
local ips_num=$(echo "$ips" | wc -w)
$PSSH -H "$ips" -p $ips_num "systemctl stop $service" &>/dev/null
$PSSH -H "$ips" -p $ips_num "systemctl disable $service" &>/dev/null
}
start_enable_service()
{
local service="$1"
local ips="$2"
local ips_num=$(echo "$ips" | wc -w)
$PSSH -H "$ips" -p $ips_num "systemctl daemon-reload" &>/dev/null
$PSSH -H "$ips" -p $ips_num "systemctl restart $service" || {
log_error "failed to restart $service service"
return 1
}
$PSSH -H "$ips" -p $ips_num "systemctl enable $service" || {
log_error "failed to enable $service service"
return 1
}
}
get_hostname_by_ip()
{
local ip="$1"
[[ $ip ]] || return 0
$SSH $ip "hostname"
}
get_interface_by_ip()
{
local ip="$1"
[[ $ip ]] || return 0
$SSH $ip "ip a | grep $ip | awk '{print \$NF}'"
}
disable_swap()
{
log_info "disable swap ..."
local ips="$1"
[[ -z "$ips" ]] && return
local ips_num=$(echo $ips | wc -w)
$PSSH -H "$ips" -p $ips_num "swapoff -a" >/dev/null || {
log_error "failed to turn off swap"
return 1
}
$PSSH -H "$ips" -p $ips_num "sed -i \"s#.* swap .*#\#\0#\" /etc/fstab" &>/dev/null
return 0
}
restore_swap()
{
log_info "enable swap ..."
local ips="$1"
[[ -z "$ips" ]] && return
local ips_num=$(echo $ips | wc -w)
$PSSH -H "$ips" -p $ips_num "swapon -a"
$PSSH -H "$ips" -p $ips_num "sed -i -e \"s/^#\(.*\) swap/\1 swap/\" /etc/fstab" &> /dev/null
return 0
}
enable_bridge_nf_call()
{
log_info "enable bridge nf call ..."
local ips="$1"
[[ -z "$ips" ]] && return
local ips_num=$(echo $ips | wc -w)
local sysctl_k8s_conf="/tmp/k8s.conf"
cat <<EOF > $sysctl_k8s_conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$PSCP -H "$ips" -p $ips_num -r $sysctl_k8s_conf /etc/sysctl.d/ >/dev/null || return
$PSSH -H "$ips" -p $ips_num "sysctl -p /etc/sysctl.d/k8s.conf" >/dev/null || return
}
update_k8s_config()
{
log_info "update kubelet config ..."
local ips="$1"
local ips_num=$(echo $ips | wc -w)
local kubelet_config_file="/etc/systemd/system/kubelet.service.d/10-kubeadm.conf"
$PSSH -H "$ips" -p $ips_num "sed -i \"s#cgroup-driver=.*#cgroup-driver=cgroupfs\\\"#\" $kubelet_config_file" || {
log_error "failed to update cgroup driver"
return 1
}
# serivce_cidr=10.96.0.0/12, cluster_dns=10.96.0.10
local cluster_dns="$(echo $SERVICE_CIDR | cut -f1-3 -d".").10"
$PSSH -H "$ips" -p $ips_num "sed -i \"s#cluster-dns=10.96.0.10#cluster-dns=$cluster_dns#\" $kubelet_config_file" || {
log_error "failed to update cluster-dns"
return 1
}
$PSSH -H "$ips" -p $ips_num "sed -i \"s#cgroup-driver=.*#cgroup-driver=cgroupfs\\\"#\" $kubelet_config_file" || {
log_error "failed to update cgroup driver"
return 1
}
# $PSSH -H "$ips" -p $ips_num "sed -i '2 i Environment="KUBELET_EXTRA_ARGS=--feature-gates=DevicePlugins=true"' $kubelet_config_file" || return
start_enable_service "kubelet" "$ips" || return
}
generate_etcd_cert()
{
log_info "generate etcd cert ..."
local cfssl_file="$K8S_SRC/pkgs/cfssl"
local cfssljson_file="$K8S_SRC/pkgs/cfssljson"
check_files_exist "$cfssl_file" "$cfssljson_file" || return
local ca_config_json="$K8S_SRC/config/cert/ca-config.json"
local ca_csr_json="$K8S_SRC/config/cert/ca-csr.json"
local client_json="$K8S_SRC/config/cert/client.json"
local config_json="$K8S_SRC/config/cert/config.json"
local etcd_cert_dir="/etc/kubernetes/pki/etcd"
local temp_cert_dir="/tmp/cert_dir"
mkdir -p $temp_cert_dir || return
# generate ca cert
$cfssl_file gencert -initca $ca_csr_json | $cfssljson_file -bare $temp_cert_dir/ca || {
log_error "failed to generate ca cert"
return 1
}
# generate client cert
$cfssl_file gencert \
-ca=$temp_cert_dir/ca.pem \
-ca-key=$temp_cert_dir/ca-key.pem \
-config=$ca_config_json \
-profile=client \
$client_json | $cfssljson_file -bare $temp_cert_dir/client || {
log_error "failed to generate client cert"
return 1
}
# update config_json's hostname and ip info
local node_hostname
local temp_config_json="/tmp/etcd_config.json"
for ip in $K8S_IP_MASTERS; do
node_hostname=$(get_hostname_by_ip $ip)
sed -e "s#\$hostname#$node_hostname#" -e "s#\$ip#$ip#" $config_json >$temp_config_json || return
# generate server cert
$cfssl_file gencert \
-ca=$temp_cert_dir/ca.pem \
-ca-key=$temp_cert_dir/ca-key.pem \
-config=$ca_config_json \
-profile=server \
$temp_config_json | $cfssljson_file -bare $temp_cert_dir/server || {
log_error "failed to generate server cert for $ip"
return 1
}
# generate peer cert
$cfssl_file gencert \
-ca=$temp_cert_dir/ca.pem \
-ca-key=$temp_cert_dir/ca-key.pem \
-config=$ca_config_json \
-profile=peer \
$temp_config_json | $cfssljson_file -bare $temp_cert_dir/peer || {
log_error "failed to generate peer cert for $ip"
return 1
}
$SSH $ip "mkdir -p $etcd_cert_dir" || return
$SCP $temp_cert_dir/* $ip:$etcd_cert_dir || return
done
}
single_config_etcd()
{
log_info "config etcd ..."
local etcd_service_file="$K8S_SRC/config/etcd/etcd.service"
local etcd_binary_file="$K8S_SRC/pkgs/etcd"
check_files_exist "$etcd_service_file" "$etcd_binary_file" || return
# etcd service will use this file to startup
cp -a $etcd_binary_file /usr/local/bin/ || return
# update etcd.service file's hostname,ip,data_dir info
local temp_etcd_service_file="/tmp/etcd.service"
sed -e "s#\$hostname#$(hostname)#" \
-e "s#\$ip#$K8S_IP_MASTER1#" \
-e "s#\$etcd_data_dir#$ETCD_DATA_DIR#" \
$etcd_service_file > $temp_etcd_service_file || return
cp -a $temp_etcd_service_file /etc/systemd/system || return
start_enable_service "etcd" "$K8S_IP_MASTER1"
}
ha_config_etcd()
{
log_info "config etcd ..."
local etcd_service_file="$K8S_SRC/config/etcd/ha_etcd.service"
local etcd_binary_file="$K8S_SRC/pkgs/etcd"
check_files_exist "$etcd_service_file" "$etcd_binary_file" || return
local ip_master1="$K8S_IP_MASTER1"
local hostname_master1=$(get_hostname_by_ip $ip_master1)
local ip_master2="$K8S_IP_MASTER2"
local hostname_master2=$(get_hostname_by_ip $ip_master2)
local ip_master3="$K8S_IP_MASTER3"
local hostname_master3=$(get_hostname_by_ip $ip_master3)
# update etcd.service file for various info
local hostname_master
local ip_master
local temp_etcd_service_file="/tmp/etcd.service"
for ip in $K8S_IP_MASTERS; do
hostname_master=$(get_hostname_by_ip $ip)
ip_master=$ip
sed -e "s#\$etcd_data_dir#$ETCD_DATA_DIR#" \
-e "s#\$hostname_master1#$hostname_master1#" \
-e "s#\$hostname_master2#$hostname_master2#" \
-e "s#\$hostname_master3#$hostname_master3#" \
-e "s#\$hostname_master#$hostname_master#" \
-e "s#\$ip_master1#$ip_master1#" \
-e "s#\$ip_master2#$ip_master2#" \
-e "s#\$ip_master3#$ip_master3#" \
-e "s#\$ip_master#$ip_master#" \
$etcd_service_file > $temp_etcd_service_file || return
$SCP $temp_etcd_service_file $ip:/etc/systemd/system || return
$SCP $etcd_binary_file $ip:/usr/local/bin/ || return
done
start_enable_service "etcd" "$K8S_IP_MASTERS" || return
}
remove_etcd()
{
log_info "removing etcd ..."
local ips="$1"
[[ -z "$ips" ]] && return
for ip in $ips; do
stop_disable_service "etcd" "$ip"
$SSH $ip "rm -rf /usr/local/bin/etcd $ETCD_DATA_DIR"
done
}
ha_config_keepalived()
{
log_info "config keepalived ..."
local keepalived_config_file="$K8S_SRC/config/keepalived/keepalived.conf"
local keepalived_script_file="$K8S_SRC/config/keepalived/check_alived.sh"
check_files_exist "$keepalived_config_file" "$keepalived_script_file" || return
yum_install_pkg "keepalived" "$K8S_IP_MASTERS" || return
local node_interface
local priority=100
local role="MASTER"
# update keepalived check script file
local temp_check_alived_script="/tmp/check_alived.sh"
sed -e "s#\$keepalived_virtual_ip#$KEEPALIVED_VIRTUAL_IP#" $keepalived_script_file > $temp_check_alived_script || return
# copy to all app nodes
local ips_num=$(echo $K8S_IP_MASTERS | wc -w)
$PSCP -H "$K8S_IP_MASTERS" -p $ips_num -r $temp_check_alived_script /etc/keepalived/ >/dev/null || return
# update keepalived config file
local temp_keepalived_config_file="/tmp/keepalived.conf"
for ip in $K8S_IP_MASTERS; do
node_interface=$(get_interface_by_ip $ip)
sed -e "s#\$interface#$node_interface#" \
-e "s#\$role#$role#" \
-e "s#\$priority#$priority#" \
-e "s#\$keepalived_virtual_router_id#$KEEPALIVED_VIRTUAL_ROUTER_ID#" \
-e "s#\$keepalived_virtual_ip#$KEEPALIVED_VIRTUAL_IP#" \
$keepalived_config_file > $temp_keepalived_config_file || return
priority=$(( $priority - 1))
role="BACKUP"
$SCP $temp_keepalived_config_file $ip:/etc/keepalived/ > /dev/null || return
done
start_enable_service "keepalived" "$K8S_IP_MASTERS" || return
}
ha_remove_keepalived()
{
log_info "removing keepalived ..."
stop_disable_service "keepalived" "$K8S_IP_MASTERS"
yum_remove_pkg "keepalived" "$K8S_IP_MASTERS"
}
ha_config_nginx()
{
log_info "config nginx ..."
local nginx_config_file="$K8S_SRC/config/nginx/nginx.conf"
check_files_exist "$nginx_config_file" || return
yum_install_pkg "nginx" "$K8S_IP_MASTERS" || return
local ip_master1="$K8S_IP_MASTER1"
local ip_master2="$K8S_IP_MASTER2"
local ip_master3="$K8S_IP_MASTER3"
# update nginx config file
local temp_nginx_config_file="/tmp/nginx.conf"
sed -e "s#\$ip_master1#$ip_master1#" \
-e "s#\$ip_master2#$ip_master2#" \
-e "s#\$ip_master3#$ip_master3#" \
-e "s#\$nginx_listen_port#$NGINX_LISTEN_PORT#" \
$nginx_config_file > $temp_nginx_config_file || return
local ips_num=$(echo $K8S_IP_MASTERS | wc -w)
$PSCP -H "$K8S_IP_MASTERS" -p $ips_num -r $temp_nginx_config_file /etc/nginx/ >/dev/null || return
start_enable_service "nginx" "$K8S_IP_MASTERS" || return
}
ha_remove_nginx()
{
log_info "removing nginx ..."
stop_disable_service "nginx" "$K8S_IP_MASTERS"
yum_remove_pkg "nginx" "$K8S_IP_MASTERS"
}
load_master_images()
{
log_info "loading master images ..."
[[ "$LOAD_IMAGES_BOOL" = "True" ]] || return 0
local ips="$1"
[[ -z "$ips" ]] && return
local ips_num=$(echo $ips | wc -w)
local master_images_dir="$K8S_SRC/images/master"
check_directories_exist "$master_images_dir" || return
$PSSH -H "$ips" -p $ips_num "mkdir -p /tmp/k8s-images" >/dev/null || return
$PSCP -H "$ips" -p $ips_num -r $master_images_dir/* /tmp/k8s-images || {
log_error "failed to scp images pkg to $ips"
return 1
}
$PSSH -H "$ips" -p $ips_num "cd /tmp/k8s-images; for image in \$(ls); do docker load -i \$image; done" >/dev/null || return
$PSSH -H "$ips" -p $ips_num "rm -rf /tmp/k8s-images" >/dev/null || return
}
load_worker_images()
{
log_info "loading worker images ..."
[[ "$LOAD_IMAGES_BOOL" = "True" ]] || return 0
local ips="$1"
[[ -z "$ips" ]] && return
local ips_num=$(echo $ips | wc -w)
local worker_images_dir="$K8S_SRC/images/worker"
check_directories_exist "$worker_images_dir" || return
$PSSH -H "$ips" -p $ips_num "mkdir -p /tmp/k8s-images" >/dev/null || return
$PSCP -H "$ips" -p $ips_num -r $worker_images_dir/* /tmp/k8s-images || {
log_error "failed to scp images pkg to $ips"
return 1
}
$PSSH -H "$ips" -p $ips_num "cd /tmp/k8s-images; for image in \$(ls); do docker load -i \$image; done" >/dev/null || return
$PSSH -H "$ips" -p $ips_num "rm -rf /tmp/k8s-images" >/dev/null || return
}
remove_docker_images()
{
log_info "removing docker images ..."
[[ "$REMOVE_IMAGES_BOOL" = "True" ]] || return 0
local ips="$1"
[[ -z "$ips" ]] && return
cat >/tmp/rm-docker-image <<-EOF
#!/bin/bash
docker_images=\$(docker images)
ret=0
while read -r line; do
image_id=\$(echo \$line | grep -e "quay.io" -e "gcr.io" | awk '{print \$3}')
if [[ \$image_id ]]; then
docker rmi --force \$image_id || ret=1
fi
done <<< "\$docker_images"
exit \$ret
EOF
chmod 755 /tmp/rm-docker-image || return
local ips_num=$(echo $ips | wc -w)
$PSCP -H "$ips" -p $ips_num -r /tmp/rm-docker-image /tmp/ || {
log_error "failed to scp rm-docker-image script to remote"
return 1
}
for ip in $ips; do
$SSH $ip "/tmp/rm-docker-image" || log_warn "failed to remove all images in $ip"
done
}
# validate pods are under running status, timeout is 5 mins
#
# run `kubectl get pods -o wide -n kube-system` output:
# ...
# kube-dns-86f4d74b45-gmdn4 3/3 Running 0 3d 180.20.128.1 k8s-master3.novalocal
# kube-dns-86f4d74b45-qjkz6 3/3 Running 0 3d 180.20.154.65 k8s-master2.novalocal
# kube-proxy-lzztt 1/1 Running 0 3d 192.168.60.12 k8s-master2.novalocal
# kube-proxy-thrr7 1/1 Running 0 3d 192.168.60.13 k8s-master3.novalocal
# ...
#
# due to kube-dns not use HostIP, need to use hostname to match/filter the specified pod
#
validate_pod_state()
{
log_info "waiting pods to be running (timeout is 5 mins) ..."
local ips="$1"
[[ -z "$ips" ]] && return
local k8s_pods="$2"
[[ -z "$ips" ]] && return
local hostname_info
local hostnames_info
for ip in $ips; do
hostname_info=$(get_hostname_by_ip $ip)
hostnames_info="$hostnames_info $hostname_info"
done
local i=1
local loops=30
local ret
local status
local pods_info
while (( $loops > $i)); do
ret=0
pods_info=$(kubectl get pods -o wide -n kube-system 2>/dev/null)
for hostname_info in $hostnames_info; do
for k8s_pod in $k8s_pods; do
status=$(echo "$pods_info" | grep $k8s_pod | grep $hostname_info | awk '{print $3}')
[[ "$status" = "Running" ]] || {
ret=1
break
}
done
[[ "$ret" = "1" ]] && break
done
if [[ "$ret" = "0" ]]; then
return 0
else
echo "waiting $(( 10 * $i)) seconds ..."
i=$((i + 1))
sleep 10
fi
done
log_error "$k8s_pods pods are not running, please check it"
return 1
}
single_init_master()
{
log_info "init master ..."
local kubeadm_init_file="$K8S_SRC/config/k8s/kubeadm-init.yaml"
check_files_exist "$kubeadm_init_file" || return
# udpate yaml file's kubernetes_version,apiserver_advertise_address and so on
local temp_kubeadm_init_file="/tmp/kubeadm-init.yaml"
sed -e "s#\$kubernetes_version#$KUBERNETES_VERSION#" \
-e "s#\$apiserver_advertise_address#$APISERVER_ADVERTISE_ADDRESS#" \
-e "s#\$ip#$K8S_IP_MASTER1#" \
-e "s#\$etcd_data_dir#$ETCD_DATA_DIR#" \
-e "s#\$pod_network_cidr#$POD_NETWORK_CIDR#" \
-e "s#\$service_cidr#$SERVICE_CIDR#" \
-e "s#\$hostname#$(hostname)#" \
$kubeadm_init_file > $temp_kubeadm_init_file || return
kubeadm init --config=$temp_kubeadm_init_file || {
log_error "failed to initialize master node"
return 1
}
mkdir -p /root/.kube || return
cp -a /etc/kubernetes/admin.conf $HOME/.kube/config || return
validate_pod_state "$K8S_IP_MASTER1" "kube-apiserver kube-scheduler kube-controller-manager" || return
}
ha_init_master()
{
log_info "init master ..."
local kubeadm_init_file="$K8S_SRC/config/k8s/ha_kubeadm-init.yaml"
check_files_exist "$kubeadm_init_file" || return
local ip_master1="$K8S_IP_MASTER1"
local hostname_master1=$(get_hostname_by_ip $ip_master1)
local ip_master2="$K8S_IP_MASTER2"
local hostname_master2=$(get_hostname_by_ip $ip_master2)
local ip_master3="$K8S_IP_MASTER3"
local hostname_master3=$(get_hostname_by_ip $ip_master3)
# udpate yaml file's kubernetes_version,apiserver_advertise_address and so on
local temp_kubeadm_init_file="/tmp/kubeadm-init.yaml"
sed -e "s#\$kubernetes_version#$KUBERNETES_VERSION#" \
-e "s#\$keepalived_virtual_ip#$KEEPALIVED_VIRTUAL_IP#" \
-e "s#\$ip_master1#$ip_master1#" \
-e "s#\$ip_master2#$ip_master2#" \
-e "s#\$ip_master3#$ip_master3#" \
-e "s#\$hostname_master1#$hostname_master1#" \
-e "s#\$hostname_master2#$hostname_master2#" \
-e "s#\$hostname_master3#$hostname_master3#" \
-e "s#\$etcd_data_dir#$ETCD_DATA_DIR#" \
-e "s#\$pod_network_cidr#$POD_NETWORK_CIDR#" \
-e "s#\$service_cidr#$SERVICE_CIDR#" \
$kubeadm_init_file > $temp_kubeadm_init_file || return
kubeadm init --config=$temp_kubeadm_init_file || {
log_error "failed to initialize master node"
return 1
}
mkdir -p /root/.kube || return
cp -a /etc/kubernetes/admin.conf /root/.kube/config || return
validate_pod_state "$K8S_IP_MASTER1" "kube-apiserver kube-scheduler kube-controller-manager" || return
}
ha_join_master()
{
log_info "join master ..."
local cert_dir="/etc/kubernetes/pki"
local kubeadm_init_file="/tmp/kubeadm-init.yaml"
for ip in $K8S_IP_MASTER2 $K8S_IP_MASTER3; do
$SCP -r $cert_dir/ca.* $cert_dir/sa.* $ip:$cert_dir/ || return
$SCP $kubeadm_init_file $ip:$kubeadm_init_file || return
$SSH $ip "kubeadm init --config=$kubeadm_init_file" || {
log_error "failed to join master in $ip"
return 1
}
$SSH $ip "mkdir -p /root/.kube && cp -a /etc/kubernetes/admin.conf /root/.kube/config" || return
validate_pod_state "$ip" "kube-apiserver kube-scheduler kube-controller-manager" || return
done
}
ha_scale_pods()
{
log_info "scaling calico-kube-controllers and kube-dns ..."
# to maintain the full HA ability
kubectl scale --replicas=3 -n kube-system deployment/calico-kube-controllers || {
log_error "failed to scale calico-kube-controllers"
return 1
}
kubectl scale --replicas=3 -n kube-system deployment/kube-dns || {
log_error "failed to scale kube-dns"
return 1
}
}
make_master_schedulable()
{
[[ "$MASTER_SCHEDULABLE_BOOL" = "False" ]] && return 0
kubectl taint nodes --all node-role.kubernetes.io/master- || {
log_warn "failed to make master schedulable"
return 0
}
}
remove_master_nodes()
{
log_info "removing master nodes ..."
for ip in $K8S_IP_MASTER2 $K8S_IP_MASTER3; do
local node_hostname
node_hostname=$(get_hostname_by_ip $ip)
kubectl drain $node_hostname --delete-local-data --force --ignore-daemonsets || log_warn "failed to drain $node_hostname"
kubectl delete node $node_hostname || log_warn "failed to delete $node_hostname"
$SSH $ip "kubeadm reset" || log_warn "failed to run kubeadm reset in $node_hostname"
done
kubectl drain $(hostname) --delete-local-data --force --ignore-daemonsets || log_warn "failed to drain $(hostname)"
kubectl delete node $(hostname) || log_warn "failed to delete $(hostname)"
kubeadm reset || log_warn "failed to run kubeadm reset in $(hostname)"
}
init_k8s_network()
{
log_info "initialize k8s network ..."
local calico_config_file="$K8S_SRC/config/calico/calico.yaml"
local rbac_config_file="$K8S_SRC/config/calico/rbac.yaml"
check_files_exist $calico_config_file $rbac_config_file || return
local etcd_key_file="/etc/kubernetes/pki/etcd/client-key.pem"
local etcd_cert_file="/etc/kubernetes/pki/etcd/client.pem"
local etcd_ca_file="/etc/kubernetes/pki/etcd/ca.pem"
local etcd_endpoints
local etcd_key_base64
local etcd_cert_base64
local etcd_ca_base64
for ip in $K8S_IP_MASTERS; do
if [[ $etcd_endpoints ]]; then
etcd_endpoints="$etcd_endpoints,https://$ip:2379"
else
etcd_endpoints="https://$ip:2379"
fi
done
etcd_key_base64=$(base64 -w 0 $etcd_key_file)
etcd_cert_base64=$(base64 -w 0 $etcd_cert_file)
etcd_ca_base64=$(base64 -w 0 $etcd_ca_file)
# udpate etcd.service file's hostname,ip,data_dir info
local temp_calico_config_file="/tmp/calico.yaml"
sed -e "s#\$calico_reachable_ip#$CALICO_REACHABLE_IP#" \
-e "s#\$pod_network_cidr#$POD_NETWORK_CIDR#" \
-e "s#\$etcd_endpoints#$etcd_endpoints#" \
-e "s#\$etcd_key_base64#$etcd_key_base64#" \
-e "s#\$etcd_cert_base64#$etcd_cert_base64#" \
-e "s#\$etcd_ca_base64#$etcd_ca_base64#" \
$calico_config_file > $temp_calico_config_file || return
kubectl apply -f $rbac_config_file >/dev/null || {
log_error "failed to init rbac"
return 1
}
kubectl apply -f $temp_calico_config_file >/dev/null || {
log_error "failed to init calico"
return 1
}
validate_pod_state "$K8S_IP_MASTER1" "calico-kube-controllers calico-node kube-dns" || return
}
remove_k8s_network()
{
log_info "remove k8s network ..."