How to set up a K3s Kubernetes Cluster on Ubuntu 22.04
How to set up a K3s Kubernetes Cluster on Ubuntu 22.04

How to set up a K3s Kubernetes Cluster on Ubuntu 22.04

0 Shares
0
0
0
0

How to set up a K3s Kubernetes Cluster on Ubuntu 22.04

Introduction

Kubernetes is one of the industry's favorite tools for container orchestration. However, setting up a Kubernetes cluster from scratch can be a daunting task and requires a lot of configuration. Furthermore, there are several ways to get started with setting up a Kubernetes cluster, but many of them can be time-consuming unless you want to create a production cluster.

To simplify the setup of a Kubernetes cluster and enable its deployment in remote resource-constrained locations—thus making it a good candidate for edge computing—Rancher Labs developed K3s. K3s is a lightweight Kubernetes distribution that allows you to install a Kubernetes cluster using a small binary in minutes.

In this tutorial, you will learn how to install K3s on Ubuntu and the additional configuration options available in K3s.

Prerequisites

To complete this tutorial, you will need:

An Ubuntu 22.04 server with at least 1 GB of RAM has been set up by following the Ubuntu 22.04 initial server setup guide.

Step 1- Install K3s

In this step, you will install the latest version of K3s on your Ubuntu machine.

If you are using password-based login, log in to your server as your sudo user (in this tutorial, it will be sammy) using the following command:

$ ssh sammy@your_server_ip

سپس با استفاده از دستور زیر K3 را نصب کنید.

$ curl -sfL https://get.k3s.io | sh -

You will be prompted to enter the user password to run the script.

This command uses curl to download the script located at https://get.k3s.io and runs the script by piping it into –sh. After running the script, the K3s cluster installation begins with default configuration options that create a single-node Kubernetes cluster.

You will get output similar to this:

[secondary_label Output]

[INFO] Finding release for channel stable

[INFO] Using v1.27.7+k3s2 as release

[INFO] Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.27.7+k3s2/sha256sum-amd64.txt

[INFO] Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.27.7+k3s2/k3s

[INFO] Verifying binary download

[INFO] Installing k3s to /usr/local/bin/k3s

[INFO] Skipping installation of SELinux RPM

[INFO] Creating /usr/local/bin/kubectl symlink to k3s

[INFO] Creating /usr/local/bin/crictl symlink to k3s

[INFO] Creating /usr/local/bin/ctr symlink to k3s

[INFO] Creating killall script /usr/local/bin/k3s-killall.sh

[INFO] Creating uninstall script /usr/local/bin/k3s-uninstall.sh

[INFO] env: Creating environment file /etc/systemd/system/k3s.service.env

[INFO] systemd: Creating service file /etc/systemd/system/k3s.service

[INFO] systemd: Enabling k3s unit

Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.

[INFO] systemd: Starting k3s

...

The script output shows the steps taken by the installation script to install and start the cluster. Next, you will check the status of the K3s service using systemctl to check if it is running or not using the following command.

$ systemctl status k3s

This command shows the status as active (running):

[secondary_label Output]

● k3s.service - Lightweight Kubernetes

Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: enabled)

Active: active (running) since Mon 2023-11-27 16:52:01 UTC; 19s ago

Docs: https://k3s.io

Process: 8396 ExecStartPre=/bin/sh -xc ! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service (code=exi>

Process: 8398 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)

Process: 8399 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)

Main PID: 8400 (k3s-server)

Tasks: 20

Memory: 467.3M

CPU: 12.952s

CGroup: /system.slice/k3s.service

├─8400 "/usr/local/bin/k3s server"

└─8421 "containerd " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

...

At this point, you have installed K3s on Ubuntu to create a single-node Kubernetes cluster. Next, you will take a look at the default Kubernetes objects deployed on the cluster.

Step 2 – Review Kubernetes Default Objects

In this step, you will review the default Kubernetes objects that were deployed after installing K3s.

Run the following command to see all Kubernetes objects deployed in the cluster in the kube namespace. kubectl is automatically installed during the K3s installation and therefore does not require a separate installation.

$ sudo kubectl get all -n kube-system

You will get output similar to this:

[secondary_label Output]

NAME                                         READY   STATUS      RESTARTS   AGE

pod/local-path-provisioner-957fdf8bc-t8vpx   1/1     Running     0          4m34s

pod/coredns-77ccd57875-4hrd9                 1/1     Running     0          4m34s

pod/helm-install-traefik-crd-j2sqs           0/1     Completed   0          4m34s

pod/helm-install-traefik-mvxhw               0/1     Completed   1          4m34s

pod/metrics-server-648b5df564-gqxcz          1/1     Running     0          4m34s

pod/svclb-traefik-18597fcd-2cf68             2/2     Running     0          4m6s

pod/traefik-768bdcdcdd-srb8d                 1/1     Running     0          4m7s



NAME                     TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)                      AGE

service/kube-dns         ClusterIP      10.43.0.10      <none>           53/UDP,53/TCP,9153/TCP       4m44s

service/metrics-server   ClusterIP      10.43.69.115    <none>           443/TCP                      4m43s

service/traefik          LoadBalancer   10.43.149.125   159.65.159.115   80:32266/TCP,443:32628/TCP   4m7s



NAME                                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE

daemonset.apps/svclb-traefik-18597fcd   1         1         1       1            1           <none>          4m7s



NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE

deployment.apps/local-path-provisioner   1/1     1            1           4m44s

deployment.apps/coredns                  1/1     1            1           4m44s

deployment.apps/metrics-server           1/1     1            1           4m44s

deployment.apps/traefik                  1/1     1            1           4m7s



NAME                                               DESIRED   CURRENT   READY   AGE

replicaset.apps/local-path-provisioner-957fdf8bc   1         1         1       4m34s

replicaset.apps/coredns-77ccd57875                 1         1         1       4m34s

replicaset.apps/metrics-server-648b5df564          1         1         1       4m34s

replicaset.apps/traefik-768bdcdcdd                 1         1         1       4m7s



NAME                                 COMPLETIONS   DURATION   AGE

job.batch/helm-install-traefik-crd   1/1           28s        4m41s

job.batch/helm-install-traefik       1/1           31s        4m41s



...
 

The output above shows the various objects deployed in the Kubernetes cluster. For example, there are 4 deployments running, one each for coredns, local-path-provisioner, metrics-server, and traefik.

If you run the command without using sudo, you may encounter the following error.

Output
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode to modify kube config permissions

error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied

...

To avoid requiring sudo when running kubectl commands, change the permissions of the configuration file with chmod, as shown below.

$ sudo chmod 644 /etc/rancher/k3s/k3s.yaml

At this point, you have verified the status of Kubernetes objects deployed in the K3s cluster by default. Next, you will understand and modify the configuration options in K3s during installation.

Step 3 – Understand and modify configuration options in K3S

You installed K3s using the default settings, however, you can adjust the configuration to achieve custom cluster-specific behavior. In this step, you will learn how to use environment variables in K3 to set settings in the installation script.

For example, default settings come with the traefik ingress controller installation, in some cases, you may want to disable the ingress controller during installation.

The environment variable INSTALL_K3S_EXEC can be used to pass flags to the K3s service. The following command can be used to disable traefik during K3 installation.

$ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--disable=traefik" sh -

Check Kubernetes objects to verify. Controller resources will not exist this time.

$ sudo kubectl get all -n kube-system

In addition to modifying configuration options using environment variables, this can also be done in the K3s configuration file. The necessary options can be specified in the configuration file and then the K3s server restarted for the changes to take effect.

K3s uses the configuration file located at etc/rancher/k3s/config.yaml/.

Run the following command to create and write the configuration file.

$ sudo nano /etc/rancher/k3s/config.yaml

Write the following in the configuration file.

$ disable: traefik

Press Ctrl+X to write and exit the file. The disabled line: traefik instructs the K3s service to remove the resources associated with the traefik installation.

Then restart the K3s service using the following command to apply the changes.

$ sudo systemctl restart k3s

Now, you can verify by listing all Kubernetes objects in the kube system namespace. There should be no resources associated with the traefik installation at this time.

$ sudo kubectl get all -n kube-system

In this step, you learned how to change the K3s configuration during and after installation. Next, you will delete the K3s cluster to clean up the virtual machines.

Step 4 – Uninstall K3s

To uninstall K3S you need to run a shell script called 'usr/local/bin/k3s-uninstall.sh/'. The script is automatically generated after K3s is installed and can be used to perform a complete cleanup. During the uninstallation, any K3s configuration and cluster tools that were created or installed during the K3s installation are removed.

To remove K3, run the following command:

$ /usr/local/bin/k3s-uninstall.sh

Confirm the uninstallation by checking the status of the K3s service using the following command:

$ systemctl status k3s

You will get output similar to this:

[secondary_label Output]

Unit k3s.service could not be found.

Result

In this article, you installed a K3s cluster on Ubuntu and understood the configuration options available in K3s and the mechanism for using them. Now that you have set up your Kubernetes cluster, you should explore the different types of objects and their functionality in Kubernetes.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like