Scenario:

You are running kubernetes locally in Docker-Desktop. You are running Linux containers. Kubernetes starts fine but when you try command like

kubectl get nodes

it shows message-

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

 

Solution:

Run command ‘kubectl config view‘, if it shows output as below

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://kubernetes.docker.internal:6443
  name: docker-desktop
contexts:
- context:
    cluster: docker-desktop
    user: docker-desktop
  name: docker-desktop
current-context: ""
kind: Config
preferences: {}
users:
- name: docker-desktop
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

 

if current-context is set NULL then run this command to set the context

kubectl config use-context docker-desktop

This will set context correctly and issue will be resolved.

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://kubernetes.docker.internal:6443
  name: docker-desktop
contexts:
- context:
    cluster: docker-desktop
    user: docker-desktop
  name: docker-desktop
current-context: docker-desktop
kind: Config
preferences: {}
users:
- name: docker-desktop
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED