How to install Kubeadm on Ubuntu with WSL2

When working with container orchestration, Kubernetes stands out as a powerful and essential tool. For Windows users who want to explore Kubernetes without sacrificing their routine working environment, the combination of Kubeadm, Ubuntu and WSL2 offers a great and practical solution! In this post, I’ll go through the entire process of installing and configuring Kubeadm on Ubuntu, running inside WSL2 (Windows Subsystem for Linux 2).

Install Kubeadm

To learn how to install Kubeadm on Ubuntu and configure it on an Ubuntu running WSL 2, you’ll need to follow a few specific steps, as shown below:

  1. Update the Ubuntu system:
sudo apt-get update && sudo apt-get upgrade -y
install kubeadm
kubernetes kubeadm install
  1. Install the necessary dependencies:
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
kubernetes kubeadm install
  1. Add the Kubernetes GPG key and the repository:
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 /etc/apt/sources.list.d/kubernetes.list

Result:

how to install kubeadm on ubuntu
  1. Re-update and install kubeadm, kubelet and kubectl:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
  1. Disable swap (Kubernetes requires swap to be disabled):
sudo swapoff -a
  1. Configure Docker (if not already installed):
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
  1. Configure the Docker cgroup driver for systemd:Create or edit the file /etc/docker/daemon.json:
sudo nano /etc/docker/daemon.json

Add the following content:

{
 "exec-opts": ["native.cgroupdriver=systemd"]
}

Restart Docker:

sudo systemctl restart docker
  1. Start the Kubernetes cluster:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
  1. Configure kubectl for the current user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  1. In order for the pods to communicate in the cluster, you’ll need to set up a network. One of the most widely used network providers is Calico, which is very simple and practical for beginners. Run the command below to install Calico:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Important comments:

  • WSL 2 does not natively support systemd, which is used by kubeadm. You need to configure SystemD support or consider using a full virtual machine with emulated Linux.
  • You may encounter network problems due to the limitations of WSL 2. You may need to set up port forwarding or use alternative network solutions.
  • For a development environment, you could consider alternatives such as Minikube or Kind, which may be easier to configure in WSL 2, rather than Kubeadm.

If you encounter specific problems during the installation, please let me know so I can help solve them!

Once you have followed all the steps above, you should see a screen similar to this when you run the kubectl get pods -A command in your terminal:

installing kubernetes with kubeadm
kubeadm in wsl2 – installing kubernetes with kubeadm

Installing Kubeadm on Ubuntu using WSL2 offers a powerful and flexible way of working with Kubernetes on Windows. This environment combines the best of both worlds: the familiarity of Windows with the power of Linux and Kubernetes.

Tips and Good Practices

  • Make sure WSL2 and Ubuntu are up to date before installing Kubeadm.
  • Use the kubeadm init command to configure Kubeadm.
  • Use the kubectl get nodes command to check that the cluster is working correctly.

Common mistakes

  • Error installing WSL2: Check that Windows Subsystem for Linux 2 is enabled in the Windows Task Manager.
  • Error installing Ubuntu: Check that Ubuntu is installed correctly on WSL2.
  • Error configuring Kubeadm: Check that Kubeadm is installed correctly and that the kubeadm init command is being executed correctly.

Known problems

The main difference between WSL1 and WSL2 is the hyper-v VM-based version of Microsoft Linux, which sounds like exciting news, at least it’s running the full Linux kernel, but there are some problems if used for DevOps/SRE activities.

Here are some known trouble spots:

  • the default systemd is not enabled, which is necessary to install packages
  • default network is NAT, always assigns dynamic IP after reboot, will cause problems for applications such as the k8s API server
  • Kubelet restarting frequently: This could be due to resource limitations. Try increasing memory allocation in WSL2.

In addition to the points mentioned above, in some cases there may be errors related to the repository where Kubeadm is available, as in this example image:

kubeadm install

For details on cases where your lab requires a Fixed IP configuration on WSL2 or you want to install it in a more automated way, I recommend these 2 materials:

Both deal with issues surrounding the use of Kubeadm in WSL2, but they are procedures recommended for more advanced users, as they can cause problems that are difficult to reverse in your environment.

Network using Cilium

In addition to Calico, another popular option for pod networks is Cilium, which is a good option to use after install Kubeadm.

Cilium provides service-level security and enhanced observability for Kubernetes clusters and is my personal choice in my labs and production environments.

Cilium provides advanced features such as identity-based security policies, traffic monitoring and integration with Prometheus for performance metrics.

To install Cilium, run the following commands:

helm install cilium cilium/cilium -f values.yaml --version 1.14.2 \
    --namespace kube-system \
    --set kubeProxyReplacement=strict \
    --set k8sServiceHost=192.168.0.107 \
    --set k8sServicePort=6443

After installation, check that Cilium is running with the command:

kubectl get pods --namespace=kube-system -l k8s-app=cilium

Expected return:

kubernetes kubeadm install cilium

In this article, we have shown you how to make a Kubernetes Kubeadm install on Ubuntu with WSL2. With this setup, you can easily and quickly set up a production-ready Kubernetes cluster on your Windows machine.

Now that you have a Kubernetes cluster up and running, you can start deploying and managing containerized applications. To learn more about Kubernetes and its capabilities, check out our articles:

Compartilhe / Share
Fernando Müller Junior
Fernando Müller Junior

I am Fernando Müller, a Tech Lead SRE with 16 years of experience in IT, I currently work at Appmax, a fintech located in Brazil. Passionate about working with Cloud Native architectures and applications, Open Source tools and everything that exists in the SRE world, always looking to develop and learn constantly (Lifelong learning), working on innovative projects!

Articles: 39

Receba as notícias por email / Receive news by email

Insira seu endereço de e-mail abaixo e assine nossa newsletter / Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *