Getting started with developing Kubernetes on Windows

Kubernetes, often referred to as K8s, is an open-source container orchestration platform that helps users deploy, scale and manage containerised workloads. It has become ubiquitous with cloud-native technologies and after over a decade of development has one of the most thriving open source communities. Contributions to the project are deployed on thousands of systems around the world and the first step to contributing is setting up the local development environment.

Now, Kubernetes is a pretty large project and setting up the development environment might not be trivial. Still, the Kubernetes community has provided a helpful resource which walks through the steps to get a development environment setup. But like many other projects in the cloud native space, it's very much Linux centric. Windows users like me might feel a bit lost.

But, it's not too complicated if you have WSL2. Developing on Windows directly is not worth it as the projects rely on a lot of tooling that Windows doesn't have. Yes, one could use MinGW or an alternative but it's not worth the hassle that might crop up later. Hence developing on WSL2 is the best option. If you are already using WSL2, you might already have a distribution installed. Although, one can start setting up the development environment in the same distribution. My advice is to create a separate distribution for K8s development. This will ensure that the development environment is isolated and is not impacted by other uses of WSL2.

Setup the WSL2 Distro

The first step is to create the WSL2 distribution that will contain the development environment. This step can be performed in multiple ways depending on your existing setup.

  1. If you have never used WSL2 before, see the documentation for instructions on how to get started.
  2. If you have used WSL2 before, you probably have an existing distribution already installed. In that case, install a new distribution from the store.
    1. If you have already installed the distribution you want earlier, you will not be able to create another instance. In that case, you will need to download the distribution and install it manually.
    2. If you haven't already installed the distribution, my suggestion is to install the distribution and export it. Then re-import the distribution with a new name and uninstall the distribution that was installed from the store. This way, this distribution can be re-installed in the future.
  3. Once the distribution is up and running, ensure you are logged in as a non-root user.

Going forward, we are using Ubuntu 24.04.1.

Setup Docker Desktop

K8s also need Docker to be present inside the WSL2 instance. The easiest way to do this is to install Docker Desktop in Windows and enable integration with the WSL distribution. See the documentation for instructions.

Setup the WSL2 Environment

Now it's time to login to the WSL2 distribution and set it up.

  1. We will start by installing the GNU tools.

    sudo apt update
    sudo apt install build-essential
    
  2. Next, we will install jq.

    sudo apt install jq
    
  3. Since Kubernetes is written in Go, we need to install it. It's important that the correct version of Go is installed depending on the version of K8s that going to be built. See the notes in the community resource on how to find that out. Install the required version of Go by following the instructions.

  4. K8s uses pyyaml for some verification tests. It needs to be installed using Python but before that, the installed version of Python needs to be configured.

    sudo apt install python3-pip
    sudo apt install python3-venv
    
    python3 -m venv .k8s-venv
    source .k8s-venv/bin/activate
    

    This installs pip and venv for the Python that comes pre-installed. It also creates and activates a virtual environment. This is needed as the Python that is pre-installed recommends to install all packages in a virtual environment instead of globally. Once that's done, we can go ahead and install pyyaml.

    pip install pyyaml
    
  5. Finally time to clone the Kubernetes git repository. Make sure you fork the repository first before cloning it.

    git clone git@github.com:<username>/kubernetes.git
    cd kubernetes
    

    Make sure to replace <username> with your the GitHub username where the fork is located.

  6. Finally, we install etcd.

    ./hack/install-etcd.sh
    

    This script will instruct you to make a change to your PATH. To make
    this permanent, add this to your .bashrc or login script:

    export PATH="$PATH:/home/<username>/kubernetes/third_party/etcd"
    

    Again replace <username> with your current WSL username.

And that's it! You should have your development environment all setup. Try running the command make from the terminal and it should build the project.