How to Install Kubernetes (K8s/K9s) on Ubuntu 24.04 LTS with WSL2

How to Install Kubernetes (K8s/K9s) on Ubuntu 24.04 LTS with WSL2

How to Install Kubernetes (K8s/K9s) on Ubuntu 24.04 LTS with WSL2

作者有一段時間沒更新了,
主要是最近在面試、持續部屬製程自動換線系統、AI 文字轉語音系統等大項目,
不小心將自己的Annual Salary談到接近增加50%, 好像有點東西?
順便幫部門逆向存在10幾年的CIM/SPC/ERP等系統,
解決掉原開發Team都不想解決的事(也許是沒效益?沒亮點?部分系統是外包!),
繞過後台偷偷的重構部分重要功能給我們的Users使用,
讓值班從每天加班到晚上10~11點下班=>每天8~9點準時下班,
只是小小的改變流程, 沒想到有點出力過頭了, 有點傻眼-.-

這次的主題是K8s怎麼架設在WSL2上(WSL2我們就簡稱WSL)
WSL其實有點像殘次品的Linux OS,
有些功能與我們所認識的Linux操作有點不太一樣
因此在WSL上架設Kubernetes(K8s)會有點卡卡的


老樣子, 最近喜歡聽的歌


Deployed System Environment

OS: Windows 11 (26100.2605)
Internet: Online
WSL: 2.3.26.0
WSL OS: Ubuntu 24.04 LTS
If you wanna use Docker on Windows, you can check this tutorial
Building a Docker Environment Completely Offline on Windows 10
or others


Step 1:
Verify Windows Features in Control Panel

# Windows PowerShell
1.
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

2.
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

3.
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -All

4. 
Reboot PC

We Dont Need Hyper-V ~~

Error 1.
If your OS is WIN10 update to WIN11, mabey will get this problem:

You Can Manual Install WSL on Official Github
WSL.2.4.8

Releases · microsoft/WSL
Issues found on WSL. Contribute to microsoft/WSL development by creating an account on GitHub.

Step 2:
Install and Update WSL2 and Ubuntu
24.04 LTS

# Windows PowerShell or CMD
wsl --set-default-version 2
wsl --update
wsl --list --online
wsl --install Ubuntu-24.04
# Then WSL will ask your Linux Account, just edit it.

Step 3: Install Kubernetes(K8s)

I like use root path, so i will use "cd" to ./root and install K8s

  1. Update System
sudo apt update && sudo apt upgrade -y
  1. Install Docker
sudo apt install -y docker.io
sudo docker --version
  1. Install Kubernetes Components
    I will use v1.30 to build K8s,if u have other Option, just edit v1.30 to new version
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
  1. Update again and install kubelet、kubeadm、kubectl, the packages to prevent automatic upgrades
1.
sudo apt update
2.
sudo apt install -y kubelet kubeadm kubectl
3.
sudo apt-mark hold kubelet kubeadm kubectl
  1. Disable Swap(WSL)
# This option cannot be used in WSL.
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

In WSL, if you want "Disable Swap", your need edit .wslconfig file
.wslconfig is on your Windows user root
Just like:
"C:\Users\{Your User Name}\.wslconfig"

Then create ".wslconfig" and Edit it:

# .wslconfig
[wsl2]
swap=0

When you done this, shutdown WSL and restart it again.

1. leacve WSL
exit or CTRL+D

2. shutdown WSL
wsl --shutdown

3. start WSL
wsl

4. check swap
cat /proc/swaps
  1. Initialize the Master Node
sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Then you will get this...

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 172.24.249.132:6443 --token 82izxk.lxj15dbn9mef02xu \
        --discovery-token-ca-cert-hash sha256:6df3fed23b2529ccd17a4b25c71832b81af292ad1010c38cd79aabe7251b25be
  1. Configure kubectl for the Master Node
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes

 Install a Pod Network Add-on

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get nodes
  1. Kubernetes autocomplete on WSL (Option)
    In WSL, K8s autocomplete is different on official step
    If you like use "k get node", then use tab to autocomplete , use this:
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
echo 'alias k=kubectl' >> ~/.profile
echo 'complete -F __start_kubectl k' >> ~/.profile
echo 'export KUBECONFIG=~/.kube/config' >> ~/.profile
source ~/.profile

Check K8s install success:

PS: If you restart your pc, then get this message:
"The connection to the server 172.24.249.132:6443 was refused - did you specify the right host or port?"
just wait it XD


Step 3: Install K9s(Option)

# Official Webside: https://github.com/derailed/k9s/releases
1.
wget https://github.com/derailed/k9s/releases/download/v0.32.7/k9s_linux_amd64.deb
2.
sudo apt install ./k9s_linux_amd64.deb
3.
rm k9s_linux_amd64.deb
4.
k9s

Enjoy it!