Setting up a Kubernetes (K8s) cluster can often be a complex and time-consuming task, particularly for those new to container orchestration. With the lightweight k3s distribution and Longhorn storage, you can get your cluster up and running quickly. This guide will walk you through the process of deploying a K8s cluster using k3s and integrating Longhorn for persistent storage. Whether you are a DevOps professional or a developer looking to explore Kubernetes, this tutorial will help you set up a functional and reliable cluster efficiently. Let’s dive in and simplify your Kubernetes setup!
NOTE:
I assume you have at least three Servers up and running.
Servers:
- 123.123.123.123 (Server 1)
- 124.124.124.124 (Agent 1)
- 125.125.125.125 (Agent 2)
Initialize the cluster
Server 1 — 123.123.123.123
$ ssh root@123.123.123.123
$ apt install curl -y
$ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--cluster-init \
--write- kubeconfig-mode 644 \
--node-label node-role.kubernetes.io/agent=true \
--node-label node-role.kubernetes.io/control-plane=true \
--node-label node-role.kubernetes.io/etcd=true \
--node-label node-role.kubernetes.io/master=true" sh -
Set the kubeconfig file path: The first command sets the KUBECONFIG
environment variable to the specified path, ensuring kubectl
uses the correct configuration file to connect to the Kubernetes cluster.
Monitor the Kubernetes cluster: The second command uses watch
to repeatedly run kubectl
commands that list all nodes and pods (with detailed information) in the cluster. This allows you to observe real-time changes and monitor the status of the cluster and its workloads as they start up and change state.
$ export KUBECONFIG="/path/to/kubeconfig"
$ watch -d "kubectl get nodes && kubectl get po -o wide --all-namespaces"
Add your agents
Agent 1 – 124.124.124.124
SSH into the remote server 124.124.124.124
as the root
user and install curl
using the apt
package manager. Next, download and execute the K3s installation script to configure a K3s Kubernetes server node with specific labels. This setup connects the node to an existing K3s cluster at https://123.123.123.123:6443
using the specified token.
$ ssh root@124.124.124.124
$ apt update && apt install curl -y
$ curl -sfL https://get.k3s.io | K3S_URL=https://123.123.123.123:6443 K3S_TOKEN=mySuperToken INSTALL_K3S_EXEC="\
--node-label node-role.kubernetes.io/agent=true \
--node-label node-role.kubernetes.io/control-plane=true \
--node-label node-role.kubernetes.io/etcd=true \
--node-label node-role.kubernetes.io/master=true" sh -
Agent 2 -125.125.125.125
SSH into a remote server 125.125.125.125
as the root
user and Install curl
on the remote server using the apt
package manager.
Download and run the K3s installation script to set up a K3s Kubernetes server node with specific labels, connecting it to a K3s cluster at https://123.123.123.123:6443
using the provided token.
$ ssh root@125.125.125.125
$ apt install curl -y
$ curl -sfL https://get.k3s.io | K3S_URL=https://123.123.123.123:6443 K3S_TOKEN=mySuperToken INSTALL_K3S_EXEC="server \
--node-label node-role.kubernetes.io/agent=true \
--node-label node-role.kubernetes.io/control-plane=true \
--node-label node-role.kubernetes.io/etcd=true \
--node-label node-role.kubernetes.io/master=true" sh -
Install Storage using Longhorn.io
The first command installs the open-iscsi
package, which is necessary for iSCSI support. The second command deploys Longhorn, a distributed storage system, to your Kubernetes cluster by applying a configuration from the Kubernetes manifest file on GitHub.
$ apt install open-iscsi -y
$ kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/master/deploy/longhorn.yaml
Thats it!
You should be ready to go!
Now you can fire your containers into kubernetes with for example Kapitan.
Cheers!
Sources:
- https://rancher.com/docs/k3s/latest/en/installation/install-options/server-config/
- https://rancher.com/docs/k3s/latest/en/installation/install-options/how-to-flags/
- https://rancher.com/docs/k3s/latest/en/storage/