kubectl: The Connection to the Server Was Refused
"The connection to the server localhost:8080 was refused" almost never means your cluster is down — it means kubectl has no kubeconfig and is guessing. Here's why, and the one-line fix.
You run any command and get:
The connection to the server localhost:8080 was refused - did you specify the right host or port?
The giveaway is localhost:8080. You almost certainly didn't configure a server at that address — that's kubectl's fallback when it can't find a kubeconfig. So this rarely means "the cluster is broken." It means kubectl doesn't know where your cluster is, defaulted to an old insecure port, and found nothing listening there.
Cause 1: No kubeconfig loaded
kubectl reads its cluster address from ~/.kube/config (or whatever KUBECONFIG points at). If that file is missing or empty, you get the localhost:8080 fallback. Check what kubectl thinks it's talking to:
kubectl config current-context # "error: current-context is not set" = no config
Point kubectl at the right file:
export KUBECONFIG=/path/to/your/kubeconfig
kubectl config current-context # should now name your cluster
Cause 2: k3s / k3d — the config is root-owned
On a fresh k3s node the kubeconfig lives at /etc/rancher/k3s/k3s.yaml and is only readable by root, so a normal-user kubectl finds nothing. Copy it to your user and export it:
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config
(Or just use sudo k3s kubectl ....)
Cause 3: The wrong context is selected
If you have several clusters, you may be pointed at one that isn't running. List them and switch:
kubectl config get-contexts # * marks the active one
kubectl config use-context my-real-cluster
Cause 4: The cluster really is down
Only after the above — if the context is correct and still refused, the API server may genuinely be off. For a local cluster, confirm it's running (k3s: systemctl status k3s; minikube: minikube status) and start it.
The checklist
- See
localhost:8080? Suspect missing kubeconfig, not a dead cluster. kubectl config current-context— is one even set?- Set
KUBECONFIG(k3s: copy/etc/rancher/k3s/k3s.yamlto~/.kube/config). - Multiple clusters?
kubectl config use-context <name>. - Only then check whether the API server / cluster is actually running.
Frequently Asked Questions
Why does kubectl say "localhost:8080"?
That's kubectl's fallback address when it can't find a kubeconfig. It almost always means no config is loaded — not that your cluster is down.
Where is the kubeconfig for k3s?
At /etc/rancher/k3s/k3s.yaml (root-owned). Copy it to ~/.kube/config and fix ownership, or just run sudo k3s kubectl.
How do I switch between clusters?
Run kubectl config get-contexts to list them, then kubectl config use-context <name> to select the right one.
Stop reading, start building
This pairs with a hands-on BytExplorer course — do it on your own machine and actually keep the skill.