Kubernetes K8s Setup on CentOS:7 with easy steps.

๐Ÿ’ก
Taking inspiration from and dedicated to our training guru, Shri Maninder sir.

Hey everyone! In this article, we'll learn how to set up a Kubernetes (k8s) cluster on a Linux CentOS 7 machine. You can do this either on cloud servers or virtual machines within your infrastructure.

If you are trying to set up a Kubernetes (k8s) cluster on an Ubuntu machine, try this article link: https://rakeshkumarjangid.hashnode.dev/how-to-setup-kubernetes-cluster-over-cloud-on-ubuntu-os, So let's start...


k8s Setting up on-premise virtual machines OR Cloud AWS Ec2 Instances.

Cluster TypeSpecificationsOS Type
On-Primise VM (1 Master + 2 Worker)4 GRB RAM, 2 Core, 20 GB storage in each machineCentos:7
Cloud AWST2.Medium or upAWS Linux

Step 1: Check your IP or create a network using #nmcli

๐Ÿ’ก
Note: This Step-1 is only for On-Primise VMs, not for cloud AWS instances. and execute this step-1 on all cluster nodes including the master node .
# cat /etc/os-release
# nmcli con show
NAME    UUID                                  TYPE      DEVICE
ens160  4443b0e3-1b19-3b0e-9861-7f0fbf825f6c  ethernet  ens160
VMs Nodes-Private-IPVMs Node-Hostname
192.168.1.1master.localhost.com
192.168.1.2w1.localhost.com
192.168.1.3w2.localhost.com
# hostnamectl set-hostname master.localhost.com && exec bash
# nmcli con mod ens160 ipv4.addresses 192.168.1.1/24 ipv4.gateway 192.168.1.255 ipv4.dns 8.8.8.8
# nmcli con up ens160
๐Ÿ’ก
Note: In the shortcut edit /etc/sysconfig/network-scripts/ifcfg-ensp* file.
# vi /etc/sysconfig/network-scripts/ifcfg-ensp***

IPADDR= ?   
NETMASK= ? 
GATEWAY=? 
DNS1=?
  • Mention the hostname and IP address in the /etc/hosts file
# vi /etc/hosts

192.168.1.1     www.master.localhost.com
192.168.1.2     www.w1.localhost.com
192.168.1.3     www.w2.localhost.com

:wq!

Step 2: Disable swap memory, selinux & firewall

# swapoff -a
# sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# setenforce 0
# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

# systemctl disable firewalld && systemctl stop firewalld

# free -h
# sestatus
# systemctl statuts firewalld

Step 3: Accessing SSH without using a password by sharing our public key.

๐Ÿ’ก
NOTE: Only From the master node, we'll generate a public key and then share it with the other worker nodes.
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.X

Step 4: Install Docker-CE Engine (Execute on all master + slave nodes both)

# yum-config-manager --add-repo https://download.docker.com/linux/centos/dockerce.repo
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum install docker-ce-20* -y
# systemctl start docker && systemctl enable docker

Step 5: To update or add kernel arguments for both infra environments, and enable the bridge for all nodes including master.

# cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# sysctl --system

Step 6: Add Kubernetes Yum Repo from Google

๐Ÿ’ก
Note: Execute on all cluster nodes including the master node
# cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

Step 7: Install kubeadm, kubelet, kubectl packages

# yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

Step 8: Start kubelet services agent

๐Ÿ’ก
Note: Execute on all cluster nodes including master node
# systemctl restart kubelet ; systemctl enable kubelet ; systemctl status kubelet

Step 9: Start Docker Host, containerd services (On all nodes including master)

# systemctl enable docker && systemctl restart docker

Step 10: Initialize kubeadm init command (On Master Node only)

# kubeadm init
๐Ÿ’ก
We will join the Worker node with the Master Node inside the Cluster in the future, so keep the output safe.
# echo "kubeadm join 172.31.4.165:6443 --token kw9d4k.l44fcziztb3br21a \
 --discovery-token-ca-cert-hash
sha256:2952821ac60c0ddb079cc82bd621a72e61aae08a4e700c4f8d457058d7f51dd7" > ~/join.txt

Step 11: Start cluster configuration at MASTER NODE Only with regular user.

$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config

Step 12: Download CNI Weave net (Execute only on Master Node) โ†’ ( Treated as Router)

๐Ÿ’ก
Official weave net link: <Official web page link>
$ https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Step 13: Go to the output of Step 10 and find the <kubeadm join> code and copy it at each worker node.

# kubeadm join 172.31.4.165:6443 --token kw9d4k.l44fcziztb3br21a \
 --discovery-token-ca-cert-hash
sha256:2952821ac60c0ddb079cc82bd621a72e61aae08a4e700c4f8d457058d7f51dd7
๐Ÿ’ก
Tip: If we forget join/hash code then we can run the following command :
# kubeadm token create --print-join-command

Step-14: Check the connected cluster status at the Master Node

$ kubectl get nodes -o wide

NAME                 STATUS    ROLES     AGE       VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION      CONTAINER-RUNTIME
master.localhost.com   Ready     master    1d        1.27.2    192.168.1.1    <none>       CentOS Linux 7 (Core)   4.19.0-19-amd64     docker://20.10.7
w1.localhost.com       Ready     <none>    1d        1.27.2    192.168.1.2    <none>       CentOS Linux 7 (Core)   4.19.0-19-amd64     docker://20.10.7
w2.localhost.com       Ready     <none>    1d        1.27.2    192.168.1.3    <none>       CentOS Linux 7 (Core)   4.19.0-19-amd64     docker://20.10.7
$ kubectl get ns
$ kubectl get pods -n kube-system -o wide
$ kubect api-resources
$ kubectl get po -n kube-system -o wide

Congratulations to all. Thanks you sir

0
Subscribe to my newsletter

Read articles from Rakesh Kumar Jangid directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rakesh Kumar Jangid
Rakesh Kumar Jangid

Let's learn together and serve the society, Make India Proud.