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/configwas never created for the current user (kubeadm only writes/etc/kubernetes/admin.conf).- You ran
sudo kubectl, so it looks in/root/.kube/configinstead of your home. KUBECONFIGpoints 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#
- 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"
- Or, if you work as root, just point
KUBECONFIGat it:
export KUBECONFIG=/etc/kubernetes/admin.conf
echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' >> /root/.bashrc
- 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"
- If
KUBECONFIGis set to a wrong path, unset it or fix it:
unset KUBECONFIG
- Don't use
sudo kubectl. Once the config is in your home, runkubectlas your normal user.
Tip: When copying the kubeconfig to a workstation, change
server: https://127.0.0.1:6443to the control-plane IP or DNS name, or the same kind of "connection refused" error will appear against127.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.