有时候因为场景需要,我们需要修改kubernetes节点的主机名,假设有三个节点分别是:
host1,host2,host3,cni组件使用calico,需要将host1改为master。
在修改kubelet节点主机名的时候也需要修改calico服务中的主机名。具体步骤如下:
一.修改系统主机名
[root@host1 ~]# hostname master
二.修改kubelet节点主机名
修改kubelet启动参数--hostname-override=master
重启kubelet服务
[root@master ~]# systemctl restart kubelet
这时查看kubelet运行日志
[root@master ~]# journalctl -xe -u kubelet
会看到如下报错
Mar 23 13:15:27 master kubelet[13508]: E0323 13:15:27.320556 13508 kubelet_node_status.go:106] Unable to register node "master" with API server: nodes "master" is forbidden: node "host1" cannot modify node "master"
停止kubelet服务并删除当前节点
[root@master ~]# systemctl stop kubelet[root@master ~]# kubectl delete node host1
删除kubelet.kubeconfig,kubelet.key,kubelet.crt,kubelet-client.key和kubelet-client.crt
[root@master ~]# rm -f /etc/kubernetes/kubelet.kubeconfig[root@master ~]# rm -f /etc/kubernetes/ssl/kubelet*
再重启kubelet
[root@master ~]# systemctl restart kubelet
查看证书状态
[root@master ~]# kubectl get csrNAME AGE REQUESTOR CONDITIONnode-csr-GIAqC5LBI_7c6TlMW8wugv_TlHfs1CShZhnEyLgxvSI 1m kubelet-bootstrap Pending
允许证书
[root@master ~]# kubectl certificate approve node-csr-GIAqC5LBI_7c6TlMW8wugv_TlHfs1CShZhnEyLgxvSI
再次查看证书状态
[root@master ~]# kubectl get csrNAME AGE REQUESTOR CONDITIONnode-csr-GIAqC5LBI_7c6TlMW8wugv_TlHfs1CShZhnEyLgxvSI 1m kubelet-bootstrap Approved,Issued
查看节点状态
[root@master ~]# kubectl get nodeNAME STATUS ROLES AGE VERSIONhost2 Ready34m v1.9.5host3 Ready 34m v1.9.5master Ready 18s v1.9.5
三.修改calico节点主机名
这时候查看calico运行状态
[root@master ~]# calicoctl node statusCalico process is not running.
calico服务会输出如下错误日志
[WARNING][9] startup.go 757: calico node 'host1' is already using the IPv4 address 10.233.119.0
切换到其他节点上查看,如host2
[root@host2 ~]# calicoctl get nodeNAME host1 host2 host3[root@host2 ~]# calicoctl node statusCalico process is running.IPv4 BGP status+--------------+-------------------+-------+----------+--------------------------------+| PEER ADDRESS | PEER TYPE | STATE | SINCE | INFO |+--------------+-------------------+-------+----------+--------------------------------+| 10.21.21.254 | node-to-node mesh | start | 05:16:47 | Active Socket: Connection || | | | | refused || 10.21.21.245 | node-to-node mesh | up | 04:44:35 | Established |+--------------+-------------------+-------+----------+--------------------------------+IPv6 BGP statusNo IPv6 peers found.
获取host1节点配置,保存输出内容到文件master.yaml中
[root@host2 ~]# calicoctl get node host1 -o yamlapiVersion: projectcalico.org/v3kind: Nodemetadata: creationTimestamp: 2018-03-23T04:44:29Z name: host1 resourceVersion: "485" uid: dfb352cf-2e54-11e8-82e7-52540000361bspec: bgp: ipv4Address: 10.21.21.254/16 ipv4IPIPTunnelAddr: 10.233.119.0
删除host1
[root@host2 ~]# calicoctl delete node host1Successfully deleted 1 'Node' resource(s)
修改master.yaml
apiVersion: projectcalico.org/v3kind: Nodemetadata: name: master uid: dfb352cf-2e54-11e8-82e7-52540000361bspec: bgp: ipv4Address: 10.21.21.254/16 ipv4IPIPTunnelAddr: 10.233.119.0
创建calico节点
[root@host2 ~]# calicoctl apply -f master.yaml Successfully applied 1 'Node' resource(s)
删除异常的calico Pod
[root@host2 ~]# kubectl get pod -n kube-systemNAME READY STATUS RESTARTS AGEcalico-kube-controllers-5f47974799-ttz7s 1/1 Running 0 6mcalico-node-274q9 2/2 Running 0 40mcalico-node-dp8dz 2/2 Running 0 40mcalico-node-rh2kd 1/2 CrashLoopBackOff 5 5m[root@host2 ~]# kubectl delete pod -n kube-system calico-node-rh2kdpod "calico-node-rh2kd" deleted
等待calico Pod重建
[root@host2 ~]# kubectl get pod -n kube-systemNAME READY STATUS RESTARTS AGEcalico-kube-controllers-5f47974799-ttz7s 1/1 Running 0 7mcalico-node-274q9 2/2 Running 0 40mcalico-node-9th4r 2/2 Running 0 12scalico-node-dp8dz 2/2 Running 0 40m