GrepBoard/Manuals
605 manuals ·

Fix kubectl connection to the server localhost:8080 was refused

·Tested on kubectl v1.37.1 (ubuntu:24.04 Docker) · Kubernetes official docs · 2026-10·Test scope ↓·7 steps

kubectl shows localhost:8080 was refused usually because it loaded no kubeconfig. Check the loaded file with -v=6, then fix KUBECONFIG, HOME or the context.

output
The connection to the server localhost:8080 was refused - did you specify the right host or port?

When the address is localhost:8080, the cluster is usually fine: kubectl loaded no kubeconfig, or loaded one with no context selected, and fell back to a default address. In that case, point kubectl at the right kubeconfig for the account that runs it and the error goes away. The message alone says nothing about cluster health; check that separately once you connect.

One exception: if the config's server value or a --server flag really is localhost:8080 (or http://localhost:8080), kubectl read its config correctly and went there on purpose. Check the server: line of kubectl config view --minify and any --server/-s in the command line or a shell alias first. In that case skip [2]–[5] and treat it like the second row of the table in [1]: check whether an API server actually listens on that address.

[1] Read the address in the message first. When kubectl loads a config, it prints that config's server address instead. Here a kubeconfig points to https://127.0.0.1:6443 (no API server listens there):

console
$ KUBECONFIG=/home/app/.kube/config kubectl get podsE1027 08:50:38.766142      81 memcache.go:381] "Couldn't get current server API group list" err="Get \"https://127.0.0.1:6443/api?timeout=32s\": dial tcp 127.0.0.1:6443: connect: connection refused"…The connection to the server 127.0.0.1:6443 was refused - did you specify the right host or port?
Address in the messageMeaningWhere to look
localhost:8080Usually no config loaded, or no context (if the config or --server really says this address, see the next row)Steps [2]–[5] below
The address in your config (…:6443 etc.)Config loaded, API server unreachableAPI server status, firewall, typo in the address

The five memcache.go lines are the same failure repeated; only the last line matters. The exit code is 1, so scripts see it as a failure:

console
$ kubectl get pods >/dev/null 2>&1; echo $?1

[2] Check which file kubectl read with -v=6. When it loads a config file, loader.go prints Config loaded from file:. When it loads nothing, that line is missing:

console
$ kubectl get pods -v=6 2>&1 | grep -c loader.go0$ KUBECONFIG=/home/app/.kube/config kubectl get pods -v=6 2>&1 | grep loader.goI1027 08:50:59.237272      39 loader.go:408] Config loaded from file:  /home/app/.kube/config

No line: go to [3] and [5]. Line present but still localhost:8080: go to [4]. A kubectl config view that shows only clusters: null and current-context: "" means the same thing.

[3] Check the default path and KUBECONFIG. Per the official docs, kubectl reads $HOME/.kube/config; if KUBECONFIG is set, it reads that list instead (colon-separated on Linux and macOS, semicolon-separated on Windows). --kubeconfig makes it use that one file only.

console
$ ls -l ~/.kube/configls: cannot access '/root/.kube/config': No such file or directory

Run echo $KUBECONFIG too. An old export KUBECONFIG=… line in ~/.bashrc or a similar startup file is a common leftover.

⚠️ kubectl did not complain when KUBECONFIG pointed to a file that does not exist. With v1.37.1 and KUBECONFIG=/root/cluster.conf (missing), the only output was localhost:8080 was refused:

console
$ export KUBECONFIG=/root/cluster.conf; kubectl get pods…The connection to the server localhost:8080 was refused - did you specify the right host or port?

A typo in that path fails silently. When only one entry in the list is missing (KUBECONFIG=/root/missing.conf:/home/app/.kube/config), kubectl still found context dev from the other file. A file that exists but has broken YAML does not fall back to 8080; kubectl stops and names the file:

console
$ kubectl --kubeconfig /root/broken.conf get podserror: error loading config file "/root/broken.conf": yaml: line 3: did not find expected node content

[4] Set current-context. A file with clusters and contexts but an empty current context still ends in localhost:8080. If the CURRENT column of get-contexts has no *, this is your case:

console
$ kubectl config --kubeconfig /home/app/.kube/config get-contextsCURRENT   NAME   CLUSTER   AUTHINFO    NAMESPACE          dev    dev       dev-admin   $ kubectl --kubeconfig /home/app/.kube/config config use-context devSwitched to context "dev".

After use-context, the address in the message changed to the config's server (127.0.0.1:6443). For a one-off command against another context, pass --context <name>. When KUBECONFIG merges several files, the first file that sets current-context wins.

[5] Check the home directory of the account running kubectl. If the config lives in another user's home and you run as root or someone else, kubectl looks for that account's ~/.kube/config and finds nothing. Same container, only HOME changed:

console
$ echo $HOME/root$ kubectl get pods…The connection to the server localhost:8080 was refused - did you specify the right host or port?$ HOME=/home/app kubectl get pods…The connection to the server 127.0.0.1:6443 was refused - did you specify the right host or port?

If you got used to sudo kubectl …, drop sudo and run as the account that owns the config, or pass --kubeconfig <path>.

[6] Create the kubeconfig the way your installer documents it:

InstallerMethod in the official docs
kubeadmCopy /etc/kubernetes/admin.conf to $HOME/.kube/config with the three lines printed by kubeadm init. As root: export KUBECONFIG=/etc/kubernetes/admin.conf
k3sAdmin kubeconfig is /etc/rancher/k3s/k3s.yaml. Point other kubectl binaries at it with KUBECONFIG or --kubeconfig. On a machine outside the cluster, replace the server value with the server's address
Amazon EKSaws eks update-kubeconfig --region region-code --name my-cluster — creates or merges into .kube/config in your home directory by default
GKEgcloud container clusters get-credentials CLUSTER_NAME --location=CONTROL_PLANE_LOCATION — saved to $HOME/.kube/config by default

The three kubeadm lines:

console
$ mkdir -p $HOME/.kube$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

⚠️ admin.conf carries a cluster-admin certificate. The kubeadm docs say not to share it with anyone and recommend separate credentials for normal users via kubeadm kubeconfig user --client-name <CN>. The super-admin.conf generated alongside it bypasses RBAC; the docs recommend moving it to a safe location. cp -i asks before overwriting an existing config. If you already have other clusters in it, copy to a different name and add it to the KUBECONFIG list instead.

[7] Verify. kubectl config current-context returns a name and -v=6 shows Config loaded from file: — the kubectl side is done. If it still cannot connect, the address in the message has changed; go to the second row of the table in [1]. To compare cluster and kubectl versions, see Kubernetes 1.35 end of life: check versions. If pods fail after you connect, continue with Find the cause of CrashLoopBackOff or Fix ImagePullBackOff.

#kubernetes#kubectl#kubeconfig