kubernetes不重置叢集更新節點ip
阿新 • • 發佈:2022-05-13
kubernetes節點ip變更後通訊就會出現問題,我們只需要通過kubeadm init phase命令,重新生成config檔案和簽名檔案就可以了。
1、備份當前k8s叢集配置檔案
cp -r /etc/kubernetes /etc/kubernetes_bak$(date +"%Y%m%d%H%M%S")
2、批量替換k8s配置檔案中ip地址(如果配置了hosts、kubelet,同樣需要一起替換)
sed -i "s/${OLD_IP}/${NEW_IP}/g" `grep -rl "${OLD_IP}" /etc/kubernetes`
sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/hosts if [ -f "/etc/default/kubelet" ]; then sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/default/kubelet elif [ -f "/etc/sysconfig/kubelet" ]; then sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/sysconfig/kubelet fi
3、重新生成apiserver證書
rm -rf /etc/kubernetes/pki/apiserver.* kubeadm init phase certs apiserver --apiserver-advertise-address ${NEW_IP}
4、重新生成admin配置(並更新k8sconfig)
rm -rf /etc/kubernetes/admin.conf kubeadm init phase kubeconfig admin --apiserver-advertise-address ${NEW_IP} \cp /etc/kubernetes/admin.conf ~/.kube/config
5、更新kube-proxy配置
kubectl -n kube-system get cm kube-proxy -oyaml > /etc/kubernetes/kube-proxy.conf sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/kubernetes/kube-proxy.conf kubectl -n kube-system apply -f /etc/kubernetes/kube-proxy.conf
注意:如果不更新kube-proxy配置,連線將會失敗(因為default.svc.kubernetes對應地址依然是舊的ip)
5、重啟docker和kubelet
systemctl restart docker && systemctl restart kubelet
6、檢查節點和系統狀態
kubectl get node | grep master | grep Ready kubectl get pod -n kube-system |grep -vE 'STATUS|Running'