Skip to content

This project demonstrates how to deploy and manage two intercommunicating Django applications on Kubernetes. It provides a step-by-step guide to writing Kubernetes YAML configuration files, containerizing Django applications with Docker, and deploying them to a Kubernetes cluster.

Notifications You must be signed in to change notification settings

Sahar-dev/Kubernetes_101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Deploying Django Applications on Kubernetes: A Friendly Guide

This guide will walk you through deploying two intercommunicating Django applications on Kubernetes. We'll focus on understanding Kubernetes concepts, writing YAML configuration files, and explaining how Kubernetes works. The code snippets will be minimal, allowing us to concentrate on the Kubernetes aspects. By the end, you'll have a clear understanding of deploying applications on Kubernetes and how its components interact.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setting Up the Django Applications
  4. Containerizing the Applications with Docker
  5. Pushing Docker Images to a Registry
  6. Understanding Kubernetes Components
  7. Writing Kubernetes YAML Configuration Files
  8. Deploying Applications to Kubernetes
  9. Testing the Deployment
  10. Troubleshooting Common Issues
  11. Conclusion
  12. Additional Resources

Introduction

Kubernetes is a powerful open-source platform for automating the deployment, scaling, and management of containerized applications. In this guide, we'll deploy two Django applications:

  • Service A: Provides an API endpoint that returns data.
  • Service B: Consumes the API from Service A and displays the data.

Our focus will be on how to configure and deploy these applications on Kubernetes, understanding each component involved.

Prerequisites

  • Basic Knowledge of Docker and Kubernetes: Understanding of containers and orchestration.
  • Docker Installed: To containerize the applications.
  • Kubernetes Cluster: You can use Minikube for local development.
  • kubectl Command-line Tool: To interact with your Kubernetes cluster.
  • Python 3.6+ and Django Installed: For setting up the applications.

Setting Up the Django Applications

We'll briefly set up the two Django applications. Detailed Django setup instructions are available in the Django documentation.

Service A: API Service

  • Create a Django project and app: Initialize a new Django project called service_a_project and an app called api_app.
  • Implement a simple API view: Create a view that returns JSON data (e.g., a list of items).
  • Configure URLs: Map a URL path to the API view.
  • Set ALLOWED_HOSTS: In settings.py, set ALLOWED_HOSTS = ['*'] for development purposes.

Service B: Web Service

  • Create a Django project and app: Initialize a new Django project called service_b_project and an app called web_app.
  • Implement a view that consumes Service A: Use Python's requests library to fetch data from Service A's API.
  • Configure URLs and Templates: Map a URL path to the view and create a template to display the data.
  • Set ALLOWED_HOSTS: In settings.py, set ALLOWED_HOSTS = ['*'] for development purposes.

Containerizing the Applications with Docker

Containerizing the applications ensures they run consistently across different environments.

Writing Dockerfiles

Service A Dockerfile

FROM python:3.9-slim 
WORKDIR /app
COPY . .
RUN pip install django
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Service B Dockerfile

FROM python:3.9-slim 
WORKDIR /app 
COPY . . 
RUN pip install django requests 
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
  • Explanation:
    • FROM: Specifies the base image.
    • WORKDIR: Sets the working directory inside the container.
    • COPY: Copies application code into the container.
    • RUN: Executes commands during the image build (e.g., installing dependencies).
    • CMD: Specifies the default command to run when the container starts.

Pushing Docker Images to a Registry

  • Build the Docker images:

    docker build -t your_username/service-a:latest ./service_a docker build -t your_username/service-b:latest ./service_b

  • Push the images to Docker Hub (or your chosen registry):

    docker push your_username/service-a:latest docker push your_username/service-b:latest

Understanding Kubernetes Architecture and Components

Kubernetes, often abbreviated as K8s, orchestrates the deployment and management of containerized applications across a cluster of nodes. Its architecture is designed to handle scaling, self-healing, and efficient resource utilization.

Core Components

  1. Master Node (Control Plane)
    • API Server: Acts as the communication hub for the Kubernetes cluster, exposing the Kubernetes API.
    • Controller Manager: Ensures the desired state of the cluster by managing controllers such as Deployments and Replicas.
    • Scheduler: Assigns pods to available nodes based on resource availability and constraints.
    • etcd: A distributed key-value store that stores all cluster configuration and state data.
  2. Worker Nodes
    • Kubelet: Ensures that containers are running in a pod. Communicates with the API server for instructions.
    • Kube Proxy: Handles networking for pods, managing access to services inside and outside the cluster.
    • Container Runtime: Responsible for running containers (e.g., Docker, containerd).
  3. Pods
    • The smallest deployable unit in Kubernetes, containing one or more tightly coupled containers.
  4. Services
    • A stable endpoint to expose a group of pods, enabling communication between components or with external systems.
  5. ConfigMaps and Secrets
    • ConfigMaps: Manage configuration data in key-value pairs.
    • Secrets: Store sensitive data, such as passwords or API keys, securely.

High-Level Workflow

  • User Interaction: Users interact with the cluster using kubectl or APIs.
  • Control Plane Processing: The API server validates requests and updates etcd. The scheduler and controllers work together to match resources with the desired state.
  • Pod Deployment: Pods are scheduled on worker nodes, where Kubelet and the container runtime take over.
  • Networking: Services and Kube Proxy handle communication between pods and external systems.

Writing Kubernetes YAML Configuration Files

Service A Kubernetes Configuration

1. Deployment for Service A

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-a-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: service-a
  template:
    metadata:
      labels:
        app: service-a
    spec:
      containers:
        - name: service-a
          image: your_username/service-a:latest
          ports:
            - containerPort: 8000

  • Explanation:
    • apiVersion, kind, and metadata: Define the resource type and metadata.
    • spec.replicas: Specifies the desired number of pod replicas.
    • spec.selector.matchLabels: Matches pods with the specified labels.
    • spec.template: Defines the pod template.
    • spec.template.spec.containers: Specifies the containers within the pod. 2. Service for Service A
---
apiVersion: v1
kind: Service
metadata:
  name: service-a
spec:
  selector:
    app: service-a
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000
  type: ClusterIP
  • Explanation:
    • spec.selector: Selects pods with the label app: service-a.
    • spec.ports: Defines the ports exposed by the service.

Service B Kubernetes Configuration

1. Deployment for Service B

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-b-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: service-b
  template:
    metadata:
      labels:
        app: service-b
    spec:
      containers:
        - name: service-b
          image: your_username/service-b:latest
          ports:
            - containerPort: 8000
          env:
            - name: SERVICE_A_HOST
              value: "service-a"
  • Explanation:
    • env: Sets environment variables inside the container, allowing Service B to locate Service A using the Kubernetes service name. 2. Service for Service B
apiVersion: v1
kind: Service
metadata:
  name: service-b
spec:
  selector:
    app: service-b
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000

Deploying Applications to Kubernetes

1. Start Your Kubernetes Cluster

If you're using Minikube:

minikube start

2. Apply the Configuration Files

kubectl apply -f service_a_k8s.yaml kubectl apply -f service_b_k8s.yaml

  • Explanation:
    • kubectl apply: Applies the configuration to the cluster, creating or updating resources.

3. Verify the Deployments and Services

kubectl get deployments kubectl get pods kubectl get services

  • Explanation:
    • kubectl get: Lists resources in your cluster.

Testing the Deployment

Accessing Service B

Since the services are only accessible within the cluster, we'll use port forwarding to access Service B from our local machine.

kubectl port-forward service/service-b 8001:8000

  • Explanation:
    • kubectl port-forward: Forwards one or more local ports to a pod.

Viewing the Application

Open a web browser and navigate to http://localhost:8001/ to see the data fetched from Service A.

Troubleshooting Common Issues

Django ALLOWED_HOSTS Error

Problem: Service A raises an Invalid HTTP_HOST error when accessed by Service B.

Solution:

  1. Update ALLOWED_HOSTS in settings.py of Service A:

    ALLOWED_HOSTS = ['service-a']

    • Explanation: In Kubernetes, services communicate using service names. Adding 'service-a' to ALLOWED_HOSTS allows Django to accept requests addressed to service-a.
  2. Rebuild and Push the Updated Docker Image:

    docker build -t your_username/service-a:latest ./service_a docker push your_username/service-a:latest

  3. Update the Deployment in Kubernetes:

    Since the image tag is the same, you might need to force Kubernetes to pull the latest image by deleting the existing pod:

    kubectl delete pod -l app=service-a

    Or update the deployment to use an image tag with a version number (e.g., :v2) to ensure Kubernetes pulls the new image.

Conclusion

Congratulations! You've deployed two intercommunicating Django applications on Kubernetes. By focusing on the Kubernetes YAML configuration files and understanding how Kubernetes works, you now have a solid foundation for deploying and managing applications in a Kubernetes cluster.

Additional Resources

Note: For production environments, ensure you follow best practices such as:

  • Managing secrets and configurations securely using Kubernetes Secrets and ConfigMaps.
  • Setting up proper networking and security policies.

About

This project demonstrates how to deploy and manage two intercommunicating Django applications on Kubernetes. It provides a step-by-step guide to writing Kubernetes YAML configuration files, containerizing Django applications with Docker, and deploying them to a Kubernetes cluster.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published