Search code examples
kubernetesdocker-desktop

Docker Desktop on Windows11: kubectl get pods gives error on my laptop: The connection to the server 127.0.0.1:49994 was refused


I want to run Kubernetes locally so I can try it out on a small Java program I have written.

I installed WSL2 on my Windows 11 laptop, Docker Desktop, and enabled Kubernetes in Docker Settings.

There are a number of SO questions with the same error but I do not see any of them regarding Windows 11 and Docker Desktop.

I open a terminal, type wsl to open a linux terminal. Then I issue the command:

$ kubectl get pods
The connection to the server 127.0.0.1:49994 was refused - did you specify the right host or port?

but I see

docker


Solution

  • Using Docker Desktop and Kubernetes on Linux Ubuntu, I got the same error, but also with Docker Desktop being unable to start normally because I already had a Docker Installation on my machine, resulting in the docker context being set to the default Docker environment instead of the required Docker Desktop.

    Confirm the following first:

    1. Make sure kubectl is correctly installed and ~/.kube/config file exists and is correctly configured on your machine because it holds the configuration of the cluster info and the port to connect to, which both are used by kubectl.

    2. Check the context with

      kubectl config view

    3. If not set to current-context: docker-desktop, for example

       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
    

    then Set the docker context to Docker Desktop on your machine

    kubectl config use-context docker-desktop

    If that doesn't solve your issue maybe you have to check for specific Windows 11 Docker Desktop Kubernetes configuration/features

    Check also: Docker Desktop Windows FAQs