We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Install and Use k3d on Rocky Linux 9 / AlmaLinux 9

Learn how to – Install and Use k3d on Rocky Linux 9 / AlmaLinux 9.

If you have been sitting and thinking of a simple solution that would help you set up a Kubernetes cluster quickly to test your application without the hassle of setting up a complete cluster with three or more nodes, then your lucky star has shined. There is this application known as k3d that we are going to illustrate how you can get installed on you Rocky /AlmaLinux box without much fuss. Before we set for the swim, let us do a couple of introductions so that we can call our names as we dive into the pool together.

So What is k3d?

In order to get a complete understanding of k3d, we shall first cover something else known as k3s that it depends on. So K3s is certified Kubernetes distribution created by Rancher whose footprint is lightweight, easy to install, deploy, and manage. Rancher were able to make K3s lightweight by trimming over 3 billion lines of code from the main Kubernetes source code. Source code that were trimmed made it possible for k3s to have fewer dependencies, cloud provider integrations, add-ons, and other components that are not absolutely necessary for installing and running Kubernetes. We hope that k3s is now clear. Let us now jump into our core business of K3d.

K3d is a lightweight wrapper to run K3s (Rancher Lab’s minimal Kubernetes distribution) in Docker. K3d makes it very easy to create single- and multi-node k3s clusters in Docker, for example, for local development on Kubernetes. Good! Now we are swimming in the same pool. We can now proceed to set it up in our Rocky Linux 9 / AlmaLinux 9 box.

Requirements

Before we can start, we have to make sure our server meets the following:

  • kubectl to interact with the Kubernetes cluster
  • Docker to be able to use k3d at all
    • Note: k3d v5.x.x requires at least Docker v20.10.5 (runc >= v1.0.0-rc93) to work properly

Step 1: Install Docker

K3d whole depends of docker and in this step, we get to set it up and make sure it runs smoothly. Lucky for us, we had already done a guide to help us set this up. Kindly refer to How to install Docker on Linux guide.

Step 2: Install kubectl and other packages

Before the installation we are going to need some packages to help us fetch, edit and manipulate files. Run the following to install all of these essential packages:

sudo def install -y vim curl wget

In order to interact with the cluster we will be creating, we need kubectl package. Install it by running the following commands:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Confirm the version installed:

kubectl version --output=yaml

You can also install kubectl using dnf package manager by running the following commands:

Add Kubernetes repository:

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-$basearch
enabled=1
gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF

Then do an install

sudo def install -y kubectl

You can also confirm the version installed:

kubectl version --output=yaml

You should see an output similar to:

clientVersion:
  buildDate: "2022-11-09T13:36:36Z"
  compiler: gc
  gitCommit: 872a965c6c6526caa949f0c6ac028ef7aff3fb78
  gitTreeState: clean
  gitVersion: v1.25.4
  goVersion: go1.19.3
  major: "1"
  minor: "25"
  platform: linux/amd64
kustomizeVersion: v4.5.7

Step 3: Enable ip_tables module

K3d comes with traefik and it will need iptables to enable some routing. In order to make its actions smooth, we need to enable the “ip_tables” module. Run the following to this up:

sudo modprobe ip_tables
echo 'ip_tables' | sudo tee -a /etc/modules

Step 4: Install k3d on Rocky Linux 9 / AlmaLinux 9

Now we are in the juiciest of part where we are going to see k3d in action. Before that, we shall install it in our server as follows

wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash

You will see an output similar to this:

Preparing to install k3d into /usr/local/bin
k3d installed into /usr/local/bin/k3d
Run 'k3d --help' to see what you can do with it.

Step 5: Create a cluster a single server node

To demonstrate the power of k3d we are going to create a single node cluster in a very simple fashion. You simply need to run the command below:

k3d cluster create geekscluster

You’ll see the following as it creates your cluster

INFO[0000] Prep: Network
INFO[0000] Created network 'k3d-geekscluster'
INFO[0000] Created image volume k3d-geekscluster-images
INFO[0000] Starting new tools node...
INFO[0000] Pulling image 'ghcr.io/k3d-io/k3d-tools:5.4.6'
INFO[0001] Creating node 'k3d-geekscluster-server-0'
INFO[0002] Starting Node 'k3d-geekscluster-tools'
INFO[0002] Pulling image 'docker.io/rancher/k3s:v1.24.4-k3s1'
INFO[0007] Creating LoadBalancer 'k3d-geekscluster-serverlb'
INFO[0008] Pulling image 'ghcr.io/k3d-io/k3d-proxy:5.4.6'
INFO[0010] Using the k3d-tools node to gather environment information
INFO[0010] HostIP: using network gateway 172.18.0.1 address
INFO[0010] Starting cluster 'geekscluster'
INFO[0010] Starting servers...
INFO[0010] Starting Node 'k3d-geekscluster-server-0'
INFO[0014] All agents already running.
INFO[0014] Starting helpers...
INFO[0014] Starting Node 'k3d-geekscluster-serverlb'
INFO[0021] Injecting records for hostAliases (incl. host.k3d.internal) and for 2 network members into CoreDNS configmap...
INFO[0023] Cluster 'geekscluster' created successfully!
INFO[0023] You can now use it like this:
kubectl cluster-info

This is amazing stuff!!

It will automatically create a KubeConfig file at “~/.kube/config” and hence you can immediately begin interacting with you cluster via the “kubectl” command. So let us check our cluster information as follows:

$ kubectl cluster-info
Kubernetes control plane is running at https://0.0.0.0:33701
CoreDNS is running at https://0.0.0.0:33701/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://0.0.0.0:33701/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy

Let us view the pods

$ kubectl get pods -n kube-system
NAME                                                     READY   STATUS      RESTARTS   AGE
coredns-b96499967-f5wfx                         1/1     Running         0             10m
local-path-provisioner-7b7dc8d6f5-brgcw   1/1     Running          0             10m
helm-install-traefik-crd-jfxv9                     0/1     Completed      0             10m
helm-install-traefik-lvn64                          0/1     Completed      1             10m
metrics-server-668d979685-mdxn2           1/1     Running          0             10m
traefik-7cd4fcff68-6kqm5                          1/1     Running          0             10m
svclb-traefik-4c2d85da-6t7g7                    2/2     Running          0             7s

And now you are ready to deploy your applications to test them locally or do your various playful escapades with it. Have a happy and wild play!

Final Remarks

With a local Kubernetes running with low compute resources footprint, you can now be able to learn, and take advantage of the features it provides before you get into the multi-node highly available clusters in production workloads. Have a wonderful time in your adventure.

More articles on Kubernetes topics.

Cart

Your Cart is Empty

Back To Shop