DEV Community

Cover image for Deployed a Todo-app on Kubernetes
Swapnil Suresh Mohite
Swapnil Suresh Mohite

Posted on

Deployed a Todo-app on Kubernetes

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!

GitHub logo SwapnilM24 / django-todo-app

This repo is for Django todo app deployment as a Devops engineer via Jenkins

django-todo

A simple todo app built with django

todo App

Setup

To get this repository, run the following command inside your git enabled terminal

$ git clone https://github.com/shreys7/django-todo.git
Enter fullscreen mode Exit fullscreen mode

You will need django to be installed in you computer to run this app. Head over to https://www.djangoproject.com/download/ for the download guide

Once you have downloaded django, go to the cloned repo directory and run the following command

$ python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode

This will create all the migrations file (database migrations) required to run this App.

Now, to apply this migrations run the following command

$ python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

One last step and then our todo App will be live. We need to create an admin user to run this App. On the terminal, type the following command and provide username, password and email for the admin user

$ python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

That was pretty simple, right? Now let's make the App live. Weโ€ฆ

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

Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

You have successfully Deployed a todo-app on Kubernetes.

Top comments (0)