Day 88 - Project 9

4 min read

Cover Image for Day 88 - Project 9

Deploying a Django Todo app on AWS EC2 using the Kubeadm Kubernetes cluster involves several steps. Here's a high-level overview of the process:

Prerequisites:

  1. You should have a Django Todo app Dockerized and pushed to a container registry (e.g., Docker Hub).

  2. You need an AWS EC2 instance for your Kubernetes cluster. Make sure you have SSH access to it.

Step 1: Set Up Your AWS EC2 Instance

  • Launch an AWS EC2 instance with an appropriate OS (typically, a Linux-based OS like Ubuntu).

    Note: minimum t3.micro and CPU 2

  • Make sure to configure security groups and firewall rules to allow traffic to your EC2 instance, especially for ports like 80 and 443 (if your Django app uses them).

  • Install Docker on your EC2 instance as Kubernetes relies on it.

Step 2: Install and Set Up Kubeadm and Kubernetes on EC2

  • SSH into your EC2 instance.

  • Install Kubeadm, Kubelet, and Kubectl following the Kubernetes official documentation.

  • To install Kubeadm, Kubelet, and Kubectl on an EC2 instance running a Linux-based operating system (e.g., Ubuntu), you can follow these general steps. Please note that specific installation commands may vary depending on your Linux distribution, so adjust the commands accordingly.

    Step 1: Update the Package Repository SSH into your EC2 instance and ensure your package repository is up to date by running:

      sudo apt update
    

    Step 2: Install Docker Kubernetes relies on Docker, so you need to install it first. You can use the following commands to install Docker:

      sudo apt install docker.io
    

    Start and enable Docker to run at boot:

      sudo systemctl start docker
      sudo systemctl enable docker
    

    Step 3: Install Kubeadm, Kubelet, and Kubectl

    Next, you'll install Kubeadm, Kubelet, and Kubectl. You can use a specific Kubernetes version or the latest available version:

      sudo apt update && sudo apt install -y apt-transport-https curl
      curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    
      # Install the Kubernetes components
      sudo apt update
      sudo apt install -y kubelet kubeadm kubectl
    

    Step 4: Enable and Start Kubelet After installing Kubelet, enable and start it:

      sudo systemctl enable kubelet
      sudo systemctl start kubelet
    

    Step 5: Initialize Kubeadm (Master Node Only) If this EC2 instance is going to be your Kubernetes master node, you'll need to initialize Kubeadm. Run the following command to initialize it:

      sudo kubeadm init
    

    Follow the on-screen instructions to complete the initialization process. Make sure to note down the kubeadm join command provided; you'll need it later to join worker nodes to the cluster.

    Step 6: Configure Kubectl To configure Kubectl to work with your Kubernetes cluster, run the following command:

      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
    

    Step 7: Join Worker Nodes (Worker Nodes Only) If you have additional EC2 instances that you want to join as worker nodes to the Kubernetes cluster, SSH into each worker node and run the kubeadm join command provided during the master node initialization. It will look something like this:

      sudo kubeadm join <MASTER_IP>:<MASTER_PORT> --token <TOKEN> --discovery-token-ca-cert-hash <CA_CERT_HASH>
    

    Replace <MASTER_IP>, <MASTER_PORT>, <TOKEN>, and <CA_CERT_HASH> with the values specific to your cluster.

    After completing these steps, you should have Kubeadm, Kubelet, and Kubectl installed on your EC2 instance. The EC2 instance will either act as the master node or join the existing cluster as a worker node, depending on your needs.

Step 3: Initialize Your Kubernetes Cluster

  • On your EC2 instance, run the kubeadm init command to initialize the Kubernetes cluster.

Step 4: Set Up a Pod Network

  • Choose a Kubernetes networking solution like Calico, Weave, or Flannel and follow its installation instructions. This step is essential for your pods to communicate with each other.

Step 5: Join Worker Nodes (if applicable)

  • If you plan to use multiple EC2 instances as worker nodes in your cluster, you'll need to SSH into each of them and join them to the cluster using the command provided by kubeadm init on the master node.

Step 6: Deploy Your Django Todo App

Github link: https://github.com/sri766/django-todo-cicd

  • Create Kubernetes deployment and service YAML files for your Django Todo app. Here's a basic example:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-todo-deployment
spec:
  replicas: 3 # Adjust as needed
  selector:
    matchLabels:
      app: django-todo
  template:
    metadata:
      labels:
        app: django-todo
    spec:
      containers:
        - name: django-todo
          image: your-django-todo-image:tag
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: django-todo-service
spec:
  selector:
    app: django-todo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer # Use LoadBalancer if you want an external IP
  • Apply the YAML files using kubectl apply -f your-deployment-service.yaml.

Step 7: Expose Your Django App

  • If you used a LoadBalancer service type, Kubernetes will automatically provision an external IP (AWS ELB) to access your app. If not, you may need to configure an Ingress controller and Ingress resource to expose your app.

Step 8: Configure Your Domain

  • If you have a custom domain, configure DNS to point to your app's IP address.

Remember that this is a high-level overview, and each step may require more detailed configuration based on your specific needs. Additionally, you should consider using Helm for managing your Kubernetes deployments and creating Helm charts for your Django Todo app to simplify deployment and updates.