Skip to content

Kubernetes Installation on WSL2 - Complete Setup Guide

Install Kubernetes on WSL

This guide walks you through installing a complete Kubernetes development environment inside WSL2, including Docker, Minikube, and K9s for cluster management. Perfect for Windows developers who want to run Kubernetes locally.

Prerequisites

  • Windows 10 version 2004 or higher, or Windows 11
  • Administrative access to install WSL2
  • At least 8GB RAM (4GB will be allocated to Minikube)
  • Internet connection for downloading components

Step 1: Enable WSL2

  1. Install WSL2

    • Open PowerShell as Administrator
    • Run the following command:
    Terminal window
    wsl --install
  2. Verify WSL2 installation

    • If WSL2 is already installed, ensure your distro is using WSL2:
    Terminal window
    wsl --set-version DISTRO-NAME 2
  3. Restart your computer

    • Reboot to complete the WSL2 installation
    • Launch your WSL2 distribution after restart

Step 2: Install Docker Engine in WSL

  1. Update package index

    • Launch your WSL2 distribution (e.g., Ubuntu)
    • Update the package repository:
    Terminal window
    sudo apt update && sudo apt upgrade -y
  2. Install Docker

    Terminal window
    sudo apt install -y docker.io
  3. Configure user permissions

    • Add your user to the docker group to avoid using sudo:
    Terminal window
    sudo usermod -aG docker $USER
    exec su -l $USER

Step 3: Enable systemd in WSL

  1. Configure WSL to use systemd

    • Edit or create the WSL configuration file:
    Terminal window
    sudo nano /etc/wsl.conf
  2. Add systemd configuration

    • Add the following content to the file:
    [boot]
    systemd=true
  3. Restart WSL

    • Exit your WSL session and shutdown WSL from PowerShell:
    Terminal window
    wsl --shutdown
    • Restart your WSL distribution

Step 4: Configure and Start Docker

  1. Enable Docker service

    • Back in your WSL distribution, enable Docker to start automatically:
    Terminal window
    sudo systemctl enable docker
    sudo systemctl start docker
  2. Verify Docker installation

    • Test that Docker is working correctly:
    Terminal window
    docker ps
    • You should see an empty table (no running containers)
  3. Test Docker functionality

    Terminal window
    # Run a simple test container
    docker run hello-world

Step 5: Install Portainer (Optional Docker UI)

  1. Create persistent volume

    • Create a Docker volume for Portainer data:
    Terminal window
    docker volume create portainer_data
  2. Deploy Portainer

    • Run Portainer container with the following configuration:
    Terminal window
    docker run -d \
    --name portainer \
    --restart=always \
    -p 9000:9000 \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v portainer_data:/data \
    portainer/portainer-ce
  3. Access Portainer

Step 6: Install Kubernetes Tools

  1. Install kubectl

    • Download and install the Kubernetes command-line tool:
    Terminal window
    curl -LO "https://dl.k8s.io/release/$(curl -Ls https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    chmod +x kubectl
    sudo mv kubectl /usr/local/bin/
  2. Install Minikube

    • Download and install Minikube for local Kubernetes clusters:
    Terminal window
    curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    chmod +x minikube-linux-amd64
    sudo mv minikube-linux-amd64 /usr/local/bin/minikube
  3. Verify installation

    • Check that both tools are installed correctly:
    Terminal window
    kubectl version --client
    minikube version

Step 7: Start Your Kubernetes Cluster

  1. Configure Docker environment

    • Set the Docker host for Minikube:
    Terminal window
    export DOCKER_HOST=unix:///var/run/docker.sock
  2. Start Minikube cluster

    • Initialize your local Kubernetes cluster:
    Terminal window
    minikube start \
    --driver=docker \
    --cpus=2 \
    --memory=4096
  3. Wait for cluster initialization

    • The first start may take several minutes as it downloads images
    • You’ll see progress updates in the terminal

Step 8: Verify Kubernetes Setup

  1. Check Minikube status

    Terminal window
    minikube status
    • All components should show “Running”
  2. Configure kubectl context

    Terminal window
    kubectl config use-context minikube
  3. Verify cluster connectivity

    Terminal window
    kubectl get pods -A
    • You should see system pods running in kube-system namespace
  4. Test with a sample deployment

    Terminal window
    # Create a test deployment
    kubectl create deployment hello-minikube --image=kicbase/echo-server:1.0
    # Expose it as a service
    kubectl expose deployment hello-minikube --type=NodePort --port=8080
    # Check the service
    kubectl get services hello-minikube

Step 9: Install K9s for Cluster Management

  1. Install K9s on Windows

    • Open PowerShell and install K9s using winget:
    Terminal window
    winget install k9s
  2. Configure K9s to use WSL kubeconfig

    • Set up environment variable to point to WSL kubeconfig:
    • Press Win + R, type sysdm.cpl, and press Enter
    • Click Environment Variables
    • Under User variables, click New:
      • Variable name: KUBECONFIG
      • Variable value: \\wsl$\Ubuntu\home\YOUR-USERNAME\.kube\config
    • Replace YOUR-USERNAME with your actual WSL username
  3. Alternative: Copy kubeconfig to Windows

    Terminal window
    # In WSL, copy kubeconfig to Windows user directory
    cp ~/.kube/config /mnt/c/Users/WINDOWS-USERNAME/.kube/config
  4. Test K9s

    • Restart your terminal and run:
    Terminal window
    k9s
    • You should see the K9s interface connected to your Minikube cluster

Useful Commands and Tips

Common kubectl Commands

Terminal window
# View cluster information
kubectl cluster-info
# Get all resources in a namespace
kubectl get all -n kube-system
# Describe a resource
kubectl describe pod POD-NAME
# View logs
kubectl logs POD-NAME
# Execute commands in a pod
kubectl exec -it POD-NAME -- /bin/bash
# Port forward to access services locally
kubectl port-forward service/SERVICE-NAME 8080:80

Minikube Management

Terminal window
# Stop the cluster
minikube stop
# Delete the cluster
minikube delete
# SSH into the Minikube VM
minikube ssh
# Open Minikube dashboard
minikube dashboard
# List available addons
minikube addons list
# Enable an addon (e.g., ingress)
minikube addons enable ingress

Troubleshooting

Check Docker status:

Terminal window
sudo systemctl status docker

Check WSL version:

Terminal window
wsl --list --verbose

Reset Minikube if needed:

Terminal window
minikube delete
minikube start --driver=docker