Skip to content
  1. LuzineDebug
  2. DevOps, servers & cloud
  3. The connection to the server localhost:8080 was…
DevOps, servers & cloud Intermediate ≈ 10 min to fix

Fix "The connection to the server localhost:8080 was refused" with kubectl

Error message
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?
In short

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.

Be the first to confirm this fix

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

  1. No kubeconfig: after kubeadm init, /etc/kubernetes/admin.conf was not copied to ~/.kube/config.
  2. Command run with sudo or by another user: root looks for /root/.kube/config, not yours.
  3. k3s or MicroK8s: the configuration lives elsewhere (/etc/rancher/k3s/k3s.yaml) or the bundled kubectl must be used (microk8s kubectl).
  4. Empty or wrong KUBECONFIG, or deleted current context: kubectl config current-context returns nothing.
  5. Cluster stopped: minikube, kind or Docker Desktop turned off (address like 127.0.0.1:xxxxx or :6443), or the API server is down.

Step-by-step fix

  1. Diagnose the configuration

    Without sudo. If current-context is empty and config view shows no cluster, kubectl has no configuration.

    bash
    echo "$KUBECONFIG"
    ls -l ~/.kube/config
    kubectl config get-contexts
    kubectl config current-context
    kubectl config view --minify
  2. 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.

    bash
    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
  3. k3s or MicroK8s

    Export the configuration to your home folder instead of making the system file world-readable (--write-kubeconfig-mode 644 exposes admin rights to every user on the machine).

    bash
    # k3s
    mkdir -p ~/.kube
    sudo k3s kubectl config view --raw > ~/.kube/config
    chmod 600 ~/.kube/config
    # MicroK8s
    microk8s config > ~/.kube/config
  4. Local cluster: start it and get the context

    minikube and kind write their context to ~/.kube/config when they start. For Docker Desktop, enable Kubernetes in the settings, then pick the docker-desktop context.

    bash
    minikube status
    minikube start
    # kind
    kind get clusters
    kind export kubeconfig --name my-cluster
    # Docker Desktop
    kubectl config use-context docker-desktop
  5. Managed cluster (EKS, GKE, AKS)

    The provider’s CLI adds the cluster and its context to your kubeconfig.

    bash
    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
  6. Pick the context and check

    Select the right context, then test. On Windows, the default file is %USERPROFILE%\.kube\config and the variable is set with $env:KUBECONFIG in PowerShell.

    bash
    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.yaml instead 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

All DevOps, servers & cloud errors →