Launching your First Kubernetes Cluster with Nginx running

Launching your First Kubernetes Cluster with Nginx running

ยท

6 min read

Ways to Install Kubernetes

There are different ways to install Kubernetes like:

  • Kubernetes using kubeadm

  • Kubernetes using Minikube

  • Kubernetes uses managed Platforms like EKS(AWS), AKS(Azure), and GKE(GCP).

In this blog, I will let you know how to install a K8s cluster using Minikube.

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

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

  2. Cross-platform (Linux, macOS, Windows)

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

  4. Multiple container runtimes (CRI-O, Containerd, Docker)

  5. Direct API endpoint for blazing-fast image load and build

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

  7. Addons for easily installed Kubernetes applications

  8. 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

  • POD can have one or more containers running inside it (It doesn't mean that you have to always run multiple containers inside the POD). POD allows you to run closely related processes together and provide them same environment.

  • Containers in a pod have shared volumes, Linux namespaces, and cgroups. Each pod has a unique IP address and the port space is shared by all the containers in that pod. This means that different containers inside a pod can communicate with each other using their corresponding ports on localhost

img

  • POD always runs on a single worker node, it never spans across multiple worker nodes.

  • Containers running inside the POD share the same Network Namespace (Linux) i.e. they share the same IP address, same loopback NIC and PORT space.

  • All PODs in a K8s cluster (irrespective of the worker nodes ), reside in a single flat shared Network space, i.e. every Pod can access every Pod with its IP address, no NATING is required, it is just like they are connected over the same LAN.

apiVersion: v1
kind: Pod
metadata:
  name: pod-name
spec:
  containers:
  - name: container1
    image: image1
  - name: container2
    image: image2
  • apiVersion:- Version of the Kubernetes API we are going to use.

  • kind:- The kind of Kubernetes object we are trying to create, which is a Pod in this case.

  • metadata:- Metadata or information that uniquely identifies the object we're creating.

  • spec:- Specification of our pod, such as container name, image name, volumes, and resource requests.

Note: apiVersion, kind, and metadata apply to all types of Kubernetes objects and are required fields. spec is also a required field; however, its layout is different for different types of objects.

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.

Task 1: Install Minikube on your 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.

 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.yaml

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 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>
ย