Docker Desktop for Windows comes shipped with kubernetes out of the box. It preconfigures a lot of stuff on your Windows machine, like downloads kubectl and puts everything in PATH variable, so that it can be accessed from powershell. But a lot of developers have took a liking to WSL which provides a more linux native environment for development. This post will go through steps to set up kubernetes for wsl.
You should have docker for desktop installed and WSL enabled to follow along.
Steps:
- Enable kubernetes if you haven't done so already. Go to docker desktop settings -> Kubernetes tab -> check "Enable Kubernetes" setting. After status says "kubernetes is running" run the following command in powershell.
kubectl get nodes
If it shows docker-for-desktop
then it means kubernetes is running correctly.
- Open WSL console and install kubectl using the following commands
sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl
- Setting up kubeconfig file: This is the magic step, kubernetes runs on port 6443 on your host computer and to access it you'll need a kubeconfig file. This is already done by docker for the windows environment but for WSL you'll need to grab the config. Turns out it's pretty easy. Just run the following command in bash and replace {username} with your windows username (not the WSL user name)
cp /mnt/c/Users/{username}/.kube/config ~/.kube/config
This command copies your kubeconfig file from windows to wsl.
- All done: Just run
kubectl get nodes
to see a docker-for-desktop
node.
Top comments (3)
Cool, but I get the error (WSL 2) with Docker Desktop on Windows host The connection to the server kubernetes.docker.internal:6443 was refused - did you specify the right host or port?
I think this post is quite old, and wouldn't work for wsl 2 since it runs inside a virtual machine. This probably doesn't answer the question, but I haven't tried wsl 2 myself so I really can't comment.
I am using WSL2 and me, here, confirm that it did work for me, thanks. 😁