k8s集群证书过期解决

一、k8s集群证书过期解决

问题现象

K8S集群证书过期后,会导无法创建Pod,通过kubectl get nodes也无法获取信息,甚至dashboard也无法访问。

执行命令发现报错:

Unable to connect to the server: x509: certificate has expired or is not yet valid

查看K8S的日志:

Part of the existing bootstrap client certificate is expired: 2023-08-29 02:29:04 +0000 UT
这是说明k8s使用的证书过期了,k8s自带证书是一年的有效期。所以我们解决问题的办法就是更换证书。

二、确认K8S证书过期时间

2.1 查看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
2.2 其它证书同理,K8s各个证书过期时间如下:
/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年有效期
2.3 或使用统一命令查看
[root@k8s-master-47 ~]# kubeadm alpha certs check-expiration

三、使用延长证书过期的方法解决K8S证书过期问题

K8S在过期之前,使用kubeadm alpha phase里的certs和kubeconfig命令,同时配合kubelet证书自动轮换机制来解决这个问题(具体操作可以百度搜索),这里介绍证书已经过期的解决方法,以下延长证书过期的方法适合kubernetes1.14、1.15、1.16、1.17、1.18版本。操作步骤如下:

3.1 下载update-kubeadm-cert.sh

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 这个脚本
3.2 把update-kubeadm-cert.sh文件上传到k8s任一master节点任意位置
3.3 在任一master节点执行如下命令
1)脚本添加可执行权限并执行
chmod +x update-kubeadm-cert.sh
./update-kubeadm-cert.sh all
2)验证
[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 ~]# 
          
3.4 更新证书失败后回滚

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
3.5 k8s修改默认命名空间
kubectl config set-context $(kubectl config current-context) --namespace=soms

四、参考:

  • http://www.taodudu.cc/news/show-3680378.html?action=onClick
  • https://github.com/yuyicai/update-kube-cert
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值