Day 88 of 90 Days of DevOps Challenge: Deploying a Django Todo App on AWS EC2 Using Kubeadm Kubernetes Cluster

In today's world, deploying applications efficiently and reliably is crucial for developers. With Kubernetes, you can easily manage containerized applications with features like auto-scaling and self-healing. In this blog post, we will guide you through the process of deploying a Django Todo application on AWS EC2 using a Kubeadm Kubernetes cluster.
Project Overview
This project aims to automate the deployment of a Django Todo application, leveraging Kubernetes for orchestration. We will set up a Kubeadm Kubernetes cluster on AWS EC2 and manage the application lifecycle through Kubernetes resources like Deployments and Services.
Key Components
1. Django Todo Application
Django is a high-level Python web framework that allows developers to create web applications quickly. The Todo application we’ll deploy will showcase CRUD operations.
2. Kubernetes
Kubernetes is a powerful container orchestration platform that enables the deployment, scaling, and management of containerized applications. With Kubeadm, setting up a Kubernetes cluster is straightforward and efficient.
Implementation Steps
Step 1: Get the Django Full Stack Application
Clone the Repository: Start by obtaining the Django Todo application code from GitHub.
git clone <your-django-todo-app-repo-url> cd <your-django-todo-app-directory>
Step 2: Set Up the Kubernetes Cluster
Launch an EC2 Instance: Log in to your AWS Management Console and launch an EC2 instance. Choose an instance type that meets your requirements (e.g., t2.medium or t2.large).
Connect to the EC2 Instance: Use SSH to connect to your EC2 instance.
ssh -i <your-key.pem> ec2-user@<your-ec2-instance-ip>
Install Docker: Before setting up the Kubernetes cluster, install Docker on the EC2 instance:
sudo yum update -y sudo amazon-linux-extras install docker sudo service docker start sudo usermod -a -G docker ec2-user
Install Kubeadm: Next, install Kubernetes components (kubeadm, kubelet, kubectl):
sudo tee /etc/yum.repos.d/kubernetes.repo <<EOF [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/doc/yum-key.gpg gpgcheck=1 repo_gpgcheck=1 enabled=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg EOF sudo yum install -y kubelet kubeadm kubectl sudo systemctl enable kubelet sudo systemctl start kubelet
Set Up the Kubernetes Cluster: Use the provided script to initialize the Kubernetes cluster. This could be a simple command like:
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
Set Up Network Add-on: After the cluster is initialized, you need to set up a network add-on (e.g., Calico, Flannel) for communication between pods:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Step 3: Setup Deployment and Service for Kubernetes
Create Deployment Configuration: In your Django application directory, create a YAML file named
deployment.yaml
for your Kubernetes deployment.apiVersion: apps/v1 kind: Deployment metadata: name: django-todo-app spec: replicas: 2 selector: matchLabels: app: django-todo-app template: metadata: labels: app: django-todo-app spec: containers: - name: django-todo-app image: <your-django-app-docker-image> # Specify your Docker image ports: - containerPort: 8000
Create Service Configuration: Create a YAML file named
service.yaml
for exposing your application:apiVersion: v1 kind: Service metadata: name: django-todo-app spec: type: LoadBalancer ports: - port: 80 targetPort: 8000 selector: app: django-todo-app
Apply the Configuration: Deploy your application and service using the following commands:
kubectl apply -f deployment.yaml kubectl apply -f service.yaml
Step 4: Run the Project and Share
Access Your Application: After applying the configurations, you can check the status of your pods:
kubectl get pods
Once the pods are running, find the external IP of your LoadBalancer service:
kubectl get services
Visit Your Application: Open a web browser and navigate to the external IP to access your Django Todo application.
Conclusion
By following the steps outlined in this blog post, you can successfully deploy a Django Todo application on AWS EC2 using a Kubeadm Kubernetes cluster. This project highlights the benefits of using Kubernetes for managing containerized applications, including auto-scaling and self-healing capabilities.
Subscribe to my newsletter
Read articles from Tushar Pant directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
