how to change the control-palne,master node's name in kube1.20.2

Viewed 1502

I succeeded in setup a control-plane,master node in my vm. Then I copied the vm, trying to join the copied vm to the existed kubernete cluster.

Problem is that the original vm(node)'s name is new-master-1, and the copied node has the same name. Even after I vi /etc/hostname and change the copied vm's name to new-master-2, after running kubectl get nodes in the copied vm, the output name is still new-master-1:

root@new-master-2:/home/hzg# kubectl get nodes
NAME           STATUS   ROLES                  AGE   VERSION
new-master-1   Ready    control-plane,master   32h   v1.20.2

I think I can only join the copied vm as another master node to the cluster after I see that the name change to new-master-2, right? How to change the node's name?

1 Answers

TL;DR:

Run: kubeadm join - adjust this command based on the output from kubeadm token create and add flags like --control-plane and --node-name if needed. Take a look at the kubeadm join before proceeding.


The kubelet registers a node under a particular name. And usually you can Reconfigure a Node's Kubelet in a Live Cluster. But it does not apply when it comes to changing the Node's name. If you try to use kubectl edit node for that, you will get an error:

error: At least one of apiVersion, kind and name was changed

There is a way however. You need to change the hostname and than remove the Node, reset and rejoin it.

Here are the steps. On the Node that needs its name to be changed:

  • Change the hostname.

  • Run: kubectl delete node <nodename> (notice that you still have to use the old Node name)

  • Run: kubeadm reset (as root if needed)

Now, on the original Master Node:

  • Run: export KUBECONFIG=/path/to/admin.conf

  • Run: kubeadm token create --print-join-command

Back on the renamed Node:

  • Run: kubeadm join - adjust this command based on the output from kubeadm token create and add flags like --control-plane and --node-name if needed. Take a look at the kubeadm join before proceeding.

You can check out this source for a tutorial video.


EXAMPLE:

kube-master:~$ kubectl get nodes
NAME          STATUS   
kube-master   Ready    
kube-node-1   Ready    
kube-node-2   Ready 



kube-master:~$ kubectl delete node kube-node-2
node "kube-node-2" deleted



kube-node-2:~$ sudo kubeadm reset
[reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.
[reset] Are you sure you want to proceed? [y/N]: y



kube-node-2:~$ sudo kubeadm join --node-name kube-node-22 <rest-of-the-join-command>

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.



kube-master:~$ kubectl get nodes
NAME           STATUS   
kube-master    Ready    
kube-node-1    Ready    
kube-node-22   Ready 

Result: the name of kube-node-2 was successfully changed to kube-node-22

Related