# Kubernetes K8s Setup on CentOS:7 with easy steps.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Taking inspiration from and dedicated to our training guru,<mark> Shri Maninder sir.</mark></div>
</div>

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): [`https://rakeshkumarjangid.hashnode.dev/how-to-setup-kubernetes-cluster-over-cloud-on-ubuntu-os`](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 Type | Specifications | OS Type |
| --- | --- | --- |
| On-Primise VM (1 Master + 2 Worker) | 4 GRB RAM, 2 Core, 20 GB storage in each machine | Centos:7 |
| Cloud AWS | T2.Medium or up | AWS Linux |

---

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

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><mark>Note: This Step-1 is only for On-Primise VMs,</mark> not for cloud AWS instances. and execute this step-1 on all cluster nodes including the master node .</div>
</div>

```plaintext
# cat /etc/os-release
# nmcli con show
NAME    UUID                                  TYPE      DEVICE
ens160  4443b0e3-1b19-3b0e-9861-7f0fbf825f6c  ethernet  ens160
```

| ***VMs Nodes-Private-IP*** | VMs Node-Hostname |
| --- | --- |
| 192.168.1.1 | master.localhost.com |
| 192.168.1.2 | w1.localhost.com |
| 192.168.1.3 | w2.localhost.com |

```plaintext
# 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
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Note: In the shortcut edit <code>/etc/sysconfig/network-scripts/ifcfg-ensp***</code> file.</div>
</div>

```plaintext
# vi /etc/sysconfig/network-scripts/ifcfg-ensp***

IPADDR= ?   
NETMASK= ? 
GATEWAY=? 
DNS1=?
```

* Mention the hostname and IP address in the `/etc/hosts` file
    

```plaintext
# 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

```plaintext
# 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.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">NOTE: Only From the master node, we'll generate a public key and then share it with the other worker nodes.</div>
</div>

```plaintext
# 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)

```plaintext
# 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.

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

```plaintext
# sysctl --system
```

---

### Step 6: Add Kubernetes Yum Repo from Google

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Note: Execute on all cluster nodes including the master node</div>
</div>

```plaintext
# 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

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

---

### Step 8: Start kubelet services agent

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Note: Execute on all cluster nodes including master node</div>
</div>

```plaintext
# systemctl restart kubelet ; systemctl enable kubelet ; systemctl status kubelet
```

---

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

```plaintext
# systemctl enable docker && systemctl restart docker
```

---

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

```plaintext
# kubeadm init
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">We will join the Worker node with the Master Node inside the Cluster in the future, so keep the output safe.</div>
</div>

```plaintext
# 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.

```plaintext
$ 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)

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Official weave net link: <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.weave.works/docs/net/latest/kubernetes/kube-addon/" style="pointer-events: none">&lt;Official web page link&gt;</a></div>
</div>

```plaintext
$ 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 &lt;`kubeadm join>` code and copy it at each worker node.

```plaintext
# kubeadm join 172.31.4.165:6443 --token kw9d4k.l44fcziztb3br21a \
 --discovery-token-ca-cert-hash
sha256:2952821ac60c0ddb079cc82bd621a72e61aae08a4e700c4f8d457058d7f51dd7
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Tip: If we forget join/hash code then we can run the following command :</div>
</div>

```plaintext
# kubeadm token create --print-join-command
```

---

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

```plaintext
$ 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
```

```plaintext
$ 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
