DEV Community

Cover image for Building a Lightweight Kubernetes Environment on K3s and the kubectl command on WSL2 Windows 11
Tak
Tak

Posted on

Building a Lightweight Kubernetes Environment on K3s and the kubectl command on WSL2 Windows 11

Table of Contents

  1. Introduction
  2. Step 1: Installation of K3s on Google Cloud
  3. Step 2: Installation of kubectl command on WSL2 Windows 11
  4. Step 3: kubectl commands gets the response from K3s
  5. Conclusion

A. Book: Kubernetes and Helm: A Primer

1. Introduction

When you start learning Kubernetes and Helm in your local environment, you often struggle to install the software stack. This brief tutorial explains the step-by-step instructions to build a lightweight Kubernetes environment with K3s on a Google VM and install the kubectl command on WSL2 on Windows 11.

Before you proceed to the following steps, you have already installed Ubuntu on a Google Cloud VM (e.g., 2 vCPU, 6GB memory, 50GB SSD) and Ubuntu on WSL2 on Windows 11. They are connected via the network. The detailed step-by-step guide for preparing the computing environments for the client and server and installing microservice applications is referred to in the book section.

2. Step 1: Installation of K3s on Google Cloud

Before you install K3s on Ubuntu VM on Google Cloud, you check the version and release number of the operating system with the "cat /etc/os-release" command in Listing 1.

Listing 1: Operating System version

rookie@rookie-gc-ubuntu-1:~$ cat /etc/os-release
PRETTY_NAME="Ubuntu 26.04 LTS"
NAME="Ubuntu"
VERSION_ID="26.04"
VERSION="26.04 LTS (Resolute Raccoon)"
VERSION_CODENAME=resolute
...
...
...
rookie@rookie-gc-ubuntu-1:~$
Enter fullscreen mode Exit fullscreen mode

You can also check the available memory and swap space using the free command in Listing 2. As shown in the output, the system has a total memory of approximately 6 GB (6,059,484 KiB), with around 5.2 GB (5,217,884 KiB) available. The "Swap: 0 0 0" line confirms that no swap space is currently configured on the system.

Listing 2: Check memory size and the swap file

rookie@rookie-gc-ubuntu-1:~$ free
               total        used        free      shared  buff/cache   available
Mem:         6059484      495828     5217884         984      587048     5563656
Swap:              0           0           0
Enter fullscreen mode Exit fullscreen mode

Before installing K3s on Ubuntu, run the "which kubectl" command to check whether the kubectl binary file is already installed. As shown in Listing 3, the which command returns no output, confirming that the kubectl binary file is not yet present on the system.

Listing 3: which kubectl (1)

rookie@rookie-gc-ubuntu-1:~$ which kubectl
rookie@rookie-gc-ubuntu-1:~$
Enter fullscreen mode Exit fullscreen mode

Installing K3s requires only a few simple steps. For a first-time installation, you can follow the official Quick Start Guide.

Run the installation script as shown in Listing 4:

Be sure to include the "--tls-san s.s.s.s" option, replacing s.s.s.s (IPv4 address) with the external IPv4 address of your VM on Google Cloud. This parameter adds the external IPv4 address to the TLS certificate's Subject Alternative Names (SAN). This step is essential because VM instances on Google Cloud assign internal IPv4 addresses to their local network interfaces, which differ from their public-facing external IPv4 addresses.

Once command execution is complete, the K3s server will be fully installed and operational.

Listing 4: K3s installation

rookie@rookie-gc-ubuntu-1:~$ curl -sfL https://get.k3s.io | sh -s - server --tls-san s.s.s.s
[INFO]  Finding release for channel stable
[INFO]  Using v1.35.5+k3s1 as release
...
...
...
[INFO]  systemd: Starting k3s
rookie@rookie-gc-ubuntu-1:~$
Enter fullscreen mode Exit fullscreen mode

After installing K3s, run the "which kubectl" command again to verify the installation. As shown in Listing 5, the command returns "/usr/local/bin/kubectl" message, confirming that the kubectl binary file was automatically installed alongside K3s.

Listing 5: which kubectl (2)

rookie@rookie-gc-ubuntu-1:~$ which kubectl
/usr/local/bin/kubectl
rookie@rookie-gc-ubuntu-1:~$
Enter fullscreen mode Exit fullscreen mode

Next, run the "sudo kubectl get node" command to check the status of the installed K3s node in Listing 6. The output displays the "Ready" under the "STATUS" column, confirming that the node is active and prepared to handle workloads. K3s has been successfully installed on your Ubuntu VM on Google Cloud in Listing 6. Your installed K3s version may differ from "v1.35.5+k3s1", as the latest version of K3s will be installed on your Ubuntu VM in Google Cloud.

Listing 6: kubectl get node

rookie@rookie-gc-ubuntu-1:~$ sudo kubectl get nodes
NAME                 STATUS   ROLES           AGE     VERSION
rookie-gc-ubuntu-1   Ready    control-plane   1m34s   v1.35.5+k3s1
rookie@rookie-gc-ubuntu-1:~$
Enter fullscreen mode Exit fullscreen mode

Copy the k3s.yaml configuration file to your home directory and update its file ownership, as shown in Listing 7.

Listing 7: Copy k3s.yaml and change owner of the file

rookie@rookie-gc-ubuntu-1:~$ sudo cp -p /etc/rancher/k3s/k3s.yaml ~/
rookie@rookie-gc-ubuntu-1:~$ sudo chown rookie:rookie ~/k3s.yaml
rookie@rookie-gc-ubuntu-1:~$ ls -l
total 4
-rw------- 1 rookie rookie 2945 Jun  3 14:00 k3s.yaml
rookie@rookie-gc-ubuntu-1:~$
Enter fullscreen mode Exit fullscreen mode

3. Step 2: Installation of kubectl command on WSL2 Windows 11

At first, run the mkdir and chmod commands shown in Listing 8 to create and secure the "~/.kube" directory for your Kubernetes configuration file.

Listing 8: Make the directory and change the mode

rookie@Tak-Win11-Pro-1:~$ mkdir ~/.kube
rookie@Tak-Win11-Pro-1:~$ ls -ld ~/.kube
drwxr-xr-x 2 rookie rookie 4096 Jun  5 00:48 /home/rookie/.kube
rookie@Tak-Win11-Pro-1:~$ chmod 700 ~/.kube
rookie@Tak-Win11-Pro-1:~$ ls -ld ~/.kube
drwx------ 2 rookie rookie 4096 Jun  5 00:48 /home/rookie/.kube
rookie@Tak-Win11-Pro-1:~$
Enter fullscreen mode Exit fullscreen mode

Next, run the following scp command, which is also shown in Listing 9, to copy the k3s.yaml file from the Ubuntu VM on Google Cloud to your Ubuntu WSL2 on Windows 11.

  • scp -p s.s.s.s:~/k3s.yaml ~/.kube/config

The k3s.yaml file of K3s is already copied in the home directory since you have completed in the previous chapter.

In the scp command, replace s.s.s.s with the external IPv4 address of your Ubuntu VM on Google Cloud. Notice that the file is renamed from k3s.yaml to config during the transfer, as the kubectl CLI automatically searches for its default configuration at "~/.kube/config".

After copying the file, remove the k3s.yaml file on your Ubuntu VM on Google Cloud using the ssh command in Listing 9.

Listing 9: scp command to copy k3s.yaml to config

rookie@Tak-Win11-Pro-1:~$ scp -p s.s.s.s:~/k3s.yaml ~/.kube/config
k3s.yaml                                                                              100% 2945    11.5KB/s   00:00
rookie@Tak-Win11-Pro-1:~$ ls -l ~/.kube/config
-rw------- 1 rookie rookie 2945 Jun  5 00:34 /home/rookie/.kube/config
rookie@Tak-Win11-Pro-1:~$ ssh s.s.s.s ls
k3s.yaml
rookie@Tak-Win11-Pro-1:~$ ssh s.s.s.s rm k3s.yaml
rookie@Tak-Win11-Pro-1:~$ ssh s.s.s.s ls
rookie@Tak-Win11-Pro-1:~$
Enter fullscreen mode Exit fullscreen mode

Next, verify that your K3s server is running properly by sending a request to TCP port 6443 using the curl command in Listing 10.

Note that TCP port 6443 is the standard port for the K3s API server. When the command returns the response ("message": "Unauthorized") in Listing 10, this is expected and confirms that the curl command for the connection is working correctly and your K3s server is accessible. This step in Listing 10 simply verifies the network connectivity to the K3s server on your Ubuntu VM on Google Cloud.

Listing 10: Check k3s server with curl command

rookie@Tak-Win11-Pro-1:~$ curl -k https://s.s.s.s:6443/
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
}rookie@Tak-Win11-Pro-1:~$
rookie@Tak-Win11-Pro-1:~$
Enter fullscreen mode Exit fullscreen mode

Finally, update the K3s server IPv4 address in your "~/.kube/config" configuration file in Listing 11. By default, the configuration file currently uses 127.0.0.1 (which is the localhost address). Change 127.0.0.1 to s.s.s.s using the "sed -i s/127.0.0.1/s.s.s.s/ ~/.kube/config" command so that the kubectl CLI connects to the K3s cluster in your Ubuntu VM on Google Cloud instead of the localhost address in Listing 11.

To confirm that the external IPv4 address was updated successfully, run the "egrep s.s.s.s ~/.kube/config" command as shown in Listing 11. When this change was applied correctly, you will see output like "server: https://s.s.s.s:6443" message.

Listing 11: Update IPv4 address in the configuration file

rookie@Tak-Win11-Pro-1:~$ egrep 127.0.0.1 ~/.kube/config
    server: https://127.0.0.1:6443
rookie@Tak-Win11-Pro-1:~$ sed -i s/127.0.0.1/s.s.s.s/ ~/.kube/config
rookie@Tak-Win11-Pro-1:~$ egrep s.s.s.s ~/.kube/config
    server: https://s.s.s.s:6443
rookie@Tak-Win11-Pro-1:~$
Enter fullscreen mode Exit fullscreen mode

4. Step 3: kubectl commands gets the response from K3s

You will install the kubectl command-line tool used to manage the K3s cluster from Ubuntu WSL2 in Windows 11. Follow the official Kubernetes documentation for detailed installation instructions.

First, download the kubectl binary file and its corresponding checksum file (kubectl.sha256), using the curl commands as shown in Listing 12. Since your Ubuntu WSL2 environment uses the x86-64 architecture, make sure to download the files corresponding to "x86-64 (amd64)." After downloading, verify the integrity of the binary file using the following echo and sha256sum commands:

  • echo "$(cat kubectl.sha256) kubectl" | sha256sum --check

When this verification succeeds, you will see the "kubectl: OK" message in Listing 12. This confirms that the downloaded kubectl binary file is intact and safe to use.

Listing 12: Download kubectl binary file and its checksum file

rookie@Tak-Win11-Pro-1:~$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 56.7M  100 56.7M    0     0  10.9M      0  0:00:05  0:00:05 --:--:-- 11.0M
rookie@Tak-Win11-Pro-1:~$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    64  100    64    0     0    964      0 --:--:-- --:--:-- --:--:--   984
rookie@Tak-Win11-Pro-1:~$ echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check
kubectl: OK
rookie@Tak-Win11-Pro-1:~$
Enter fullscreen mode Exit fullscreen mode

Next, install the kubectl binary file to your Ubuntu WSL2 environment
using the install command in Listing 13. Note that because this command uses the sudo command.

  • sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

This command places the kubectl binary file into the "/usr/local/bin/" directory while setting the owner to root, the group to root, and the file permissions (mode) to 0755 so that it can be executed by any user.

To confirm that the permissions and ownership are configured correctly, check the file details with the "ls -l /usr/local/bin/kubectl" command
in Listing 13. You see the permission of the kubectl file with its output.

Listing 13: Install kubectl command

rookie@Tak-Win11-Pro-1:~$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
[sudo] password for rookie:
rookie@Tak-Win11-Pro-1:~$ ls -l /usr/local/bin/kubectl
-rwxr-xr-x 1 root root 59502754 Jun  5 00:59 /usr/local/bin/kubectl
rookie@Tak-Win11-Pro-1:~$
Enter fullscreen mode Exit fullscreen mode

Finally, run the "kubectl get nodes" command to verify that your K3s cluster is operational in Listing 14. When everything is configured correctly, you will see a list of Kubernetes nodes in the terminal in Listing 14.

Note that in this output in Listing 14, the K3s version is also displayed as "v1.35.5+k3s1". Because K3s is updated frequently, the version and the release number in your output may differ slightly. It will be the latest version of K3s.

Listing 14: kubectl get nodes command on WSL2

rookie@Tak-Win11-Pro-1:~$ kubectl get nodes
NAME                 STATUS   ROLES           AGE   VERSION
rookie-gc-ubuntu-1   Ready    control-plane   25m   v1.35.5+k3s1
rookie@Tak-Win11-Pro-1:~$
Enter fullscreen mode Exit fullscreen mode

5. Conclusion

Now you have a lightweight Kubernetes environment up and running! If you want to review these setup steps in detail or dive deeper into Pods, Deployments, Services, Persistent Volume Claims (PVCs), and package management with Helm, check out the book below:

Book:Kubernetes and Helm: A Prime

This tutorial is an excerpt from the 563-page hands-on book, 📖"Kubernetes and Helm: A Primer", available on Amazon. The book offers comprehensive, step-by-step instructions for building a K3s cluster on Google Cloud, configuring the kubectl command on WSL2 (Windows 11), and orchestrating microservices for applications using both kubectl and helm commands.

Whether you are a software developer, SRE beginner, or college/university student, this hands-on book walks you through setting up your own computing environment, deploying real-world applications (including NGINX, WordPress, Drupal, Redis, and Nextcloud), and mastering Helm using hundreds of working terminal outputs and production-ready YAML manifests.

Top comments (0)