Just set up a brand new cluster? Changed the domain or IP of your admin node? Then you may have encountered the error Unable to connect to the server: x509: certificate is valid for …. The following is a fix for this common issue. However, there are often other reasons to rebuild your cluster cert, and it’s relatively easy.
TL;DR: “I don’t care about the fix, I need to remote control my cluster. Security? What’s that?”:
kubectl --insecure-skip-tls-verify --context=some-context get pods
§2026 Update
The error and the quick escape hatch are unchanged: kubectl --insecure-skip-tls-verify still works exactly as shown, for when you just need in and will deal with TLS later. The real fix, though, used a couple of commands that have since been removed, so the steps below are updated to current kubeadm.
Two things changed. First, kubeadm alpha phase certs ... is gone. Certificate operations graduated out of alpha. Use kubeadm init phase certs apiserver to regenerate the API server certificate with new SANs, and kubeadm certs renew to rotate certs in place. To see what is actually expiring, reach for kubeadm certs check-expiration, which also answers the “cert expired?” branch in the diagram above.
Second, docker rm no longer kills the API server on most clusters. Kubernetes dropped Docker as a runtime in v1.24, so on a current containerd cluster you use crictl instead, or just let the kubelet recreate the static pod after you touch its manifest.
The article continues below. The 2026 Update above summarizes what changed; the walkthrough that follows has been updated to current commands.
Let’s say you want to fix the issue and not just skip-tls-verify. SSH to the control-plane node and run the following:
# remove the existing API server cert and key
rm /etc/kubernetes/pki/apiserver.*
# regenerate the API server cert with the new SAN
# (kubeadm skips generation if the files still exist, hence the rm above)
kubeadm init phase certs apiserver --apiserver-cert-extra-sans=new.example.com
# restart the API server static pod (containerd, via crictl)
crictl ps --name kube-apiserver -q | xargs -r crictl stop
# restart the kubelet
systemctl restart kubelet
The kubelet recreates the API server pod from its static manifest once the container stops, picking up the new certificate.
§Port Forwarding / Local Development
Check out kubefwd for a simple command line utility that bulk forwards services of one or more namespaces to your local workstation.
§Resources
- systemctl - How To Use Systemctl to Manage Systemd Services and Units
- Kubernetes
- kubeadm - Using kubeadm to Create a Cluster
- Stack Overflow