Deploying web applications on Kubernetes offers scalability, flexibility, and ease of management. In this tutorial, we’ll guide you through the process of deploying a Django Todo app on a Kubernetes cluster using kubeadm. We’ll cover setting up the Kubernetes cluster, creating the necessary YAML files for deployment and service, and running the project.
Prerequisites
Before you start, ensure you have the following:
An AWS account.
An Ubuntu machine with Git installed.
Basic familiarity with Docker, Kubernetes, and Django.
Step 1: Clone the GitHub Repository
First, clone the Django Todo app repository from GitHub:
git clone https://github.com/LondheShubham153/django-todo-cicd.git
Step 2: Set Up the Kubernetes Cluster
Master Node Setup
On the master node, execute the following commands to install Docker, kubeadm, kubectl, and kubelet:
# Update package lists
apt update -y
# Install Docker
apt install docker.io -y
# Start and enable Docker service
systemctl start docker
systemctl enable docker
# Add the Kubernetes repository and update package lists
curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg
echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list
apt update -y
# Install specific versions of kubeadm, kubectl, and kubelet
apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
# Initialize the Kubernetes cluster on the Master Node
kubeadm init
# Set up Kubernetes configuration
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Apply a Weave networking addon to enable communication between pods
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
# Generate the join command for the Worker Node
kubeadm token create --print-join-command
Worker Node Setup
Run the following commands on the worker node to join it to the Kubernetes cluster:
# Update package lists
apt update -y
# Install Docker
apt install docker.io -y
# Start and enable Docker service
systemctl start docker
systemctl enable docker
# Add the Kubernetes repository and update package lists
curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg
echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list
apt update -y
# Install specific versions of kubeadm, kubectl, and kubelet
apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
# Reset the Worker Node (if already initialized)
kubeadm reset
# Paste the join command generated from the Master Node and append "--v=5" at the end to enable verbose output during join
kubeadm join <master_node_ip>:6443 --token <token> \
--discovery-token-ca-cert-hash <hash> --v=5
Replace <master_node_ip>
, <token>
, and <hash>
with the appropriate values obtained from the master node.
Step 3: Setup Deployment and Service for Kubernetes
Create the Deployment YAML File
Create a deployment.yaml
file for the Django app:
apiVersion: apps/v1
kind: Deployment
metadata:
name: django-todo-app-deployment
spec:
replicas: 2
selector:
matchLabels:
app: django-todo-app
template:
metadata:
labels:
app: django-todo-app
spec:
containers:
- name: django-todo-app
image: estebanmorenoit/django-notes-app:latest
ports:
- containerPort: 80
Apply the deployment YAML:
kubectl apply -f deployment.yaml
Create the Service YAML File
Create a service.yaml
file for the Django app:
apiVersion: v1
kind: Service
metadata:
name: django-todo-app-service
spec:
type: NodePort
selector:
app: django-todo-app
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
Apply the service YAML:
kubectl apply -f service.yaml
Step 4: Run the Project
With the Kubernetes deployment and service set up, your Django Todo app should now be accessible through the specified port on the master server. You can access it using the public IP or DNS name of the master server.
http://<master_node_ip>:30080
Conclusion
Congratulations! You have successfully deployed a Django app on a Kubernetes cluster using kubeadm. By following these steps, you’ve set up a Kubernetes cluster, created necessary YAML files for deployment and service, and run the Django Todo app on the cluster. This process showcases the power of Kubernetes in managing and scaling containerized applications.
Happy Learning!
Follow me on LinkedIn.