'Kubernetes: expired certificate
Our Kubernetes 1.6 cluster had certificates generated when the cluster was built on April 13th, 2017.
On December 13th, 2017, our cluster was upgraded to version 1.8, and new certificates were generated [apparently, an incomplete set of certificates].
On April 13th, 2018, we started seeing this message within our Kubernetes dashboard for api-server:
[authentication.go:64] Unable to authenticate the request due to an error: [x509: certificate has expired or is not yet valid, x509: certificate has expired or is not yet valid]
Tried pointing client-certificate & client-key within /etc/kubernetes/kubelet.conf
at the certificates generated on Dec 13th [apiserver-kubelet-client.crt
and apiserver-kubelet-client.crt
], but continue to see the above error.
Tried pointing client-certificate & client-key within /etc/kubernetes/kubelet.conf
at different certificates generated on Dec 13th [apiserver.crt
and apiserver.crt
] (I honestly don't understand the difference between these 2 sets of certs/keys), but continue to see the above error.
Tried pointing client-certificate & client-key within /etc/kubernetes/kubelet.conf
at non-existent files, and none of the kube* services would start, with /var/log/syslog
complaining about this:
Apr 17 17:50:08 kuber01 kubelet[2422]: W0417 17:50:08.181326 2422 server.go:381] invalid kubeconfig: invalid configuration: [unable to read client-cert /tmp/this/cert/does/not/exist.crt for system:node:node01 due to open /tmp/this/cert/does/not/exist.crt: no such file or directory, unable to read client-key /tmp/this/key/does/not/exist.key for system:node:node01 due to open /tmp/this/key/does/not/exist.key: no such file or directory]
Any advice on how to overcome this error, or even troubleshoot it at a more granular level? Was considering regenerating certificates for api-server (kubeadm alpha phase certs apiserver
), based on instructions within https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-alpha/#cmd-phase-certs ... but not sure if I'd be doing more damage.
Relatively new to Kubernetes, and the gentleman who set this up is not available for consult ... any help is appreciated. Thanks.
Solution 1:[1]
Each node within the Kubernetes cluster contains a config file for running kubelet ... /etc/kubernetes/kubelet.conf
... and this file is auto-generated by kubeadm. During this auto-generation, kubeadm uses /etc/kubernetes/ca.key
to create a node-specific file, /etc/kubernetes/kubelet.conf
, within which are two very important pieces ... client-certificate-data and client-key-data. My original thought process led me to believe that I needed to find the corresponding certificate file & key file, renew those files, convert both to base64, and use those values within kubelet.conf
files across the cluster ... this thinking was not correct.
Instead, the fix was to use kubeadm to regenerate kubectl.conf
on all nodes, as well as admin.conf
, controller-manager.conf
, and scheduler.conf
on the cluster's master node. You'll need /etc/kubernetes/pki/ca.key
on each node in order for your config files to include valid data for client-certificate-data and client-key-data.
Pro tip: make use of the --apiserver-advertise-address
parameter to ensure your new config files contain the correct IP address of the node hosting the kube-apiserver service.
Solution 2:[2]
I think you need re-generate the apiserver certificate /etc/kubernetes/pki/apiserver.crt
you can view current expire date like this.
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not '
Not Before: Dec 20 14:32:00 2017 GMT
Not After : Dec 20 14:32:00 2018 GMT
Here is the steps I used to regenerate the certificates on v1.11.5 cluster. compiled steps from here https://github.com/kubernetes/kubeadm/issues/581
to check all certificate expire date:
find /etc/kubernetes/pki/ -type f -name "*.crt" -print|egrep -v 'ca.crt$'|xargs -L 1 -t -i bash -c 'openssl x509 -noout -text -in {}|grep After'
Renew certificate on Master node.
*) Renew certificate
mv /etc/kubernetes/pki/apiserver.key /etc/kubernetes/pki/apiserver.key.old
mv /etc/kubernetes/pki/apiserver.crt /etc/kubernetes/pki/apiserver.crt.old
mv /etc/kubernetes/pki/apiserver-kubelet-client.crt /etc/kubernetes/pki/apiserver-kubelet-client.crt.old
mv /etc/kubernetes/pki/apiserver-kubelet-client.key /etc/kubernetes/pki/apiserver-kubelet-client.key.old
mv /etc/kubernetes/pki/front-proxy-client.crt /etc/kubernetes/pki/front-proxy-client.crt.old
mv /etc/kubernetes/pki/front-proxy-client.key /etc/kubernetes/pki/front-proxy-client.key.old
kubeadm alpha phase certs apiserver --config /root/kubeadm-kubetest.yaml
kubeadm alpha phase certs apiserver-kubelet-client
kubeadm alpha phase certs front-proxy-client
mv /etc/kubernetes/pki/apiserver-etcd-client.crt /etc/kubernetes/pki/apiserver-etcd-client.crt.old
mv /etc/kubernetes/pki/apiserver-etcd-client.key /etc/kubernetes/pki/apiserver-etcd-client.key.old
kubeadm alpha phase certs apiserver-etcd-client
mv /etc/kubernetes/pki/etcd/server.crt /etc/kubernetes/pki/etcd/server.crt.old
mv /etc/kubernetes/pki/etcd/server.key /etc/kubernetes/pki/etcd/server.key.old
kubeadm alpha phase certs etcd-server --config /root/kubeadm-kubetest.yaml
mv /etc/kubernetes/pki/etcd/healthcheck-client.crt /etc/kubernetes/pki/etcd/healthcheck-client.crt.old
mv /etc/kubernetes/pki/etcd/healthcheck-client.key /etc/kubernetes/pki/etcd/healthcheck-client.key.old
kubeadm alpha phase certs etcd-healthcheck-client --config /root/kubeadm-kubetest.yaml
mv /etc/kubernetes/pki/etcd/peer.crt /etc/kubernetes/pki/etcd/peer.crt.old
mv /etc/kubernetes/pki/etcd/peer.key /etc/kubernetes/pki/etcd/peer.key.old
kubeadm alpha phase certs etcd-peer --config /root/kubeadm-kubetest.yaml
*) Backup old configuration files
mv /etc/kubernetes/admin.conf /etc/kubernetes/admin.conf.old
mv /etc/kubernetes/kubelet.conf /etc/kubernetes/kubelet.conf.old
mv /etc/kubernetes/controller-manager.conf /etc/kubernetes/controller-manager.conf.old
mv /etc/kubernetes/scheduler.conf /etc/kubernetes/scheduler.conf.old
kubeadm alpha phase kubeconfig all --config /root/kubeadm-kubetest.yaml
mv $HOME/.kube/config .$HOMEkube/config.old
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
chmod 777 $HOME/.kube/config
export KUBECONFIG=.kube/config
Reboot the node and check the logs for etcd, kubeapi and kubelet.
Note: Remember to update your CI/CD job kubeconfig file. If you’re using helm command test that also.
Solution 3:[3]
This topic is also discussed in:
- https://github.com/kubernetes/kubeadm/issues/581
- after 1.15 kubeadm upgrade automatically will renewal the certificates for you!
- also 1.15 added a command to check cert expiration in kubeadm
- Renew kubernetes pki after expired
Kubernetes v1.15 provides docs for "Certificate Management with kubeadm":
- https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/kubeadm-certs/
- Check certificate expiration:
kubeadm alpha certs check-expiration
- Automatic certificate renewal:
- kubeadm renews all the certificates during control plane upgrade.
- Manual certificate renewal:
- You can renew your certificates manually at any time with the
kubeadm alpha certs renew
command. - This command performs the renewal using CA (or front-proxy-CA) certificate and key stored in /etc/kubernetes/pki.
- You can renew your certificates manually at any time with the
For Kubernetes v1.14 I find this procedure the most helpful:
- https://stackoverflow.com/a/56334732/1147487
- backup and re-generate all certs:
$ cd /etc/kubernetes/pki/
$ mv {apiserver.crt,apiserver-etcd-client.key,apiserver-kubelet-client.crt,front-proxy-ca.crt,front-proxy-client.crt,front-proxy-client.key,front-proxy-ca.key,apiserver-kubelet-client.key,apiserver.key,apiserver-etcd-client.crt} ~/
$ kubeadm init phase certs all --apiserver-advertise-address <IP>
- backup and re-generate all kubeconfig files:
$ cd /etc/kubernetes/
$ mv {admin.conf,controller-manager.conf,kubelet.conf,scheduler.conf} ~/
$ kubeadm init phase kubeconfig all
$ reboot
- copy new admin.conf:
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
Solution 4:[4]
On k8s 1.7 I faced a similar problem (x509 expired error included inside /var/log/kube-apiserver.log) and could not find any certificate expired. We decided to restart only the apiserver docker on the master node. It resolved the problem.
$ sudo docker ps -a | grep apiserver
af99f816c7ec gcr.io/google_containers/kube-apiserver@sha256:53b987e5a2932bdaff88497081b488e3b56af5b6a14891895b08703129477d85 "/bin/sh -c '/usr/loc" 15 months ago Up 19 hours k8s_kube-apiserver_kube-apiserver-ip-xxxxxc_0
40f3a18050c3 gcr.io/google_containers/pause-amd64:3.0 "/pause" 15 months ago Up 15 months k8s_POD_kube-apiserver-ip-xxxc_0
$ sudo docker restart af99f816c7ec
af99f816c7ec
$
Solution 5:[5]
If you have already updated the certs or it has been updated automatically, you would have to restart the kube-apiserver on all masters nodes.
Go to the masters and look fordocker ps | grep -i kube-apiserver
Kill them with docker kill
the containers and wait for 10-15 seconds it should start working.
For me it solved it.
Solution 6:[6]
You could use this command to check the expiring date
kubectl get secret remote-certs -o json | jq -r '.data | ."remote.ca.crt"' | base64 -d | openssl x509 -noout -text | grep -A 2 -i validity
Validity Not Before: Dec 2 17:19:35 2021 GMT Not After : Dec 2 17:29:35 2022 GMT
Solution 7:[7]
For a microk8s environment, this error can occur. Then your whole kubernetes setup won't work when it's the case. It happened for me after an upgrade & reboot of my Ubuntu dedicated server.
Unable to connect to the server: x509: certificate has expired or is not yet valid: current time 2022-04-02T16:38:24Z is after 2022-03-16T14:24:02Z
The solution for it is to ask microk8s to refresh its inner certificates, including the kubernetes ones.
To do that you can use:
sudo microk8s.refresh-certs
And reboot the server. It worked for me.
Solution 8:[8]
I had this issue (microk8s - ubuntu 20.04.3) and updating the time fixed it:
sudo timedatectl set-ntp off
sudo timedatectl set-ntp on
Solution 9:[9]
For anyone that stumbles upon this in the future, which are running a newer version of kubernetes >1.17, this is probably the simplest way to renew your certs.
The following renews all certs, restarts kubelet, takes a backup of the old admin config and applies the new admin config:
kubeadm certs renew all
systemctl restart kubelet
cp /root/.kube/config /root/.kube/.old-$(date --iso)-config
cp /etc/kubernetes/admin.conf /root/.kube/config
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | NoobSkywalker |
Solution 2 | |
Solution 3 | |
Solution 4 | user3541649 |
Solution 5 | Kafil Hussain |
Solution 6 | benson23 |
Solution 7 | Micaël Félix |
Solution 8 | jbsulli |
Solution 9 | kruserr |