Deployed a Todo-app on Kubernetes
Tags: devops, kubernetes, docker, beginners
Published: 2023-09-10 19:28:58

Kubernetes is a powerful container orchestration platform that allows you to deploy, scale, and manage containerized applications with ease. In this blog, we will walk you through the steps to deploy a Todo app on Kubernetes. We will start by cloning the source code, containerize the application using Docker, push the Docker image to DockerHub, write Kubernetes manifest files, and finally deploy and expose the app on Kubernetes. Let's get started!
{% embed https://github.com/SwapnilM24/django-todo-app.git %}

TABLE OF CONTENTS
`Step 1: Clone the source code
Step 2: Containerize the Application using Docker
Step 3: Building Docker-Image
Step 4: Push the Image To DockerHub
Step 5: Write a Kubernetes Manifest File
Write Deployment.yml file
Write Service.yml file
Step 6: Deploy the app to Kubernetes and create a Service for it
Step 7: Expose the app
You have successfully Deployed a todo-app on Kubernetes.`


**Prerequisite for Deployment of a Todo-app on Kubernetes**

Before we begin with the Project, we need to make sure we have the following prerequisites installed:

EC2 ( AMI- Ubuntu, Type- t2.micro )

EC2 ( AMI- Ubuntu, Type- t2.medium )

Docker

Minikube

kubectl

You can Install all this by doing the below steps one by one. and these steps are for Ubuntu AMI.

**NOTE**: In the t2.micro instance, only step one, Docker Installation, should be completed; in the t2.medium instance, both stages should be completed.


```
# Steps:-

# For Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

# For Minikube & Kubectl
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube 

sudo snap install kubectl --classic
minikube start --driver=docker

```
**Step 1: Clone the Source Code**
To begin, you'll need the source code of your Todo app. For this example, we'll assume you have a Git repository containing your app. Clone the repository to your local machine using the following command:

```
Copy code
git clone <repository_url>
cd <app_directory>
```
**Step 2: Containerize the Application using Docker**
Kubernetes works with containerized applications, so we need to containerize our Todo app using Docker. Create a Dockerfile in the root of your project directory with the following content:

```
  FROM python:3

  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"]

```
**Step 3: Building Docker Image**
Now it's time to build Docker Image from this Dockerfile. `docker build -t <DockerHub_Username>/<Imagename>` use this command to build a docker image.

**Step 4: Push the Image To DockerHub**
To deploy the app on Kubernetes, you need to make the Docker image accessible from a container registry like DockerHub. Push the image to DockerHub:

```
docker login
docker tag todo-app <your-dockerhub-username>/todo-app:v1
docker push <your-dockerhub-username>/todo-app:v1
```
**Step 5: Write a Kubernetes Manifest File**
To deploy an application on Kubernetes, you need to create YAML manifest files describing the desired state of your application. We'll create two files: **Deployment.yml** and **Service.yml**.

**Deployment.yml**

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app-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: wakeel532687/todo-app
        ports:
        - containerPort: 8000

```
**Write Service.yml file**

```
apiVersion: v1
kind: Service
metadata:
  name: todo-app-service
  labels:
    app: todo-app
spec:
  type: NodePort
  ports:
  - port: 8000
    targetPort: 8000
    nodePort: 32000
  selector:
    app: todo-app

```
**Step 6: Deploy the App to Kubernetes and Create a Service for it**
Apply the Kubernetes manifest files to deploy your Todo app:

```
kubectl apply -f Deployment.yml
kubectl apply -f Service.yml

```
If You want to check your deployment & Service use the command `kubectl get deployment` & `kubectl get services`

**Step 7: Expose the app**
First We need to expose our deployment so use `kubectl expose deployment todo-app-deployment --type=NodePort` command.

Then We have to expose our app service `kubectl port-forward svc/todo-app-service 8000:8000 --address 0.0.0.0 &
`
**Note**:- Make sure you open the 8000 port in a security group of your t2.medium Ec2 Instance.


[www.linkedin.com/in/swapnil-mohite-278848171](url)

**You have successfully Deployed a todo-app on Kubernetes.**