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

## **🙏 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.
    

```plaintext
git clone https://github.com/hassanb111/django-notes-app.git
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701711486428/c26adecd-fb20-4702-ad67-02452f04c9df.png?auto=compress,format&format=webp align="left")

1. Build the Docker image
    

```plaintext
docker build -t django-todo-cicd .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701712080491/eea26513-171d-4aff-8012-9f7b9be715ed.png?auto=compress,format&format=webp align="left")

1. Run the Docker container
    

```plaintext
docker run -d -p 8000:8000 django-todo-cicd:latest
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701712290215/8d23d849-1ca8-436c-99b6-f230b04b2755.png?auto=compress,format&format=webp align="left")

1. Verify that the application is running
    

```plaintext
docker ps
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701712436298/0e75791e-1caa-4961-b143-d25a2f44dd0b.png?auto=compress,format&format=webp align="left")

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.
    

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701713345560/94f09f1d-672c-4f3d-aa87-1fc283234af5.png?auto=compress,format&format=webp align="left")

1. Kill the container that we created previously
    

```plaintext
 docker kill 417159cf5930
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701714135405/97fc06ca-9802-4313-b991-ed7cca5a007f.png?auto=compress,format&format=webp align="left")

1. Create a new pod.yaml file. Copy the sample pod.yaml configuration from the official Kubernetes documentation, then make the changes as required.
    

```plaintext
apiVersion: v1
kind: Pod
metadata:
  name: todo-pod
spec:
  containers:
  - name: todo-pod
    image: sutish/django-todo-cicd:latest
    ports:
    - containerPort: 8000
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701714293258/2069789d-607b-4462-a2da-2481ebb90dbf.png?auto=compress,format&format=webp align="left")

1. Apply the pod.yaml file
    

```plaintext
kubectl apply -f pod.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701714424149/e5530676-5957-4bf2-a364-04c24217c7b7.png?auto=compress,format&format=webp align="left")

1. Verify the pod is running
    

```plaintext
kubectl get pods
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701714517781/aeb86c8a-0306-45f1-9b18-bc0727d3356d.png?auto=compress,format&format=webp align="left")

1. Create a deployment.yaml file. Copy the sample deployment.yaml configuration from the official Kubernetes documentation, then make the changes as required.
    

```plaintext
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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701714966236/5aae0bde-c71e-4d9d-a3cf-6405675e1c1a.png?auto=compress,format&format=webp align="left")

1. Apply the deployment.yaml file
    

```plaintext
kubectl apply -f deployment.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701715103158/56b3f128-4cb4-4fcf-95cb-5cc3f433653b.png?auto=compress,format&format=webp align="left")

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

1. Check the status of the deployment and its replicas
    

```plaintext
kubectl get deployments
kubectl get pods
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701715257490/d41c14fe-cb62-4ae9-86fe-dfbff6be5726.png?auto=compress,format&format=webp align="left")

1. To demonstrate the auto-healing feature, kill one of the running pods
    

```plaintext
kubectl delete pod todo-deployment-6d86879976-5859r
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701715499418/e2f1cb3d-127f-475c-8b58-d8010600cd65.png?auto=compress,format&format=webp align="left")

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
    

```plaintext
kubectl scale deployment todo-deployment --replicas=4
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701715684542/63da80a1-ba35-4fe9-b653-da13f37f6438.png?auto=compress,format&format=webp align="left")
