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
-
Install WSL2
- Open PowerShell as Administrator
- Run the following command:
Terminal window wsl --install -
Verify WSL2 installation
- If WSL2 is already installed, ensure your distro is using WSL2:
Terminal window wsl --set-version DISTRO-NAME 2 -
Restart your computer
- Reboot to complete the WSL2 installation
- Launch your WSL2 distribution after restart
Step 2: Install Docker Engine in WSL
-
Update package index
- Launch your WSL2 distribution (e.g., Ubuntu)
- Update the package repository:
Terminal window sudo apt update && sudo apt upgrade -y -
Install Docker
Terminal window sudo apt install -y docker.io -
Configure user permissions
- Add your user to the docker group to avoid using sudo:
Terminal window sudo usermod -aG docker $USERexec su -l $USER
Step 3: Enable systemd in WSL
-
Configure WSL to use systemd
- Edit or create the WSL configuration file:
Terminal window sudo nano /etc/wsl.conf -
Add systemd configuration
- Add the following content to the file:
[boot]systemd=true -
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
-
Enable Docker service
- Back in your WSL distribution, enable Docker to start automatically:
Terminal window sudo systemctl enable dockersudo systemctl start docker -
Verify Docker installation
- Test that Docker is working correctly:
Terminal window docker ps- You should see an empty table (no running containers)
-
Test Docker functionality
Terminal window # Run a simple test containerdocker run hello-world
Step 5: Install Portainer (Optional Docker UI)
-
Create persistent volume
- Create a Docker volume for Portainer data:
Terminal window docker volume create portainer_data -
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 -
Access Portainer
- Open your web browser and navigate to:
- http://localhost:9000
- Create an admin account on first access
Step 6: Install Kubernetes Tools
-
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 kubectlsudo mv kubectl /usr/local/bin/ -
Install Minikube
- Download and install Minikube for local Kubernetes clusters:
Terminal window curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64chmod +x minikube-linux-amd64sudo mv minikube-linux-amd64 /usr/local/bin/minikube -
Verify installation
- Check that both tools are installed correctly:
Terminal window kubectl version --clientminikube version
Step 7: Start Your Kubernetes Cluster
-
Configure Docker environment
- Set the Docker host for Minikube:
Terminal window export DOCKER_HOST=unix:///var/run/docker.sock -
Start Minikube cluster
- Initialize your local Kubernetes cluster:
Terminal window minikube start \--driver=docker \--cpus=2 \--memory=4096 -
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
-
Check Minikube status
Terminal window minikube status- All components should show “Running”
-
Configure kubectl context
Terminal window kubectl config use-context minikube -
Verify cluster connectivity
Terminal window kubectl get pods -A- You should see system pods running in kube-system namespace
-
Test with a sample deployment
Terminal window # Create a test deploymentkubectl create deployment hello-minikube --image=kicbase/echo-server:1.0# Expose it as a servicekubectl expose deployment hello-minikube --type=NodePort --port=8080# Check the servicekubectl get services hello-minikube
Step 9: Install K9s for Cluster Management
-
Install K9s on Windows
- Open PowerShell and install K9s using winget:
Terminal window winget install k9s -
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
- Variable name:
- Replace
YOUR-USERNAME
with your actual WSL username
-
Alternative: Copy kubeconfig to Windows
Terminal window # In WSL, copy kubeconfig to Windows user directorycp ~/.kube/config /mnt/c/Users/WINDOWS-USERNAME/.kube/config -
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
# View cluster informationkubectl cluster-info
# Get all resources in a namespacekubectl get all -n kube-system
# Describe a resourcekubectl describe pod POD-NAME
# View logskubectl logs POD-NAME
# Execute commands in a podkubectl exec -it POD-NAME -- /bin/bash
# Port forward to access services locallykubectl port-forward service/SERVICE-NAME 8080:80
Minikube Management
# Stop the clusterminikube stop
# Delete the clusterminikube delete
# SSH into the Minikube VMminikube ssh
# Open Minikube dashboardminikube dashboard
# List available addonsminikube addons list
# Enable an addon (e.g., ingress)minikube addons enable ingress
Troubleshooting
Check Docker status:
sudo systemctl status docker
Check WSL version:
wsl --list --verbose
Reset Minikube if needed:
minikube deleteminikube start --driver=docker