K8S集群证书过期后,会导无法创建Pod,通过kubectl get nodes也无法获取信息,甚至dashboard也无法访问。
Unable to connect to the server: x509: certificate has expired or is not yet valid
Part of the existing bootstrap client certificate is expired: 2023-08-29 02:29:04 +0000 UT
这是说明k8s使用的证书过期了,k8s自带证书是一年的有效期。所以我们解决问题的办法就是更换证书。
[root@k8s-master-47 ~]# openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text | grep Not
Not Before: Aug 29 03:32:16 2021 GMT
Not After : Aug 26 03:32:16 2023 GMT
/etc/kubernetes/pki/apiserver.crt #1年有效期
/etc/kubernetes/pki/front-proxy-ca.crt #10年有效期
/etc/kubernetes/pki/ca.crt #10年有效期
/etc/kubernetes/pki/apiserver-etcd-client.crt #1年有效期
/etc/kubernetes/pki/front-proxy-client.crt #1年有效期
/etc/kubernetes/pki/etcd/server.crt #1年有效期
/etc/kubernetes/pki/etcd/ca.crt #10年有效期
/etc/kubernetes/pki/etcd/peer.crt #1年有效期
/etc/kubernetes/pki/etcd/healthcheck-client.crt #1年有效期
/etc/kubernetes/pki/apiserver-kubelet-client.crt #1年有效期
[root@k8s-master-47 ~]# kubeadm alpha certs check-expiration
K8S在过期之前,使用kubeadm alpha phase里的certs和kubeconfig命令,同时配合kubelet证书自动轮换机制来解决这个问题(具体操作可以百度搜索),这里介绍证书已经过期的解决方法,以下延长证书过期的方法适合kubernetes1.14、1.15、1.16、1.17、1.18版本。操作步骤如下:
giuhub地址:
https://github.com/yuyicai/update-kube-cert
脚本地址:
https://github.com/yuyicai/update-kube-cert/blob/master/update-kubeadm-cert.sh
如果你用containerd
作为 CRI runtime
:
update-kubeadm-cert-crictl.sh
这个脚本chmod +x update-kubeadm-cert.sh
./update-kubeadm-cert.sh all
[root@k8s-master-47 ~]# openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text | grep Not
Not Before: Aug 29 03:32:16 2023 GMT
Not After : Aug 26 03:32:16 2033 GMT
[root@k8s-master-47 ~]# kubeadm alpha 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 -oyaml'
CERTIFICATE EXPIRES RESIDUAL TIME CERTIFICATE AUTHORITY EXTERNALLY MANAGED
admin.conf Aug 26, 2033 03:32 UTC 9y no
apiserver Aug 26, 2033 03:32 UTC 9y ca no
apiserver-etcd-client Aug 26, 2033 03:32 UTC 9y etcd-ca no
apiserver-kubelet-client Aug 26, 2033 03:32 UTC 9y ca no
controller-manager.conf Aug 26, 2033 03:32 UTC 9y no
etcd-healthcheck-client Aug 26, 2033 03:32 UTC 9y etcd-ca no
etcd-peer Aug 26, 2033 03:32 UTC 9y etcd-ca no
etcd-server Aug 26, 2033 03:32 UTC 9y etcd-ca no
front-proxy-client Aug 26, 2033 03:32 UTC 9y front-proxy-ca no
scheduler.conf Aug 26, 2033 03:32 UTC 9y no
CERTIFICATE AUTHORITY EXPIRES RESIDUAL TIME EXTERNALLY MANAGED
ca Aug 24, 2031 11:28 UTC 7y no
etcd-ca Aug 24, 2031 11:28 UTC 7y no
front-proxy-ca Aug 24, 2031 11:28 UTC 7y no
[root@k8s-master-47 ~]#
The script will back up the /etc/kubernetes
directory into /etc/kubernetes.old-$(date +%Y%m%d)
(for example: kubernetes.old-20200325
)
If the the script is failed to be executed, use the backup directory to overide the /etc/kubernetes
directory.
[root@k8s-master-47 ~]# ls /etc/kubernetes.old-20230829
kubectl config set-context $(kubectl config current-context) --namespace=soms