Kubectl certificate has expired

Kubectl certificate Unable to connect to the server: x509: certificate has expired or is not yet valid: current time 2022-12-28T11:59:22+07:00 is after 2022-12-27T06:58:37Z

  • check expiration
$ kubeadm certs check-expiration
$ kubeadm certs check-expiration
...
root@215120-U:~# 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                 Dec 27, 2022 06:58 UTC   <invalid>       ca                      no
apiserver                  Dec 27, 2022 06:58 UTC   <invalid>       ca                      no
apiserver-etcd-client      Dec 27, 2022 06:58 UTC   <invalid>       etcd-ca                 no
apiserver-kubelet-client   Dec 27, 2022 06:58 UTC   <invalid>       ca                      no
controller-manager.conf    Dec 27, 2022 06:58 UTC   <invalid>       ca                      no
etcd-healthcheck-client    Dec 27, 2022 06:58 UTC   <invalid>       etcd-ca                 no
etcd-peer                  Dec 27, 2022 06:58 UTC   <invalid>       etcd-ca                 no
etcd-server                Dec 27, 2022 06:58 UTC   <invalid>       etcd-ca                 no
front-proxy-client         Dec 27, 2022 06:58 UTC   <invalid>       front-proxy-ca          no
scheduler.conf             Dec 27, 2022 06:58 UTC   <invalid>       ca                      no

  • Backup from all exists certificates
root@kube-master-1:~# cp /etc/kubernetes/admin.conf /etc/kubernetes/admin.conf.backup
root@kube-master-1:~# cp /etc/kubernetes/controller-manager.conf /etc/kubernetes/controller-manager.conf.backup
root@kube-master-1:~# cp /etc/kubernetes/kubelet.conf /etc/kubernetes/kubelet.conf.backup
root@kube-master-1:~# cp /etc/kubernetes/scheduler.conf /etc/kubernetes/scheduler.conf.backup
  • renew
$ kubeadm certs renew all
root@215120-U:~# 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.
root@215120-U:~# 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'
W1228 12:09:23.074063 1609609 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                 Dec 28, 2023 05:09 UTC   364d            ca                      no
apiserver                  Dec 28, 2023 05:09 UTC   364d            ca                      no
apiserver-etcd-client      Dec 28, 2023 05:09 UTC   364d            etcd-ca                 no
apiserver-kubelet-client   Dec 28, 2023 05:09 UTC   364d            ca                      no
controller-manager.conf    Dec 28, 2023 05:09 UTC   364d            ca                      no
etcd-healthcheck-client    Dec 28, 2023 05:09 UTC   364d            etcd-ca                 no
etcd-peer                  Dec 28, 2023 05:09 UTC   364d            etcd-ca                 no
etcd-server                Dec 28, 2023 05:09 UTC   364d            etcd-ca                 no
front-proxy-client         Dec 28, 2023 05:09 UTC   364d            front-proxy-ca          no
scheduler.conf             Dec 28, 2023 05:09 UTC   364d            ca                      no

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Dec 25, 2031 06:58 UTC   8y              no
etcd-ca                 Dec 25, 2031 06:58 UTC   8y              no
front-proxy-ca          Dec 25, 2031 06:58 UTC   8y              no
  • kubectl get nodes
kubectl get nodes
...
root@215120-U:~# kubectl get nodes
error: You must be logged in to the server (Unauthorized)
  • cp /etc/kubernetes/admin.conf $HOME/.kube/config
cp /etc/kubernetes/admin.conf $HOME/.kube/config
  • get node again
root@215120-U:~# kubectl get nodes
NAME       STATUS   ROLES                  AGE    VERSION
215120-u   Ready    control-plane,master   365d   v1.23.1
215121-u   Ready    <none>                 365d   v1.23.1
215122-u   Ready    <none>                 365d   v1.23.1