Issue#

Right after setting up a cluster (or logging in as a different user), every kubectl command fails:

$ kubectl get nodes E1202 09:00:12.345678 12345 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused The connection to the server localhost:8080 was refused - did you specify the right host or port?

The API server is running fine, and sudo crictl ps shows kube-apiserver up.

Cause#

kubectl can't find a kubeconfig. When it has no config, it falls back to http://localhost:8080, which nothing listens on. Common reasons:

  • ~/.kube/config was never created for the current user (kubeadm only writes /etc/kubernetes/admin.conf).
  • You ran sudo kubectl, so it looks in /root/.kube/config instead of your home.
  • KUBECONFIG points to a file that doesn't exist or isn't readable.

Check what kubectl actually sees:

echo "$KUBECONFIG" ls -l ~/.kube/config kubectl config view

An empty kubectl config view (clusters: null) confirms the problem.

Solution#

  1. On a kubeadm control-plane node, copy the admin kubeconfig for your user:
mkdir -p "$HOME/.kube" sudo cp -i /etc/kubernetes/admin.conf "$HOME/.kube/config" sudo chown "$(id -u):$(id -g)" "$HOME/.kube/config" chmod 600 "$HOME/.kube/config"
  1. Or, if you work as root, just point KUBECONFIG at it:
export KUBECONFIG=/etc/kubernetes/admin.conf echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' >> /root/.bashrc
  1. On k3s, the kubeconfig lives somewhere else:
mkdir -p "$HOME/.kube" sudo cp /etc/rancher/k3s/k3s.yaml "$HOME/.kube/config" sudo chown "$(id -u):$(id -g)" "$HOME/.kube/config"
  1. If KUBECONFIG is set to a wrong path, unset it or fix it:
unset KUBECONFIG
  1. Don't use sudo kubectl. Once the config is in your home, run kubectl as your normal user.

Tip: When copying the kubeconfig to a workstation, change server: https://127.0.0.1:6443 to the control-plane IP or DNS name, or the same kind of "connection refused" error will appear against 127.0.0.1:6443.

Verify#

kubectl config current-context kubectl cluster-info kubectl get nodes

You should see the API server URL (for example https://10.0.0.10:6443) instead of localhost:8080, and your nodes listed.