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

In this Post I will shoe you how to renew the kubernetes api cert in Microk8s.

# microk8s kubectl get all --all-namespaces
Unable to connect to the server: x509: certificate has expired or is not yet valid: current time 2023-01-11T10:50:25+01:00 is after 2022-07-21T13:27:27Z

So the k8s api certificate probably expired. Test it:

# sudo microk8s.refresh-certs -c
The CA certificate will expire in 0 days.

Ok so we renew the certificate:

# microk8s.refresh-certs
Taking a backup of the current certificates under /var/snap/microk8s/2530/var/log/ca-backup/
Creating new certificates
Signature ok
subject=/C=GB/ST=Canonical/L=Canonical/O=Canonical/OU=Canonical/CN=127.0.0.1
Getting CA Private Key
Signature ok
subject=/CN=front-proxy-client
Getting CA Private Key
1
Creating new kubeconfig file
2023-01-11T10:53:03+01:00 INFO Waiting for "snap.microk8s.daemon-cluster-agent.service" to stop.
Stopped.
Started.

The CA certificates have been replaced. Kubernetes will restart the pods of your workloads.
Any worker nodes you may have in your cluster need to be removed and re-joined to become aware of the new CA.