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

Problem:

You want to configure a Kubernetes service using kubectl using a command like

kubectl_patch_example.sh
kubectl patch service/"my-elasticsearch-svc" --namespace "default"   --patch '{"spec": {"type": "LoadBalancer"}}'

but you only see this error message:

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

Solution

Kubernetes does not have the correct credentials to access the cluster.

Add the correct credentials to the kubectl config using

gcloud_get_credentials.sh
gcloud container clusters get-credentials [cluster name] --zone [cluster zone]

e.g.

gcloud_container_list.sh
gcloud container clusters get-credentials cluster-1 --zone europe-west3-c

After that, retry your original command.

In case you don’t know your cluster name or zone, use

example.sh
gcloud container clusters list

to display the cluster metadata.

Credits to this StackOverflow answer for the original solution.


Check out similar posts by category: Allgemein, Cloud, Container, Kubernetes