先看新效果图
node节点脚本代码
#!/usr/bin/env bash
# Author:xiaolang
# Blog:https://www.cnblogs.com/xiaolang666
# node需要部署组件kubelet、kube-proxy、flanneld
# 逻辑判断函数执行失败就终止运行
function judge() {
if [ $2 -eq 0 ];then
echo -e "\033[32m $1 \033[0m \n"
else
echo -e "\033[41;36m $1 \033[0m \n"
exit 0
fi
}
# 分发k8s软件包到node节点
for i in n1 n2
do
scp /usr/local/sbin/{flanneld,mk-docker-opts.sh,kubelet,kube-proxy} root@$i:/usr/local/sbin/
judge "$i 网络插件flanneld 集群通信插件kubelet 容器通信插件kube-proxy 安装完毕" $?
done
# 分发证书
for i in n1 n2
do
ssh root@$i "mkdir -pv /etc/kubernetes/ssl"
scp -pr /etc/kubernetes/ssl/{ca*.pem,admin*pem,kube-proxy*pem} root@$i:/etc/kubernetes/ssl/
judge "k8s 集群证书分发完毕" $?
done
# 分发配置文件
# flanneld、etcd的证书、docker.service
for i in n1 n2
do
ssh root@$i "mkdir -pv /etc/etcd/ssl"
scp /etc/etcd/ssl/* root@$i:/etc/etcd/ssl
judge "$i etcd证书分发完毕" $?
done
# 分发flannel和docker的启动脚本
for i in n1 n2
do
scp /usr/lib/systemd/system/docker.service root@$i:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/flanneld.service root@$i:/usr/lib/systemd/system/
judge "$i flannel和docker的启动脚本分发完毕" $?
done
# 重载systemd并启动docker和flannel
for i in n1 n2
do
ssh root@$i "systemctl daemon-reload"
ssh root@$i "systemctl enable --now flanneld"
judge "$i flannel启动成功" $?
ssh root@$i "systemctl restart docker"
judge "$i docker重启成功" $?
done
# 部署kubelet 分发配置文件
for i in n1 n2
do
ssh root@$i "mkdir -pv /etc/kubernetes/cfg";
scp /etc/kubernetes/cfg/kubelet.conf root@$i:/etc/kubernetes/cfg/kubelet.conf;
scp /etc/kubernetes/cfg/kubelet-config.yaml root@$i:/etc/kubernetes/cfg/kubelet-config.yaml;
scp /etc/kubernetes/cfg/kubelet.kubeconfig root@$i:/etc/kubernetes/cfg/kubelet.kubeconfig;
scp /etc/kubernetes/cfg/kubelet-bootstrap.kubeconfig root@$i:/etc/kubernetes/cfg/kubelet-bootstrap.kubeconfig;
scp /etc/kubernetes/cfg/token.csv root@$i:/etc/kubernetes/cfg/token.csv;
scp /usr/lib/systemd/system/kubelet.service root@$i:/usr/lib/systemd/system/kubelet.service;
judge "kubelet 配置文件分发完毕" $?
done
# 修改配置文件kubelet-config.yml和kubelet.conf
for i in n1 n2
do
ssh root@$i "sed -i \"s#$(hostname -I | cut -d' ' -f 1)#\$(hostname -I | cut -d' ' -f 1)#g\" /etc/kubernetes/cfg/kubelet-config.yaml"
ssh root@$i "mkdir -pv /var/log/kubernetes/kubelet"
ssh root@$i "sed -i \"s#$(hostname)#\$(hostname)#g\" /etc/kubernetes/cfg/kubelet.conf"
judge "$i kubelet 配置文件修改完成" $?
done
# 启动kubelet
for i in n1 n2
do
ssh root@$i "systemctl daemon-reload"
ssh root@$i "systemctl enable --now kubelet"
judge "$i kubelet 启动成功" $?
done
# 部署kube-proxy
for i in n1 n2
do
scp /etc/kubernetes/cfg/kube-proxy.conf root@$i:/etc/kubernetes/cfg/kube-proxy.conf;
scp /etc/kubernetes/cfg/kube-proxy-config.yaml root@$i:/etc/kubernetes/cfg/kube-proxy-config.yaml;
scp /usr/lib/systemd/system/kube-proxy.service root@$i:/usr/lib/systemd/system/kube-proxy.service;
scp /etc/kubernetes/cfg/kube-proxy.kubeconfig root@$i:/etc/kubernetes/cfg/kube-proxy.kubeconfig;
judge "$i kube-proxy 配置文件分发完毕" $?
done
# 修改kube-proxy-config.ymal中IP和主机名
for i in n1 n2
do
ssh root@$i "sed -i \"s#$(hostname)#\$(hostname)#g\" /etc/kubernetes/cfg/kube-proxy-config.yaml "
ssh root@$i "mkdir -pv /var/log/kubernetes/kube-proxy"
ssh root@$i "sed -i \"s#$(hostname -I | cut -d' ' -f 1)#\$(hostname -I | cut -d' ' -f 1)#g\" /etc/kubernetes/cfg/kube-proxy-config.yaml "
judge "$i kube-proxy-config.yaml 配置文件修改完成" $?
done
# 启动kube-proxy
for i in n1 n2
do
ssh root@$i "systemctl daemon-reload"
ssh root@$i "systemctl enable --now kube-proxy"
judge "$i kube-proxy 启动成功" $?
done
# 批准加入
kubectl certificate approve `kubectl get csr | grep "Pending" | awk '{print $1}'`
judge "kubelet节点批准加入" $?
# 查看加入状态
kubectl get csr
judge "kubelet节点加入成功" $?
# 查看加入节点
sleep 5
kubectl get nodes
# 设置集群角色master 并打上污点禁止调度到master节点
for i in master1 master2 master3
do
kubectl taint nodes $i node-role.kubernetes.io/master=$i:NoSchedule --overwrite
judge "$i 集群角色master配置完毕" $?
done
# 设置集群角色node
for i in node1 node2
do
kubectl label nodes $i node-role.kubernetes.io/node=$i
judge "$i 集群角色node配置完毕" $?
done