About Me

My photo
I know the last digit of PI

Friday, November 26, 2021

Running Kubernetes cluster on Windows

The easiest way to run kubernetes cluster on Windows is via Docker desktop Once you've installed docker dekstop click on the "Settings" / "Kubernets" and select "Enable Kubernetes".
Now you can execute kubectl command and it should succeed.
# kubectl get pods
No resources found in default namespace.

However the cluster is accessible only from the same machine since the cluster is listening on 127.0.0.1:6443 . As a workaround to allow remote connections a port proxy can be added. Use elevated command prompt and execute following command:
# netsh interface portproxy add v4tov4 listenaddress=192.168.0.42 listenport=6443 connectaddress=127.0.0.1 connectport=6443 protocol=tcp
It is important to replace the 192.168.0.42 with the machine IP address. Using 0.0.0.0 prevents the cluster from starting, so the specific IP address should be used.

Next step is to add a firewall rule that allows all connections to port 6443. Open elevated PowerShell and execute:
New-NetFirewallRule -DisplayName 'Docker Desktop Kubernetes cluster on 6443' -Profile 'Private' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 6443

Copy the content of the %HOMEPATH%\.kube\config file to the remote machine and modify the host from "kubernetes.docker.internal" to "vm.docker.internal" and any occurance of the text "docker-desktop" to "remote-docker-desktop".
The final version of the config file should look like:
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: xxxxxx
    server: https://vm.docker.internal:6443
  name: remote-docker-desktop
contexts:
- context:
    cluster: remote-docker-desktop
    user: remote-docker-desktop
  name: remote-docker-desktop
current-context: docker-desktop
kind: Config
preferences: {}
users:
- name: remote-docker-desktop
  user:
    client-certificate-data: yyyyy
    client-key-data: zzzz

On the remote machine add "vm.docker.internal" to the /etc/hosts file
192.168.0.42 vm.docker.internal
Now executing kubectl command on the remote machine should succeed.
# kubectl get pods
No resources found in default namespace.

No comments: