Launching your First Kubernetes Cluster with Nginx running

Launching your First Kubernetes Cluster with Nginx running

What is minikube?

Minikube is a lightweight and open-source tool that enables developers to run a single-node Kubernetes cluster on their local machine. It facilitates local testing and development of applications on Kubernetes without the need for a full-scale cluster setup.

Features of minikube

  1. Local Kubernetes Cluster: It allows developers to set up a single-node Kubernetes cluster on their local machine, providing an environment for testing and developing applications.

  2. Cross-platform: Minikube supports various operating systems like Windows, macOS, and Linux, making it accessible to a wide range of developers.

  3. Cluster Management: Developers can easily start, stop, and delete clusters, enabling quick iterations during the development process.

  4. Add-ons and Plugins: Minikube supports various add-ons and plugins, allowing users to customize the cluster's behavior and add additional functionality as needed.

  5. Easy Deployment: It simplifies the deployment of Kubernetes workloads, making it an ideal choice for local development, learning, and experimentation.

  6. Docker Integration: Minikube seamlessly integrates with Docker, enabling developers to use their existing Docker images with Kubernetes.

  7. Resource Constraints: It allows users to specify resource constraints for the virtual machine running the Kubernetes cluster, making it suitable for machines with limited resources.

  8. Kubernetes Version Management: Developers can choose specific versions of Kubernetes to run on Minikube, ensuring compatibility with their application's target environment.

Define Pod

A Pod is the smallest deployable unit in Kubernetes, representing a single instance of a running process. It can contain one or more tightly coupled containers that share the same network namespace and can communicate with each other using the localhost. Pods are scheduled to run on nodes in the cluster and can be horizontally scaled up or down. They provide an isolated environment for applications and include shared resources like storage volumes and IP addresses. Pods are designed to be ephemeral and can be easily replaced or rescheduled in case of failures.

Task 1: Install Minikube on your local

Create a new VM instance having 2 CPUs, 4GB of free memory, 20 GB of free disk space.

When creating a new EC2 instance select t2.medium.

Install Docker in your system.

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

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

 sudo usermod -aG docker $USER && newgrp docker

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

And then install Kubelet.

 sudo snap install kubectl --classic

Start Minikube as per the image and check Minikube status

 minikube start --driver=docker

Check if minikube cluster has been set up successfully or not by checking pods or namespace status.

 kubectl get pods

 kubectl get namespace

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

  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.

    In the manifest file, we are passing values:

    apiVersion → Kubernetes Version

    Kind → Type of deployment

    metadata → More Details about pod

    container → Details of containers in object

    containerPort → The port where the pod will deploy

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

Run the kubectl command to create a pod.

 kubectl apply -f pod.yml

Check the pod's status by kubectl get pods, you can see a NGINX pod is created successfully by it's status

 kubectl get pods

Run the kubectl get pods -o wide command to get more detailed information about the pod-like IP, node, age of node, and status.

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>

Task 3: Create NGINX pod on K8s through Kubeadm

Create 2 VM instances for Master and Node.

Install Docker on both Master & Node

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

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

Install Kebeadm on both master and node.

Again update the system.

 sudo apt update -y

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

Connect Master with Node:

Initialized Kubeadm:

Run the following command only on Master:

 sudo su
 kubeadm init

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

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

Now create a token to join the Master & Node connection

 kubeadm token create --print-join-command

We will get nodes for master

After that open port 6443 in master

Then on the Worker Node reset the checks so it can't assign as Master.

sudo su
kubeadm reset pre-flight checks

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

Verify by running the command in Master:

kubectl gets nodes

Kubeadm Installation Step

Create the Nginx Pod

By default, the kubectl run command creates a deployment and a replica set along with the pod. If you only want to create a pod without creating a deployment or replica set, you can use the --restart=Never flag.

But if you pass --restart=Always, if your pod is deleted or having an issue, then a new pod will be replaced immediately.

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

Now we can see the docker container in the worker node

 docker ps

To check if the pods are running or not

 kubectl get pods

Get the details of the pod

 kubectl get pods -o wide

To delete a pod in

 # kubectl delete pod  <pod-name>
 kubectl delete pod nginx

For another Jenkins project.

Follow me on LinkedIn to see interesting posts like this : )

linkedin.com/in/prabir-kumar-mahatha-6a0025..

Visit my git hub profile: github.com/PrabirKumarMahatha