How to fix kubectl - The connection to the server localhost:8080 was refused - did you specify the right host or port? (kubeadm, Kubespray, minikube, k3s, EKS)
TL;DR
The connection to the server localhost:8080 was refusedmeans kubectl found no kubeconfig and fell back to its hard-coded default,http://localhost:8080. Nothing listens there, and nothing should: API servers listen on 6443.- kubectl looks for a kubeconfig in exactly three places, in this order: the
--kubeconfigflag, theKUBECONFIGenvironment variable, then$HOME/.kube/config. Whichever is missing for the user you are running as, that is the bug. - The fix after
kubeadm initor Kubespray is three lines:mkdir -p $HOME/.kube,sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config,sudo chown $(id -u):$(id -g) $HOME/.kube/config. Run them as the user who runs kubectl, never withsudo kubectl. sudo kubectlfails because sudo changes$HOMEto/root; k3s fails because/etc/rancher/k3s/k3s.yamlis root-only (mode 600); EKS, GKE and AKS need the cloud CLI to write the file first. I reproduced every variant below with kubectl v1.37.2 against a kind v0.33.0 cluster and a fresh k3s v1.37.2+k3s1 install on Ubuntu 24.04 on 2026-11-08; the outputs are in section 7.- Once the error names your real server (
:6443) instead oflocalhost:8080, the kubeconfig is fixed and you have a different problem: the API server itself, a firewall on port 6443, or an expired certificate. If your cluster never came up at all, start with the kubeadm preflight errors post instead of this one.
You have just installed a Kubernetes cluster - with kubeadm, with Kubespray, on a fresh EC2 instance - you type kubectl get nodes, and instead of your nodes you get -
1$ kubectl get nodes
2The connection to the server localhost:8080 was refused - did you specify the right host or port?
Nothing is listening on localhost:8080, and nothing should be. Kubernetes API servers listen on 6443. The error is not telling you the cluster is broken - it is telling you that kubectl has no idea where your cluster is, so it fell back to its built-in default address. In other words: kubectl could not find a kubeconfig file. I have hit this error on every kind of cluster I have built for this blog, and I still hit it when I type sudo kubectl out of habit. In this post I give you the three-line fix, show you the exact output of every way the kubeconfig goes missing (I ran all of them again for this update), explain why it happens with sudo and root, and cover every cluster type.
Table of Content
- Why the error says localhost:8080
- The fix after kubeadm or Kubespray (three lines)
- Why it works as root but not as your user (or the other way round)
- The same fix for minikube, k3s, EKS, GKE, AKS and kind
- Verify which kubeconfig kubectl is using
- When the API server really is unreachable
- What broke when I ran this
- Conclusion
1. Why the error says localhost:8080
kubectl needs a kubeconfig - a YAML file with the cluster's API server address, the CA certificate and your credentials. It looks for one in this order -
- the
--kubeconfigflag on the command line, - the
KUBECONFIGenvironment variable (a colon-separated list of files, merged), $HOME/.kube/config- the default location for the current user.
If none of the three exists, kubectl assumes an unauthenticated API server on http://localhost:8080 - a default from the very early days of Kubernetes - tries to connect, and nothing answers. Hence the message. The kubectl reference and the kubeconfig docs describe the lookup; the short version is in the diagram -
So the fix is always the same: give kubectl a kubeconfig - either copy it to ~/.kube/config or point KUBECONFIG at it.
2. The fix after kubeadm or Kubespray (three lines)
Both kubeadm and Kubespray write the admin kubeconfig to /etc/kubernetes/admin.conf on the control-plane node, readable by root only. kubeadm init even prints the fix at the end of its output (creating a cluster with kubeadm) - the lines everybody scrolls past -
1mkdir -p $HOME/.kube
2sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
3sudo chown $(id -u):$(id -g) $HOME/.kube/config
Then -
1kubectl get nodes
2# NAME STATUS ROLES AGE VERSION
3# kmaster Ready control-plane 12m v1.37.0
That is the whole fix for 90% of the people who search this error. The chown matters - without it the file belongs to root and you get a different error (error loading config file ... permission denied). On a Kubespray cluster the file is in the same place on the first control-plane node (kube_control_plane group); it is step 11 of my Kubespray guide. To use the cluster from your laptop, copy that file down (scp user@kmaster:~/.kube/config ~/.kube/config) and change the server: address from the private IP to one you can reach - the API server certificate must include that name or IP (kubeadm init --apiserver-cert-extra-sans).
A security note from the kubeadm docs: admin.conf has cluster-admin rights - do not hand it to colleagues. Generate per-user credentials with kubeadm kubeconfig user --client-name <name> and bind roles with kubectl create rolebinding; and super-admin.conf, which bypasses RBAC entirely, should be moved somewhere safe.
3. Why it works as root but not as your user (or the other way round)
The kubeconfig lives in $HOME, and $HOME is different for every user. The three classic situations -
- You ran the fix as
vagrant/ubuntu, thensudo kubectl get nodesfails -sudoruns as root withHOME=/root, where there is no.kube/config. Do not run kubectl with sudo; or, as root,export KUBECONFIG=/etc/kubernetes/admin.conf. - You did everything as root, then logged in as a normal user - the normal user has no
~/.kube/config. Run the three lines as that user. - It worked yesterday, fails today - you had
export KUBECONFIG=...in a shell session; the new shell does not have it. Put the export in~/.bashrc, or copy the file to~/.kube/configso no variable is needed.
1# as root, the documented way
2export KUBECONFIG=/etc/kubernetes/admin.conf
3echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' >> ~/.bashrc
4. The same fix for minikube, k3s, EKS, GKE, AKS and kind
Same error, different location of the file -
| Cluster | Where the kubeconfig is | Fix |
|---|---|---|
| minikube | writes ~/.kube/config itself when you run minikube start | if you deleted or overwrote it: minikube update-context; if minikube is stopped: minikube start (minikube and kubectl, my Minikube guide) |
| k3s | /etc/rancher/k3s/k3s.yaml (root-readable) | export KUBECONFIG=/etc/rancher/k3s/k3s.yaml or sudo k3s kubectl get nodes, or install with --write-kubeconfig-mode 644 (k3s cluster access) |
| Amazon EKS | not on any node - generated from the AWS API | aws eks update-kubeconfig --region eu-central-1 --name my-cluster with working AWS credentials (EKS kubeconfig) |
| GKE | generated by gcloud | gcloud container clusters get-credentials my-cluster --region europe-west3 |
| AKS | generated by az | az aks get-credentials --resource-group rg --name my-cluster |
| kind | written to ~/.kube/config on kind create cluster | kind get kubeconfig --name kind > ~/.kube/config |
| Docker Desktop | written to ~/.kube/config when Kubernetes is enabled | enable Kubernetes in Docker Desktop settings, kubectl config use-context docker-desktop |
Several clusters at once? Keep them all in one file and switch with kubectl config use-context, or merge files with KUBECONFIG=~/.kube/config:~/.kube/prod kubectl config view --flatten > merged. The kubectl config reference lists the sub-commands.
5. Verify which kubeconfig kubectl is using
1echo $KUBECONFIG # empty = using ~/.kube/config
2ls -l ~/.kube/config # exists? owned by you?
3kubectl config view --minify # the cluster/user/context in effect - server: should be https://<ip>:6443
4kubectl config current-context
5kubectl config get-contexts
6kubectl cluster-info # talks to the API server
7kubectl version # client and server versions - server appears only if connected
If kubectl config view shows server: https://10.0.1.10:6443 and cluster-info works, the kubeconfig problem is solved and any remaining error is a real connectivity or authentication problem - next section.
6. When the API server really is unreachable
Once a kubeconfig is in place the error changes wording, and each wording has a cause -
1. The connection to the server 10.0.1.10:6443 was refused - (in my test above it read 127.0.0.1:35161 was refused after I deleted the kind cluster) The API server is not running on the control plane. On the node: sudo crictl ps | grep apiserver, sudo systemctl status kubelet, sudo journalctl -u kubelet -f. Common causes: swap is on (the swap error), the container runtime is down, or etcd is unhealthy (etcd cluster is unavailable).
2. dial tcp 10.0.1.10:6443: i/o timeout - Network: a firewall or AWS security group does not allow 6443 from where you are, or the IP in the kubeconfig is a private address you cannot reach from your laptop. Open 6443 (security groups) or use the public IP / a bastion.
3. x509: certificate signed by unknown authority / certificate is valid for 10.0.1.10, not 3.70.1.2 - You edited server: to another address that is not in the API server certificate. Re-init with --apiserver-cert-extra-sans, or kubeadm init phase certs apiserver to regenerate, or (lab only) --insecure-skip-tls-verify.
4. error: You must be logged in to the server (Unauthorized) - Expired or wrong client certificate/token. kubeadm certs expire after one year: sudo kubeadm certs check-expiration, sudo kubeadm certs renew all, then copy the new admin.conf again. On EKS, your AWS identity is not mapped in the cluster's access entries.
5. error: error loading config file "/home/ubuntu/.kube/config": open ...: permission denied - The file is owned by root - the chown line from section 2.
6. Unable to connect to the server: net/http: TLS handshake timeout - The API server is overloaded or the node is out of resources (the memory check in Kubespray).
7. What broke when I ran this
For this update I ran every situation from this post again on a clean Ubuntu 24.04 machine on 2026-11-08: kubectl v1.37.2, a kind v0.33.0 cluster (node image v1.37.0) and a fresh k3s v1.37.2+k3s1 install. Five of the ten cases surprised me, and three of them are not what the error message suggests.
1. The error is the same whether the file is missing or the variable points at nothing. With no ~/.kube/config and KUBECONFIG unset, this is the full output. The five memcache.go lines are kubectl retrying the API discovery call; the last line is the one everybody searches for.
1$ kubectl get pods
2E1108 12:54:36.414068 2211 memcache.go:381] "Couldn't get current server API group list" err="Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"
3(the same line four more times)
4The connection to the server localhost:8080 was refused - did you specify the right host or port?
5
6$ kubectl config view
7apiVersion: v1
8clusters: null
9contexts: null
10current-context: ""
11kind: Config
12users: null
13
14$ kubectl config current-context
15error: current-context is not set
Note the [::1]:8080: on this machine localhost resolved to IPv6 first, so even the address in the message changes between machines. kubectl config view printing clusters: null is the fastest confirmation that kubectl sees no kubeconfig at all.
2. KUBECONFIG pointing at a file that does not exist is silently ignored. I expected a "no such file" error. Instead kubectl behaved exactly as if the variable were unset and fell back to localhost:8080. A typo in export KUBECONFIG=~/.kube/confg therefore produces the same error as no configuration at all, which is why echo $KUBECONFIG is the first check in section 5.
1$ export KUBECONFIG=/tmp/lab/does-not-exist.yaml
2$ kubectl get pods
3E1108 12:54:36.552737 2225 memcache.go:381] "Couldn't get current server API group list" err="Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"
4(the same line four more times)
5The connection to the server localhost:8080 was refused - did you specify the right host or port?
6
7$ kubectl --kubeconfig /tmp/lab/nope.yaml get pods
8error: stat /tmp/lab/nope.yaml: no such file or directory
The --kubeconfig flag, by contrast, fails loudly with a stat error. If you are scripting, prefer the flag: it cannot fall back to the wrong cluster.
3. A kubeconfig without a current-context gives the same 8080 error, even though the file exists. I took a valid file, deleted only the current-context: line, and got the identical message. kubectl had a cluster and a context in the file and still used localhost:8080 because no context was selected. The fix is kubectl config use-context <name>, not copying files around.
1$ sed '/current-context/d' wrong.yaml > nocontext.yaml
2$ kubectl --kubeconfig /tmp/lab/nocontext.yaml get pods
3E1108 12:54:36.714220 2242 memcache.go:381] "Couldn't get current server API group list" err="Get \"http://localhost:8080/api?timeout=32s\": dial tcp [::1]:8080: connect: connection refused"
4(the same line four more times)
5The connection to the server localhost:8080 was refused - did you specify the right host or port?
4. A context whose user has no credentials asks for a username. The same file with its current-context intact, pointing at https://127.0.0.1:6443 where nothing listens, did not say "connection refused" at all. kubectl saw a user entry with no certificate or token and prompted for basic auth before trying the network:
1$ kubectl --kubeconfig /tmp/lab/wrong.yaml get pods
2Please enter Username: error: EOF
If you ever see Please enter Username, the users: section of your kubeconfig is empty, which usually means a hand-edited or half-merged file.
5. sudo really does change HOME, and both fixes work. On the kind cluster my own user could list nodes, sudo kubectl could not, and sudo env showed why. Passing the file with --kubeconfig or exporting KUBECONFIG and using sudo -E both worked on the first try.
1$ kubectl get nodes -o wide
2NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
3lab-control-plane Ready control-plane 25s v1.37.0 172.18.0.2 <none> Debian GNU/Linux 13 (trixie) 7.0.0-1017-azure (amd64) containerd://2.3.4
4
5$ ls -l /home/runner/.kube/config
6-rw------- 1 runner runner 5576 Nov 8 12:55 /home/runner/.kube/config
7
8$ sudo kubectl get nodes
9(five memcache.go lines)
10The connection to the server localhost:8080 was refused - did you specify the right host or port?
11
12$ sudo env | grep -E '^(HOME|KUBECONFIG)='
13HOME=/root
14
15$ sudo kubectl --kubeconfig /home/runner/.kube/config get nodes
16NAME STATUS ROLES AGE VERSION
17lab-control-plane Ready control-plane 25s v1.37.0
18
19$ export KUBECONFIG=$HOME/.kube/config
20$ sudo -E kubectl get nodes
21NAME STATUS ROLES AGE VERSION
22lab-control-plane Ready control-plane 26s v1.37.0
6. Another user gets permission denied, not 8080. kind writes the kubeconfig with mode 600 (see the ls -l above). Running kubectl as a different user against that file is the one case where the error is honest about the cause:
1$ sudo -u nobody kubectl --kubeconfig /home/runner/.kube/config get nodes
2error: error loading config file "/home/runner/.kube/config": open /home/runner/.kube/config: permission denied
7. A stale kubeconfig names the right port and still says refused. After kind delete cluster the saved file still pointed at the old API server port, and the message named it. This is the "section 6" shape of the error: the kubeconfig is fine, the server is gone.
1$ kind delete cluster --name lab
2Deleting cluster "lab" ...
3Deleted nodes: ["lab-control-plane"]
4
5$ kubectl --kubeconfig /tmp/lab/stale.yaml get nodes
6The connection to the server 127.0.0.1:35161 was refused - did you specify the right host or port?
8. k3s: even sudo kubectl fails until you point at k3s.yaml. k3s wrote /etc/rancher/k3s/k3s.yaml with mode 600 and owned by root (I installed with --write-kubeconfig-mode 600, the secure default). sudo k3s kubectl worked because the wrapper knows the path; plain kubectl failed for my user and, more surprisingly, so did sudo kubectl, because root's $HOME/.kube/config does not exist either and k3s does not set KUBECONFIG for anyone. A readable copy plus export KUBECONFIG fixed it; the k3s context is simply called default.
1$ sudo k3s kubectl get nodes
2NAME STATUS ROLES AGE VERSION
3runnervm7aw5e Ready control-plane 20s v1.37.2+k3s1
4
5$ ls -l /etc/rancher/k3s/k3s.yaml
6-rw------- 1 root root 2949 Nov 8 12:55 /etc/rancher/k3s/k3s.yaml
7
8$ kubectl get nodes
9(five memcache.go lines)
10The connection to the server localhost:8080 was refused - did you specify the right host or port?
11
12$ sudo kubectl get nodes
13(five memcache.go lines)
14The connection to the server localhost:8080 was refused - did you specify the right host or port?
15
16$ sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/k3s.yaml && sudo chown $(id -u):$(id -g) ~/.kube/k3s.yaml && chmod 600 ~/.kube/k3s.yaml
17$ export KUBECONFIG=~/.kube/k3s.yaml
18$ kubectl get nodes
19NAME STATUS ROLES AGE VERSION
20runnervm7aw5e Ready control-plane 21s v1.37.2+k3s1
21
22$ kubectl config current-context
23default
What I take from the ten runs: the message is wrong about the cause in nine of them. The reliable diagnostic is not the error text but the three-command check in section 5, starting with kubectl config view. The whole scenario is a 100-line shell script; the kind cluster was ready 19 seconds after kind create cluster, and the k3s install took about 20 seconds more, so you can rerun all of it in under 3 minutes on any Ubuntu machine.
8. Conclusion
"The connection to the server localhost:8080 was refused" means kubectl has no kubeconfig and used its ancient default address. Give it one -
- After kubeadm or Kubespray, the three lines:
mkdir -p $HOME/.kube,sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config,sudo chown $(id -u):$(id -g) $HOME/.kube/config- as the user who will run kubectl, not with sudo. - As root,
export KUBECONFIG=/etc/kubernetes/admin.conf; for k3s point at/etc/rancher/k3s/k3s.yaml; for EKS, GKE, AKS generate the file with the cloud CLI; minikube and kind write it for you. - Verify with
kubectl config view --minifyandkubectl cluster-info- and once the address in the error is your API server's:6443, the problem is connectivity, certificates or the API server itself, not the kubeconfig.
The official pages are organizing cluster access using kubeconfig files and the kubeadm cluster creation guide. The complete cluster setups this error usually follows are on the Kubernetes table of content.
Learn more On Kubernetes -
- Setup kubernetes on Ubuntu
- Setup Kubernetes on CentOs
- Setup HA Kubernetes Cluster with Kubespray
- Setup HA Kubernetes with Minikube
- Setup Kubernetes Dashboard for local kubernetes cluster
- Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
- How to use Persistent Volume and Persistent Volume Claims in Kubernetes
- Deploy Spring Boot Microservice on local Kubernetes cluster
- Deploy Spring Boot Microservice on Cloud Platform(GCP)
- Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
- How to setup kubernetes jenkins pipeline on AWS?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- How to fix kubernetes pods getting recreated?
- How to delete all kubernetes PODS?
- How to use Kubernetes secrets?
- Share kubernetes secrets between namespaces?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Delete Kubernetes POD stuck in terminating state?
- Kubernetes IP Calculator - the free tool that sizes node subnets, Pod ranges and max pods per node for EKS, GKE and AKS
- Kubernetes IP address planning - 22 problems the calculator solves on EKS, GKE, AKS and on-premises (kubeadm, Mirantis MKE, RKE2, OpenShift, Tanzu, Talos)
Did this save you a debugging session? Stay in the loop -
- Subscribe on YouTube - every post here has a screen-recorded companion video.
- Code on GitHub - the repositories, Terraform and scripts behind these posts.