Day 32 Task: Launching your Kubernetes Cluster with Deployment

Day 32 Task: Launching your Kubernetes Cluster with Deployment

What is Deployment in k8s

A Deployment provides a configuration for updates for Pods and ReplicaSets.

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new replicas for scaling, or to remove existing Deployments and adopt all their resources with new Deployments.

Task-1:

Create one Deployment file to deploy a sample todo-app on K8s using "Auto-healing" and "Auto-Scaling" feature

  • add a deployment.yml file (sample is kept in the folder for your reference)

  • apply the deployment to your k8s (minikube) cluster by command kubectl apply -f deployment.yml

Deploy a Sample todo-app on Kubernetes

Let’s apply our Deployment configuration:

  1. Clone the repository for your application:

      git clone https://github.com/anand-raval-git/django-todo.git
    
  2. Create a Docker image from your Dockerfile:

     # Dockerfile
     FROM python:3.11
    
     WORKDIR /data
    
     RUN pip install django==3.2
    
     COPY . .
    
     RUN python manage.py migrate
    
     EXPOSE 8000
    
     CMD ["python","manage.py","runserver","0.0.0.0:8000"]
    
     docker build -t anandraval12/django-todo-app .
    
  3. Push the Docker image to DockerHub:

     docker login
     docker push anandraval12/django-todo-app:latest
    
  4. Create the Deployment YAML file

    Create deployment.yml file for deploying a todo-app:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: django-todo-deployment
       namespace: default
     spec:
       replicas: 1
       selector:
         matchLabels:
           app: django-todo
       template:
         metadata:
           labels:
             app: django-todo
         spec:
           containers:
           - name: django-todo
             image: anandraval12/django-todo-app:latest
             ports:
             - containerPort: 8000
    
  5. Apply the Deployment to your Kubernetes (Minikube) cluster:

      kubectl apply -f deployment.yml
    

  6. Verify the Pods are running:

      kubectl get pods
    

  7. Test the application on the worker node:

      docker ps
      sudo docker exec -it <docker-container> bash
      curl -L http://127.0.0.1:8000
    
  8. Test Auto-healing by deleting a Pod and ensuring a new one is created:

     kubectl delete pod <pod-name>
    
  9. Delete the Deployment when done:

     kubectl delete -f deployment.yml
    

Thankyou for reading !!!!!!