Docker & Kubernetes Setup
Tags: linux, docker, kubernetes
Published: 2023-03-02 02:06:39

## Introduction

> **Docker** is a platform that allows developers to create, deploy, and run applications in containers. Docker Compose simplifies managing multi-container applications by defining and running multiple containers as a single application with dependencies and configurations. Compose plugins extend the functionality of Docker Compose, allowing developers to add new commands, modify behavior, or integrate with external services


![Docker Virtualization](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnx6nueoygyapc2vbr38.png)



> **Kubernetes** is an open-source platform for container orchestration and management that automates deployment, scaling, and management of containerized applications. It is often used in conjunction with Docker to manage containerized applications. 

✨  Kubernetes provides a framework for automating deployment, scaling, and operations of application containers across clusters of hosts, while Docker provides a standardized way to package and distribute those containers

👉 I will be installing Docker in 3 CentOS7 Virtual Machines in my VMWare workstation

> 192.168.1.10 👉 Master
192.168.1.20 👉 Node-1
192.168.1.30 👉 Node-2

Before I begin, I will share official documentations available on installing the docker engine and compose

- https://docs.docker.com/engine/install/centos/
- https://docs.docker.com/compose/install/linux/

---

## Installing Docker

In all systems,

```bash
yum -y install yum-utils

# Saving the docker repository to install docker from it
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
```

Checking docker files

```bash
yum list docker-ce --showduplicates | sort -r
```
👉 This should show us different versions of docker available. I will be proceeding with version 18.x


Installing the Docker Engine

```bash
yum -y install docker-ce-18.09.8 docker-ce-cli-18.09.8 containerd.io docker-compose-plugin
```

Checking the docker version

```bash
rpm -qa | grep docker
docker-ce-cli-18.09.8-3.el7.x86_64
docker-compose-plugin-2.6.0-3.el7.x86_64
docker-ce-18.09.8-3.el7.x86_64
```

Enabling and starting the docker service

```bash
systemctl start docker
systemctl enable docker
```

We can check the Docker version

```bash
docker version
```

👉 Working only from the 'Master' Linux

Installing Docker Compose

```bash
curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose version
Docker Compose version v2.2.3
```

---

## Installing Kubernetes

> 👉 I have VSCode and Kubernetes installed in my Host PC to write manifest files for Kubernetes with ease

> ✨ From the 'Master' VM

Configuring SWAP memory to be deactivated

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

Creating the `daemon.json` file

```bash
vi /etc/docker/daemon.json

{
    "exec-opts": ["native.cgroupdriver=systemd"]
}
```

Reloading the daemon and restarting docker

```bash
systemctl daemon-reload
systemctl restart docker
```

👉 This will change the Docker cgroup drive

Adding the Kubernetes Local Repository

```bash
vi /etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

# Installing Kuber
yum install -y kubelet-1.19.16-0.x86_64 kubectl-1.19.16-0.x86_64 kubeadm-1.19.16-0.x86_64 
```

Confirming kubernetes installation

```bash
rpm -qa | grep kube
kubelet-1.19.16-0.x86_64
kubectl-1.19.16-0.x86_64
kubernetes-cni-0.8.7-0.x86_64
kubeadm-1.19.16-0.x86_64
```

Enabling ports used

```bash
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2379/tcp
firewall-cmd --permanent --add-port=2380/tcp
firewall-cmd --permanent --add-port=6443/tcp
firewall-cmd --permanent --add-port=8472/udp
firewall-cmd --permanent --add-port=9099/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=10251/tcp
firewall-cmd --permanent --add-port=10252/tcp
firewall-cmd --permanent --add-port=10254/tcp
firewall-cmd --permanent --add-port=10255/tcp
firewall-cmd --permanent --add-port=30000-32767/tcp
firewall-cmd --permanent --add-port=30000-32767/udp
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
```

Configuring the Kuber cluster on our Master

```bash
kubeadm init --apiserver-advertise-address=192.168.1.10 --pod-network-cidr=10.244.0.0/16

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.1.10:6443 --token y20gfe.s5kx71a4nh0gzhsw \
    --discovery-token-ca-cert-hash sha256:7c46fa0f4ce64ea4642183250afb3305ca17a89867ed877e2eacdf2a835095b3
```
👉 The final line says to use the "join" command when adding nodes to the cluster.

👉 I specified the Master Node's IP address with the "apiserver-advertise-address" command and the network area for Pod usage with the "pod-network-cidr" command

Moving the authentication data to use `kubectl` under `root` user's Home directory `.kube`

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

Installing the Network Plugin to be used in Kuber cluster (Flannel)

```bash
curl -O -L https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
```

👉 As I am using a VM with NAT connection, I needed to add the NIC device name to the `flannel.yml` file

```bash
vi kube-flannel.yml

args:
        - --ip-masq
        - --kube-subnet-mgr
        - --iface=ens32
```

Now, we just need to apply the flannel plugin

```bash
kubectl apply -f kube-flannel.yml
systemctl restart kubelet
```

> ✨ From Both Node-1 and Node-2 VMs

👉 Same steps as Master node till firewall settings in both nodes

After firewall settings are done, we will use the `join` command that we got from the Master node

```bash
kubeadm join 192.168.1.10:6443 --token 172vji.r0u77jcmcnccm6no \
    --discovery-token-ca-cert-hash sha256:72b9648c647f724ab52471847cb06c47b23097375f2e67633b745fc69db16e8d 
```

👉 This will add both nodes to the Kuber cluster created by the Master

```bash
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
```

Upon successful joining,

```bash
kubectl get nodes
NAME     STATUS   ROLES    AGE    VERSION
master   Ready    master   107m   v1.19.16
node-1   Ready    <none>   91s    v1.19.16
node-2   Ready    <none>   48s    v1.19.16
```

We can also check `pods` 

```bash
kubectl get pods --all-namespaces
```

👉 A pod represents a single instance of a running process in the cluster

---

✍ Today I walked through installing Docker and Kubernetes in Linux systems and joined 2 working nodes to the Master Kuber cluster