主机必须安装有容器运行时、kubelet 和 kubeadm,master提前部署好高可用,免密登录等不在此赘述。

提前将新加节点/etc/hosts解析加进集群中

echo "192.168.26.104 master03" >> /etc/hosts

1.在唯一的master节点上执行 初始化主节点(master)时上传证书到 etcd 中

[root@master01 ~]# kubeadm init phase upload-certs --upload-certs
I0122 16:13:51.931296    9789 version.go:256] remote version is much newer: v1.29.1; falling back to: stable-1.28
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
1b0dc9596a9e70d39874e799b935a23a26b295650579a23c4e4f23c138784aee

2.继续执行生成加入集群的token

[root@master01 ~]# kubeadm token create --print-join-command
kubeadm join 192.168.26.222:16443 --token kwrdra.06ip9206udqm7ojc --discovery-token-ca-cert-hash sha256:41b6a4f7cf1e374cba12770671176f024e9ea9db31051a21f21e4edab961d754

3.将得到的token和key拼接,得到的命令在要新加入的主机上执行,要加上–control-plane --certificate-key ,不然就会添加为node节点而不是master

[root@master02 ~]# kubeadm join 192.168.26.222:16443 --token kwrdra.06ip9206udqm7ojc --discovery-token-ca-cert-hash sha256:41b6a4f7cf1e374cba12770671176f024e9ea9db31051a21f21e4edab961d754 --control-plane --certificate-key 1b0dc9596a9e70d39874e799b935a23a26b295650579a23c4e4f23c138784aee

#如果在第一个集群使用kubeadm初始化时没有指定vip则会出现以下报错:
[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 -o yaml'
error execution phase preflight: 
One or more conditions for hosting a new control plane instance is not satisfied.

unable to add a new control plane instance to a cluster that doesn't have a stable controlPlaneEndpoint address

Please ensure that:
* The cluster has a stable controlPlaneEndpoint address.
* The certificates that must be shared among control plane instances are provided.

#解决办法如下:
1.在主master节点查看kubeadm-config.yaml
[root@master01 ~]# kubectl -n kube-system get cm kubeadm-config -oyaml|grep controlPlaneEndpoint
如果没有筛选到controlPlaneEndpoint,代表初始化时没有配置

2.编辑kubeadm-config,添加controlPlaneEndpoint
kubectl -n kube-system edit cm kubeadm-config

3.新加入一行插入在此位置
[root@master01 ~]# kubectl -n kube-system get cm kubeadm-config -oyaml|grep -C 3 controlPlaneEndpoint
    apiVersion: kubeadm.k8s.io/v1beta3
    certificatesDir: /etc/kubernetes/pki
    clusterName: kubernetes
    controlPlaneEndpoint: 192.168.26.222:16443 #高可用的vip
    controllerManager: {}
    dns: {}
    etcd:

4.再次使用join命令加入master

#根据提示,执行新建文件夹等命令

在这里插入图片描述
4.新加入的master节点无法使用kubectl命令解决

cp /etc/kubernetes/admin.conf ~/.kube/config
#退出再重新登录就可以访问了
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐