Automated CI/CD Pipeline for Django and React Applications.

Automated CI/CD Pipeline for Django and React Applications.

Project Description -

Build Kubernetes Cluster on AWS from Scratch with Minikube; Setup and Managed Docker Containers for Django and React Applications into Kubernetes Pods, Managed Deployment, Replication, Auto Healing, and Auto Scaling for Kubernetes Cluster; Managed network and Services with Host IP allocation.

Step 1: Build Kubernetes Cluster on AWS from Scratch with Minikube

  • Head to AWS EC2

  • Launch an Instance and Create a Server Name

      'Create a Server Name of your wish' I have used 'kubernetes-tut' as a server name
    
  • Select the Applications and OS Images(Amazon Machine Image). Take any OS which you prefer. I have used 'ubuntu'

  • Now change the instance type to t2.medium coz we require 2 CPUs for the processing. (This is not included in Free Tier)

  • Then add a Key Pair

      TWS-Key
    
  • Finally, Click on Create Instance

  • Select the instance and Click on Connect

  • Copy the SSH Client

  • Head to your Terminal and type Sudo and Paste the Link

  • Instal Docker

https://docs.docker.com/get-docker/
  • Install Minukube
https://minikube.sigs.k8s.io/docs/start/
  • After installing type --> sudo usermod -aG docker $USER && newgrp docker

  • Then type --> minikube start --driver=docker

  • Now to communicate with the master and the node you need to install the kubctl tool

sudo snap install kubectl

Step 2: Setup and Managed Docker Containers for Django and React Applications into Kubernetes Pods

  • Create an SSH for the instance that you have created in Amazon EC2

  • Clone the Django todo app

   git clone
  • Go to the docker file directory and then type
docker build -t <dockerhub_id>/<image_name>:latest
  • Now create a container of the image which you have just created
docker run -d -p 8000:8000 <dockerhub_id>/<image_name>:latest

Here you are doing "port-binding" put the 'exposed' port number

  • To check whether it is working or not. Copy the IP address from Amaxon EC2, and type the command :
curl -L http://<ip_address>
  • Now, we will create a Kubernetes Pods :

  • Now we will create a new folder. I have used k8s

mkdir k8s
cd k8s/
ls
  • Now we will write a pod file.

  • Generally, when we create files we pull image from Docker Hub.

docker push <dockerhub_id>/<image_name>
  • Here your created image went to DockerHub and when the Kubernetes pod will be created it will get automatically get pulled.

  • Creating Pod

vim pod.yaml

-- Then copy this file and do the necessary changes.

apiVersion: v1

kind: Pod

metadata:

  name: <give a name as you like>

spec:

  containers:

  - name: <container name as you like>

    image: <dockerhub_id>/<image_name>:latest

    ports:

    - containerPort: 8000


 (esc :wq to exit)
  • Now kill the ports after writing 'docker ps' from the SSH Terminal
docker kill <FIRST_PORTS_NAME_AFTER_RUNNING_ABOVE_COMMAND>
  • Command to create the pod
kubectl apply -f pod.yaml
  • Congrats Step 2 Done

Step 3: Managed Deployment, Replication, Auto Healing, and Auto Scaling for Kubernetes Cluster.

  • Create a deployment <filename>.yaml

  • Now copy the piece of code to the deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: todo-deployment

  labels:

    app: todo-app

spec:

  replicas: 3

  selector:

    matchLabels:

      app: todo-app 

  template:

    metadata:

      labels:

        app: todo-app 

    spec:

      containers:

      - name: todo-app 

        image: <imagename>

        ports:

        - containerPort: 8000
  • After saving the file
kubectl apply -f deployment.yaml
  • To check everything is working fine or not
kubectl get deployments
  • Now you will see the replicas and no of replicas is equal to the number of pods

  • The number of replicas are present there for the Auto-Scaling Process

  • To see pods

kubectl get pods
  • Now to delete pods
kubectl delete pods <pod_name>
  • Now try to delete any deployment pod, and recheck to see. Here you able to see the deployment pod once again as this process is Auto-Healing.

Managed network and Services with Host IP allocation.

  • To do this you face a problem follow this steps to understand:

  • Command: To see IP

kubectl get pods -o wide
curl -L http://<IP>
  • But here you will not able to see the pod because there is no route. This pod is running inside Minikube Kubernetes Cluster

  • Now you need a service.yaml file to manage network and Services.

vim service.yaml
apiVersion: v1
kind: Service
metadata:
  name: todo-service
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 8000
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30005
  • Now you need to apply the file.
kubectl apply -f service.yaml
  • To check
kubectl get svc
  • You will not get the external IP therefore need to go to the minikube service
minikube service todo-service --url
  • Now
curl -L <http_link>
  • Now we will give the IP name

  • Here we will get all the IPs

sudo vim /etc/hosts
  • Now enter the previous IP which you want to customize

  • Now bind xxx.xxx.xx.x -NOW

curl -L <any name.com>:30005

#Achievement

  • Reduce Downtime by 75% in Production Environments.

  • Coz, If you delete the pod also, It will be Up Again. Therefore you have reduced downtime. 25% were being kept for scheduled maintenance in the website down.

-Congratulation🥳 and Happy Coding👩‍💻🧑‍💻!!!