Day 31 Task: Launching your First Kubernetes Cluster with Nginx running

Day 31 Task: Launching your First Kubernetes Cluster with Nginx running

What is MiniKube?

Minikube is a tool that quickly sets up a local Kubernetes cluster on macOS, Linux, and Windows. It can deploy as a VM, a container, or on bare metal.

Minikube is a pared-down version of Kubernetes that gives you all the benefits of Kubernetes with a lot less effort.

This makes it an interesting option for users who are new to containers, and also for projects in the world of edge computing and the Internet of Things.

Features of MIniKube

Supports the latest Kubernetes release (+6 previous minor versions)

Cross-platform (Linux, macOS, Windows)

Deploy as a VM, a container, or on bare-metal

Multiple container runtimes (CRI-O, containers, docker)

Direct API endpoint for blazing-fast image load and build

Advanced features such as LoadBalancer, filesystem mounts, FeatureGates, and network policy

Addons for easily installed Kubernetes applications

Supports common CI environments

Define Pod

  • PODS are the atom of the Kubernetes cluster.

  • In Kubernetes instead of deploying the containers individually, we deploy Pods.

  • Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

  • A pod can have any number of containers running in it.

  • A pod is basically a wrapper around containers running on a node.

Why POD?

The answer is quite simple. For an application user, the container is a kind of Virtual Machine, the end user can log in, install some packages, and stop/start it, so it behaves like a VM to the end user. Also, the containers are designed to run a single process per container. What is an Application requires multiple processes that communicate via IPC or through local files, so in that case they need to run on the same machine(or VM). As mentioned grouping multiple processes in a single container is NOT the best practice, so what the solution is ?? This is where PODs come into the picture. So, POD

Life Cycle of a Pod:-

  • Pending:- This means that the pod has been submitted to the cluster, but the controller hasn't created all its containers yet.

  • Running:- This state means that the pod has been assigned to one of the cluster nodes and at least one of the containers is either running or is in the process of starting up.

  • Succeeded:- This state means that the pod has run, and all of its containers have been terminated with success.

  • Failed:- This state means the pod has run and at least one of the containers has terminated with a non-zero exit code,

  • Unknown:- This means that the state of the pod could not be found. This may be because of the inability of the controller to connect with the node that the pod was assigned to.

Types of Installation of K8s

  1. Mini Kube (Docker Inside Docker DIND) → least use in Prod → Easiest

  2. Kubeadm :→ Baremetal (open-source tool) → Used in Prod → Intermediate

  3. Managed K8s Cluster

    AWS → EKS (Elastic Cloud Kubernetes)

    Azure → AKS (Azure Kubernetes Service)

    GCP → GKE (Google Kubernetes Engine)

  4. KIND (Kubernetes in Docker)

Task 01-Install MiniKube on Local

Step 1:- Firstly we need to create an EC2 instance and while launching the EC2 machine need to select t2.medium. Because if you need to install Kubernetes in any EC2 machine, the configuration should be having 2 CPUs, 4GB of free memory, and 20 GB of free disk space.

Step 2:- Then need to Install Docker in your system by running the below commands

sudo apt update -y
 sudo apt install docker.io -y

 sudo systemctl start docker
 sudo systemctl enable docker
 sudo systemctl status docker

Step 3:- Need to Add the user to the docker group by the below command

sudo usermod -aG docker $USER && newgrp docker

Step 4:- Now need to install Minikube in the system.

 curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

 sudo install minikube-linux-amd64 /usr/local/bin/minikube

Step 5:- If we need to interact then we need to install CLI as a kubelet

 sudo snap install kubectl --classic

Step 6:- Now we can start Minikube

minikube start --driver=docker

Task 2: Create your first pod on Kubernetes through Minikube.

Step 1:- To create a pod, we have to write a YAML file which is a.k.a Manifest file. So to create a pod for NGINX we have to pass the values & attributes in key-value format.

Write the below script in pod. yaml

 apiVersion: v1
 kind: Pod
 metadata:
   name: nginx
 spec:
   containers:
   - name: nginx
     image: nginx:1.14.2
     ports:
     - containerPort: 80

Step 2:- Now we need to run the command to create the pod

kubectl apply -f pod.yml

Step 3:- Check the pod's status by kubectl get pods, you can see an NGINX pod is created successfully by its status

 kubectl get pods

Step 4:- To check if nginx is running locally or not, do we have to ssh the minikube and go inside the minikube cluster? Then curl the IP address of the pod.

 #Get the IP
 kubectl get pods -o wide

 # SSH into minikube
 minikube ssh

 # Curl the IP address to access the NGINX
 curl http://<IP-Addr>

Task 3: Create NGINX pod on K8s through Kubeadm

Step 1:- Firstly we need to launch 2 EC2 instances and while launching the EC2 machine need to select t2.medium. Because if you need to install Kubernetes in any EC2 machine, the configuration should be having 2 CPUs, 4GB of free memory, and 20 GB of free disk space.

Step 2:- Then need to Install Docker in both machines by running the below commands

sudo apt update -y
sudo apt install docker.io -y

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker

Step 3:- Now need to Install Kebeadm on both master and node. and update both systems

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF

 sudo apt update -y

Step 4:- need to Install Kubeadm, Kubectl and Kebelet in both Master and Node.

 sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y

Step 5:- Now need to Connect Master with Node and Initialized Kubeadm by running the below command

sudo su
kubeadm init

Step 6:- Setup the kubeconfig for the current user

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

Step 7:- Finish the Master Setup using the following Command:

 kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Step 8:- Now we need to go to the worker and run the below command.

sudo su
kubeadm reset pre-flight checks

Note: Docker and Kubernetes should be installed on the worker node also.

Step 9:- Paste the Join command on the worker node and append --v=5 at the end

Step 10:- Now from the master node run the below command we can see both nodes

kubectl get nodes

Step 11:- Need to run the below image

kubectl run nginx --image=nginx --restart=Never

Step 12:- Need to check the docker image

docker ps

Step 13:- Get the details of the pod

kubectl get pods -o wide