kubectl or Jenkins cannot talk to the Kubernetes API because the certificate lists other addresses
Your error
tls: failed to verify certificate: x509: certificate is valid for 10.96.0.1, 10.0.0.5, not 203.0.113.102 more lines it can showFix it fast
Most common cause, 1 of 3The client connects through an address the certificate never had
Keep the outside address for the connection, but tell kubectl which certificate name to check. tls-server-name overrides the host name used for validation in that cluster entry.
# cluster entry name first, then any name the error listed
kubectl config set-cluster kubernetes --tls-server-name=kubernetesShort answer
x509: certificate is valid for 10.x.x.x, not 203.0.113.10 means the API server's certificate does not list the address in your kubeconfig, usually a public IP, NAT address or load balancer name. Either connect with tls-server-name set to a listed name, or add the address to the certificate's SANs (kubeadm certSANs or k3s tls-san). Applies to Kubernetes 1.37 and k3s 1.37.
Which cause do you have?
Print the server address your kubeconfig uses and compare it with the list in the error. The error already names every address the certificate accepts; the one after not is what the client tried.
kubectl config view --minify -o jsonpath="{.clusters[0].cluster.server}"Then find the line you see in this list and open its fix:
- 1The client connects through an address the certificate never had
The server line shows a public IP, NAT address or load balancer name, and one of the listed names reaches the same API server - 2A kubeadm cluster was built without the address in certSANs
Several clients need the new address, and kubeadm certs renew did not change the error - 3A k3s server was started without the outside name in tls-san
The cluster runs k3s, and the listed IPs include 10.43.0.1, the first address of the default k3s service range 10.43.0.0/16
What does the error look like?
tls: failed to verify certificate: x509: certificate is valid for 10.96.0.1, 10.0.0.5, not 203.0.113.10Unable to connect to the server: tls: failed to verify certificate: x509: certificate is valid for kubernetes, kubernetes.default, not k8s.example.comx509: certificate is valid for 10.43.0.1, 127.0.0.1, not 198.51.100.7
Cause 1 of 3
The client connects through an address the certificate never had
You seeThe server line shows a public IP, NAT address or load balancer name, and one of the listed names reaches the same API server
Check
A certificate is valid for ... not ... failure here means the TLS layer compared the host in the server URL with the certificate's names and found no match. Copying an admin kubeconfig to a laptop or a Jenkins agent and swapping in an outside IP produces exactly this.
kubectl config get-clustersFix
Keep the outside address for the connection, but tell kubectl which certificate name to check. tls-server-name overrides the host name used for validation in that cluster entry.
# cluster entry name first, then any name the error listed
kubectl config set-cluster kubernetes --tls-server-name=kubernetesVerify
kubectl reaches the cluster with no x509 message.
kubectl get nodesConfirmed by kubernetes.io
Cause 2 of 3
A kubeadm cluster was built without the address in certSANs
You seeSeveral clients need the new address, and kubeadm certs renew did not change the error
Check
kubeadm writes the API server certificate from ClusterConfiguration. Renewing keeps the old subject alternative names, because renew copies attributes from the existing certificate rather than the config. An address added later needs a newly written certificate.
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -ext subjectAltNameFix
Add the address under apiServer.certSANs in the kubeadm-config ConfigMap, save that configuration to a local file, back up /etc/kubernetes, move the old apiserver.crt and apiserver.key aside (kubeadm skips existing pairs), write a new pair, then restart the API server static pod by moving its manifest out and back after about 20 seconds.
kubectl edit cm -n kube-system kubeadm-config
kubectl get cm kubeadm-config -n kube-system -o=jsonpath="{.data.ClusterConfiguration}" > kubeadm.yaml
sudo cp -a /etc/kubernetes /root/kubernetes-backup
sudo mv /etc/kubernetes/pki/apiserver.crt /etc/kubernetes/pki/apiserver.key /root/
sudo kubeadm init phase certs apiserver --config kubeadm.yaml
sudo mv /etc/kubernetes/manifests/kube-apiserver.yaml /root/ && sleep 20
sudo mv /root/kube-apiserver.yaml /etc/kubernetes/manifests/Verify
The SAN list now contains the new address and kubectl works through it without tls-server-name.
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -ext subjectAltNameConfirmed by kubernetes.io
Cause 3 of 3
A k3s server was started without the outside name in tls-san
You seeThe cluster runs k3s, and the listed IPs include 10.43.0.1, the first address of the default k3s service range 10.43.0.0/16
Check
k3s only adds extra host names or IPs to its serving certificate when they are passed with the tls-san option. A server installed with defaults lists its own addresses, so a DNS name or public IP put in front of it fails verification.
sudo cat /etc/rancher/k3s/config.yamlFix
List every outside name or IP under tls-san in the k3s config file, then restart the k3s service on each server node.
# /etc/rancher/k3s/config.yaml
tls-san:
- "k8s.example.com"
- "203.0.113.10"
sudo systemctl restart k3sVerify
kubectl through the outside name returns the node list.
kubectl get nodesConfirmed by docs.k3s.io
How do I stop it coming back?
- Decide the API endpoint name before installing and put it in certSANs or tls-san on day one.
- Point kubeconfigs at a stable DNS name rather than a node IP that can change.
- Keep the kubeadm config file used for init in version control, so certificate rewrites reuse the same SAN list.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.