Skip to content
ImagineWorld

K3s Advanced Configurations and Management

node_idnode-book_edge-computing-systems-with-kubernetes_chapter-03-k3s-advanced-configurations-and-management
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num3
chapter_titleK3s Advanced Configurations and Management
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 3 — K3s Advanced Configurations and Management

Section titled “Chapter 3 — K3s Advanced Configurations and Management”

This chapter covers more advanced configurations for your K3s clusters. By default, K3s includes a load balancer called KlipperLB, but it has some limitations. For example, you don’t have to repeat a port while creating a service, and it affects the way that you use a regular load balancer and NodePort service. It works well for simple deployments. In case you need another load balancer instead of Klipper, we cover how to install MetalLB, a bare metal load balancer. Then, we cover how to use advanced storage configuration to support read/write access modes for storage volumes with Longhorn, substituting the default local storage class provided by K3s. After this, we will do some common cluster management, including upgrading K3s, backing up, and restoring the cluster.

In this chapter, we’re going to cover the following main topics:

  • Bare metal load balancer with MetalLB
  • Setting up Longhorn for storage
  • Upgrading your cluster
  • Backing up and restoring your K3s configurations

For this chapter, you need the following:

  • Raspberry Pi 4 model B with 4 GB RAM (minimum suggested)
  • A cloud server or VM with Ubuntu 20.04 LTS
  • Helm v3 installed in your device or client

With this, we are ready to learn this advanced configuration for K3s. So, let’s get started.

In this section, you are going to explore MetalLB as a bare metal load balancer, which can give you powerful features to expose your services at the edge.

Before starting with KlipperLB, it’s necessary to give some context about load balancers in Kubernetes. Kubernetes uses services to communicate or access your application. A ClusterIP service creates a DNS record, so this service could be reachable from within the cluster. A NodePort service exposes the service on each node’s IP at a static port. This port is in the range of 30000–32767. And, finally, Kubernetes supports a load balancer service that exposes the service externally using a cloud provider’s load balancer. In the case of K3s, it’s going to use KlipperLB by default.

KlipperLB and MetalLB as bare metal load balancers

Section titled “KlipperLB and MetalLB as bare metal load balancers”

Edge devices and edge computing don’t have a lot of resources, so it is common to find clusters that only have a single node. Generally, a Kubernetes load balancer service depends on the implementation of a specific cloud provider. It also works in layer 4 (the transport layer) to transmit Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) protocols, and this load balancer is connected to a NodePort service too. So, in the case of edge devices, K3s implements KlipperLB.

KlipperLB works really nicely on low-resource devices or environments as k3s’ load balancer. But when you have multi-node clusters, maybe KlipperLB doesn’t offer the best features for service availability. That’s where MetalLB comes into the game. KlipperLB and MetalLB offer a bare metal load balancer service on Kubernetes. In this case, you can use these implementations on K3s.

KlipperLB and MetalLB – the goods and the bads

Section titled “KlipperLB and MetalLB – the goods and the bads”

Now, let’s mention the pros and cons of each of those bare metal load balancers in terms of the implementation, dependencies, and use case. So, let’s get started with KlipperLB.

The pros of KlipperLB are as follows:

  • Pretty lightweight
  • Simple to use with enough features for single node clusters

The cons of KlipperLB are as follows:

  • Depends on hostPort or available ports to expose a pod.
  • If the port is not available, the load balancer service stays on the pending state.

Talking about MetalLB, it uses layer 2 (the data link layer) where the format of data is defined. In this way, MetalLB uses a node for load balancing and has its own advantages and disadvantages. The next table summarizes this information:

In general, choose KlipperLB if you have a single node cluster and you want to avoid complex installations that use unique ports. Use MetalLB for multi-node clusters or installations where you can reuse ports and a more robust load balancer service.

You need a K3s installation with the --disable servicelb option; if you have a previous installation, you have to reinstall K3s. To install K3s with this option, follow these steps:

  1. Log in to your virtual machine (VM) or device using the following command:
    $ ssh ubuntu@YOUR_VM_IP
  2. Install K3s using the following line. This applies to a simple ARM device for a basic installation without installing KlipperLB:
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--write-kubeconfig-mode 644 --no-deploy traefik --disable traefik --disable servicelb" sh -s -

(Optional) Install K3s using the following lines. First, set the PUBLIC_IP environment variable with the IP of your device or VM:

$ PUBLIC_IP=YOUR_PUBLIC_IP|YOUR_PRIVATE_IP

Then, use the next lines to install K3s in a node that has a public IP:

$ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--write-kubeconfig-mode 644 --no-deploy traefik --disable traefik --tls-san "$PUBLIC_IP" --node-external-ip "$PUBLIC_IP" --disable servicelb" sh -s -
  1. Create a MetalLB namespace (metallb-system) with the official manifests, executing the following lines:
    $ kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/namespace.yaml
  2. Before running the command to install MetalLB, you have to create a ConfigMap called metallb-config inside the metallb-system namespace. Let’s call this file config.yaml with the following content:
    apiVersion: v1
    kind: ConfigMap
    metadata:
    namespace: metallb-system
    name: config
    data:
    config: |
    address-pools:
    - name: default
    protocol: layer2
    addresses:
    - 192.168.0.240-192.168.0.250
  3. Now, create the ConfigMap by executing the following command:
    $ kubectl apply -f config.yaml
  4. Install MetalLB with the official manifests by executing the following lines:
    $ kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/metallb.yaml

Now that MetalLB is installed using YAML files, let’s continue with the installation using Helm instead of YAML files.

Important Note

If you want to delete this or other installations, use the delete option instead of apply using the same command – for example, kubectl delete -f YOUR_YAML_FILE.

In case you want to install MetalLB using Helm v3, follow these steps:

  1. Add the Helm Chart repository of MetalLB using the following commands:
    $ helm repo add metallb https://metallb.github.io/metallb
  2. Install MetalLB using Helm by executing the following command:
    $ helm install metallb -n metallb-system metallb/metallb
  3. If you want to install MetalLB with the values.yaml file, execute the following lines:
    $ helm install metallb -n metallb-system metallb/metallb -f values.yaml
  4. You have to create the values.yaml file, with the following example content:
    configInline
    address-pools:
    - name: default
    protocol: layer2
    addresses:
    - 192.168.0.240-192.168.0.250
  5. Now, you have to create the ConfigMap based on the installation using kubectl and change the namespace to metallb-system and the name to metallb-config. Then, apply YAML:
    $ kubectl apply -f config.yaml

Important Note

The addresses field corresponds to the range of IP addresses that MetalLB will use to assign to your services every time that you create a LoadBalancer service in Kubernetes.

  1. Now, MetalLB is installed and ready to use.

Now you have a fresh installation of MetalLB ready to use. Now you have to learn how to troubleshoot MetalLB in the next section.

Sometimes, it’s necessary to troubleshoot our installations. If you are having trouble with your installation, here are some commands that you can use to troubleshoot a new installation of MetalLB. The following are steps and commands that you can use for this:

  1. Log in to your VM or device:
    $ ssh ubuntu@NODE_IP
  2. Create a pod to check whether your cluster can schedule pods:
    $ kubectl run nginx --image=nginx --restart=Never
  3. Create a service to expose the pod created previously and test whether the LoadBalancer service works:
    $ kubectl expose pod/nginx --port=8001 \
    --target-port=80 \
    --type=LoadBalancer
  4. Execute the following command if you want to check whether the services and port work to expose your services, which can be either LoadBalancer or NodePort:
    $ kubectl get services
  5. Now, perform an access check for the assigned external IP to the NGINX service and execute the following command to check that MetalLB exposed your service:
    $ curl http://EXTERNAL_IP:8001

In case you want to check the logs of MetalLB in case of errors, look at the next pods inside the metallb-system namespace:

  • Controller
  • Speaker

Now you know how to do basic troubleshooting of MetalLB. Let’s move to a more advanced storage configuration using Longhorn in the next section.

In terms of persistent information, you will find two types of containers, stateless and stateful containers. A stateless or ephemeral container doesn’t persist information generated inside a container. A stateful container can persist the information even when this is deleted. K3s includes, by default, a way to persist data using a storage type (called storage class in Kubernetes) called local-path. This storage is a basic and pretty lightweight implementation, designed for edge devices. A common feature used on Kubernetes is to have a persistent volume claim that allows your pods to consume (write and read data) from different nodes. And this is a persistence volume configuration with the access mode key, set as ReadWriteMany (RWX). This feature is often used in production scenarios and it’s pretty important because it enables you to share information from your different services. Longhorn provides this feature in a pretty lightweight presentation and it’s optimized for edge devices. Let’s move to learn what Longhorn is and how you can install it.

Longhorn is designed to be a distributed and hyper-converged storage stack. Hyper-converged storage means that virtualization software abstracts and pools storage. Longhorn doesn’t use a lot of resources, which gives you the ability to use it for advanced storage in edge devices. You can even simplify your workflows of snapshots, backups, and even disaster recovery. So, if you are looking for lightweight and advanced edge solutions for storage, Longhorn can fit your needs. There are other options, such as Rook, but Longhorn is an easy piece of software that can give you extra storage power without having to sacrifice resource consumptions. So, let’s move on to learn how to install it and create a simple persistent volume claim for a pod in the next section.

Installing Longhorn with ReadWriteMany mode

Section titled “Installing Longhorn with ReadWriteMany mode”

To install Longhorn, follow these steps:

  1. Log in to your VM or device:
    $ ssh ubuntu@NODE_IP
  2. If you want to install the ReadWriteMany PVC mode, you have to install nfs-common on each VM with Ubuntu installed in your cluster. For this, execute the following command:
    $ sudo apt install -y nfs-common
  3. Apply the official Longhorn manifests, as follows:
    $ kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/v1.3.1/deploy/longhorn.yaml

Important Note

Longhorn will be installed in the longhorn-system namespace.

  1. Create a pvc.yaml file:
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: longhorn-volv-pvc
    spec:
    accessModes:
    - ReadWriteMany
    storageClassName: longhorn
    resources:
    requests:
    storage: 2Gi
  2. Apply the pvc.yaml file:
    $ kubectl create -f pvc.yaml

Important Note

You can use different PVC modes such as ReadWriteOnce or ReadOnlyMany. By default, the storage classes at least support ReadWriteOnce. So, ReadWriteMany is a special feature that uses NFS and is included in Longhorn.

Now, it’s time to create a pod using this PVC using the Longhorn storage class. To do this, follow these steps:

  1. Create the pod.yaml file to create a pod using the previously created PVC:
    echo "
    apiVersion: v1
    kind: Pod
    metadata:
    name: volume-test
    namespace: default
    spec:
    containers:
    - name: volume-test
    image: nginx:stable-alpine
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: volv
    mountPath: /data
    ports:
    - containerPort: 80
    volumes:
    - name: volv
    persistentVolumeClaim:
    claimName: longhorn-volv-pvc" > pod.yaml
  2. Apply the pod.yaml file to create the pod:
    $ kubectl create -f pod.yaml

Now, you have Longhorn installed and running. So, let’s move on to learn how to use the Longhorn UI in the next section.

If you want to access the Longhorn UI, you have to check the services created on longhorn-system and execute a port-forward; if you installed MetalLB, you can create a LoadBalancer service to expose the Longhorn UI.

To access Longhorn with a port-forward, execute the following steps:

  1. Run the next port-forward command locally in order to access the UI in your browser:
    $ kubectl port-forward svc/longhorn-frontend -n longhorn-system 8080:80
  2. Now, open your browser at http://localhost:8080; you will see the following dashboard:

Figure 3.1 – Longhorn UI

With this dashboard, you can manage your Persistent Volume Claims (PVCs) using the UI; for more references, you can visit the following link: https://longhorn.io/docs/1.3.1/deploy/accessing-the-ui.

Now you know how to install and use Longhorn. Let’s go ahead and do some basic troubleshooting.

Using the preceding example as reference, to troubleshoot the PVC creation using Longhorn, you can use the following commands:

  1. Check whether the Longhorn pods are running successfully with the following command:
    $ kubectl get pods –n longhonr-system
  2. Check whether the PV was created:
    $ kubectl get pv
  3. Check whether the PVC was created:
    $ kubectl get pvc
  4. Check whether the pod from pod.yaml using the new Longhorn storage class was created:
    $ kubectl get pods

With these commands, you can find errors that come up when a pod or deployment uses a PVC with the Longhorn storage class.

Important Note

The previous four commands will return errors in case something goes wrong. For more information about this, you can check https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage or https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class.

Now, we are ready to learn another advanced topic about upgrading the cluster. So, let’s move to the next section.

Sometimes, you want to be up to date with the new versions and features of K3s. The next sections explain how to perform these upgrading processes.

To perform an upgrade in your nodes, you have to follow these steps:

  1. First, you have to stop K3s on your device with the following command:
    $ /usr/local/bin/k3s-killall.sh
  2. Now, you have to choose the version which you want to upgrade to. In general, there are three options – choose the latest or most stable channel, or pick a specific version. The next command will update your cluster to the latest stable version available:
    $ curl -sfL https://get.k3s.io | sh -
  3. Now, if you want to update to the latest version, which is not so stable, you can execute the following command:
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL=latest sh -
  4. The last option is to pick a specific version. For this, you have to execute the following command:
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=vX.Y.Z-rc1 sh -

Important Note

You can visit https://update.k3s.io/v1-release/channels to check the latest, stable, or specific available version of K3s or the official site of k3s at https://k3s.io in the GitHub section.

Now you know how to upgrade your cluster using the K3s scripts. Let’s move on to learn this manually in the next section.

If you want to perform a manual upgrading of the K3s version, you can follow the following steps, based on the official K3s website documentation:

  1. Download your desired version of the K3s binary from releases. To do this, check this link: https://github.com/k3s-io/k3s/releases.
  2. Copy the downloaded binary to the /usr/local/bin folder.
  3. Stop the old k3s binary. For this, you can execute the following command:
    $ /usr/local/bin/k3s-killall.sh
  4. Delete the old binary.
  5. Launch the new K3s binary with the next command:
    $ sudo systemctl restart k3s

Now, you know how to do the K3s manually, but there is something that you have to know, and that is to restart the service to apply the next changes. This is covered in the next section.

When you perform software or hardware upgrades, or when a restart is needed to fix errors, you can restart K3s services using systemd and OpenRC.

To restart K3s using systemd, follow these steps:

  1. To restart the K3s service in your master node, execute the following command:
    $ sudo systemctl restart k3s
  2. To restart the K3s agent service in your agent nodes, execute the following command:
    $ sudo systemctl restart k3s-agent

To restart K3s using OpenRC, follow these steps:

  1. To restart the K3s service in your master node, execute the following command:
    $ sudo service k3s restart
  2. To restart the K3s-agent service in your agent nodes, execute the following command:
    $ sudo service k3s-agent restart

Now that you know all the necessary steps to upgrade your K3s cluster, it’s time to move on to other advanced topics – backups and restorations. Let’s move on to the next section to learn about this.

Backing up and restoring your K3s configurations

Section titled “Backing up and restoring your K3s configurations”

Backups and restoration of your Kubernetes objects are something to consider in production environments. This section explains how to perform these kinds of tasks for the default storage, SQLite, how to install and manage etcd on K3s, and basic resources if you are using the SQL backends of K3s.

If you are using the default storage, SQLite, follow these steps:

  1. Log in to your master node:
    $ ssh ubuntu@NODE_IP
  2. Stop the K3s using the following command:
    $ k3s-killall.sh
  3. Change to the /var/lib/rancher/k3s/ directory server:
    $ sudo cd /var/lib/rancher/k3s
  4. Copy the folder server inside the k3s folder:
    $ sudo cp -R /var/lib/rancher/k3s folder_of_destination
  5. Download this folder on another device if necessary.

Backups and restoring from the SQL database K3s backend

Section titled “Backups and restoring from the SQL database K3s backend”

If you are using external storage – let’s say, for example, MySQL – you have to use a tool or the command to back up your database.

In the case of MySQL, you can execute the following steps to back up your K3s configurations:

  1. Get your database credentials to use the mysqldump command.
  2. Run the following command to back up your database, which in this case is called k3s, using the YOUR_USER user, the YOUR_PASSWORD password, and an output file called output.sql from the YOUR_HOST host:
    $ mysqldump -h YOUR_HOST -u YOUR_USER -pYOUR_PASSWORD k3s > output.sql

Important Note

You can modify the YOUR_HOST, YOUR_USER, and YOUR_PASSWORD values, the database name instead of k3s, and even the name of the output file to customize your backup command. The –h option can be optional if you are connected to the same host where the database is installed. By default, it connects to localhost. You can check this link for other examples to back up your MySQL: https://www.tecmint.com/mysql-backup-and-restore-commands-for-database-administration.

Now the backup is ready to be used. In the next section, you are going to use the backup to restore your database.

Now it is time to restore your database. Follow the next steps for the restoration:

  1. Get your database credentials to use the database with the mysql command.
  2. Run the following command to restore your database backup. We are using the k3s database. Change YOUR_HOST and YOUR_PASSWORD parameters according to the database used as data storage for your k3s cluster. Finally, the output.sql file is used to load your backup and restore your database:
    $ mysql -h YOUR_HOST -u YOUR_USER -pYOUR_PASSWORD k3s < output.sql

Important Note

You can modify the values from the previous command to perform your restoration with the output.sql file.

Backing up and restoring other data storages

Section titled “Backing up and restoring other data storages”

If you are using other K3s backends, such as PostgreSQL or etcd, you can check the official documentation for each database.

For PostgreSQL, check the following link: https://www.postgresql.org/docs/8.3/backup-dump.html.

For etcd, check the following link: https://etcd.io.

Now that you have learned how to restore your MySQL data storage for your K3s cluster, let’s move on to the next section to understand how to use etcd as your data storage.

etcd is the default type of storage to store all the Kubernetes objects in your cluster. etcd, by default, was removed from K3s, but you can install it. K3s customized how etcd works for your cluster; this includes some custom features that you can’t find in a regular Kubernetes cluster that uses etcd. So, let’s get started with installing etcd in K3s.

If you want to install it, follow these steps:

  1. To install K3s with the etcd backend, you have to execute the following command to include etcd in the K3s installation. This has to be executed in the master node:
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --cluster-init" sh -s -
  2. Set your TOKEN variable, with the YOUR_TOKEN master token, to join the nodes to the cluster:
    $ TOKEN=YOUR_TOKEN
  3. Now, if you need a multi-cluster configuration, execute the following command:
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server
    --server https://MASTER_IP:6443" K3S_TOKEN=$TOKEN sh -s -

Now that you have learned how to install the etcd feature for K3s, let’s move on to the next section to learn how to create and restore etcd snapshots for your Kubernetes objects configurations.

K3s includes an experimental feature to back up and restore etcd. In this section, you are going to learn how to perform etcd snapshots and restoration for etcd. The snapshots are enabled by default with this backend. These snapshots are stored in /var/lib/rancher/k3s/server/db/snapshots. To create a backup, manually execute the following steps:

  1. Create a backup manually:
    $ k3s etcd-snapshot --name=mysnapshot

This will generate a file inside the snapshots folder.

  1. To restore your etcd from this backup, execute the following command:
    $ k3s server \
    --cluster-reset \
    --cluster-reset-restore-path=<PATH-TO-SNAPSHOT>
  2. You can automate the snapshot generation with the following option:
    --etcd-snapshot-schedule-cron

For more references to configure this, visit this link: https://rancher.com/docs/k3s/latest/en/backup-restore/#options.

You can even use the official documentation of etcd: https://github.com/etcd-io/website/blob/main/content/en/docs/v3.5/op-guide/recovery.md.

That’s how you manage your etcd snapshots. Now, let’s take a recap of what we have covered in this chapter.

This chapter covered common advanced configurations for Kubernetes edge clusters using Ubuntu and K3s. One of these common configurations was to install a bare metal load balancer using MetalLB. We also discussed the pros and cons of this as compared to the default K3s load balancer, KlipperLB, followed by the use cases of when to use each one. Then, we jumped to the advanced storage configurations of Longhorn, which is a really lightweight storage solution, and easy to install and configure for ReadWriteMany access modes for storage. Finally, we saw how to upgrade our cluster, and perform backups and restorations when using different data storage such as SQL or etcd. With all this knowledge, you are ready to create a production-ready cluster. In the next chapter, we are going to learn how to use k3OS to create your clusters using the K3s ISO image and overlay installation.

Here are a few questions to validate your new knowledge:

  1. When do you choose KlipperLB or MetalLB as a bare metal load balancer solution?
  2. How can I troubleshoot my MetalLB installation?
  3. How can I install Longhorn to get more robust data storage solutions for my deployments?
  4. How can I troubleshoot my Longhorn installation?
  5. What other data storage solutions can I use instead of Longhorn?
  6. What are the steps to upgrade my K3s clusters?
  7. What are the steps to back up or restore my Kubernetes object configurations if I use a SQL backend or etcd?

You can refer to the following references for more information on the topics covered in this chapter: