kubectl connection refused ☸️ Kubernetes

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

kubectl has no usable kubeconfig, so it falls back to localhost:8080 — or the cluster API server is unreachable.

Seen on: Kubernetes AWS Azure

Meaning

localhost:8080 almost always means “no kubeconfig found” (KUBECONFIG unset, wrong user, or running with sudo). Other hosts in the message mean the API server is down or blocked, or your credentials expired (“You must be logged in to the server (Unauthorized)”).

Common causes

  • No ~/.kube/config for this user (or sudo uses root’s home)
  • KUBECONFIG pointing to a missing file
  • Wrong current context
  • API server down / VPN not connected
  • Expired cloud credentials (EKS/GKE/AKS tokens)

⚡ Quick fix

  1. kubectl config get-contexts and kubectl config use-context …
  2. Regenerate kubeconfig (aws eks update-kubeconfig, gcloud container clusters get-credentials, az aks get-credentials)
  3. Don’t run kubectl with sudo
  4. k3s/kubeadm: copy /etc/rancher/k3s/k3s.yaml or /etc/kubernetes/admin.conf to ~/.kube/config

Detailed fix by platform

Kubernetes

  1. Check which config is used:
    bash
    echo $KUBECONFIG
    kubectl config view --minify
    kubectl config get-contexts
    aws eks update-kubeconfig --name prod-cluster --region ap-south-1

How to diagnose

  1. Host in message — localhost:8080 (no config) or a real API endpoint?
  2. Context — Current context correct?
  3. Credentials — Expired token?
  4. Network — VPN / firewall to the API server?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.