Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

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 show

Fix it fast

Most common cause, 1 of 3

The 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.

Fix
# cluster entry name first, then any name the error listed
kubectl config set-cluster kubernetes --tls-server-name=kubernetes
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to Kubernetes 1.37 (kubeadm), k3s v1.37.2+k3s1, kubectl 1.37
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
kubectl config view --minify -o jsonpath="{.clusters[0].cluster.server}"

Then find the line you see in this list and open its fix:

  1. 1The client connects through an address the certificate never hadThe server line shows a public IP, NAT address or load balancer name, and one of the listed names reaches the same API server
  2. 2A kubeadm cluster was built without the address in certSANsSeveral clients need the new address, and kubeadm certs renew did not change the error
  3. 3A k3s server was started without the outside name in tls-sanThe 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.10
  • Unable to connect to the server: tls: failed to verify certificate: x509: certificate is valid for kubernetes, kubernetes.default, not k8s.example.com
  • x509: 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.

Check
kubectl config get-clusters

Fix

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.

Fix
# cluster entry name first, then any name the error listed
kubectl config set-cluster kubernetes --tls-server-name=kubernetes

Verify

kubectl reaches the cluster with no x509 message.

Verify
kubectl get nodes

Confirmed by kubernetes.io

Did this fix it?

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.

Check
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -ext subjectAltName

Fix

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.

Fix
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.

Verify
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -ext subjectAltName

Confirmed by kubernetes.io

Did this fix it?

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.

Check
sudo cat /etc/rancher/k3s/config.yaml

Fix

List every outside name or IP under tls-san in the k3s config file, then restart the k3s service on each server node.

Fix
# /etc/rancher/k3s/config.yaml
tls-san:
  - "k8s.example.com"
  - "203.0.113.10"

sudo systemctl restart k3s

Verify

kubectl through the outside name returns the node list.

Verify
kubectl get nodes

Confirmed by docs.k3s.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.