Fix "The connection to the server localhost:8080 was refused" with kubectl
The connection to the server localhost:8080 was refused - did you specify the right host or port?
- E1128 10:12:03.512345 48213 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
- The connection to the server 127.0.0.1:6443 was refused - did you specify the right host or port?
- error: no configuration has been provided, try setting KUBERNETES_MASTER environment variable
- Unable to connect to the server: dial tcp 192.168.49.2:8443: connect: no route to host
- The connection to the server 192.168.1.10:6443 was refused - did you specify the right host or port?
kubectl finds no kubeconfig file and falls back to localhost:8080, where no cluster is listening. Copy the cluster configuration to ~/.kube/config (or point KUBECONFIG at it) and select the right context; if the address is a real IP such as :6443, the cluster itself is stopped or unreachable.
What this error means
kubectl reads the Kubernetes API address, certificates and current context from a kubeconfig file: ~/.kube/config by default, or the files listed in the KUBECONFIG variable. If it finds none, or the current context is empty, it uses a legacy default, http://localhost:8080, and the connection fails. Seeing localhost:8080 in the message is therefore almost always a client-side configuration problem, not a cluster one. With another address (127.0.0.1:6443, 192.168.49.2:8443), the configuration is read, but the API server does not answer: local cluster stopped, node down or firewall.
Common causes
- No kubeconfig: after
kubeadm init,/etc/kubernetes/admin.confwas not copied to~/.kube/config. - Command run with
sudoor by another user: root looks for/root/.kube/config, not yours. - k3s or MicroK8s: the configuration lives elsewhere (
/etc/rancher/k3s/k3s.yaml) or the bundledkubectlmust be used (microk8s kubectl). - Empty or wrong
KUBECONFIG, or deleted current context:kubectl config current-contextreturns nothing. - Cluster stopped: minikube, kind or Docker Desktop turned off (address like
127.0.0.1:xxxxxor:6443), or the API server is down.
Step-by-step fix
-
Diagnose the configuration
Without
sudo. Ifcurrent-contextis empty andconfig viewshows no cluster, kubectl has no configuration.echo "$KUBECONFIG" ls -l ~/.kube/config kubectl config get-contexts kubectl config current-context kubectl config view --minify -
kubeadm cluster: copy admin.conf
These are the commands printed at the end of
kubeadm init. The file grants admin access to the cluster: keep it readable by you only.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 -
k3s or MicroK8s
Export the configuration to your home folder instead of making the system file world-readable (
--write-kubeconfig-mode 644exposes admin rights to every user on the machine).# k3s mkdir -p ~/.kube sudo k3s kubectl config view --raw > ~/.kube/config chmod 600 ~/.kube/config # MicroK8s microk8s config > ~/.kube/config -
Local cluster: start it and get the context
minikube and kind write their context to
~/.kube/configwhen they start. For Docker Desktop, enable Kubernetes in the settings, then pick thedocker-desktopcontext.minikube status minikube start # kind kind get clusters kind export kubeconfig --name my-cluster # Docker Desktop kubectl config use-context docker-desktop -
Managed cluster (EKS, GKE, AKS)
The provider’s CLI adds the cluster and its context to your kubeconfig.
aws eks update-kubeconfig --region eu-west-3 --name my-cluster gcloud container clusters get-credentials my-cluster --region europe-west1 az aks get-credentials --resource-group my-rg --name my-cluster -
Pick the context and check
Select the right context, then test. On Windows, the default file is
%USERPROFILE%\.kube\configand the variable is set with$env:KUBECONFIGin PowerShell.kubectl config use-context my-cluster kubectl cluster-info kubectl get nodes
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Keep one kubeconfig file per cluster and combine them with
KUBECONFIG=~/.kube/config:~/.kube/prod.yamlinstead of overwriting~/.kube/config. - Do not use
sudo kubectl: copy the configuration into the home folder of the user who needs it. - Show the current context in your prompt (kube-ps1, Starship) to avoid acting on the wrong cluster.
Frequently asked questions
Why localhost:8080 when my cluster is on port 6443?
Because kubectl read no configuration: localhost:8080 is its legacy default. As soon as a valid kubeconfig is found, the cluster address it contains is used.
It works with sudo but not without, why?
The configuration is in /root/.kube/config. Copy it to your ~/.kube/config and take ownership of the file with chown.
What about on a worker node?
Worker nodes have no admin kubeconfig by default. Run kubectl from the control plane node or from your workstation, with a securely copied configuration.
Useful tools for this error
More DevOps, servers & cloud errors
- Fix "upstream timed out (110: Connection timed out)" and 504 Gateway Time-out in Nginx
- Fix "nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)"
- Fix "413 Request Entity Too Large" (Nginx, PHP, Express, Cloudflare)
- Fix "Certbot failed to authenticate some domains" (Let’s Encrypt, Timeout during connect, acme-challenge 404)
- Fix "Resource not accessible by integration" in GitHub Actions
- Fix the 404 on refresh of a SPA on Vercel ("404: NOT_FOUND"), Netlify and Nginx
- Fix "An error occurred (AccessDenied) when calling the PutObject operation" on AWS S3