继:使用shell脚本自动部署单master节点多node k8s集群。
shell自动部署k8s集群:新增加的work node节点加入k8s集群
一、准备工作
这里演示添加 172.29.6.165 k8s-04 机器为例!
1.1、修改新增node计算机名。
[root@localhost kubeadm-single-master]# hostnamectl set-hostname k8s-04
1.2、修改hosts文件添加新node主机IP-计算机名解析。
[root@k8s-01 kubeadm-single-master]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.29.6.187 k8s-01
172.29.6.161 k8s-02
172.29.6.164 k8s-03
172.29.6.165 k8s-04
1.3、将hosts文件scp到所有master、work主机。
注:这一步通过脚本完成。
hosts=$(cat /etc/hosts | awk '{print $1}')
for h in ${hosts[*]}
do
echo ">>> ${h} copy-hosts"
scp /etc/hosts root@${h}:/etc/
done
1.4、修改environment.sh 文件,修改其中的 node_ips 和计算机名。
#!/usr/bin/bash
# 集群master 服务器ip 数组 默认 etcd集群也是用集群master服务器
export MASTER_NODE_IPS=(172.29.6.187)
#Master 服务器主机名 数组
export MASTER_NODE_NAMES=(k8s-01)
# 集群各Worker机器 IP 数组。
export NODE_IPS=(172.29.6.165)
# 集群各Worker IP 对应的主机名数组
export NODE_NAMES=(k8s-04)
1.5、运行脚本文件。
[root@k8s-01 kubeadm-single-master]# bash worknode-join-k8s.sh
1.6、查看kubeadm join字符串
[root@k8s-01 kubeadm-single-master]# cat /usr/local/src/kubeadm-deploy.log
1.7、新增节点手动加入k8s集群。
[root@k8s-04 src]# kubeadm join 172.29.6.187:6443 --token abcdef.0123456789abcdef \
> --discovery-token-ca-cert-hash sha256:f6dea2fe18eb8b08347430e2a50ff401a5332332eb920f5ba8c3812a928fac0b
W0211 15:49:33.235035 4519 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.18" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
1.8、master上查看node
[root@k8s-01 kubeadm-single-master]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-01 Ready master 4h35m v1.18.20
k8s-02 Ready <none> 4h29m v1.18.20
k8s-03 Ready <none> 4h29m v1.18.20
k8s-04 NotReady <none> 24s v1.18.20
同样需要等calico组件和proxy组件准备妥当就可以了。
[root@k8s-01 kubeadm-single-master]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-01 Ready master 5h14m v1.18.20
k8s-02 Ready <none> 5h8m v1.18.20
k8s-03 Ready <none> 5h8m v1.18.20
k8s-04 Ready <none> 40m v1.18.20