【k8s主机重启后服务异常】

主要表现:
主机上相关k8s相关组件均未启动,使用kubectl命令报错(The connection to the server localhost:6443 was refused - did you specify the right host or port?)

重启docker与kubelet服务,启动k8s相关组件服务

[root@master01 opt]# systemctl restart docker
[root@master01 opt]# systemctl restart kubelet
[root@master01 opt]# docker ps
[root@master01 opt]# docker ps -a
[root@master01 opt]# docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)	# 部分组件有依赖性,执行后部分组件未启动,可重复执行该命令,此处启动顺序需要再研究一下
[root@master01 opt]# ss -utnlp | grep 6443	# 查看端口是否启动
[root@master01 opt]# kubectl get pods -n kube-system			# 查看相关组件是否正常执行
[root@master01 opt]# kubectl get pods -n kubernetes-dashboard		# 查看你dashboard是否正常执行,未安装可不执行
# 以上命令需要在所有节点上执行

相关组件状态

[root@master01 opt]# kubectl get pod -n kube-system
NAME                              READY   STATUS    RESTARTS   AGE
coredns-6d56c8448f-f7klr          1/1     Running   0          24h
coredns-6d56c8448f-nsmwr          1/1     Running   0          24h
etcd-master1                      1/1     Running   0          24h
kube-apiserver-master1            1/1     Running   0          24h
kube-controller-manager-master1   1/1     Running   0          24h
kube-flannel-ds-b5gtc             1/1     Running   0          24h
kube-flannel-ds-lq9qf             1/1     Running   0          24h
kube-flannel-ds-mkp27             1/1     Running   0          24h
kube-proxy-689hm                  1/1     Running   0          24h
kube-proxy-6sd9v                  1/1     Running   0          24h
kube-proxy-zxmpq                  1/1     Running   0          24h
kube-scheduler-master1            1/1     Running   0          24h

查看节点状态

[root@master01 opt]# kubectl get nodes
NAME      STATUS     ROLES    AGE   VERSION
master1   NotReady   master   24h   v1.19.3
node01    NotReady   <none>   24h   v1.19.3
node02    NotReady   <none>   24h   v1.19.3

排除节点状态异常(NotReady)
思路一:是否为swap分区未关闭,重启主机后可能swap分区重新开启,检查firewalld和selinux是否关闭,是否因为重启后又开启

[root@master01 opt]# swapoff -a		# 关闭swap分区
[root@master01 opt]# systemctl restart kubelet		# 重启kubelet组件,以上两步在所有节点上执行
[root@master01 opt]# kubectl get nodes		# 查看所有节点
NAME      STATUS   ROLES    AGE   VERSION
master1   Ready    master   25h   v1.19.3
node01    Ready    <none>   25h   v1.19.3
node02    Ready    <none>   25h   v1.19.3

思路二:节点重做,重新添加集群(本人环境为测试环境,所有可使用该方法)
node节点上执行

[root@node01 ~]# kubeadm reset			# 执行该命令时应当慎重,检查是否在node节点上
[root@node01 ~]# systemctl daemon-reload
[root@node01 ~]# systemctl restart kubelet
[root@node01 ~]# rm -rf /etc/kubernetes && systemctl stop kubelet

master主机上执行, 获取安装node节点时的 join 命令

[root@master01 opt]# kubeadm token create --ttl 0 --print-join-command		# 该步骤在master主机上执行
W0328 21:01:03.449078   22889 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
kubeadm join 192.168.1.81:6443 --token 9ojbht.0ls0fy23snu8qt02     --discovery-token-ca-cert-hash sha256:ae20513dd4da4b85fc855572ce7b38a25838596eee6ec7e45184373b24ca4b5f 

node节点上执行

[root@node01 ~]# kubeadm join 192.168.1.81:6443 --token aa15mj.b6acbq9zrzu7r9uz     --discovery-token-ca-cert-hash sha256:ae20513dd4da4b85fc855572ce7b38a25838596eee6ec7e45184373b24ca4b5f

最后在master上查看node节点状态

[root@master01 opt]# kubectl get nodes
NAME      STATUS   ROLES    AGE   VERSION
master1   Ready    master   25h   v1.19.3
node01    Ready    <none>   25h   v1.19.3
node02    Ready    <none>   25h   v1.19.3

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐