Kubernetes自动化一键部署脚本

时间:2022-12-30 15:57:04

Kubernetes 简介

Kubernetes是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。

Kubernetes一个核心的特点就是能够自主的管理容器来保证云平台中的容器按照用户的期望状态运行着(比如用户想让apache一直运行,用户不需要关心怎么去做,Kubernetes会自动去监控,然后去重启,新建,总之,让apache一直提供服务),管理员可以加载一个微型服务,让规划器来找到合适的位置,同时,Kubernetes也系统提升工具以及人性化方面,让用户能够方便的部署自己的应用(就像canary deployments)。

此脚本使用的是kubeadm方式安装,有需要的朋友可以参考下。

系统环境:

操作系统:Centos 7.8

脚本功能:

1、支持kubernetes版本选择

2、支持Dcoker-ce版本选择

3、支持Master、Node节点安装

注意:Node需要跟Master做好免密钥哦

PS:脚本下载链接在底部

脚本内容如下:

[root@localhost ~]# vim auto_install_kubernetes.sh

  1. #!/bin/bash
  2. #Date:2020-9-8 09:41:00
  3. #Author Blog:
  4. # https://www.yangxingzhen.com
  5. # https://www.i7ti.cn
  6. #Author WeChat:
  7. # 微信公众号:小柒博客
  8. #Author mirrors site:
  9. # https://mirrors.yangxingzhen.com
  10. #About the Author
  11. # BY:YangXingZhen
  12. # Mail:xingzhen.yang@yangxingzhen.com
  13. # QQ:675583110
  14. #Auto Install Kubernetes

  15. source /etc/init.d/functions

  16. SYS_VERSION=$(cat /etc/redhat-release)
  17. IPADDR=$(hostname -I |awk '{print $1}')
  18. Uptime_day=$(uptime |awk '{print $3,$4}')
  19. CPU_NUM=$(grep -c 'processor'/proc/cpuinfo)
  20. Uptime=$(uptime -p |awk '{print $6,$7,$8,$9}')
  21. MEM_INFO=$(free -m |awk '/Mem/ {print "内存容量:",$2"M"}')
  22. CPU_Model=$(awk -F:'/name/ {print $NF}'/proc/cpuinfo |uniq)
  23. MEM_Avail=$(free -m |awk '/Mem/ {print "内存可用容量:",$4"M"}')
  24. DISK_INFO=$(df -h |grep -w "/"|awk '{print "磁盘容量:",$1,$2}')
  25. DISK_Avail=$(df -h |grep -w "/"|awk '{print "磁盘可用容量:",$1,$4}')
  26. LOAD_INFO=$(uptime |awk '{print "CPU负载: "$(NF-2),$(NF-1),$NF}'|sed 's/\,//g')

  27. if[ ${UID}-ne 0];then
  28. action "请使用ROOT用户运行Kubernetes安装脚本..."/bin/false
  29. exit1
  30. fi

  31. functionKubernetes_Version(){
  32. echo -e "\033[32mVersion:1.19.0 Available...\033[0m"
  33. echo -e "\033[32mVersion:1.18.8 Available...\033[0m"
  34. echo -e "\033[32mVersion:1.18.6 Available...\033[0m"
  35. echo -e "\033[32mVersion:1.18.5 Available...\033[0m"
  36. echo -e "\033[32mVersion:1.18.4 Available...\033[0m"
  37. echo -e "\033[32mVersion:1.18.3 Available...\033[0m"
  38. echo -e "\033[32mVersion:1.18.2 Available...\033[0m"
  39. echo -e "\033[32mVersion:1.18.1 Available...\033[0m"
  40. echo -e "\033[32mVersion:1.18.0 Available...\033[0m"
  41. echo -e "\033[32mVersion:1.17.11 Available...\033[0m"
  42. echo -e "\033[32mVersion:1.17.9 Available...\033[0m"
  43. echo -e "\033[32mVersion:1.17.8 Available...\033[0m"
  44. echo -e "\033[32mVersion:1.17.7 Available...\033[0m"
  45. echo -e "\033[32mVersion:1.17.6 Available...\033[0m"
  46. echo -e "\033[32mVersion:1.17.5 Available...\033[0m"
  47. echo -e "\033[32mVersion:1.17.4 Available...\033[0m"
  48. echo -e "\033[32mVersion:1.17.3 Available...\033[0m"
  49. echo -e "\033[32mVersion:1.17.2 Available...\033[0m"
  50. echo -e "\033[32mVersion:1.17.1 Available...\033[0m"
  51. echo -e "\033[32mVersion:1.17.0 Available...\033[0m"
  52. }

  53. function system_info (){
  54. echo -e "\033[32m-------------System Infomation-------------\033[0m"
  55. echo -e "\033[32m>>>>>> 系统运行时间:${Uptime_day}${Uptime} \033[0m"
  56. echo -e "\033[32m>>>>>> 操作系统: ${SYS_VERSION} \033[0m"
  57. echo -e "\033[32m>>>>>> 内网IP: ${IPADDR} \033[0m"
  58. echo -e "\033[32m>>>>>> CPU型号:${CPU_Model} \033[0m"
  59. echo -e "\033[32m>>>>>> CPU核数: ${CPU_NUM} \033[0m"
  60. echo -e "\033[32m>>>>>> ${DISK_INFO} \033[0m"
  61. echo -e "\033[32m>>>>>> ${DISK_Avail} \033[0m"
  62. echo -e "\033[32m>>>>>> ${MEM_INFO} \033[0m"
  63. echo -e "\033[32m>>>>>> ${MEM_Avail} \033[0m"
  64. echo -e "\033[32m>>>>>> ${LOAD_INFO} \033[0m"
  65. }

  66. functionInstall_Kubernetes_Master(){
  67. system_info
  68. echo -e "\033[32m================================================\033[0m"
  69. echo -e "\033[32m>>>>>> 正在安装Kubernetes版本:${Version}\033[0m"
  70. echo -e "\033[32m================================================\033[0m"
  71. echo -e "\033[32m>>>>>> 一键安装Kubernetes集群_Master端\033[0m"
  72. echo -e "\033[32m================================================\033[0m"
  73. echo -e "\033[32m>>>>>> 配置网易yum源\033[0m"
  74. mkdir -p /etc/yum.repos.d/bak
  75. \mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
  76. [ -f $(which wget) ] || yum -y install wget >/dev/null
  77. wget -q -P /etc/yum.repos.d http://mirrors.163.com/.help/CentOS7-Base-163.repo
  78. yum clean all >/dev/null
  79. yum makecache >/dev/null

  80. echo -e "\033[32m================================================\033[0m"
  81. echo -e "\033[32m>>>>>> 更新系统\033[0m"
  82. yum -y update >/dev/null

  83. echo -e "\033[32m================================================\033[0m"
  84. echo -e "\033[32m>>>>>> 关闭防火墙、selinux\033[0m"
  85. SYSTEM_VERSION=$(awk -F. '{print $1}' /etc/redhat-release |awk '{print $NF}')
  86. if [ ${SYSTEM_VERSION} -eq 6 ];then
  87. service iptables stop
  88. chkconfig iptables off
  89. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  90. setenforce 0 >/dev/null
  91. else
  92. systemctl stop firewalld.service
  93. systemctl disable firewalld.service
  94. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  95. setenforce 0 >/dev/null
  96. fi

  97. echo -e "\033[32m================================================\033[0m"
  98. echo -e "\033[32m>>>>>> 配置主机名\033[0m"
  99. hostnamectl set-hostname master

  100. echo -e "\033[32m================================================\033[0m"
  101. echo -e "\033[32m>>>>>> 配置hosts\033[0m"
  102. echo "${IPADDR} master" >>/etc/hosts

  103. echo -e "\033[32m================================================\033[0m"
  104. echo -e "\033[32m>>>>>> 关闭swap,注释swap分区\033[0m"
  105. swapoff -a
  106. sed -i '/swap/s/^/#/g' /etc/fstab

  107. echo -e "\033[32m================================================\033[0m"
  108. echo -e "\033[32m>>>>>> 配置内核参数,将桥接的IPv4流量传递到iptables的链\033[0m"
  109. cat >/etc/sysctl.d/k8s.conf <<EOF
  110. net.bridge.bridge-nf-call-ip6tables = 1
  111. net.bridge.bridge-nf-call-iptables = 1
  112. EOF
  113. modprobe br_netfilter
  114. sysctl -p /etc/sysctl.d/k8s.conf >/dev/null
  115. echo "1" > /proc/sys/net/ipv4/ip_forward

  116. echo -e "\033[32m================================================\033[0m"
  117. echo -e "\033[32m>>>>>> 安装Docker-ce、配置开机自启动、启动Docker\033[0m"
  118. rpm -qa |grep docker |grep -v grep >/dev/null
  119. if [ $? -ne 0 ];then
  120. #curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
  121. yum -y install yum-utils device-mapper-persistent-data lvm2 >/dev/null
  122. yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  123. if [ ${Version} = "1.18.8" -o ${Version} = "1.19.0" ];then
  124. version="19.03.12"
  125. else
  126. version="18.09.9"
  127. fi
  128. yum -y install docker-ce-${version} docker-ce-cli-${version} containerd.io >/dev/null
  129. systemctl enable docker
  130. systemctl start docker
  131. if [ $? -eq 0 ];then
  132. echo -e "\033[32m================================================\033[0m"
  133. echo -e "\033[32m>>>>>> Docker Start Success...\033[0m"
  134. else
  135. echo -e "\033[32m================================================\033[0m"
  136. echo -e "\033[32m>>>>>> Docker Start Failed...\033[0m"
  137. exit 1
  138. fi
  139. else
  140. echo -e "\033[32m================================================\033[0m"
  141. echo -e "\033[32m>>>>>> Docker版本:$(docker --version |awk -F ',' '{print $1}') \033[0m"
  142. fi

  143. echo -e "\033[32m================================================\033[0m"
  144. echo -e "\033[32m>>>>>> 添加阿里云docker仓库加速器\033[0m"
  145. mkdir -p /etc/docker
  146. cat >/etc/docker/daemon.json <<EOF
  147. {
  148. "registry-mirrors": ["https://fl791z1h.mirror.aliyuncs.com"],
  149. "exec-opts": ["native.cgroupdriver=systemd"]
  150. }
  151. EOF
  152. systemctl daemon-reload
  153. systemctl restart docker

  154. echo -e "\033[32m================================================\033[0m"
  155. echo -e "\033[32m>>>>>> 添加阿里kubernetes源\033[0m"
  156. cat >/etc/yum.repos.d/kubernetes.repo <<EOF
  157. [kubernetes]
  158. name=Kubernetes
  159. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
  160. enabled=1
  161. gpgcheck=1
  162. repo_gpgcheck=1
  163. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  164. EOF

  165. echo -e "\033[32m================================================\033[0m"
  166. echo -e "\033[32m>>>>>> 安装kubectl、kubelet、kubeadm\033[0m"
  167. yum -y install kubectl-${Version} kubelet-${Version} kubeadm-${Version} >/dev/null
  168. rpm -qa |grep kubelet >/dev/null
  169. if [ $? -eq 0 ];then
  170. systemctl enable kubelet
  171. systemctl start kubelet
  172. if [ $? -eq 0 ];then
  173. echo -e "\033[32m================================================\033[0m"
  174. action "kubelet-${Version} Start Success..." /bin/true
  175. else
  176. echo -e "\033[32m================================================\033[0m"
  177. action "kubelet-${Version} Start Failed..." /bin/false
  178. exit 1
  179. fi
  180. else
  181. action "kubelet-${Version} Install Failed..." /bin/false
  182. exit 1
  183. fi

  184. echo -e "\033[32m================================================\033[0m"
  185. echo -e "\033[32m>>>>>> 初始化k8s集群,这里指定版本为${Version}\033[0m"
  186. #echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
  187. kubeadm init --kubernetes-version=${Version} \
  188. --apiserver-advertise-address=${IPADDR} \
  189. --image-repository registry.aliyuncs.com/google_containers \
  190. --service-cidr=172.16.0.0/16 --pod-network-cidr=172.17.0.0/16

  191. mkdir -p ${HOME}/.kube
  192. sudo cp -i /etc/kubernetes/admin.conf ${HOME}/.kube/config
  193. sudo chown $(id -u):$(id -g) ${HOME}/.kube/config

  194. echo -e "\033[32m================================================\033[0m"
  195. echo -e "\033[32m>>>>>> kubectl命令补全功能\033[0m"
  196. rpm -qa |grep bash-completion >/dev/null
  197. if [ $? -ne 0 ];then
  198. yum -y install bash-completion >/dev/null
  199. source /etc/profile.d/bash_completion.sh
  200. fi

  201. #grep "kubectl completion bash" ~/.bashrc >/dev/null
  202. #if [ $? -ne 0 ];then
  203. # echo "# kubectl命令补全功能" >> ~/.bashrc
  204. # echo "source <(kubectl completion bash)" >> ~/.bashrc
  205. #fi

  206. echo -e "\033[32m================================================\033[0m"
  207. echo -e "\033[32m>>>>>> 安装calico网络\033[0m"
  208. kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

  209. echo -e "\033[32m================================================\033[0m"
  210. echo -e "\033[32m>>>>>> 安装kubernetes-dashboard\033[0m"
  211. wget -q https://mirrors.yangxingzhen.com/kubernetes/recommended.yaml
  212. kubectl create -f recommended.yaml

  213. sleep 60 >/dev/null
  214. echo -e "\033[32m================================================\033[0m"
  215. echo -e "\033[32m>>>>>> 查看凭证,获取token\033[0m"
  216. kubectl -n kubernetes-dashboard get secret
  217. Token=$(kubectl -n kubernetes-dashboard get secret |awk '/kubernetes-dashboard-token/ {print $1}')
  218. #echo "Token:$(kubectl describe secrets -n kubernetes-dashboard ${Token} |grep token |awk 'NR==3 {print $2}')"

  219. sleep 60 >/dev/null
  220. echo -e "\033[32m================================================\033[0m"
  221. echo -e "\033[32m>>>>>> 查看node\033[0m"
  222. kubectl get node

  223. sleep 60 >/dev/null
  224. echo -e "\033[32m================================================\033[0m"
  225. echo -e "\033[32m>>>>>> 查看pod\033[0m"
  226. kubectl get pod --all-namespaces

  227. sleep 60 >/dev/null
  228. echo -e "\033[32m================================================\033[0m"
  229. echo -e "\033[32m>>>>>> 查看service\033[0m"
  230. kubectl get svc -n kubernetes-dashboard

  231. echo -e "\033[32m================================================\033[0m"
  232. echo -e "\033[32m>>>>>> 授权kubernetes-dashboard,防止找不到namespace资源\033[0m"
  233. kubectl create clusterrolebinding serviceaccount-cluster-admin --clusterrole=cluster-admin --user=system:serviceaccount:kubernetes-dashboard:kubernetes-dashboard

  234. # 避免时间过短导致kubernetes-dashboard正在创建中,预计运行所有任务完毕时间为:4m40s(1.19.0)
  235. sleep 60 >/dev/null
  236. echo -e "\033[32m==========================================================================\033[0m"
  237. echo -e "\033[32mKubernetes ${Version} 部署完成...\033[0m"
  238. echo -e "\033[32m通过浏览器输入https://${IPADDR}:30000访问kubernetes-dashboard页面\033[0m"
  239. echo -e "\033[32mToken获取:kubectl describe secrets -n kubernetes-dashboard ${Token} |grep token |awk 'NR==3 {print \$2}'\033[0m"
  240. echo -e "\033[32m==========================================================================\033[0m"
  241. }

  242. function Install_Kubernetes_Node (){
  243. IPADDR=$(hostname -I |awk '{print $1}')
  244. echo -e "\033[32m================================================\033[0m"
  245. echo -e "\033[32m>>>>>> 正在安装Kubernetes版本:${Version}\033[0m"
  246. echo -e "\033[32m================================================\033[0m"
  247. echo -e "\033[32m>>>>>> 一键安装Kubernetes集群_Node端\033[0m"
  248. echo -e "\033[32m================================================\033[0m"
  249. echo -e "\033[32m>>>>>> 配置网易yum源\033[0m"
  250. mkdir -p /etc/yum.repos.d/bak
  251. \mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
  252. [ -f $(which wget) ] || yum -y install wget >/dev/null
  253. wget -q -P /etc/yum.repos.d http://mirrors.163.com/.help/CentOS7-Base-163.repo
  254. yum clean all >/dev/null
  255. yum makecache >/dev/null

  256. echo -e "\033[32m================================================\033[0m"
  257. echo -e "\033[32m>>>>>> 更新系统\033[0m"
  258. yum -y update >/dev/null

  259. echo -e "\033[32m================================================\033[0m"
  260. echo -e "\033[32m>>>>>> 关闭防火墙、selinux\033[0m"
  261. SYSTEM_VERSION=$(awk -F. '{print $1}' /etc/redhat-release |awk '{print $NF}')
  262. if [ ${SYSTEM_VERSION} -eq 6 ];then
  263. service iptables stop
  264. chkconfig iptables off
  265. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  266. setenforce 0 >/dev/null
  267. else
  268. systemctl stop firewalld.service
  269. systemctl disable firewalld.service
  270. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  271. setenforce 0 >/dev/null
  272. fi

  273. echo -e "\033[32m================================================\033[0m"
  274. echo -e "\033[32m>>>>>> 配置主机名\033[0m"
  275. NUM=$(ssh root@${Master_IP} "awk '/node/ {print \$2}' /etc/hosts |sed -n '\$p' |awk -F 'node' '{print \$2}'")
  276. [ -z ${NUM} ] && NUM="0"
  277. hostnamectl set-hostname node$(expr ${NUM} + 1)

  278. echo -e "\033[32m================================================\033[0m"
  279. echo -e "\033[32m>>>>>> 配置Hosts\033[0m"
  280. echo "${Master_IP} master" >>/etc/hosts
  281. echo "${IPADDR} node$(expr ${NUM} + 1)" >>/etc/hosts
  282. # 添加Hosts到Master端
  283. ssh root@${Master_IP} "echo "${IPADDR} node$(expr ${NUM} + 1)" >>/etc/hosts"

  284. echo -e "\033[32m================================================\033[0m"
  285. echo -e "\033[32m>>>>>> 关闭swap,注释swap分区\033[0m"
  286. swapoff -a
  287. sed -i '/swap/s/^/#/g' /etc/fstab

  288. echo -e "\033[32m================================================\033[0m"
  289. echo -e "\033[32m>>>>>> 配置内核参数,将桥接的IPv4流量传递到iptables的链\033[0m"
  290. cat >/etc/sysctl.d/k8s.conf <<EOF
  291. net.bridge.bridge-nf-call-ip6tables = 1
  292. net.bridge.bridge-nf-call-iptables = 1
  293. EOF
  294. modprobe br_netfilter
  295. sysctl -p /etc/sysctl.d/k8s.conf >/dev/null
  296. echo "1" > /proc/sys/net/ipv4/ip_forward

  297. echo -e "\033[32m================================================\033[0m"
  298. echo -e "\033[32m>>>>>> 安装Docker-ce、配置开机自启动、启动Docker\033[0m"
  299. rpm -qa |grep docker |grep -v grep >/dev/null
  300. if [ $? -ne 0 ];then
  301. #curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
  302. yum -y install yum-utils device-mapper-persistent-data lvm2 >/dev/null
  303. yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  304. if [ ${Version} = "1.18.8" -o ${Version} = "1.19.0" ];then
  305. version="19.03.12"
  306. else
  307. version="18.09.9"
  308. fi
  309. yum -y install docker-ce-${version} docker-ce-cli-${version} containerd.io >/dev/null
  310. systemctl enable docker
  311. systemctl start docker
  312. if [ $? -eq 0 ];then
  313. echo -e "\033[32m================================================\033[0m"
  314. echo -e "\033[32m>>>>>> Docker Start Success...\033[0m"
  315. else
  316. echo -e "\033[32m================================================\033[0m"
  317. echo -e "\033[32m>>>>>> Docker Start Failed...\033[0m"
  318. exit 1
  319. fi
  320. else
  321. echo -e "\033[32m================================================\033[0m"
  322. echo -e "\033[32m>>>>>> Docker版本:$(docker --version |awk -F ',' '{print $1}') \033[0m"
  323. fi

  324. echo -e "\033[32m================================================\033[0m"
  325. echo -e "\033[32m>>>>>> 添加阿里云docker仓库加速器\033[0m"
  326. mkdir -p /etc/docker
  327. cat >/etc/docker/daemon.json <<EOF
  328. {
  329. "registry-mirrors": ["https://fl791z1h.mirror.aliyuncs.com"],
  330. "exec-opts": ["native.cgroupdriver=systemd"]
  331. }
  332. EOF
  333. systemctl daemon-reload
  334. systemctl restart docker

  335. echo -e "\033[32m================================================\033[0m"
  336. echo -e "\033[32m>>>>>> 添加阿里kubernetes源\033[0m"
  337. cat >/etc/yum.repos.d/kubernetes.repo <<EOF
  338. [kubernetes]
  339. name=Kubernetes
  340. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
  341. enabled=1
  342. gpgcheck=1
  343. repo_gpgcheck=1
  344. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  345. EOF

  346. echo -e "\033[32m================================================\033[0m"
  347. echo -e "\033[32m>>>>>> 安装kubectl、kubelet、kubeadm\033[0m"
  348. yum -y install kubectl-${Version} kubelet-${Version} kubeadm-${Version} >/dev/null
  349. rpm -qa |grep kubelet >/dev/null
  350. if [ $? -eq 0 ];then
  351. systemctl enable kubelet
  352. systemctl start kubelet
  353. if [ $? -eq 0 ];then
  354. echo -e "\033[32m================================================\033[0m"
  355. action "kubelet-${Version} Start Success..." /bin/true
  356. else
  357. echo -e "\033[32m================================================\033[0m"
  358. action "kubelet-${Version} Start Failed..." /bin/false
  359. exit 1
  360. fi
  361. else
  362. action "kubelet-${Version} Install Failed..." /bin/false
  363. exit 1
  364. fi

  365. # 加入集群
  366. ssh root@${Master_IP} "kubeadm token list" >/dev/null
  367. if [ $? -eq 0 ];then
  368. TOKEN=$(ssh root@${Master_IP} "kubeadm token list |grep token |awk '{print \$1}' |sed -n '1p'")
  369. Hash=$(ssh root@${Master_IP} "openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'")
  370. kubeadm join ${Master_IP}:6443 --token ${TOKEN} --discovery-token-ca-cert-hash sha256:${Hash}
  371. else
  372. ssh root@${Master_IP} "kubeadm token create" >/dev/null
  373. TOKEN=$(ssh root@${Master_IP} "kubeadm token list |grep token |awk '{print \$1}' |sed -n '1p'")
  374. Hash=$(ssh root@${Master_IP} "openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'")
  375. kubeadm join ${Master_IP}:6443 --token ${TOKEN} --discovery-token-ca-cert-hash sha256:${Hash}
  376. fi
  377. }

  378. function Main (){
  379. stty erase '^H'
  380. Code=""
  381. while true
  382. do
  383. read -p "$(echo -e "\033[32m请输入需要安装的节点名称[master/node]:\033[0m")" Name
  384. if [ -z ${Name} ];then
  385. echo -e "\033[31m输入错误,节点名称不能为空...\033[0m"
  386. elif [ ${Name} = "master" ];then
  387. while true
  388. do
  389. read -p "$(echo -e "\033[32m请输入需要安装的Kubernetes版本号[Default:1.19.0],输入s/S查看可用版本:\033[0m")" Version
  390. if [ -z ${Version} ];then
  391. Version="1.19.0"
  392. Install_Kubernetes_Master
  393. Code="break"
  394. elif [ "${Version}" = "q" -o "${Version}" = "Q" ];then
  395. exit 1
  396. elif [ "${Version}" = "s" -o "${Version}" = "S" ];then
  397. Kubernetes_Version
  398. else
  399. Install_Kubernetes_Master
  400. Code="break"
  401. fi
  402. ${Code}
  403. done
  404. elif [ ${Name} = "node" ];then
  405. while true
  406. do
  407. read -p "$(echo -e "\033[32m请输入需要安装的Kubernetes版本号[Default:1.19.0],输入s/S查看可用版本:\033[0m")" Version
  408. if [ -z ${Version} ];then
  409. Version="1.19.0"
  410. Code="break"
  411. elif [ "${Version}" = "q" -o "${Version}" = "Q" ];then
  412. exit 1
  413. elif [ "${Version}" = "s" -o "${Version}" = "S" ];then
  414. Kubernetes_Version
  415. else
  416. while true
  417. do
  418. read -p "$(echo -e "\033[32m请输入需要Kubernetes的Master节点IP:\033[0m")" Master_IP
  419. if [ -z ${Master_IP} ];then
  420. echo -e "\033[31m输入错误,Master节点IP不能为空...\033[0m"
  421. else
  422. Install_Kubernetes_Node
  423. Code="break"
  424. fi
  425. ${Code}
  426. done
  427. fi
  428. ${Code}
  429. done
  430. else
  431. echo -e "\033[31m输入错误,节点名称不存在...\033[0m"
  432. fi
  433. ${Code}
  434. done
  435. }

  436. Main

脚本执行方式:

Master端

[root@Edu_1 ~]# sh auto_install_kubernetes.sh

Kubernetes自动化一键部署脚本

Kubernetes自动化一键部署脚本

Node端

[root@Test ~]# sh auto_install_kubernetes.sh

Kubernetes自动化一键部署脚本

脚本执行完之后在Master端执行

Kubernetes自动化一键部署脚本

  • 输入编号:7500,直达文章
  • 输入m|M,直达目录列表