-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s_install.sh
2751 lines (2302 loc) · 88.7 KB
/
k8s_install.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
###################################################
# 参数设置 #
###################################################
# 资源规划
etcd_nodes=(
"k8s-master-01|192.168.0.151"
"k8s-master-02|192.168.0.152"
"k8s-master-03|192.168.0.153"
)
k8s_master_nodes=(
"k8s-master-01|192.168.0.151"
"k8s-master-02|192.168.0.152"
"k8s-master-03|192.168.0.153"
)
k8s_worker_nodes=(
"k8s-worker-01|192.168.0.161"
"k8s-worker-02|192.168.0.162"
"k8s-worker-03|192.168.0.163"
)
k8s_control_plane_nodes=(
"k8s-master-01|192.168.0.151"
)
# 在 k8s-master 中选择一个座位主节点, 其他的作为备用节点
keepalived_master="k8s-master-01"
# 网卡名 使用 ip addr 可以看到
network_interface_name="ens33"
# apiserver使用虚拟ip地址和端口, 要在keepalived中配置转发到 k8s-master 节点上
k8s_apiserver_vip="192.168.0.250"
k8s_apiserver_port="8443"
# k8s pod CIDR
k8s_pod_ip_range="196.16.0.0/16"
# k8s service CIDR
k8s_service_ip_range="10.96.0.0/16"
k8s_service_ip="10.96.0.1"
# coreDns 配置的集群内DNS地址
cluster_dns="10.96.0.10"
# kubelet boostrap 使用的 token
k8s_bootstrap_token_id="c8ad9c"
k8s_bootstrap_token_secret="2e4d610cf3e7426e"
# 工具及安装包的版本, 仅设置版本号, 不要带 "v", 使用的时候会在需要的位置加上 "v"
cfssl_version="1.6.4"
etcd_version="3.5.9"
containerd_version="1.7.5"
cni_plugins_version="1.3.0"
runc_version="1.1.9"
libseccomp_version="2.5.4"
nerdctl_version="1.5.0"
kubernetes_version="1.28.1"
helm_version="3.12.3"
coredns_version="1.26.0"
metrics_server_version="0.6.4"
calico_version="3.26.1"
# 文件路径
# local_xxx 是执行这个安装脚本的节点上的文件路径, 如果使用相对路径是相对脚本文件的位置
# 本机安装包文件存放路径
local_package_dir="packages"
# 临时目录, 每次脚本会先 ## 清空 ## , 再将安装过程中生成的文件放入这个目录,注意不要指向存有重要数据的目录
local_tmp_dir="tmp"
# remote_xxx 是 k8s 集群中的节点的位置, 文件要先传输过去才能使用
# 远程节点接收文件的基准路径
remote_tmp_dir="/root/tmp"
# 其他参数
# 设置dns列表
dns_server_list="8.8.8.8 114.114.114.114"
# 设置时区
timezone="Asia/Shanghai"
# 设置时间同步服务器
ntp_server="edu.ntp.org.cn"
# 路径及文件名参数, 通常不需要修改
etcd_pki_ca="etcd-ca"
etcd_pki_client="etcd-client"
etcd_pki_dir="/etc/etcd/pki"
k8s_pki_ca="kubernetes-ca"
k8s_pki_etcd_ca="etcd-ca"
k8s_pki_etcd_client="etcd-client"
k8s_pki_kube_apiserver="kube-apiserver"
k8s_pki_service_account="kubernetes-service-account"
k8s_pki_front_proxy_ca="kubebernetes-front-proxy-ca"
k8s_pki_front_proxy_client="kubebernetes-front-proxy-client"
# 工具包本地导出目录
local_tools_save_dir="tools"
# 工具包远程节点临时存放目录
remote_tools_save_dir="/root/tmp/tools"
# ubuntu 22
system_name="jammy"
# 镜像本地导出目录
local_image_save_dir="images"
# 镜像远程节点临时存放目录
remote_image_save_dir="/root/tmp/images"
# 镜像的命名空间
image_namespace=k8s.io
###################################################
# 参数设置结束 #
###################################################
# 准备变量
# 安装的工具列表
tools=(
"psmisc"
"vim"
"net-tools"
"nfs-kernel-server"
"telnet"
"lvm2"
"git"
"tar"
"curl"
"selinux-utils"
"wget"
"ipvsadm"
"ipset"
"sysstat"
"conntrack"
"gnupg2"
"software-properties-common"
"apt-transport-https"
"ca-certificates"
"ntpdate"
"gcc"
"gperf"
"make"
"keepalived"
"haproxy"
"jq"
)
etcd_hostnames=()
etcd_ips=()
for node in "${etcd_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node"
etcd_hostnames+=("$hostname")
etcd_ips+=("$ip")
done
echo "etcd_hostnames"
for name in "${etcd_hostnames[@]}"; do
echo "$name"
done
echo "etcd_ips:"
for ip in "${etcd_ips[@]}"; do
echo "$ip"
done
etcd_hostnames_string=${etcd_hostnames[@]}
echo "etcd_hostnames_string:${etcd_hostnames_string}"
etcd_hostnames_string_comma=$(IFS=,; echo "${etcd_hostnames[*]}")
echo "etcd_hostnames_string_comma:${etcd_hostnames_string_comma}"
etcd_ips_string=${etcd_ips[@]}
echo "etcd_ips_string:${etcd_ips_string}"
etcd_ips_string_comma=$(IFS=,; echo "${etcd_ips[*]}")
echo "etcd_ips_string_comma:${etcd_ips_string_comma}"
etcd_urls_string_comma=""
for ip in "${etcd_ips[@]}"; do
if [ -n "$etcd_urls_string_comma" ]; then
etcd_urls_string_comma+=","
fi
etcd_urls_string_comma+="https://$ip:2379"
done
echo "etcd_urls_string_comma:$etcd_urls_string_comma"
k8s_master_hostnames=()
k8s_master_ips=()
for node in "${k8s_master_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node"
k8s_master_hostnames+=("$hostname")
k8s_master_ips+=("$ip")
done
echo "k8s_master_hostnames"
for name in "${k8s_master_hostnames[@]}"; do
echo "$name"
done
echo "k8s_master_ips:"
for ip in "${k8s_master_ips[@]}"; do
echo "$ip"
done
k8s_master_hostnames_string=${k8s_master_hostnames[@]}
echo "k8s_master_hostnames_string:${k8s_master_hostnames_string}"
k8s_master_hostnames_string_comma=$(IFS=,; echo "${k8s_master_hostnames[*]}")
echo "k8s_master_hostnames_string_comma:${k8s_master_hostnames_string_comma}"
k8s_master_ips_string=${k8s_master_ips[@]}
echo "k8s_master_ips_string:${k8s_master_ips_string}"
k8s_master_ips_string_comma=$(IFS=,; echo "${k8s_master_ips[*]}")
echo "k8s_master_ips_string_comma:${k8s_master_ips_string_comma}"
k8s_worker_hostnames=()
k8s_worker_ips=()
for node in "${k8s_worker_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node"
k8s_worker_hostnames+=("$hostname")
k8s_worker_ips+=("$ip")
done
echo "k8s_worker_hostnames"
for name in "${k8s_worker_hostnames[@]}"; do
echo "$name"
done
echo "k8s_worker_ips:"
for ip in "${k8s_worker_ips[@]}"; do
echo "$ip"
done
k8s_worker_hostnames_string=${k8s_worker_hostnames[@]}
echo "k8s_worker_hostnames_string:${k8s_worker_hostnames_string}"
k8s_worker_hostnames_string_comma=$(IFS=,; echo "${k8s_worker_hostnames[*]}")
echo "k8s_worker_hostnames_string_comma:${k8s_worker_hostnames_string_comma}"
k8s_worker_ips_string=${k8s_worker_ips[@]}
echo "k8s_worker_ips_string:${k8s_worker_ips_string}"
k8s_worker_ips_string_comma=$(IFS=,; echo "${k8s_worker_ips[*]}")
echo "k8s_worker_ips_string_comma:${k8s_worker_ips_string_comma}"
tmp_all_k8s_nodes=("${k8s_master_nodes[@]}" "${k8s_worker_nodes[@]}")
k8s_nodes=()
for node in "${tmp_all_k8s_nodes[@]}"; do
# 判断是否已经保存到 k8s_nodes 中
if [[ ! " ${k8s_nodes[@]} " =~ " ${node} " ]]; then
k8s_nodes+=("$node")
fi
done
k8s_node_hostnames=()
k8s_node_ips=()
for node in "${k8s_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node"
if [[ ! " ${k8s_node_hostnames[@]} " =~ " ${hostname} " ]]; then
k8s_node_hostnames+=("$hostname")
fi
if [[ ! " ${k8s_node_ips[@]} " =~ " ${ip} " ]]; then
k8s_node_ips+=("$ip")
fi
done
echo "k8s_node_hostnames"
for name in "${k8s_node_hostnames[@]}"; do
echo "$name"
done
echo "k8s_node_ips:"
for ip in "${k8s_node_ips[@]}"; do
echo "$ip"
done
k8s_node_hostnames_string=${k8s_node_hostnames[@]}
echo "k8s_node_hostnames_string:${k8s_node_hostnames_string}"
k8s_node_hostnames_string_comma=$(IFS=,; echo "${k8s_node_hostnames[*]}")
echo "k8s_node_hostnames_string_comma:${k8s_node_hostnames_string_comma}"
k8s_node_ips_string=${k8s_node_ips[@]}
echo "k8s_node_ips_string:${k8s_node_ips_string}"
k8s_node_ips_string_comma=$(IFS=,; echo "${k8s_node_ips[*]}")
echo "k8s_node_ips_string_comma:${k8s_node_ips_string_comma}"
tmp_all_nodes=("${etcd_nodes[@]}" "${k8s_master_nodes[@]}" "${k8s_worker_nodes[@]}" "${k8s_control_plane_nodes[@]}")
cluster_nodes=()
for node in "${tmp_all_nodes[@]}"; do
# 判断是否已经保存到 cluster_nodes 中
if [[ ! " ${cluster_nodes[@]} " =~ " ${node} " ]]; then
cluster_nodes+=("$node")
fi
done
cluster_node_hostnames=()
cluster_node_ips=()
for node in "${cluster_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node"
cluster_node_hostnames+=("$hostname")
cluster_node_ips+=("$ip")
done
echo "cluster_node_hostnames"
for name in "${cluster_node_hostnames[@]}"; do
echo "$name"
done
echo "cluster_node_ips:"
for ip in "${cluster_node_ips[@]}"; do
echo "$ip"
done
k8s_control_plane_hostnames=()
k8s_control_plane_ips=()
for node in "${k8s_control_plane_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node"
k8s_control_plane_hostnames+=("$hostname")
k8s_control_plane_ips+=("$ip")
done
echo "k8s_control_plane_hostnames"
for name in "${k8s_control_plane_hostnames[@]}"; do
echo "$name"
done
echo "k8s_control_plane_ips:"
for ip in "${k8s_control_plane_ips[@]}"; do
echo "$ip"
done
k8s_inner_hostnames=(
"kubernetes"
"kubernetes.default"
"kubernetes.default.svc"
"kubernetes.default.svc.cluster"
"kubernetes.default.svc.cluster.local"
)
k8s_inner_hostnames_string=${k8s_inner_hostnames[@]}
echo "k8s_inner_hostnames_string:${k8s_inner_hostnames_string}"
k8s_node_hostnames_string_comma=$(IFS=,; echo "${k8s_inner_hostnames[*]}")
echo "k8s_node_hostnames_string_comma:${k8s_node_hostnames_string_comma}"
k8s_apiserver_url="https://$k8s_apiserver_vip:$k8s_apiserver_port"
echo "k8s_apiserver_url:${k8s_apiserver_url}"
# 下载工具列表
tools_string="${tools[*]}"
packages=(
"cfssl_${cfssl_version}_linux_amd64|https://github.com/cloudflare/cfssl/releases/download/v${cfssl_version}/cfssl_${cfssl_version}_linux_amd64"
"cfssljson_${cfssl_version}_linux_amd64|https://github.com/cloudflare/cfssl/releases/download/v${cfssl_version}/cfssljson_${cfssl_version}_linux_amd64"
"etcd-v${etcd_version}-linux-amd64.tar.gz|https://github.com/etcd-io/etcd/releases/download/v${etcd_version}/etcd-v${etcd_version}-linux-amd64.tar.gz"
"containerd-${containerd_version}-linux-amd64.tar.gz|https://github.com/containerd/containerd/releases/download/v${containerd_version}/containerd-${containerd_version}-linux-amd64.tar.gz"
"cni-plugins-linux-amd64-v${cni_plugins_version}.tgz|https://github.com/containernetworking/plugins/releases/download/v${cni_plugins_version}/cni-plugins-linux-amd64-v${cni_plugins_version}.tgz"
"runc-v${runc_version}.amd64|https://github.com/opencontainers/runc/releases/download/v${runc_version}/runc.amd64"
"libseccomp-${libseccomp_version}.tar.gz|https://github.com/seccomp/libseccomp/releases/download/v${libseccomp_version}/libseccomp-${libseccomp_version}.tar.gz"
"nerdctl-${nerdctl_version}-linux-amd64.tar.gz|https://github.com/containerd/nerdctl/releases/download/v${nerdctl_version}/nerdctl-${nerdctl_version}-linux-amd64.tar.gz"
"kubernetes-server-${kubernetes_version}-linux-amd64.tar.gz|https://dl.k8s.io/v${kubernetes_version}/kubernetes-server-linux-amd64.tar.gz"
"helm-v${helm_version}-linux-amd64.tar.gz|https://get.helm.sh/helm-v${helm_version}-linux-amd64.tar.gz"
"coredns-${coredns_version}.tgz|https://github.com/coredns/helm/releases/download/coredns-${coredns_version}/coredns-${coredns_version}.tgz"
# https://docs.projectcalico.org/manifests/calico.yaml
"calico-v${calico_version}.yaml|https://raw.githubusercontent.com/projectcalico/calico/v${calico_version}/manifests/calico.yaml"
"metrics-server-v${metrics_server_version}_high-availability-1.21+.yaml|https://github.com/kubernetes-sigs/metrics-server/releases/download/v${metrics_server_version}/high-availability-1.21+.yaml"
)
echo "###############################"
echo "# start #"
echo "###############################"
echo "###############################"
echo "# set env for cluster node #"
echo "###############################"
# 设置 hostname
echo "set hostname"
for node in "${cluster_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node"
echo "set hostname as $hostname in $ip"
ssh "root@$ip" "hostnamectl set-hostname $hostname"
done
# 将ip host 信息添加到 /etc/hosts 文件
# 生成要添加到 /etc/hosts 的内容
echo "set hosts"
for node_info in "${cluster_nodes[@]}"; do
IFS='|' read -r hostname ip <<< "$node_info"
hosts_content="$ip $hostname"
for remote_ip in "${cluster_node_ips[@]}"; do
ssh "root@$remote_ip" "grep -Fq \"${hosts_content}\" /etc/hosts || echo -e \"$hosts_content\" >> /etc/hosts"
done
grep -Fq "${hosts_content}" /etc/hosts || echo -e "${hosts_content}" >> /etc/hosts
done
echo "set hosts finished"
# hello
echo "hello"
for node in "${cluster_node_hostnames[@]}"; do
ssh "root@$node" "echo \"hello from $node\""
done
echo "hello finished"
# 更新系统
echo "update and upgrade"
for node in "${cluster_node_hostnames[@]}"; do
(
echo "update and upgrade in $node"
ssh "root@$node" "apt update && apt upgrade -y"
) &
done
wait
echo "update and upgrade finished"
# 安装工具
echo "install tools"
for node in "${cluster_node_hostnames[@]}"; do
(
echo "install tools in ${node}, tools:${tools_string}"
ssh "root@$node" "apt install -y ${tools_string}"
) &
done
wait
echo "install tools finished"
# 修改DNS
echo "set dns server"
file_name="/etc/systemd/resolved.conf"
file_append_content="[Resolve]\nDNS=${dns_server_list}"
# Loop through the remote nodes
for node in "${cluster_node_hostnames[@]}"; do
# Check if the file exists
ssh "root@$node" "[ -e $file_name ]"
if [ $? -eq 0 ]; then
# Check if content exists in the file
ssh "root@$node" "grep -q '^$file_append_content' $file_name"
if [ $? -ne 0 ]; then
# Append content to the file
ssh "root@$node" "echo '$file_append_content' >> $file_name"
echo "Added $file_append_content to $file_name on $node_name"
else
echo "$file_append_content already exists in $file_name on $node_name"
fi
else
# Create the file and write content to it
ssh "root@$node" "echo '$file_append_content' > $file_name"
echo "Created $file_name with $file_append_content on $node_name"
fi
done
# 设置时区
echo "set timezone"
for node in "${cluster_node_hostnames[@]}"; do
(
echo "set timezone to ${timezone} in $node"
ssh "root@$node" "timedatectl set-timezone ${timezone}"
# ntp 同步时钟
ssh "root@$node" "ntpdate ${ntp_server}"
) &
done
wait
echo "#######################################"
echo "# check install package and tools #"
echo "#######################################"
# 下载安装包
echo "download packages"
for package in "${packages[@]}"; do
IFS='|' read -r filename remote_url <<< "$package"
# 检查本地是否存在文件
if [ ! -f "${local_package_dir}/$filename" ]; then
echo "file $filename not exists, downloading..."
wget "$remote_url" -O "${local_package_dir}/$filename"
if [ $? -eq 0 ]; then
echo "download finished"
else
echo "download failed"
fi
else
echo "$filename exists, skip downloading"
fi
done
echo "download packages finished"
echo "###############################"
echo "# set env for k8s node #"
echo "###############################"
# 关闭selinux
echo "disable selinux"
file_name="/etc/selinux/config"
file_append_content="SELINUX=disabled"
# Loop through the remote nodes
for node in "${k8s_node_hostnames[@]}"; do
# Check if the file exists
ssh "root@$node" "[ -e $file_name ]"
if [ $? -eq 0 ]; then
# Check if content exists in the file
ssh "root@$node" "grep -q '^$file_append_content' $file_name"
if [ $? -ne 0 ]; then
# Append content to the file
ssh "root@$node" "echo '$file_append_content' >> $file_name"
echo "Added $file_append_content to $file_name on $node_name"
else
echo "$file_append_content already exists in $file_name on $node_name"
fi
else
# Create the file and write content to it
ssh "root@$node" "echo '$file_append_content' > $file_name"
echo "Created $file_name with $file_append_content on $node_name"
fi
done
# 关闭交换分区
echo "swap off"
for node in "${k8s_node_hostnames[@]}"; do
echo "swap off in $node"
ssh "root@$node" "swapoff -a && sysctl -w vm.swappiness=0"
ssh "root@$node" "sed -i '/^\/swap.img/ s/^/#/' /etc/fstab"
done
# 修改资源限制
echo "set ulimit"
ulimit -SHn 65535
file_name="/etc/security/limits.conf"
file_append_content="
* soft nofile 655360
* hard nofile 131072
* soft nproc 655350
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
"
# Loop through the remote nodes
for node in "${k8s_node_hostnames[@]}"; do
# Check if the file exists
ssh "root@$node" "[ -e $file_name ]"
if [ $? -eq 0 ]; then
# Check if content exists in the file
ssh "root@$node" "grep -Fq '^$file_append_content' $file_name"
if [ $? -ne 0 ]; then
# Append content to the file
ssh "root@$node" "echo '$file_append_content' >> $file_name"
echo "Added $file_append_content to $file_name on $node_name"
else
echo "$file_append_content already exists in $file_name on $node_name"
fi
else
# Create the file and write content to it
ssh "root@$node" "echo '$file_append_content' > $file_name"
echo "Created $file_name with $file_append_content on $node_name"
fi
done
# 启用 ipvs
echo "enable ipvs"
file_name="/etc/modules-load.d/ipvs.conf"
file_append_content="
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
"
# Loop through the remote nodes
for node in "${k8s_node_hostnames[@]}"; do
# Check if the file exists
ssh "root@$node" "[ -e $file_name ]"
if [ $? -eq 0 ]; then
# Check if content exists in the file
ssh "root@$node" "grep -Fq '^$file_append_content' $file_name"
if [ $? -ne 0 ]; then
# Append content to the file
ssh "root@$node" "echo '$file_append_content' >> $file_name"
echo "Added $file_append_content to $file_name on $node_name"
ssh "root@$node" "systemctl restart systemd-modules-load.service"
else
echo "$file_append_content already exists in $file_name on $node_name"
fi
else
# Create the file and write content to it
ssh "root@$node" "echo '$file_append_content' > $file_name"
echo "Created $file_name with $file_append_content on $node_name"
ssh "root@$node" "systemctl restart systemd-modules-load.service"
fi
done
# 修改内核参数
echo "update kernal param for kubernetes"
file_name="/etc/sysctl.d/k8s.conf"
file_append_content="
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
fs.may_detach_mounts = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.default.disable_ipv6 = 0
net.ipv6.conf.lo.disable_ipv6 = 0
net.ipv6.conf.all.forwarding = 1
"
# Loop through the remote nodes
for node in "${k8s_node_hostnames[@]}"; do
# Check if the file exists
ssh "root@$node" "[ -e $file_name ]"
if [ $? -eq 0 ]; then
# Check if content exists in the file
ssh "root@$node" "grep -Fq '^$file_append_content' $file_name"
if [ $? -ne 0 ]; then
# Append content to the file
ssh "root@$node" "echo '$file_append_content' >> $file_name"
echo "Added $file_append_content to $file_name on $node_name"
ssh "root@$node" "sysctl --system"
else
echo "$file_append_content already exists in $file_name on $node_name"
fi
else
# Create the file and write content to it
ssh "root@$node" "echo '$file_append_content' > $file_name"
echo "Created $file_name with $file_append_content on $node_name"
ssh "root@$node" "sysctl --system"
fi
done
echo "###############################"
echo "# start install modules #"
echo "###############################"
# 清理本地的临时目录
echo "clean dirs in local and cluster nodes"
mkdir -p ${local_tmp_dir}
rm -rf ${local_tmp_dir}/*
# 清理集群节点的临时目录
for node in "${cluster_node_hostnames[@]}"; do
ssh "root@$node" "mkdir -p ${remote_tmp_dir}"
ssh "root@$node" "rm -rf ${remote_tmp_dir}/*"
done
# 安装 containerd
echo "###############################"
echo "# install contained #"
echo "###############################"
mkdir ${local_tmp_dir}/containerd
rm -rf mkdir ${local_tmp_dir}/containerd/*
echo "clean containerd"
for node in "${k8s_node_hostnames[@]}"; do
ssh "root@$node" "systemctl is-active containerd.service && systemctl stop containerd.service"
ssh "root@$node" "[ -f /etc/systemd/system/containerd.service ] && rm /etc/systemd/system/containerd.service"
done
# 将 containerd 安装包分发到所有的 k8s 节点
echo "copying contained install package to k8s nodes"
for node in "${k8s_node_hostnames[@]}"; do
echo "copy containerd install package to ${node} "
ssh root@${node} "mkdir -p ${remote_tmp_dir}/containerd"
ssh root@${node} "rm -rf ${remote_tmp_dir}/containerd/*"
scp ${local_package_dir}/containerd-${containerd_version}-linux-amd64.tar.gz root@${node}:${remote_tmp_dir}/containerd/containerd-${containerd_version}-linux-amd64.tar.gz
ssh "root@$node" "tar -xzvf ${remote_tmp_dir}/containerd/containerd-${containerd_version}-linux-amd64.tar.gz -C /usr/local"
done
# 配置 containerd 所需的模块
file_name="/etc/modules-load.d/containerd.conf"
file_content="
modprobe
overlay
br_netfilter
"
for node in "${k8s_node_hostnames[@]}"; do
ssh "root@$node" "echo '$file_content' > ${file_name}"
# 加载模块
ssh "root@$node" "systemctl restart systemd-modules-load.service"
done
# 配置 containerd 所需的内核
file_name="/etc/sysctl.d/99-kubernetes-cri.conf"
file_content="
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
"
for node in "${k8s_node_hostnames[@]}"; do
ssh "root@$node" "echo '$file_content' > ${file_name}"
# 加载模块
ssh "root@$node" "sysctl --system"
done
# 生成并修改containerd配置文件
tar -zxvf ${local_package_dir}/containerd-${containerd_version}-linux-amd64.tar.gz -C ${local_tmp_dir}/containerd
${local_tmp_dir}/containerd/bin/containerd config default > ${local_tmp_dir}/containerd/config.toml
# 修改
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' ${local_tmp_dir}/containerd/config.toml
sed -i "s#config_path\ \=\ \"\"#config_path\ \=\ \"/etc/containerd/certs.d\"#g" ${local_tmp_dir}/containerd/config.toml
sed -i "s|registry.k8s.io/pause:3.8|registry.k8s.io/pause:3.9|" ${local_tmp_dir}/containerd/config.toml
# 发送到 k8s 的所有节点
for node in "${k8s_node_hostnames[@]}"; do
ssh root@$node "mkdir -p /etc/containerd"
ssh root@$node "[ -f /etc/containerd/config.toml ] && rm /etc/containerd/config.toml"
scp ${local_tmp_dir}/containerd/config.toml root@$node:/etc/containerd/config.toml
done
# 在本机生成镜像仓库代理配置文件
mkdir -p ${local_tmp_dir}/containerd/certs.d
rm -rf ${local_tmp_dir}/containerd/certs.d/*
# docker hub镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/docker.io
cat > ${local_tmp_dir}/containerd/certs.d/docker.io/hosts.toml << EOF
server = "https://docker.io"
[host."https://dockerproxy.com"]
capabilities = ["pull", "resolve"]
[host."https://docker.m.daocloud.io"]
capabilities = ["pull", "resolve"]
[host."https://reg-mirror.qiniu.com"]
capabilities = ["pull", "resolve"]
[host."https://registry.docker-cn.com"]
capabilities = ["pull", "resolve"]
[host."http://hub-mirror.c.163.com"]
capabilities = ["pull", "resolve"]
EOF
# registry.k8s.io镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/registry.k8s.io
tee ${local_tmp_dir}/containerd/certs.d/registry.k8s.io/hosts.toml << 'EOF'
server = "https://registry.k8s.io"
[host."https://k8s.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# docker.elastic.co镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/docker.elastic.co
tee ${local_tmp_dir}/containerd/certs.d/docker.elastic.co/hosts.toml << 'EOF'
server = "https://docker.elastic.co"
[host."https://elastic.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# gcr.io镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/gcr.io
tee ${local_tmp_dir}/containerd/certs.d/gcr.io/hosts.toml << 'EOF'
server = "https://gcr.io"
[host."https://gcr.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# ghcr.io镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/ghcr.io
tee ${local_tmp_dir}/containerd/certs.d/ghcr.io/hosts.toml << 'EOF'
server = "https://ghcr.io"
[host."https://ghcr.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# k8s.gcr.io镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/k8s.gcr.io
tee ${local_tmp_dir}/containerd/certs.d/k8s.gcr.io/hosts.toml << 'EOF'
server = "https://k8s.gcr.io"
[host."https://k8s-gcr.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# mcr.m.daocloud.io镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/mcr.microsoft.com
tee ${local_tmp_dir}/containerd/certs.d/mcr.microsoft.com/hosts.toml << 'EOF'
server = "https://mcr.microsoft.com"
[host."https://mcr.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# nvcr.io镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/nvcr.io
tee ${local_tmp_dir}/containerd/certs.d/nvcr.io/hosts.toml << 'EOF'
server = "https://nvcr.io"
[host."https://nvcr.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# quay.io镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/quay.io
tee ${local_tmp_dir}/containerd/certs.d/quay.io/hosts.toml << 'EOF'
server = "https://quay.io"
[host."https://quay.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# registry.jujucharms.com镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/registry.jujucharms.com
tee ${local_tmp_dir}/containerd/certs.d/registry.jujucharms.com/hosts.toml << 'EOF'
server = "https://registry.jujucharms.com"
[host."https://jujucharms.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# rocks.canonical.com镜像加速
mkdir -p ${local_tmp_dir}/containerd/certs.d/rocks.canonical.com
tee ${local_tmp_dir}/containerd/certs.d/rocks.canonical.com/hosts.toml << 'EOF'
server = "https://rocks.canonical.com"
[host."https://rocks-canonical.m.daocloud.io"]
capabilities = ["pull", "resolve", "push"]
EOF
# 将containerd的镜像仓库代理配置文件发送到各个节点
for node in "${k8s_node_hostnames[@]}"; do
ssh "root@$node" "mkdir -p /etc/containerd/certs.d"
echo "copying contained repository proxy settings file to $node"
scp -r ${local_tmp_dir}/containerd/certs.d/* root@$node:/etc/containerd/certs.d/
done
# https://github.com/containerd/containerd/blob/main/docs/getting-started.md
# download the containerd.service unit file from
# https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
# into /usr/local/lib/systemd/system/containerd.service
# 配置 containerd.service
file_name="/usr/lib/systemd/system/containerd.service"
file_content="
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
"
for node in "${k8s_node_hostnames[@]}"; do
ssh "root@$node" "echo '${file_content}' > ${file_name}"
done
# installing CNI plugins
echo "installing CNI plugins"
for node in "${k8s_node_hostnames[@]}"; do
echo "installing CNI plugins for ${node}"
ssh root@${node} "mkdir -p /opt/cni/bin"
ssh root@${node} "rm -rf /opt/cni/bin/*"
ssh root@${node} "mkdir -p ${remote_tmp_dir}/cni-plugins"
ssh root@${node} "rm -rf ${remote_tmp_dir}/cni-plugins/*"
scp ${local_package_dir}/cni-plugins-linux-amd64-v${cni_plugins_version}.tgz root@${node}:${remote_tmp_dir}/cni-plugins/cni-plugins-linux-amd64-v${cni_plugins_version}.tgz
ssh root@${node} "tar -xzvf ${remote_tmp_dir}/cni-plugins/cni-plugins-linux-amd64-v${cni_plugins_version}.tgz -C /opt/cni/bin"
echo "installing cni-plugins for ${node} finished"
done
echo "installing CNI plugins finished"
# 创建 cni plugins 配置文件
cat > ${local_tmp_dir}/containerd/10-containerd-net.conflist << EOF
{
"cniVersion": "1.0.0",
"name": "containerd-net",
"plugins": [
{
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"promiscMode": true,
"ipam": {
"type": "host-local",
"ranges": [
[{
"subnet": "10.88.0.0/16"
}],
[{
"subnet": "2001:4860:4860::/64"
}]
],
"routes": [
{ "dst": "0.0.0.0/0" },
{ "dst": "::/0" }
]
}
},
{
"type": "portmap",
"capabilities": {"portMappings": true}
}
]
}
EOF
for node in "${k8s_node_hostnames[@]}"; do
ssh "root@$node" "mkdir -p /etc/cni/net.d"
scp ${local_tmp_dir}/containerd/10-containerd-net.conflist root@$node:/etc/cni/net.d/10-containerd-net.conflist
done
# install libseccomp
echo "installing libseccomp"
# 先安装依赖工具
for node in "${k8s_node_hostnames[@]}"; do
(
ssh root@${node} "apt install -y gcc make gperf"
) &