top of page
  • Writer's picturealexchiri

Easily reproducible local Kubernetes environment with WSL

Updated: Mar 18

This is a post specifically for Windows users. I might be getting a lot of eggs in my face for saying this, but I think that Windows Subsystem for Linux (WSL) is one of the best features for developers on Windows (despite its ugly name). In the last years, Microsoft has stepped up its game on making Windows a developer friendly OS and I believe that Windows is the most developer feature-full OS out-of-the-box of all. But that is the subject of another post.

 

WSL and its tight integration with Windows, makes it a great playground for developers, because you can easily create VMs based on different Linux distributions and make experiments without impacting your host (including running GUI apps inside the VM) or run developer environments.

 

The bonus is that you can programmatically create these WSL VMs using Dockerfiles, making it even easier and faster to experiment with different sets of configuration and applications using a language that is ubiquitous by now. I will exemplify with how to create a simple sandbox VM in WSL that runs Kubernetes. This would be a great learning or development environment for Kubernetes services. Stay tuned below!

 

It all starts with a Dockerfile


Here's a Dockerfile that installs minikube and several tools that are nice to have when working with Kubernetes. Minikube is a great way to run locally a Kubernetes cluster, there are many other options, but picked minikube for this case.


This is a summary of what this Dockerfile does:

  • installs some common Linux tools

  • creates a new user called ubuntu

  • installs Docker and adds ubuntu in the docker group

  • installs various Kubernetes tools: kubectl, stern, argocd-cli, minikube, helm, kubectx and kubens

  • configures zsh and adds autocomplete and alias for kubectl


Requirements

 

Before you can get started with this, make sure you have WSL ready on your Windows machine, check this official guide on how to do that. Also, Docker Desktop needs to be installed and running. I also recommend having Windows Terminal installed. Finally, in order to check out the repo which contains the Dockerfile, you need git.

 

Getting it up and running


Once done with the requirements, we need to get the Docker image built, create a container and save its user space to a file. Check out the project-dev-palace-all repo, cd into the k8s-with-docker-full folder and build the Docker image, export the container userspace to tar and then import it in as a WSL VM:


git clone https://github.com/alexchiri/project-dev-palace-all.git
cd k8s-with-docker-full
docker build -t k8s-with-docker:full .
$containerId = docker create k8s-with-docker:full
docker container export -o k8s-with-docker-full.tar $containerId
wsl --import Minikube C:\Users\alex\WSLs .\k8s-with-docker-full.tar --version 2

A few notes on the snippet above:

  1. This was done for the Powershell terminal

  2. docker container export -o k8s-with-docker-full.tar $containerId - fails with the latest version of Docker Desktop for Windows because of a bug that generates an extra line in the docker create output. If this command fails with an error, print the $containerId and manually put the container id in the docker container export command

  3. the wsl --import command contains a path, that is where the WSL VM disk will be created, feel free to set your own there


With that being done, we now have a fully working VM which has docker and minikube installed, we only need to get it up and running.


Open Windows Terminal and start a new session with the Minikube profile (if you had Windows Terminal open when the VM was imported, restart it in order to see the VM in the list:



In the Minikube VM, switch to the ubuntu user, start Docker and create a Minikube cluster:

sudo service docker start
minikube start

After minikube has finished, you should be able to say k get nodes and get an output similar to this:



Refresh?


If you break for some reason the VM or you made some config that is hard to revert and you want to start over, it is as easy as deleting the VM and re-importing it:


wsl --unregister Minikube
wsl --import Minikube C:\Users\alex\WSLs .\k8s-with-docker-full.tar --version 2

If you want to make some changes to the environment, maybe install a different version of kubectl for example, you can change the Dockerfile, rebuild, create & export container space and remove & import the VM using the commands already shown above.


I am considering to put out here some more guides on what kind of nice experiments you could do with such a cluster, so stay tuned! 🎉




40 views0 comments
bottom of page