Best Steps To Set up k3d on AlmaLinux 9 – OrcaCore

Posted on

Best Steps To Set up k3d on AlmaLinux 9 - OrcaCore

Best Steps To Set up k3d on AlmaLinux 9 – OrcaCore

This guide is designed to walk you through the process of setting up k3d on AlmaLinux 9. k3d is a lightweight tool designed to run K3s clusters within Docker containers. K3s itself is a lightweight, CNCF-certified Kubernetes distribution and Sandbox project. Engineered for resource-constrained environments, K3s is packaged as a single binary with a memory footprint of less than 512MB of RAM.

k3d leverages a Docker image constructed from the K3s repository to launch multiple K3s nodes within Docker containers on any machine that has Docker installed. This allows a single physical or virtual machine (referred to as the Docker Host) to simultaneously host multiple K3s clusters, each containing multiple server and agent nodes.

Follow the steps below to successfully set up k3d on AlmaLinux 9.

Before starting, ensure you are logged into your AlmaLinux 9 server as a non-root user with sudo privileges. If you haven’t already, you can follow our guide on Initial Server Setup with AlmaLinux 9 to configure this.

Since k3d relies heavily on Docker, you must have Docker installed on your server. If Docker is not yet installed, you can refer to our guide on Install and Use Docker on AlmaLinux 9.

1. Install kubectl on AlmaLinux 9

To interact with the Kubernetes cluster that we will create using k3d, you’ll need the kubectl package. Install it using 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

After installation, verify that kubectl is working correctly by checking its version:

kubectl version --output=yaml
Example Output:
clientVersion:
  buildDate: "2023-03-15T13:44:59Z"
  compiler: gc
  gitCommit: 6e521a65a74747cb48bf66d5e6095c65f7627a0c
  gitTreeState: clean
  gitVersion: v1.27.0
  goVersion: go1.20.3
  major: "1"
  minor: "27"
  platform: linux/amd64
kustomizeVersion: v5.0.1
serverVersion:
  buildDate: "2023-03-15T13:36:15Z"
  compiler: gc
  gitCommit: 6e521a65a74747cb48bf66d5e6095c65f7627a0c
  gitTreeState: clean
  gitVersion: v1.27.0
  goVersion: go1.20.3
  major: "1"
  minor: "27"
  platform: linux/amd64

2. Enable ip_tables module on AlmaLinux 9

K3d uses Traefik as an ingress controller, which relies on iptables for routing. Ensure the ip_tables module is enabled by running the following commands:

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

This ensures that the ip_tables module is loaded and also persists across reboots by adding it to /etc/modules.

3. Install k3d on AlmaLinux 9

Now you can proceed with installing k3d. Use the following command to download and install k3d on your AlmaLinux 9 server:

wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
**<mark>Output</mark>**
Preparing to install k3d into /usr/local/bin
k3d installed into /usr/local/bin/k3d

This script downloads the installation script from the k3d GitHub repository and executes it, placing the k3d binary in /usr/local/bin.

4. How To Use k3d?

Let’s create a single-node cluster with k3d. You can use the following command, choosing your desired cluster name:

k3d cluster create <mark>orcacluster</mark>

You will see output similar to this while your cluster is being created:

INFO[0000] Using k3d network: k3d-k3s-default          
INFO[0000] Creating cluster [orcacluster]               
INFO[0000] Creating server node [k3d-orcacluster-server-0] 
INFO[0005] Pulling image 'rancher/k3s:v1.27.1-k3s1'   
INFO[0017] Creating LoadBalancer [k3d-orcacluster-serverlb] 
INFO[0018] Exposing port 8080 for LoadBalancer [k3d-orcacluster-serverlb] 
INFO[0018] Exposing port 8443 for LoadBalancer [k3d-orcacluster-serverlb] 
INFO[0018] Starting cluster [orcacluster]               
INFO[0021] Starting server node [k3d-orcacluster-server-0] 
INFO[0029] Cluster [orcacluster] is running...         
INFO[0029] You can now use the cluster with:             
INFO[0029] kubectl cluster-info                         
INFO[0029] k3d kubeconfig merge orcacluster -o ~/.kube/config
INFO[0029] NOTE: To have the kubectl CLI use the cluster immediately, run:
INFO[0029] export KUBECONFIG="$(k3d kubeconfig write orcacluster)"
INFO[0029] Done! 

k3d automatically creates a KubeConfig file at ~/.kube/config, enabling you to immediately interact with your cluster using the kubectl command. Let’s verify our cluster information:

kubectl cluster-info
**<mark>Output</mark>**
Kubernetes control plane is running at https://0.0.0.0:45135
CoreDNS is running at https://0.0.0.0:45135/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://0.0.0.0:45135/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy

You can also view the running pods in the kube-system namespace:

kubectl get pods -n kube-system
**<mark>Output</mark>**
NAME                                      READY   STATUS      RESTARTS   AGE
coredns-597584b69b-654kg                  1/1     Running     0          4m33s
local-path-provisioner-79f67d76f8-pzlp8   1/1     Running     0          4m33s
helm-install-traefik-crd-bhqhh            0/1     Completed   0          4m33s
metrics-server-5f9f776df5-vtszz           1/1     Running     0          4m33s
helm-install-traefik-55rrs                0/1     Completed   2          4m33s
svclb-traefik-188963af-jhzbm              2/2     Running     0          3m57s
traefik-66c46d954f-k25gc                  1/1     Running     0          3m57s

Now you are all set to deploy your applications for local testing or experimentation.

Conclusion

k3d provides a convenient and lightweight method for running K3s clusters within Docker containers. It’s an excellent tool for local development, testing, and CI/CD pipelines. Following these steps, you have successfully set up k3d on AlmaLinux 9.

We hope you found this guide helpful. Here are some other articles you might be interested in:

Install and Use SQLite on AlmaLinux 9

Install and Use Podman on AlmaLinux 9

GNU Image Manipulation Program on AlmaLinux 9

Sendmail mail server setup AlmaLinux 9

Cinnamon Desktop For AlmaLinux 9

Add and Remove Users on AlmaLinux 9

AlmaLinux 9: Run Nginx in a Docker Container

Froxlor Server Management For AlmaLinux 9

Alternative Solutions for Running Kubernetes Locally on AlmaLinux 9

While k3d provides a straightforward way to run K3s in Docker, there are alternative solutions for creating local Kubernetes clusters on AlmaLinux 9. Two popular options are Minikube and Kind (Kubernetes in Docker).

1. Minikube

Minikube is a tool that allows you to run a single-node Kubernetes cluster inside a VM on your local machine. It’s a great option if you want a more complete Kubernetes experience closer to a production environment than what K3s (and therefore k3d) typically offers. Minikube supports various hypervisors like VirtualBox, KVM, and Hyper-V.

Installation and Setup on AlmaLinux 9:

First, you need to have a hypervisor installed. For this example, we will use KVM.

sudo dnf install -y qemu-kvm libvirt virt-install bridge-utils
sudo systemctl enable --now libvirtd

Next, download and install Minikube:

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

Make sure you have kubectl installed (as described in the original article). Then, start the Minikube cluster:

minikube start --driver=kvm2

Minikube will download the necessary Kubernetes components and start the cluster. You can then interact with the cluster using kubectl.

Example Usage:

kubectl get nodes

This will display the single node in your Minikube cluster.

Pros of Minikube:

  • Simulates a more complete Kubernetes environment.
  • Supports various hypervisors.
  • Easy to set up and use.

Cons of Minikube:

  • Requires a hypervisor.
  • Can be more resource-intensive than k3d.
  • Slower startup time compared to k3d.

2. Kind (Kubernetes in Docker)

Kind (Kubernetes in Docker) is another tool for running local Kubernetes clusters using Docker containers. Unlike Minikube, which runs Kubernetes inside a VM, Kind runs each Kubernetes node as a Docker container. This makes it very lightweight and fast.

Installation and Setup on AlmaLinux 9:

First, ensure you have Docker installed. Then, download and install Kind:

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

Create a Kubernetes cluster with Kind:

kind create cluster --name=kind-cluster

This will create a Kubernetes cluster named kind-cluster. Kind automatically configures kubectl to connect to the new cluster.

Example Usage:

kubectl get nodes

This will display the nodes in your Kind cluster.

Pros of Kind:

  • Very lightweight and fast.
  • Easy to create and destroy clusters.
  • Uses Docker, so no hypervisor is required.

Cons of Kind:

  • Might not perfectly simulate a production environment as everything runs in Docker.
  • Networking configurations can be more complex in some scenarios.

These are two viable alternatives to k3d for setting up a Kubernetes environment on AlmaLinux 9. The best choice depends on your specific needs and priorities. If you need a lightweight and fast solution for local development, k3d or Kind are excellent choices. If you want a more complete Kubernetes experience, Minikube might be a better fit. Regardless of the method you choose, running Kubernetes locally is a valuable skill for any developer or operator working with containerized applications.

Leave a Reply

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