google cloud platform

Kubernetes Error : did you specify the right host or port ?

This tutorial guides you on how to fix error ‘ The connection to the server refused – did you specify the right host or port? ‘. While I was trying to run the demo app in kubernetes using kubectl command I am stuck with the error mentioned. Let’s see how I troubleshooted to fix the same.

Kubernetes Error : did you specify the right host or port

Below is the error I faced while trying to run demo app in Kubernetes.

$ kubectl run demo --image=sneppets/myhello --port=9999 --labels app=demo
The connection to the server localhost:8080 was refused - did you specify the right host or port?

First, I checked whether my kubeconfig is all set right. I tried running the kubectl config view command as shown below. Then figured out that required kubeconfig configurations are missing.

$ kubectl config view
apiVersion: v1
clusters: null
contexts: null
current-context: ""
kind: Config
preferences: {}
users: null

You can also check kubeconfig content using vim editor in the following path /home/sneppets/.kube/config

Solution

Therefore, I tried to autogenerate the required config for the kubernetes using the following command.

$ gcloud container clusters get-credentials --zone us-central1-f myhello
Fetching cluster endpoint and auth data.
kubeconfig entry generated for myhello.

Now, the entries are generated. You can check the entries by running the kubectl config view command again as shown below.

$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://34.66.18.243
  name: gke_poised-shift-300712_us-central1-f_myhello
contexts:
- context:
    cluster: gke_poised-shift-300712_us-central1-f_myhello
    user: gke_poised-shift-300712_us-central1-f_myhello
  name: gke_poised-shift-300712_us-central1-f_myhello
current-context: gke_poised-shift-300712_us-central1-f_myhello
kind: Config
preferences: {}
users:
- name: gke_poised-shift-300712_us-central1-f_myhello
  user:
    auth-provider:
      config:
        cmd-args: config config-helper --format=json
        cmd-path: /usr/lib/google-cloud-sdk/bin/gcloud
        expiry-key: '{.credential.token_expiry}'
        token-key: '{.credential.access_token}'
      name: gcp

Finally, I tried running the pod again and now the error disappeared and pod was created successfully and running.

$ kubectl run demo --image=sneppets/myhello --port=9999 --labels app=demo
pod/demo created

Similarly, you can do deployment also using kubectl and it should deploy and run pods successfully after the above fix.

Therefore, the kubernetes error did you specify the right host or port was due to incorrect kubeconfig.

Note, in case if there are wrong entries in the kubeconfig file then you need to unset users, contexts, clusters and the current-context then do re-generate again to fix the issue.

That’s it. Hope it helped 🙂

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments