k8s证书过期

[root@k8s-master102 ~]# kubectl get pod -A
Unable to connect to the server: x509: certificate has expired or is not yet valid: current time 2023-07-25T15:14:00+08:00 is after 2023-07-24T16:25:58Z

解决方案

  1. 备份 kubernetes配置
cp -r /etc/kubernetes  /etc/kubernetes_bak

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ed3YZzZ5-1690270542353)(evernotecid://9C1982F7-8B3C-4BAE-AF9A-5C0FF5841ACD/appyinxiangcom/22553815/ENResource/p1112)]

  1. 检测证书过期
kubeadm certs check-expiration

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hK7Xj6i9-1690270542354)(evernotecid://9C1982F7-8B3C-4BAE-AF9A-5C0FF5841ACD/appyinxiangcom/22553815/ENResource/p1113)]

  1. 更新证书
kubeadm certs renew all

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jgZrJpVC-1690270542355)(evernotecid://9C1982F7-8B3C-4BAE-AF9A-5C0FF5841ACD/appyinxiangcom/22553815/ENResource/p1114)]

  1. 重启kube-apiserver、kube-controller-manage、kube-scheduler
# 重启 kubelet
systemctl restart kubelet

# 重启kube-apiserver、kube-controller-manage、kube-scheduler
docker ps |grep kube-apiserver|grep -v pause|awk '{print $1}'|xargs -i docker restart {}
docker ps |grep kube-controller-manage|grep -v pause|awk '{print $1}'|xargs -i docker restart {}
docker ps |grep kube-scheduler|grep -v pause|awk '{print $1}'|xargs -i docker restart {}

在这里插入图片描述

  1. 更新用户配置
# 备份
cp -rp $HOME/.kube/config $HOME/.kube/config.bak
# 并生成新的配置文件
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

此步骤可能会出现报错error: You must be logged in to the server (Unauthorized),请参考注意事项

注意事项

  1. 第3步更新证书以后需要重新启动
# 重启 kubelet
systemctl restart kubelet

# 重启kube-apiserver、kube-controller-manage、kube-scheduler
# 如果是docker作为容器的话,可执行如下命令。其余容器方法类似
docker ps |grep kube-apiserver|grep -v pause|awk '{print $1}'|xargs -i docker restart {}
docker ps |grep kube-controller-manage|grep -v pause|awk '{print $1}'|xargs -i docker restart {}
docker ps |grep kube-scheduler|grep -v pause|awk '{print $1}'|xargs -i docker restart {}
  1. 第4步不生效,始终报错error: You must be logged in to the server (Unauthorized)

解决方案:

# 删除 cache
$HOME/.kube/cache
# 重新生成
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
Logo

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

更多推荐