Docker & Kubernetes Setup

waji97

Waji

Posted on March 2, 2023

Docker & Kubernetes Setup

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

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


Installing Docker

In all systems,

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
Enter fullscreen mode Exit fullscreen mode

Checking docker files

yum list docker-ce --showduplicates | sort -r
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ This should show us different versions of docker available. I will be proceeding with version 18.x

Installing the Docker Engine

yum -y install docker-ce-18.09.8 docker-ce-cli-18.09.8 containerd.io docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

Checking the docker version

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
Enter fullscreen mode Exit fullscreen mode

Enabling and starting the docker service

systemctl start docker
systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

We can check the Docker version

docker version
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ Working only from the 'Master' Linux

Installing Docker Compose

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
Enter fullscreen mode Exit fullscreen mode

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

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

Creating the daemon.json file

vi /etc/docker/daemon.json

{
    "exec-opts": ["native.cgroupdriver=systemd"]
}
Enter fullscreen mode Exit fullscreen mode

Reloading the daemon and restarting docker

systemctl daemon-reload
systemctl restart docker
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ This will change the Docker cgroup drive

Adding the Kubernetes Local Repository

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 
Enter fullscreen mode Exit fullscreen mode

Confirming kubernetes installation

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
Enter fullscreen mode Exit fullscreen mode

Enabling ports used

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
Enter fullscreen mode Exit fullscreen mode

Configuring the Kuber cluster on our Master

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
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ 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

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

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

curl -O -L https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ As I am using a VM with NAT connection, I needed to add the NIC device name to the flannel.yml file

vi kube-flannel.yml

args:
        - --ip-masq
        - --kube-subnet-mgr
        - --iface=ens32
Enter fullscreen mode Exit fullscreen mode

Now, we just need to apply the flannel plugin

kubectl apply -f kube-flannel.yml
systemctl restart kubelet
Enter fullscreen mode Exit fullscreen mode

āœØ 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

kubeadm join 192.168.1.10:6443 --token 172vji.r0u77jcmcnccm6no \
    --discovery-token-ca-cert-hash sha256:72b9648c647f724ab52471847cb06c47b23097375f2e67633b745fc69db16e8d 
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ This will add both nodes to the Kuber cluster created by the Master

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.
Enter fullscreen mode Exit fullscreen mode

Upon successful joining,

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
Enter fullscreen mode Exit fullscreen mode

We can also check pods

kubectl get pods --all-namespaces
Enter fullscreen mode Exit fullscreen mode

šŸ‘‰ 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

šŸ’– šŸ’Ŗ šŸ™… šŸš©
waji97
Waji

Posted on March 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related