修改Kubernetes集群Node主机名
1. 背景
在Kubernetes集群环境中,有时候需要修改 Node 节点主机名,这时我们应该如何操作?
对centos7系统熟悉的同学来说,可能会通过执行如下命令来修改主机名:
hostnamectl set-hostname k8s-nodexxx
systemctl daemon-reload
systemctl restart network
抛开kubernetes集群,使用上面的方式修改主机名,是绝对没有问题的,但是在已经运行过的kubernetes集群中,通过上面的方式,仅仅是修改了所在主机的hostname,kubernetes集群节点中使用的hostname仍然是修改前的hostname。
说到这里了解过kubernetes的同学可能会说,使用如下命令重启一下 kubelet 和kube-proxy不就可以了吗!
kubectl restart kubelet
kubectl restart kube-proxy
但是,很抱歉,当你通过上面的命令重启完kubelet和kube-proxy后,你会惊奇的发现
kusystemctl status kubelet -l
● kubelet.service - Kubernetes Kubelet
Loaded: loaded (/usr/lib/systemd/system/kubelet.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2024-06-14 13:30:31 CST; 17s ago
Docs: https://github.com/kubernetes/kubernetes
Main PID: 13527 (kubelet)
Tasks: 13
Memory: 26.8M
CGroup: /system.slice/kubelet.service
└─13527 /usr/local/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/kubelet-bootstrap.kubeconfig --cert-dir=/etc/kubernetes/ssl --kubeconfig=/etc/kubernetes/kubelet.kubeconfig --config=/etc/kubernetes/kubelet.json --rotate-certificates --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.2 --v=2
Jun 14 13:30:44 k8s-master03 kubelet[13527]: I0614 13:30:44.273685 13527 kubelet_node_status.go:70] "Attempting to register node" node="k8s-master03"
#当前节点修改前的hostname是k8s-worker01,修改后的hostname是k8s-master03
Jun 14 13:30:44 k8s-master03 kubelet[13527]: E0614 13:30:44.275532 13527 kubelet_node_status.go:92] "Unable to register node with API server" err="nodes \"k8s-master03\" is forbidden: node \"k8s-worker01\" is not allowed to modify node \"k8s-master03\"" node="k8s-master03"
Jun 14 13:30:44 k8s-master03 kubelet[13527]: I0614 13:30:44.555345 13527 csi_plugin.go:913] Failed to contact API server when waiting for CSINode publishing: csinodes.storage.k8s.io "k8s-master03" is forbidden: User "system:node:k8s-worker01" cannot get resource "csinodes" in API group "storage.k8s.io" at the cluster scope: can only access CSINode with the same name as the requesting node
Jun 14 13:30:45 k8s-master03 kubelet[13527]: I0614 13:30:45.555175 13527 csi_plugin.go:913] Failed to contact API server when waiting for CSINode publishing: csinodes.storage.k8s.io "k8s-master03" is forbidden: User "system:node:k8s-worker01" cannot get resource "csinodes" in API group "storage.k8s.io" at the cluste