kubectl error: “The connection to the server localhost:8080 was refused – did you specify the right host or port?”

Problem

You want to use the kubectl to get your cluster information but you are getting the following error message

$ kubectl cluster-info
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
The connection to the server localhost:8080 was refused - did you specify the right host or port?

Solution

That could happen if you have already setup kubectl before or have used a different environment.

So you will need to first unset the environment variable KUBECONFIG with:

$ unset KUBECONFIG

and then use the gcloud client to set your environment again:


$ gcloud container clusters get-credentials your_cluster_name --zone europe-west2-a --project your_project_name

So after this you should be able to use kubectl to get the cluster information

kubeconfig entry generated for your_cluster_name.

$ kubectl cluster-info
Kubernetes master is running at https://xxx.xxx.xxx.xxx
GLBCDefaultBackend is running at https://xxx.xxx.xxx.xxx/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy
Heapster is running at https://xxx.xxx.xxx.xxx/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://xxx.xxx.xxx.xxx/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://xxx.xxx.xxx.xxx/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy