Skip to content
ohsudo, Home
Kubernetes

kubectl "The connection to the server localhost:8080 was refused": your kubeconfig is missing

Fix kubectl "The connection to the server localhost:8080 was refused": kubectl has no kubeconfig. Set it up for kubeadm, k3s, minikube, EKS, GKE or AKS, and under sudo.

Beginner3 min readApplies to Kubernetes 1.28+ · kubectl · kubeadm · k3s · minikube · kind · EKS · GKE · AKS

by OhSudo ··

Checked on real software: kubectl · November 2026How we check

Not sure this is your cause?Find it step by step: Pod not running →

Part of a learning path:Kubernetes: debugging pods

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

Newer versions print a few couldn't get current server API group list lines before it. The clue is the address. Your cluster doesn’t run on localhost:8080: the API server normally listens on port 6443. localhost:8080 is kubectl’s fallback when it has no configuration at all.

So the cluster may be perfectly healthy. kubectl just doesn’t know where it is.

  1. kubectllooks for a kubeconfig
  2. No configuration found~/.kube/config, KUBECONFIG, sudo(breaks here)
  3. localhost:8080nothing there
localhost:8080 is kubectl's fallback when it has no kubeconfig at all. The cluster may be healthy; kubectl just doesn't know where it is.

TL;DR

Terminal window
kubectl config view # empty = no kubeconfig found
echo $KUBECONFIG
ls -l ~/.kube/config

Then get a config for your kind of cluster (section 2).

1. Confirm it

Terminal window
kubectl config view
apiVersion: v1
clusters: null
contexts: null
current-context: ""
kind: Config
preferences: {}
users: null

clusters: null is the proof. kubectl reads the files listed in $KUBECONFIG, or ~/.kube/config if that variable is empty. ~ depends on who you are, and that’s behind section 3.

2. Get a kubeconfig for your cluster

kubeadm, on the control plane:

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

k3s keeps it elsewhere, and readable only by root:

Terminal window
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config

From another machine, also change server: https://127.0.0.1:6443 in that file to the server’s address.

Local clusters write it when they start, so start them:

Terminal window
minikube start
kind create cluster

Cloud clusters have a command that adds it:

Terminal window
aws eks update-kubeconfig --region eu-central-1 --name my-cluster
gcloud container clusters get-credentials my-cluster --region europe-west1
az aks get-credentials --resource-group my-group --name my-cluster

Then check:

Terminal window
kubectl config current-context
kubectl get nodes

3. It works without sudo, but not with it (or the reverse)

sudo kubectl runs as root, whose home is /root, so it reads /root/.kube/config, and sudo normally drops your KUBECONFIG too. You rarely need sudo for kubectl: permissions come from the config file, not your Linux user. Run it without sudo. If you must, pass the file:

Terminal window
sudo kubectl --kubeconfig=/home/me/.kube/config get pods

The same catches scripts, cron jobs and CI: they run with a different user or environment. Set KUBECONFIG explicitly there. In a pipeline, store it as a secret: the CI/CD Pipeline Generator shows one way.

4. Several files, or the wrong context

KUBECONFIG can list several files, separated by : on Linux and macOS:

Terminal window
export KUBECONFIG=~/.kube/config:~/.kube/staging.yaml
kubectl config get-contexts
kubectl config use-context staging

Put the export in your shell’s profile, or it’s gone in the next terminal.

Warning

Keep kubeconfig files private: chmod 600 ~/.kube/config. The admin.conf from kubeadm gives full control of the cluster, so don’t copy it around or commit it to git. Give people and pipelines their own limited credentials.

5. When the address is a real one

If the error names your real server, kubectl has its config, and the problem is the connection:

The connection to the server 10.0.0.10:6443 was refused - did you specify the right host or port?

That’s a different job: the API server is down, or something’s in the way. On the control plane:

Terminal window
sudo ss -tlnp | grep 6443
sudo systemctl status kubelet --no-pager
sudo crictl ps -a | grep kube-apiserver

If it times out instead of being refused, look at firewalls, as “Connection timed out” vs “Connection refused” explains. The Port Lookup has an entry for 6443, and why it shouldn’t be open to everyone.

Shell, we have a problem!

Similar, but different: Unable to connect to the server: x509: certificate signed by unknown authority means kubectl reached a server, but the config has the wrong certificate: see that article. error: You must be logged in to the server (Unauthorized) means your credentials are wrong or expired. And Forbidden means you’re logged in, without the rights. For pods that won’t start once you’re connected, see Pod Pending or CrashLoopBackOff. To write the manifests themselves, try the Kubernetes Manifest Generator. For the commands around this, see the kubectl Cheat Sheet.

$ ./practice.sh

Practice this in the terminal

Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.

Open the terminal →

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles