In this article, I'll guide you step-by-step through the process of deploying a Django Todo application on AWS EC2 using a Kubeadm Kubernetes cluster. This guide is perfect for new learners who want to understand the basics of deploying applications in a scalable and resilient environment.
1. Introduction
Deploying applications in the cloud using Kubernetes provides several advantages, including auto-scaling and auto-healing capabilities. This means your application can handle varying loads and recover from failures without manual intervention. In this tutorial, we'll use a Django Todo app, AWS EC2, and Kubernetes to demonstrate these concepts.
2. Prerequisites
Before we start, make sure you have the following:
An AWS account
Basic knowledge of Django and Python
Familiarity with the command line interface
Git installed on your local machine
3. Getting the Django Full Stack Application from GitHub
First, we need to obtain a Django Todo application from GitHub. You can use any Django application, but for this tutorial, we’ll use a sample Django Todo app.
Clone the Repository: Open your terminal and run the following command to clone the repository:
git clone https://github.com/username/django-todo-app.git cd django-todo-app
Set Up the Virtual Environment: Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
Install Dependencies: Install the required dependencies using pip:
pip install -r requirements.txt
Run the Application Locally: Ensure everything is working by running the application locally:
shCopy codepython manage.py runserver
Visit
http://127.0.0.1:8000
in your browser to check if the application is running correctly.
4. Setting Up the Kubernetes Cluster Using Kubeadm
Next, we'll set up a Kubernetes cluster using Kubeadm. Kubernetes clusters can be set up on various platforms, but for this tutorial, we'll use AWS EC2 instances.
Launch EC2 Instances:
Go to the AWS Management Console.
Launch a few EC2 instances (one master and at least two worker nodes) with a suitable Linux distribution (e.g., Ubuntu 20.04).
Install Docker: On each instance, install Docker, which is required for Kubernetes:
sudo apt-get update sudo apt-get install -y docker.io
Install Kubeadm, Kubelet, and Kubectl: On each instance, install the necessary Kubernetes components:
sudo apt-get update sudo apt-get install -y apt-transport-https ca-certificates curl sudo curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl
Initialize the Kubernetes Master Node: On the master node, initialize the cluster:
sudo kubeadm init
Follow the instructions provided after initialization to set up
kubectl
for the current user:mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Join Worker Nodes to the Cluster: On each worker node, run the command provided by
kubeadm init
on the master node to join them to the cluster. It looks something like this:sudo kubeadm join <master-node-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Deploy a Network Add-On: To enable communication between nodes, deploy a network add-on (e.g., Calico):
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
5. Setting Up Deployment and Service for Kubernetes
Now, we need to create Kubernetes manifests to deploy and expose our Django application.
Create a Docker Image: First, create a Docker image of your Django application.
# Dockerfile FROM python:3.8-slim WORKDIR /app COPY . /app RUN pip install -r requirements.txt CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Build and push the Docker image to a container registry (e.g., Docker Hub).
docker build -t <your-dockerhub-username>/django-todo-app . docker push <your-dockerhub-username>/django-todo-app
Create Kubernetes Manifests: Create YAML files for Deployment and Service.
# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: django-todo-app spec: replicas: 3 selector: matchLabels: app: django-todo-app template: metadata: labels: app: django-todo-app spec: containers: - name: django-todo-app image: <your-dockerhub-username>/django-todo-app ports: - containerPort: 8000
# service.yaml apiVersion: v1 kind: Service metadata: name: django-todo-app spec: type: LoadBalancer selector: app: django-todo-app ports: - protocol: TCP port: 80 targetPort: 8000
Apply the Manifests: Deploy the application to the Kubernetes cluster.
kubectl apply -f deployment.yaml kubectl apply -f service.yaml
Access the Application: Once the service is up, you can access your Django application through the external IP provided by the LoadBalancer service.
6. Conclusion
Congratulations! You have successfully deployed a Django Todo application on AWS EC2 using a Kubeadm Kubernetes cluster. This setup ensures that your application is scalable and resilient, thanks to Kubernetes' auto-scaling and auto-healing capabilities.
By following this guide, you’ve learned how to:
Set up a Kubernetes cluster
Deploy a Django application using Kubernetes
Configure a service to expose your application