How to Install Kubernetes Cluster Using K3s on Centos 7
Introduction:
In this guide, we’ll set up a lightweight Kubernetes cluster using K3s on CentOS 7. K3s is a fast and easy way to run Kubernetes without heavy resource usage. With one master and multiple worker nodes, you can build a clean, efficient, and production-ready cluster in just a few steps.
Lab Details
- VM1: k8s-master —
<public-ip> - VM2: worker-node1 —
<public-ip> - VM3: worker-node2 —
<public-ip> - VM4: worker-node3 —
<public-ip>
1. Set Hostname on Each Node
Set the hostname on every server.
Master Node:
sudo hostnamectl set-hostname k8s-master && exec bash
Worker Nodes:
$sudo hostnamectl set-hostname worker-node1 && exec bash
$sudo hostnamectl set-hostname worker-node2 && exec bash
$sudo hostnamectl set-hostname worker-node3 && exec bash
Update /etc/hosts on all nodes
Add the public IPs and hostnames of all nodes:
<public-ip> k8s-master
<public-ip> worker-node1
<public-ip> worker-node2
<public-ip> worker-node3
2. Install Required Tools
Install basic utilities on all nodes:
$sudo yum install curl wget -y
3. Install K3s on Master Node:
Run the following command only on the master:
$curl -sfL https://get.k3s.io | sh -
Check whether K3s is running:
$sudo systemctl status k3s
4. Configure kubectl on 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
Verify the cluster:
$kubectl get nodes
You should see the master node in Ready state.
5. Join Worker Nodes to the Cluster
Get the K3s token from master:
$sudo cat /var/lib/rancher/k3s/server/node-token
Copy the token.
Run on all worker nodes:
$curl -sfL https://get.k3s.io | K3S_URL=https://k8s-master:6443 K3S_TOKEN=<TOKEN> sh -
(Replace <TOKEN> with the actual key.)
Add master host entry (on workers):
<Master-IP> k8s-master
Verify from master:
kubectl get nodes
All nodes should appear in Ready status.
6. Final Verification:
Run the following commands:
$kubectl get pods -A
$kubectl get nodes
$kubectl cluster-info
Output:
If all components (CoreDNS, metrics-server, node agents) are running, your K3s Kubernetes cluster is successfully set up.
Conclusion:
With just a few simple steps, K3s lets you build a clean, lightweight, and fully functional Kubernetes cluster on CentOS 7. It’s fast to install, easy to manage, and perfect for development, testing, and small production setups.
