Day 32-  Elevate Your Kubernetes Skills: Launching a Todo-App with Deployment  (Dec 28, 2023)

Day 32- Elevate Your Kubernetes Skills: Launching a Todo-App with Deployment (Dec 28, 2023)

ยท

4 min read

๐Ÿ™ Introduction:

In this blog, we will explore deployment in Kubernetes and how it works.

What is Deployment in Kubernetes?

In Kubernetes, a Deployment is an object that provides declarative updates to manage application deployments. It defines the desired state of the application and handles the creation, scaling, and updating of the application's replicas. Deployments are a key component of managing containerized applications in a Kubernetes cluster.

A Deployment ensures that the desired number of replicas of an application is running and manages the lifecycle of those replicas. It allows for rolling updates, rollback to previous versions, and scaling of the application replicas based on the defined configuration.

Deployments are often used to manage stateless applications, where each instance of the application is independent of others. They are particularly useful in scenarios where high availability, fault tolerance, and easy scaling are required.

Benefits of Using Deployments in Kubernetes

Using Deployments in Kubernetes offers several benefits:

  1. Ease of Management: Deployments provide a declarative way to define and manage the state of applications. You can specify the desired state of your application, and Kubernetes takes care of creating, updating, and scaling the replicas to match that state.

  2. Rolling Updates and Rollbacks: Deployments support rolling updates, allowing you to update your application gradually without downtime. If an update introduces issues, you can easily roll back to a previous version of the application.

  3. Scalability: Deployments enable easy scaling of your application by simply adjusting the number of replicas. This allows you to handle increased traffic or load by adding more instances of the application.

  4. High Availability: Deployments ensure that the desired number of replicas are always running. If a replica fails, Kubernetes automatically replaces it to maintain the desired state and ensure high availability.

  5. Self-Healing: Kubernetes continuously monitors the health of application replicas managed by a Deployment. If a replica becomes unhealthy or fails, Kubernetes automatically replaces it with a new one, ensuring the overall health of the application.

๐Ÿ’ผTask: 1

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

Here are the detailed steps to create a Deployment file to deploy a sample todo-app on Kubernetes with Auto-healing and Auto-scaling features:

  1. Clone the django-todo-cicd repository from Github.
git clone https://github.com/hassanb111/django-notes-app.git

  1. Build the Docker image
docker build -t django-todo-cicd .

  1. Run the Docker container
docker run -d -p 8000:8000 django-todo-cicd:latest

  1. Verify that the application is running
docker ps

  1. Create a new directory called k8s in the same directory where the django-todo-cicd repository is located.

  2. Push the Docker image to your Dockerhub account.

docker tag django-todo-cicd:latest sutish/django-todo-cicd:latest
docker login
docker push sutish/django-todo-cicd:latest

  1. Kill the container that we created previously
 docker kill 417159cf5930

  1. Create a new pod.yaml file. Copy the sample pod.yaml configuration from the official Kubernetes documentation, then make the changes as required.
apiVersion: v1
kind: Pod
metadata:
  name: todo-pod
spec:
  containers:
  - name: todo-pod
    image: sutish/django-todo-cicd:latest
    ports:
    - containerPort: 8000

  1. Apply the pod.yaml file
kubectl apply -f pod.yaml

  1. Verify the pod is running
kubectl get pods

  1. Create a deployment.yaml file. Copy the sample deployment.yaml configuration from the official Kubernetes documentation, then make the changes as required.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-deployment
  labels:
    app: todo-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: todo-app
  template:
    metadata:
      labels:
        app: todo-app
    spec:
      containers:
      - name: todo-app
        image: sutish/django-todo-cicd:latest
        ports:
        - containerPort: 8000

  1. Apply the deployment.yaml file
kubectl apply -f deployment.yaml

This will create the deployment and start the specified number of replicas.

  1. Check the status of the deployment and its replicas
kubectl get deployments
kubectl get pods

  1. To demonstrate the auto-healing feature, kill one of the running pods
kubectl delete pod todo-deployment-6d86879976-5859r

Kubernetes will automatically create a new pod to replace the deleted one.

  1. To demonstrate the auto-scaling feature, increase the number of replicas in the deployment
kubectl scale deployment todo-deployment --replicas=4

ย