kubernetes 证书过期更新(1.23版本)

kubectl 报错如下:

Unable to connect to the server: x509: certificate has expired or is not yet valid: current time 2024-02-18T16:26:34+08:00 is after 2024-02-13T02:02:05Z

根据提示,可以看出是集群的证书过期所致,需要进行证书的更新,具体操作步骤如下

检查证书时间

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'
[check-expiration] Error reading configuration from the Cluster. Falling back to default configuration

CERTIFICATE                EXPIRES                  RESIDUAL TIME   CERTIFICATE AUTHORITY   EXTERNALLY MANAGED
admin.conf                 Feb 13, 2024 02:02 UTC   <invalid>                               no      
apiserver                  Feb 13, 2024 02:02 UTC   <invalid>       ca                      no      
apiserver-etcd-client      Feb 13, 2024 02:02 UTC   <invalid>       etcd-ca                 no      
apiserver-kubelet-client   Feb 13, 2024 02:02 UTC   <invalid>       ca                      no      
controller-manager.conf    Feb 13, 2024 02:02 UTC   <invalid>                               no      
etcd-healthcheck-client    Feb 13, 2024 02:02 UTC   <invalid>       etcd-ca                 no      
etcd-peer                  Feb 13, 2024 02:02 UTC   <invalid>       etcd-ca                 no      
etcd-server                Feb 13, 2024 02:02 UTC   <invalid>       etcd-ca                 no      
front-proxy-client         Feb 13, 2024 02:02 UTC   <invalid>       front-proxy-ca          no      
scheduler.conf             Feb 13, 2024 02:02 UTC   <invalid>                               no      

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

备份原证书以及配置文件

cp -r /etc/kubernetes /etc/kubernetes.bak

更新所有配置证书

kubeadm certs renew all

输出如下

[renew] Reading configuration from the cluster...
[renew] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[renew] Error reading configuration from the Cluster. Falling back to default configuration

certificate embedded in the kubeconfig file for the admin to use and for kubeadm itself renewed
certificate for serving the Kubernetes API renewed
certificate the apiserver uses to access etcd renewed
certificate for the API server to connect to kubelet renewed
certificate embedded in the kubeconfig file for the controller manager to use renewed
certificate for liveness probes to healthcheck etcd renewed
certificate for etcd nodes to communicate with each other renewed
certificate for serving etcd renewed
certificate for the front proxy client renewed
certificate embedded in the kubeconfig file for the scheduler manager to use renewed

Done renewing certificates. You must restart the kube-apiserver, kube-controller-manager, kube-scheduler and etcd, so that they can use the new certificates.

重启相关组件
这里因为证书未生效,kubectl不可用的原因,直接使用docker命令重启

docker ps |grep -E 'k8s_kube-apiserver|k8s_kube-controller-manager|k8s_kube-scheduler|k8s_etcd_etcd-master' | awk -F ' ' '{print $1}' |xargs docker restart

当然,也可以先执行下更新本地config文件的步骤,然后使用kubectl delete pod的方式重启

[root@feng ~]# cp /etc/kubernetes/admin.conf .kube/config 
cp: overwrite ‘.kube/config’? y

最后,确认证书过期时间

[root@bkce-192121 ~]# 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'

CERTIFICATE                EXPIRES                  RESIDUAL TIME   CERTIFICATE AUTHORITY   EXTERNALLY MANAGED
admin.conf                 Feb 17, 2025 08:59 UTC   364d                                    no      
apiserver                  Feb 17, 2025 08:59 UTC   364d            ca                      no      
apiserver-etcd-client      Feb 17, 2025 08:59 UTC   364d            etcd-ca                 no      
apiserver-kubelet-client   Feb 17, 2025 08:59 UTC   364d            ca                      no      
controller-manager.conf    Feb 17, 2025 08:59 UTC   364d                                    no      
etcd-healthcheck-client    Feb 17, 2025 08:59 UTC   364d            etcd-ca                 no      
etcd-peer                  Feb 17, 2025 08:59 UTC   364d            etcd-ca                 no      
etcd-server                Feb 17, 2025 08:59 UTC   364d            etcd-ca                 no      
front-proxy-client         Feb 17, 2025 08:59 UTC   364d            front-proxy-ca          no      
scheduler.conf             Feb 17, 2025 08:59 UTC   364d                                    no      

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Feb 10, 2033 02:02 UTC   8y              no      
etcd-ca                 Feb 10, 2033 02:02 UTC   8y              no      
front-proxy-ca          Feb 10, 2033 02:02 UTC   8y              no      
最后修改:2024 年 12 月 18 日
如果觉得我的文章对你有用,请随意赞赏