问题描述

K8S 各个组件需要与 api-server 进行通信,通信使用的证书都存放在 /etc/kubernetes/pki 路径下,kubeadm 生成的证书大部分默认有效期为 1 年,因此需要定时更新证书,否则证书到期会导致整个集群不可用

检查证书是否过期

通过kubeadm certs check-expiration命令来检查集群里的证书是否过期,其中EXPIRES为过期时间, RESIDUAL TIME表示还有多久过期
(我这里因为已经更新过了,所以时间显示都是没过期的)

root@master:/etc/kubernetes/pki# kubeadm certs check-expiration
[check-expiration] Reading configuration from the cluster...
[check-expiration] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
W0617 12:40:58.742236 1515160 utils.go:69] The recommended value for "resolvConf" in "KubeletConfiguration" is: /run/systemd/resolve/resolv.conf; the provided value is: /run/systemd/resolve/resolv.conf

CERTIFICATE                EXPIRES                  RESIDUAL TIME   CERTIFICATE AUTHORITY   EXTERNALLY MANAGED
admin.conf                 Mar 05, 2025 02:53 UTC   260d            ca                      no
apiserver                  Mar 05, 2025 02:52 UTC   260d            ca                      no
apiserver-etcd-client      Feb 22, 2025 02:10 UTC   249d            etcd-ca                 no
apiserver-kubelet-client   Feb 22, 2025 02:10 UTC   249d            ca                      no
controller-manager.conf    Mar 05, 2025 02:53 UTC   260d            ca                      no
etcd-healthcheck-client    Feb 22, 2025 02:10 UTC   249d            etcd-ca                 no
etcd-peer                  Mar 05, 2025 02:53 UTC   260d            etcd-ca                 no
etcd-server                Mar 05, 2025 02:53 UTC   260d            etcd-ca                 no
front-proxy-client         Feb 22, 2025 02:10 UTC   249d            front-proxy-ca          no
scheduler.conf             Mar 05, 2025 02:53 UTC   260d            ca                      no

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Feb 20, 2034 02:10 UTC   9y              no
etcd-ca                 Feb 20, 2034 02:10 UTC   9y              no
front-proxy-ca          Feb 20, 2034 02:10 UTC   9y              no

如果你的kubernetes不是通过kubeadm部署的,或者因为其他原因导致上述命令无法使用,可以使用openssl命令,也可以查看证书的过期时间

# 进入kubernetes中存放证书的目录
root@master:/# cd /etc/kubernetes/pki
root@master:/etc/kubernetes/pki# openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not '
            Not Before: Feb 23 02:10:47 2024 GMT
            Not After : Mar  5 02:52:58 2025 GMT

更新证书

master节点操作

1.证书备份(更改任何系统文件之前,都建议先备份)

cp -r /etc/kubernetes /etc/kubernetes-$(date +%Y-%m-%d).bak
cp -r /var/lib/kubelet /var/lib/kubelet-$(date +%Y-%m-%d).bak

2.删除旧的过期的证书
有些证书是十年才过期的,就不需要删除,我这里通过上述方式查看是以下这些证书即将或者已经过期的

cd /etc/kubernetes/pki
rm -rf apiserver-etcd-client.*
rm -rf apiserver-kubelet-client.*
rm -rf etcd/healthcheck-client.*
rm -rf front-proxy-client.*

3.重新生成新的证书
上面删除了哪些证书便重新生成哪些证书

kubeadm init phase certs apiserver-etcd-client
kubeadm init phase certs apiserver-kubelet-client
kubeadm init phase certs etcd-healthcheck-client
kubeadm init phase certs front-proxy-client

4.重新生成kubeconfig文件

cd /etc/kubernetes
rm -f admin.conf kubelet.conf controller-manager.conf scheduler.conf
kubeadm init phase kubeconfig all
#覆盖默认的配置文件
cp /etc/kubernetes/admin.conf $HOME/.kube/config

5.重启docker和kubelet

systemctl daemon-reload
systemctl restart docker
systemctl restart kubelet

6.删除node节点
这里跟上篇更换集群IP一样,因为node节点的证书问题处理比较麻烦,我这里选择之间重新加入集群,重新颁发证书(如果你有更好的方式,可以不这么做)

kubectl delete node node1
kubectl delete node node2

node节点操作

1.备份要更改的文件,在更改每个配置文件前,最好先备份

cp -r /etc/kubernetes{,-$(date +%Y-%m-%d).bak}
cp -r /var/lib/kubelet/pki{,-$(date +%Y-%m-%d).bak}

2.重置kubernetes配置

kubeadm reset -f

3.重启服务器

reboot

4.重新加入master节点所在的kubernetes集群(因为node节点的证书问题处理起来有点麻烦,所以直接重新加入简单点)

#先在master节点生成加入口令
kubeadm token create --print-join-command
#在node节点上执行kuberadm join命令
kubeadm join 10.98.99.140:6443 --token ne2dbu.74etnc2273wx6ysg --discovery-token-ca-cert-hash sha256:bd373617bc307f5192cb38ab0d0ccd261d95d6aa4c64a97d4c1acf1f7ffa02cd 
Logo

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

更多推荐