K3S cluster create on On-prem Vm

virendrasing rajput
3 min readJul 10, 2023

--

Hardware Requirements: Ensure that VM meets the minimum hardware requirements for running k3s. These requirements typically include CPU, memory, and storage. The exact specifications may vary based on your workload and cluster size. As a general guideline, allocate at least 2 CPU cores and 2GB of RAM to VM.

  1. Ubuntu and Debian-based systems to update the package lists and repositories on your machine. It ensures that you have the latest information about available packages and their versions. You need to update your system.
sudo su
apt-get update

This command fetches the latest information about available packages and their versions from the repositories.

2. Install Docker Engine

apt-get install docker.io
systemctl start docker
systemctl status docker

3. Install k3s cluster on master server.

curl -sfL https://get.k3s.io | sh -s server --cluster-init --token "master-token1234"
sed -e '/server \\/,$d' -e 's@ExecStart=.*@ExecStart=/usr/local/bin/k3s server@' -i /etc/systemd/system/k3s.service
systemctl daemon-reload
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
kubectl --kubeconfig /etc/rancher/k3s/k3s.yaml get nodes
kubectl get all -n kube-system
kubectl get nodes

4. Helm is a package manager for Kubernetes that helps you manage and deploy applications on a Kubernetes cluster. It provides a convenient way to package, version, and distribute Kubernetes applications and their dependencies. Install helm with following commands.

wget https://get.helm.sh/helm-v3.8.2-linux-amd64.tar.gz
tar -zxvf helm-v3.8.2-linux-amd64.tar.gz
sudo mv linux-amd64/helm /usr/local/bin/helm
helm version

5. MetalLB is an open-source load balancer implementation for bare metal Kubernetes clusters. It allows you to allocate external IP addresses to services running in your cluster and enables load balancing of network traffic to those services.

Use following commands to install metallb

helm repo add metallb https://metallb.github.io/metallb
helm install metallb metallb/metallb -n metallb-system --create-namespace
vi IPAddressPool.yaml
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: first-pool
namespace: metallb-system
spec:
addresses:
- 172.168.1.240-172.168.1.250
kubectl apply -f IPAddressPool.yaml -n metallb-system
kubectl get all -n metallb-system

You can edit any service with type LoadBalancer and update its configuration to use the assigned IP address. For example, to update the “my-service” service, you can use the following command:

kubectl edit service service-name

After editing the service, the external traffic will be routed to the pods associated with the service using the LoadBalancer IP address assigned by MetalLB.

Either you can check the IP address assigned by MetalLB to LoadBalancer services that are running in the kube-system namespace, you can use the following command:

6. Install nginx web server

kubectl create deployment nginx-deployment --image=nginx
kubectl expose deployment nginx-deployment --port=80 --target-port=80 --type=NodePort
kubectl get all

To access the Nginx web server, you can use node IP addresses along with the assigned NodePort.

--

--

virendrasing rajput

Hello everyone, I am a DevOps Engineer. I Specialize in RHCSA & AWS Cloud. Skilled in a wide range of DevOps tools and passionate about driving automation.