Connection to Server Refused
When you see similar error as below:
1user@example:~$ kubectl get nodes
2The connection to the server localhost:8080 was refused - did you specify the right host or port?
Check for the "$KUBECONFIG" if set and "kubectl config view" output. Chances are, both are empty like below:
1user@example:~$ echo $KUBECONFIG
1user@example:~$ kubectl config view
2apiVersion: v1
3clusters: null
4contexts: null
5current-context: ""
6kind: Config
7preferences: {}
8users: null
In this example, we're using the DOKS or DigitalOcean Managed Kubernetes.
To fix the problem: Get the config file and set the value of "$KUBECONFIG" to it.
1user@example:~$ export KUBECONFIG=./k8s-infra-kubeconfig.yaml
2user@example:~$ echo $KUBECONFIG
3./k8s-infra-kubeconfig.yaml
and output of "kubectl config view" would also have something like below:
1user@example:~$ kubectl config view
2apiVersion: v1
3clusters:
4- cluster:
5 certificate-authority-data: DATA+OMITTED
6 server: https://b91642bd-a291-4200-c1ad-135ec3094be8.k8s.ondigitalocean.com
7 name: do-nyc1-k8s-infra
8contexts:
9- context:
10 cluster: do-nyc1-k8s-infra
11 user: do-nyc1-k8s-infra-admin
12 name: do-nyc1-k8s-infra
13current-context: do-nyc1-k8s-infra
14kind: Config
15preferences: {}
16users:
17- name: do-nyc1-k8s-infra-admin
18 user:
19 token: d44a591ac43024737ad524ffd17039b318d21646b685a140754391ebe23dea6c
Going back to our earlier command that has the error, you should now see like below:
1user@example:~$ kubectl get nodes
2NAME STATUS ROLES AGE VERSION
3default-pool-9yhb2 Ready <none> 6h40m v1.18.3
4default-pool-9yhbd Ready <none> 6h40m v1.18.3
More troubleshooting: https://github.com/kubernetes/kubernetes/issues/50295