Pengen Ngulik Infra? Setup KVM di WSL2 aja! Modal Laptop Biasa Cukup

Blog Series

Kubernetes Lab on KVM WSL Journey

Part 1 of 5

18 min read
2299 wordsDevOps
kvmwsl2terraform

Tutorial lengkap bikin homelab KVM di laptop biasa pakai WSL2. Gak perlu sewa cloud mahal-mahal, cukup modal laptop kentang!

TL;DR: Pengen belajar infrastruktur tapi budget pas-pasan? Tenang, kita bakal transform laptop biasa jadi homelab KVM yang powerful pakai WSL2. Siap-siap jadi infrastructure engineer tanpa keluar duit banyak! πŸš€

Kenapa Harus Repot-repot? πŸ€”

Sebelum kita dive in, mungkin ada yang bertanya: β€œNgapain sih ribet-ribet setup KVM di laptop kalau bisa sewa VPS?”

Simple answer: Skills > Money πŸ’ͺ

  • VPS costs money yang mungkin belum ada sebagai fresh graduate
  • Learning by doing jauh lebih powerful daripada cuma baca-baca teori
  • Portfolio builder - recruiter bakal impressed sama yang punya pengalaman hands-on
  • Flexibility - bisa eksperimen sebanyak yang diinginkan tanpa takut tagihan membengkak

πŸ”§ Pre-requisites: Yang Dibutuhin

Before we start, pastikan sudah punya:

βœ… Windows 10/11 dengan WSL2 enabled
βœ… Minimal 8GB RAM (16GB recommended kalau mau nyaman)
βœ… CPU yang support virtualization (Intel VT-x atau AMD-V)
βœ… Space kosong minimal 50GB buat VM-VM nanti
βœ… Mental yang kuat buat debugging (this is important! πŸ˜…)


πŸš€ Step 1: Setup Foundation - Enable Nested Virtualization

First things first, kita perlu mastiin nested virtualization aktif. Ini basically virtualization di dalam virtualization - mind blown right? 🀯

Cek Status Current

cat /sys/module/kvm_intel/parameters/nested

Expected output: Y (kalau Intel) atau cek /sys/module/kvm_amd/parameters/nested kalau AMD.

Kalau Belum Aktif…

Buka PowerShell as Administrator di Windows, terus jalanin:

# Cek nama WSL distro
wsl --list --verbose
 
# Replace 'Ubuntu' dengan nama distro
wsl --shutdown Ubuntu
Get-VMProcessor -VMName "Ubuntu" | Set-VMProcessor -ExposeVirtualizationExtensions $true
wsl -d Ubuntu

Pro tip: Kalau command di atas error, coba restart Windows dulu. Sometimes Windows just needs a good ol’ reboot πŸ”„


πŸ› οΈ Step 2: Install KVM Stack - The Real Deal

Sekarang saatnya install semua tools yang dibutuhin. Copy-paste is your friend here:

# Update system dulu biar gak ada drama
sudo apt update && sudo apt upgrade -y
 
# Install KVM dan kawan-kawannya
sudo apt install -y \
  qemu-system \
  qemu-utils \
  libvirt-clients \
  libvirt-daemon-system \
  bridge-utils \
  virt-manager \
  cpu-checker \
  libguestfs-tools \
  libosinfo-bin
 
# Bonus tools yang bakal berguna nanti
sudo apt install -y \
  python3 \
  python3-pip \
  genisoimage \
  wget \
  curl \
  unzip

Add User ke Libvirt Group

sudo usermod -aG libvirt $USER
sudo usermod -aG kvm $USER
 
# Logout terus login lagi biar changes take effect
exit
# Buka WSL lagi

⚑ Step 3: Fire Up the Services

# Enable dan start libvirt services
sudo systemctl enable --now libvirtd
sudo systemctl enable --now virtlogd
 
# Cek status - harus running semua
sudo systemctl status libvirtd
sudo systemctl status virtlogd

Validation Time! 🎯

# Command ini bakal ngecek semua requirements
sudo virt-host-validate
 
# Kalau ada yang FAIL, jangan panik dulu
# Beberapa warnings itu normal di environment WSL2

What to expect: Beberapa checks mungkin WARN atau FAIL, especially yang berkaitan dengan hardware security. That’s totally fine untuk learning environment!


πŸ—οΈ Step 4: Setup Terraform + Libvirt Provider

Sekarang kita bakal install Terraform buat Infrastructure as Code experience yang lebih pro:

Install Terraform

# Add HashiCorp GPG key
wget -O- https://apt.releases.hashicorp.com/gpg | \
  sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
 
# Add repository
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
 
# Install Terraform
sudo apt update && sudo apt install terraform -y
 
# Verify installation
terraform --version

Setup Libvirt Provider

# Create directory structure
mkdir -p ~/.local/share/terraform/plugins/registry.terraform.io/dmacvicar/libvirt/0.7.1/linux_amd64
 
# Download libvirt provider (adjust version as needed)
cd /tmp
wget https://github.com/dmacvicar/terraform-provider-libvirt/releases/download/v0.8.3/terraform-provider-libvirt_0.8.3_linux_amd64.zip
 
# Extract dan install
unzip 
mv terraform-provider-libvirt_v0.8.3 ~/.local/share/terraform/plugins/registry.terraform.io/dmacvicar/libvirt/0.8.3/linux_amd64/terraform-provider-libvirt
chmod +x ~/.local/share/terraform/plugins/registry.terraform.io/dmacvicar/libvirt/0.8.3/linux_amd64/terraform-provider-libvirt

πŸ’Ύ Step 5: Storage Management yang Rapi

Kita perlu setup storage pools buat organize ISO files dan VM disks. Think of it as creating folders, but fancier:

# Create directories
sudo mkdir -p /data/isos
sudo mkdir -p /data/vms
sudo chown -R $USER:$USER /data/
 
# Define storage pools
virsh pool-define-as vms dir - - - - "/mnt/d/kvm/vms" # Adjust path as needed
virsh pool-define-as isos dir - - - - "/mnt/d/kvm/isos" # Adjust path as needed
 
# Set autostart (biar otomatis start pas boot)
virsh pool-autostart vms
virsh pool-autostart isos
 
# Start the pools
virsh pool-start vms
virsh pool-start isos
 
# Verify
virsh pool-list --all

Expected output: Both pools should show as active dan autostart yes.


🌐 Step 6: Virtual Networking Setup

Time to create isolated networks buat VM-VM. Ini penting banget buat simulate real-world scenarios:

Create Network Definition

# Create network config file
cat > /tmp/net-lab.xml << 'EOF'
<network>
  <name>net-lab</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr-lab' stp='on' delay='0'/>
  <domain name='lab.local'/>
  <ip address='192.168.100.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.100.10' end='192.168.100.100'/>
    </dhcp>
  </ip>
</network>
EOF
 
# Define and start the network
virsh net-define /tmp/net-lab.xml
virsh net-autostart net-lab
virsh net-start net-lab
 
# Verify
virsh net-list --all

🎯 Step 7: Quick Test - Download dan Test ISO

Mari kita test setup dengan download ISO dan create VM pertama:

# Download Ubuntu Server ISO (lightweight option)
cd /mnt/d/kvm/isos
wget https://releases.ubuntu.com/noble/ubuntu-24.04.2-live-server-amd64.iso
 
# Create your first VM via command line
virt-install \
  --name test-vm \
  --ram 1024 \
  --vcpus 1 \
  --disk path=/mnt/d/kvm/vms/test-vm.qcow2,size=10 \
  --os-variant ubuntu24.04 \
  --network network=net-lab \
  --graphics none \
  --console pty,target_type=serial \
  --location /mnt/d/kvm/isos/ubuntu-24.04.2-live-server-amd64.iso,\
initrd=casper/initrd,\
kernel=casper/vmlinuz \
  --extra-args 'console=ttyS0,115200n8 serial --- quiet'

🚨 Common Issues & Troubleshooting

”Permission denied” errors

# Fix libvirt permissions
sudo chmod 666 /var/run/libvirt/libvirt-sock
# Or add to group (better solution)
sudo usermod -aG libvirt $USER

”Nested virtualization not supported”

  • Restart Windows completely
  • Check BIOS settings - enable Intel VT-x atau AMD-V
  • Verify WSL2 VM settings dalam Hyper-V

VMs super slow

  • Allocate more RAM ke WSL2 di .wslconfig
  • Enable CPU extensions di VM creation

πŸŽ‰ What’s Next? Level Up Your Game!

Congrats! Sekarang sudah punya homelab KVM yang functional. Selanjutnya bisa:

πŸ”₯ Create multi-VM clusters buat simulate production environments
πŸ”₯ Experiment dengan Kubernetes tanpa takut break production
πŸ”₯ Practice Infrastructure as Code dengan Terraform
πŸ”₯ Build CI/CD pipelines yang deploy ke VM clusters
πŸ”₯ Learn container orchestration dengan setup sendiri

Pro Tips buat Next Steps:

  1. Automate everything - Write Terraform configs buat VM provisioning
  2. Monitor your lab - Install Prometheus + Grafana
  3. Practice disaster recovery - Backup dan restore VMs
  4. Document everything - Future self will thank present self

πŸ’­ Closing Thoughts

Setup KVM di WSL2 might seem overwhelming di awal, tapi trust me - ini investment terbaik yang bisa dilakukan buat career development. Dengan homelab sendiri, bisa:

  • Experiment freely tanpa takut biaya cloud
  • Build real-world experience yang recruiters appreciate
  • Stay updated dengan latest infrastructure trends
  • Debug skills yang invaluable di production environments

Remember: Every senior engineer started somewhere. Yang penting mulai, learn from mistakes, dan keep iterating. Good luck, dan happy learning! πŸš€


Punya pertanyaan atau stuck di salah satu step? Bisa reach out di email phamminhphueur@gmail.com! Let’s build this infrastructure community together πŸ’ͺ

Next post: β€œBikin Kubernetes Cluster di KVM - From Zero to Hero” - Stay tuned! πŸ‘€

Written by Minh Phu Pham

Published on June 4, 2025

Share this article:

Β© 2025 | Minh Phu Pham. All rights reserved.