將 master 節點伺服器從 k8s 叢集中移除並重新加入

dudu發表於2020-01-10

背景

1 臺 master 加入叢集后發現忘了修改主機名,而在 k8s 叢集中修改節點主機名非常麻煩,不如將 master 退出叢集改名並重新加入叢集(前提是用的是高可用叢集)。

操作步驟

ssh 登入另外一臺 master 節點將要改名的 master 節點移出叢集。

kubectl drain blog-k8s-n0
kubectl delete node blog-k8s-n0

登入已退出叢集的 master 伺服器重置 kubelet 配置並重新加入叢集。

kubeadm reset
kubeadm join k8s-api:6443 --token ****** \
    --discovery-token-ca-cert-hash ****** \
    --control-plane --certificate-key ****** \
    --v=5

加入失敗,錯誤資訊如下:

I0109 21:26:48.440711    3893 etcd.go:107] etcd endpoints read from pods: https://10.0.1.81:2379,https://10.0.1.32:2379,https://10.0.1.33:2379
I0109 21:26:48.455921    3893 etcd.go:166] etcd endpoints read from etcd: https://10.0.1.81:2379,https://10.0.1.32:2379,https://10.0.1.33:2379
I0109 21:26:48.455975    3893 etcd.go:125] update etcd endpoints: https://10.0.1.81:2379,https://10.0.1.32:2379,https://10.0.1.33:2379
I0109 21:27:08.469938    3893 etcd.go:388] Failed to get etcd status for https://10.0.1.81:2379: failed to dial endpoint https://10.0.1.81:2379 with maintenance client: context deadline exceeded
failed to dial endpoint https://10.0.1.81:2379 with maintenance client: context deadline exceeded
etcd cluster is not healthy

從錯誤資訊看 etcd 叢集不知道 10.0.1.81 已經退出 k8s 叢集,etcd 叢集中還儲存著 10.0.1.81 的資訊,所以在加入時連線 10.0.1.81 失敗。

登入其中一臺正常的 master ,進入 etcd 容器:

docker exec -it $(docker ps -f name=etcd_etcd -q) /bin/sh

檢視 etcd 叢集的成員列表,果然其中還有那臺已經退出叢集的伺服器 blog-k8s-n0

$ etcdctl --endpoints 127.0.0.1:2379 --cacert /etc/kubernetes/pki/etcd/ca.crt --cert /etc/kubernetes/pki/etcd/server.crt --key /etc/kubernetes/pki/etcd/server.key member list
12637f5ec2bd02b8, started, blog-k8s-n0, https://10.0.1.81:2380, https://10.0.1.81:2379, false
17d58f8d29164d23, started, k8s-master1, https://10.0.1.32:2380, https://10.0.1.32:2379, false
19441808830db070, started, k8s-master2, https://10.0.1.33:2380, https://10.0.1.33:2379, false

在容器中將這臺伺服器從 etcd 叢集中移除

etcdctl --endpoints 127.0.0.1:2379 --cacert /etc/kubernetes/pki/etcd/ca.crt --cert /etc/kubernetes/pki/etcd/server.crt --key /etc/kubernetes/pki/etcd/server.key member remove 12637f5ec2bd02b8

移除之後之前退出叢集的伺服器就能成功加入叢集。

相關文章