How to Install Kubernetes Cluster Using K3s on RHEL 7

From PheonixSolutions
Jump to navigation Jump to search

How to Install Kubernetes Cluster Using K3s on RHEL 7

[edit]

The below steps will show you how to install the Kubernetes cluster using K3s on RHEL 7. We will set up a single master node and one worker node to provide a scalable foundation for the containerized applications.

K3s is a lightweight, easy-to-install, and efficient Kubernetes distribution designed for resource-constrained environments, edge computing, and situations where simplicity and speed are paramount. K3s aims to simplify the deployment and operation of Kubernetes clusters.

Prerequisites

  1. Set Hostname on Each Node
  2. Install Required Tools
  3. Download and Install Kubernetes Cluster Using K3s on RHEL 7
  4. Configure Kubectl on the Master Node
  5. Join Worker Nodes to K3s Kubernetes Cluster
  6. Test K3s Kubernetes Cluster Installation

Lab Details:

  1. VM1: K3s-master – < server IP >
  2. VM2: k3s-worker01 – < server IP >

1) Set Hostname on Each Node

As we are using two nodes, one master and one worker node, set their respective hostname using the `hostnamectl` command.

$ sudo hostnamectl set-hostname "k3s-master" && exec bash  // Run on VM1 (master node)
$ sudo hostnamectl set-hostname "k3s-worker01" && exec bash  // Run on VM2 (worker node)

Add the following entries in `/etc/hosts` file on each node.

< server IP > k3s-master
< server IP > k3s-worker01

2) Install Required Tools

Install `curl` and `wget` on each node using the following `dnf` command.

$ sudo dnf install curl wget -y

3) Download and Install Kubernetes Cluster Using K3s on RHEL 7

Run the following command on the master node only:

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

This script installs K3s and starts the service automatically.

Post K3s successful installation, verify K3s service using the following command.

$ sudo systemctl status k3s

4) Configure Kubectl on Master Node

To interact with the Kubernetes cluster, we must configure `kubectl` utility, and execute the following set of commands on the master node.

$ mkdir ~/.kube
$ sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
$ sudo chown $USER ~/.kube/config
$ sudo chmod 600 ~/.kube/config
$ export KUBECONFIG=~/.kube/config

Now, try to get node and cluster information using `kubectl` commands.

The output above confirms that our master node is in Ready State and the Control plane, CoreDNS, and Metrics-server are up and running.

In the next step, we will join worker nodes to this cluster.

5) Join Worker Node to K3s Kubernetes Cluster

The K3s installation generates a token for worker nodes to join the cluster. Retrieve it from the master node:

$ sudo cat /var/lib/rancher/k3s/server/node-token

Once the above command is executed, it will give the token. Copy the token as you will need it in the next steps.

Now login to the worker nodes, and run the following command.

$ curl -sfL https://get.k3s.io | K3S_URL=https://k3s-master:6443 K3S_TOKEN=<token> sh -

Replace the Token with the token you copied.

$ curl -sfL https://get.k3s.io | K3S_URL=https://k3s-master:6443 K3S_TOKEN=K1074af52efefxxxxxxxxxxxxxxxxxx::server:48xxxxxxxxxxxxxxxxxxxxx sh -

Now head back to the master node and run `kubectl get nodes` command to verify whether worker nodes have joined the cluster or not.

$ kubectl get nodes

The above output confirms that the worker node has joined the cluster successfully.

6) Test K3s Kubernetes Cluster Installation

To test K3s Kubernetes cluster installation, let's deploy an Nginx-based application, and run the following commands from the master node.

$ kubectl create deployment nginx-web --image nginx --replicas 2
$ kubectl get deployment nginx-web
$ kubectl get pods

Expose the above-created deployment with NodePort type.

$ kubectl expose deployment nginx-web --type NodePort --port 80
$ kubectl get svc nginx-web

Now try to access the above deployed Nginx application using `curl` command.

$ curl http://ipaddress:30677

The output above confirms that our K3s Kubernetes cluster is working fine as we can access our Nginx sample application.