ubuntu22.04一键部署k8s-v1.25.0集群

Ubuntu 22.04 LTS

k8s-v1.25.0

containerd-1.6.8

  1. ntpdate cn.pool.ntp.org
  2. apt-get install ntpdate
  1. ssh-keygen
  2. for i in master node{1..2}; do echo ">>> $i";ssh-copy-id $i;done
  1. # Ubuntu 22.04 LTS(清华源)
  2. cat > /etc/apt/sources.list << 'EOF'
  3. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
  4. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
  5. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
  6. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
  7. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
  8. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
  9. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
  10. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
  11. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
  12. # 预发布软件源,不建议启用
  13. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
  14. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
  15. EOF
  16. apt update

一键通用安装脚本

  1. cat > k8s-1.25.0.sh << 'eof'
  2. #!/bin/bash
  3. start=$(date +%s)
  4. node=$1
  5. # 环境准备
  6. # 1、关闭防火墙
  7. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 关闭防火墙 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  8. for i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "ufw disable"; done
  9. # 2、关闭swap
  10. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 关闭 swap の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  11. for i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "swapoff -a"; done
  12. for i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "sed -i 's/.*swap.*/#&/g' /etc/fstab"; done
  13. # 3、加载IPVS模块
  14. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 加载IPVS模块 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  15. for i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "apt install ipset ipvsadm -y"; done
  16. cat > /etc/modules-load.d/ipvs.conf << EOF
  17. modprobe -- ip_vs
  18. modprobe -- ip_vs_rr
  19. modprobe -- ip_vs_wrr
  20. modprobe -- ip_vs_sh
  21. modprobe -- nf_conntrack
  22. EOF
  23. for i in ${node[*]};do
  24. echo -e "\e[32;5m>>> $i\e[0m";
  25. scp /etc/modules-load.d/ipvs.conf root@$i:/etc/modules-load.d;
  26. done
  27. for i in ${node[*]};do
  28. echo -e "\e[32;5m>>> $i\e[0m";
  29. ssh root@$i "bash -x /etc/modules-load.d/ipvs.conf";
  30. done
  31. # 4、安装container
  32. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 安装 container.io-v1.6.8-1 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  33. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  34. for i in ${node[*]};do
  35. echo -e "\e[32;5m>>> $i\e[0m";
  36. scp /etc/apt/keyrings/docker.gpg root@$i:/etc/apt/keyrings;
  37. done
  38. echo \
  39. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  40. $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
  41. for i in ${node[*]};do
  42. echo -e "\e[32;5m>>> $i\e[0m";
  43. scp /etc/apt/sources.list.d/docker.list root@$i:/etc/apt/sources.list.d;
  44. done
  45. for i in ${node[*]};do
  46. echo -e "\e[32;5m>>> $i\e[0m";
  47. ssh root@$i "apt-get update && apt-get install containerd.io=1.6.8-1";
  48. done
  49. cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
  50. overlay
  51. br_netfilter
  52. EOF
  53. for i in ${node[*]};do
  54. echo -e "\e[32;5m>>> $i\e[0m";
  55. scp /etc/modules-load.d/containerd.conf root@$i:/etc/modules-load.d;
  56. done
  57. for i in ${node[*]};do
  58. echo -e "\e[32;5m>>> $i\e[0m";
  59. ssh root@$i "modprobe overlay && modprobe br_netfilter";
  60. done
  61. cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
  62. net.bridge.bridge-nf-call-iptables = 1
  63. net.ipv4.ip_forward = 1
  64. net.bridge.bridge-nf-call-ip6tables = 1
  65. EOF
  66. for i in ${node[*]};do
  67. echo -e "\e[32;5m>>> $i\e[0m";
  68. scp /etc/sysctl.d/99-kubernetes-cri.conf root@$i:/etc/sysctl.d;
  69. done
  70. for i in ${node[*]};do
  71. echo -e "\e[32;5m>>> $i\e[0m";
  72. ssh root@$i sysctl --system;
  73. done
  74. for i in ${node[*]};do
  75. echo -e "\e[32;5m>>> $i\e[0m";
  76. ssh root@$i "mkdir -p /etc/containerd && containerd config default > /etc/containerd/config.toml";
  77. done
  78. # 修改cgroup Driver为systemd
  79. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 修改cgroup Driver为 systemd の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  80. for i in ${node[*]};do
  81. echo -e "\e[32;5m>>> $i\e[0m";
  82. ssh root@$i "sed -ri 's#SystemdCgroup = false#SystemdCgroup = true#' /etc/containerd/config.toml";
  83. done
  84. # 更改sandbox_image为pause:3.8
  85. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 更改sandbox_image为pause:3.8 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  86. for i in ${node[*]};do
  87. echo -e "\e[32;5m>>> $i\e[0m";
  88. ssh root@$i "sed -ri 's#k8s.gcr.io\/pause:3.6#registry.aliyuncs.com\/google_containers\/pause:3.8#' /etc/containerd/config.toml";
  89. done
  90. # endpoint位置添加阿里云的镜像源
  91. for i in ${node[*]};do
  92. echo -e "\e[32;5m>>> $i\e[0m";
  93. ssh root@$i "sed -ri 's#https:\/\/registry-1.docker.io#https:\/\/registry.aliyuncs.com#' /etc/containerd/config.toml";
  94. done
  95. for i in ${node[*]};do
  96. echo -e "\e[32;5m>>> $i\e[0m";
  97. ssh root@$i "systemctl daemon-reload && systemctl restart containerd";
  98. done
  99. # 5、安装k8s-1.25.0
  100. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 安装 kubelet-v1.25.0 kubelet-v1.25.0 kubectl-v1.25.0 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  101. curl -fsSL https://repo.huaweicloud.com/kubernetes/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/kubernetes-archive-keyring.gpg
  102. for i in ${node[*]};do
  103. echo -e "\e[32;5m>>> $i\e[0m";
  104. scp /usr/share/keyrings/kubernetes-archive-keyring.gpg root@$i:/usr/share/keyrings;
  105. done
  106. cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
  107. deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/kubernetes/apt kubernetes-xenial main
  108. EOF
  109. for i in ${node[*]};do
  110. echo -e "\e[32;5m>>> $i\e[0m";
  111. scp /etc/apt/sources.list.d/kubernetes.list root@$i:/etc/apt/sources.list.d;
  112. done
  113. for i in ${node[*]};do
  114. echo -e "\e[32;5m>>> $i\e[0m";
  115. ssh root@$i "apt-get update && apt-get install -y kubelet=1.25.0-00 kubeadm=1.25.0-00 kubectl=1.25.0-00";
  116. done
  117. # 设置crictl
  118. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 设置crictl の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  119. cat << EOF >> /etc/crictl.yaml
  120. runtime-endpoint: unix:///var/run/containerd/containerd.sock
  121. image-endpoint: unix:///var/run/containerd/containerd.sock
  122. timeout: 10
  123. debug: false
  124. EOF
  125. for i in ${node[*]};do
  126. echo -e "\e[32;5m>>> $i\e[0m";
  127. scp /etc/crictl.yaml root@$i:/etc/crictl.yaml;
  128. done
  129. # 初始化yml
  130. mkdir ~/kubeadm_init && cd ~/kubeadm_init
  131. cat > kubeadm-init.yaml << EOF
  132. apiVersion: kubeadm.k8s.io/v1beta3
  133. bootstrapTokens:
  134. - groups:
  135. - system:bootstrappers:kubeadm:default-node-token
  136. token: abcdef.0123456789abcdef
  137. ttl: 24h0m0s
  138. usages:
  139. - signing
  140. - authentication
  141. kind: InitConfiguration
  142. localAPIEndpoint:
  143. advertiseAddress: `hostname -I` #master_ip
  144. bindPort: 6443
  145. nodeRegistration:
  146. criSocket: unix:///var/run/containerd/containerd.sock
  147. imagePullPolicy: IfNotPresent
  148. name: master
  149. taints:
  150. - effect: "NoSchedule"
  151. key: "node-role.kubernetes.io/master"
  152. ---
  153. apiServer:
  154. timeoutForControlPlane: 4m0s
  155. apiVersion: kubeadm.k8s.io/v1beta3
  156. certificatesDir: /etc/kubernetes/pki
  157. clusterName: kubernetes
  158. controllerManager: {}
  159. dns: {}
  160. etcd:
  161. local:
  162. dataDir: /var/lib/etcd
  163. imageRepository: registry.aliyuncs.com/google_containers
  164. kind: ClusterConfiguration
  165. kubernetesVersion: v1.25.0
  166. networking:
  167. dnsDomain: cluster.local
  168. serviceSubnet: 10.96.0.0/12
  169. podSubnet: 10.244.0.0/16
  170. scheduler: {}
  171. ---
  172. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  173. kind: KubeProxyConfiguration
  174. mode: ipvs
  175. ---
  176. apiVersion: kubelet.config.k8s.io/v1beta1
  177. kind: KubeletConfiguration
  178. cgroupDriver: systemd
  179. EOF
  180. # 预拉取镜像
  181. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 预拉取镜像 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  182. kubeadm config images pull --config kubeadm-init.yaml
  183. for i in ${node[*]};do
  184. echo -e "\e[32;5m>>> $i\e[0m";
  185. ssh root@$i "crictl pull registry.aliyuncs.com/google_containers/pause:3.8 && crictl pull registry.aliyuncs.com/google_containers/kube-proxy:v1.25.0";
  186. done
  187. # 初始化集群
  188. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 初始化集群 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  189. kubeadm init --config=kubeadm-init.yaml | tee kubeadm-init.log
  190. mkdir -p $HOME/.kube
  191. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  192. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  193. # 加入集群
  194. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 加入集群 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  195. cat ~/kubeadm_init/kubeadm-init.log |grep token |tail -2 >join.token.sh
  196. for i in ${node[*]};do
  197. echo -e "\e[32;5m>>> $i\e[0m";
  198. scp ~/kubeadm_init/join.token.sh root@$i:/root/join.token.sh;
  199. done
  200. for i in ${node[*]};do
  201. echo -e "\e[32;5m>>> $i\e[0m";
  202. ssh root@$i "bash /root/join.token.sh 1>/dev/null 2>&1";
  203. done
  204. # 安装flannel
  205. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 安装flannel の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  206. cat > ~/kube-flannel.yml << 'EOF'
  207. ---
  208. kind: Namespace
  209. apiVersion: v1
  210. metadata:
  211. name: kube-flannel
  212. labels:
  213. pod-security.kubernetes.io/enforce: privileged
  214. ---
  215. kind: ClusterRole
  216. apiVersion: rbac.authorization.k8s.io/v1
  217. metadata:
  218. name: flannel
  219. rules:
  220. - apiGroups:
  221. - ""
  222. resources:
  223. - pods
  224. verbs:
  225. - get
  226. - apiGroups:
  227. - ""
  228. resources:
  229. - nodes
  230. verbs:
  231. - list
  232. - watch
  233. - apiGroups:
  234. - ""
  235. resources:
  236. - nodes/status
  237. verbs:
  238. - patch
  239. ---
  240. kind: ClusterRoleBinding
  241. apiVersion: rbac.authorization.k8s.io/v1
  242. metadata:
  243. name: flannel
  244. roleRef:
  245. apiGroup: rbac.authorization.k8s.io
  246. kind: ClusterRole
  247. name: flannel
  248. subjects:
  249. - kind: ServiceAccount
  250. name: flannel
  251. namespace: kube-flannel
  252. ---
  253. apiVersion: v1
  254. kind: ServiceAccount
  255. metadata:
  256. name: flannel
  257. namespace: kube-flannel
  258. ---
  259. kind: ConfigMap
  260. apiVersion: v1
  261. metadata:
  262. name: kube-flannel-cfg
  263. namespace: kube-flannel
  264. labels:
  265. tier: node
  266. app: flannel
  267. data:
  268. cni-conf.json: |
  269. {
  270. "name": "cbr0",
  271. "cniVersion": "0.3.1",
  272. "plugins": [
  273. {
  274. "type": "flannel",
  275. "delegate": {
  276. "hairpinMode": true,
  277. "isDefaultGateway": true
  278. }
  279. },
  280. {
  281. "type": "portmap",
  282. "capabilities": {
  283. "portMappings": true
  284. }
  285. }
  286. ]
  287. }
  288. net-conf.json: |
  289. {
  290. "Network": "10.244.0.0/16",
  291. "Backend": {
  292. "Type": "vxlan"
  293. }
  294. }
  295. ---
  296. apiVersion: apps/v1
  297. kind: DaemonSet
  298. metadata:
  299. name: kube-flannel-ds
  300. namespace: kube-flannel
  301. labels:
  302. tier: node
  303. app: flannel
  304. spec:
  305. selector:
  306. matchLabels:
  307. app: flannel
  308. template:
  309. metadata:
  310. labels:
  311. tier: node
  312. app: flannel
  313. spec:
  314. affinity:
  315. nodeAffinity:
  316. requiredDuringSchedulingIgnoredDuringExecution:
  317. nodeSelectorTerms:
  318. - matchExpressions:
  319. - key: kubernetes.io/os
  320. operator: In
  321. values:
  322. - linux
  323. hostNetwork: true
  324. priorityClassName: system-node-critical
  325. tolerations:
  326. - operator: Exists
  327. effect: NoSchedule
  328. serviceAccountName: flannel
  329. initContainers:
  330. - name: install-cni-plugin
  331. #image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)
  332. image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
  333. command:
  334. - cp
  335. args:
  336. - -f
  337. - /flannel
  338. - /opt/cni/bin/flannel
  339. volumeMounts:
  340. - name: cni-plugin
  341. mountPath: /opt/cni/bin
  342. - name: install-cni
  343. #image: flannelcni/flannel:v0.19.0 for ppc64le and mips64le (dockerhub limitations may apply)
  344. image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0
  345. command:
  346. - cp
  347. args:
  348. - -f
  349. - /etc/kube-flannel/cni-conf.json
  350. - /etc/cni/net.d/10-flannel.conflist
  351. volumeMounts:
  352. - name: cni
  353. mountPath: /etc/cni/net.d
  354. - name: flannel-cfg
  355. mountPath: /etc/kube-flannel/
  356. containers:
  357. - name: kube-flannel
  358. #image: flannelcni/flannel:v0.19.0 for ppc64le and mips64le (dockerhub limitations may apply)
  359. image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0
  360. command:
  361. - /opt/bin/flanneld
  362. args:
  363. - --ip-masq
  364. - --kube-subnet-mgr
  365. resources:
  366. requests:
  367. cpu: "100m"
  368. memory: "50Mi"
  369. limits:
  370. cpu: "100m"
  371. memory: "50Mi"
  372. securityContext:
  373. privileged: false
  374. capabilities:
  375. add: ["NET_ADMIN", "NET_RAW"]
  376. env:
  377. - name: POD_NAME
  378. valueFrom:
  379. fieldRef:
  380. fieldPath: metadata.name
  381. - name: POD_NAMESPACE
  382. valueFrom:
  383. fieldRef:
  384. fieldPath: metadata.namespace
  385. - name: EVENT_QUEUE_DEPTH
  386. value: "5000"
  387. volumeMounts:
  388. - name: run
  389. mountPath: /run/flannel
  390. - name: flannel-cfg
  391. mountPath: /etc/kube-flannel/
  392. - name: xtables-lock
  393. mountPath: /run/xtables.lock
  394. volumes:
  395. - name: run
  396. hostPath:
  397. path: /run/flannel
  398. - name: cni-plugin
  399. hostPath:
  400. path: /opt/cni/bin
  401. - name: cni
  402. hostPath:
  403. path: /etc/cni/net.d
  404. - name: flannel-cfg
  405. configMap:
  406. name: kube-flannel-cfg
  407. - name: xtables-lock
  408. hostPath:
  409. path: /run/xtables.lock
  410. type: FileOrCreate
  411. EOF
  412. kubectl apply -f ~/kube-flannel.yml
  413. # kubectl补全
  414. echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の kubectl补全 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"
  415. apt install bash-completion -y
  416. source /etc/profile.d/bash_completion.sh
  417. echo "source <(crictl completion bash)" >> ~/.bashrc
  418. echo "source <(kubectl completion bash)" >> ~/.bashrc
  419. echo -e "\033[0;33m
  420. ██╗ ██╗ █████╗ ███████╗
  421. ██║ ██╔╝██╔══██╗██╔════╝
  422. █████╔╝ ╚█████╔╝███████╗
  423. ██╔═██╗ ██╔══██╗╚════██║
  424. ██║ ██╗╚█████╔╝███████║
  425. ╚═╝ ╚═╝ ╚════╝ ╚══════ has been installed !!!\033[0m"
  426. # 脚本执行时间
  427. end=$(date +%s)
  428. take=$(( end - start ))
  429. echo -e "\e[32;5m[=======================]\e[0m"
  430. echo -e "\e[32;5m脚本执行时间为 ---> ${take} seconds \e[0m"
  431. echo -e "\e[32;5m[=======================]\e[0m"
  432. eof
  1. chmod +x k8s-1.25.0.sh
  2. ./k8s-1.25.0.sh "master node1 node2"
  3. su -

一主两从

master

node1

node2

如果需要多个node节点,在./k8s-1.25.0.sh “master node1 node2 node3 node4”后面加