Skip to content

kubectl: connection refused – API server dead or wrong port?

Kubernetestested on Kubernetes v1.37.2, containerd v2.4.2

When kubectl answers "connection refused", nobody is listening on that address. Two cases: the API server is dead – then kubectl can't help and you ask the container runtime. Or kubectl talks into the void because the kubeconfig names a wrong port.

Symptom

k get nodes
... dial tcp 172.16.0.126:6443: connect: connection refused ...

After a change to the manifest in /etc/kubernetes/manifests it takes about a minute until the API server is gone – and one to two minutes until it answers again after the repair.

Diagnosis

Without an API server, go one level deeper: crictl ps -a also shows exited containers, and their logs reveal the reason.

sudo crictl ps -a --name kube-apiserver
CONTAINER       IMAGE           CREATED                  STATE     NAME             ...
a1dbdea6509df   7a918dfe48696   Less than a second ago   Created   kube-apiserver   ...
7686936e813f5   7a918dfe48696   22 seconds ago           Exited    kube-apiserver   ...
ID=$(sudo crictl ps -a --name kube-apiserver --state exited -q | head -1)
sudo crictl logs $ID 2>&1 | tail -1
E1117 22:49:41.420449  1 run.go:72] "command failed" err="error creating storage
factory:
etcd grpc connection not ready: context deadline exceeded"

Causes

ExitedThe API server keeps dying

An error in the static pod manifest, in the course a wrong etcd port. The wrong address appears further up in the log. The same logs are also stored under /var/log/pods/kube-system_kube-apiserver-cp1_*/.

Port 6444The kubeconfig points to the wrong port

Not every "connection refused" is a dead API server. The port is in the error message – compare it with --secure-port=6443 in the manifest.

k config view --minify -o jsonpath='{.clusters[0].cluster.server}{"\n"}'
The connection to the server 172.16.0.126:6444 was refused - did you specify the right
host
or port?
https://172.16.0.126:6444

Fix

Correct the error in the manifest or in the kubeconfig. The kubelet recreates the static pod by itself. Afterwards /readyz?verbose is the quickest health check – it checks every part of the API server including etcd.
sudo sed -i 's#127.0.0.1:2399#127.0.0.1:2379#' \
  /etc/kubernetes/manifests/kube-apiserver.yaml
sed -i 's#:6444#:6443#' ~/.kube/config
k get --raw='/readyz?verbose' | tail -2
[+]shutdown ok
readyz check passed

Don't: Putting a backup of a manifest into /etc/kubernetes/manifests. The kubelet would start it as a second pod – backups belong outside that directory.

▶
Break it yourself and find it – free
CKA hands-on, day 14: break and repair the kubelet, container runtime, scheduler, controller manager and API server.
Go to day 14 →

FAQ

How long do I have to wait after fixing a manifest?

One to two minutes. The kubelet needs time to recreate the pod.

Related error patterns

More