Code-Memo

Kubernetes

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating containerized applications. It helps us manage and orchestrate those containers at scale. It mainly consists of:

1. Pods

A Pod is the smallest and most basic deployable unit in Kubernetes. It represents a single instance of a running process in your cluster. A pod can contain one or more containers that share the same network namespace and storage.

Let’s say you have a Django application and a Redis instance that must always run together. Instead of deploying them as separate pods, you could place them in a single pod. This way, they share the same IP address and can communicate with each other more efficiently.

apiVersion: v1
kind: Pod
metadata:
  name: django-redis-pod
spec:
  containers:
    - name: django-container
      image: your-django-image
      ports:
        - containerPort: 8000
    - name: redis-container
      image: redis
      ports:
        - containerPort: 6379

2. Nodes

Nodes are the physical or virtual machines that make up the Kubernetes cluster. Each node is responsible for running the pods assigned to it.

Suppose you have a Kubernetes cluster with three worker nodes. When you deploy your Django app, Kubernetes might spread the pods across these nodes. If one node fails, Kubernetes can automatically reschedule the pods on another available node, ensuring high availability.

3. Cluster

Definition:

A Kubernetes cluster is a set of nodes (master and worker) that are orchestrated and managed together to run your containerized applications.

If you have a Django application that needs to be highly available, you might deploy a Kubernetes cluster across three different availability zones. The cluster will ensure that if one zone goes down, your application continues running in the other zones.

4. Control Plane

The control plane is the central nervous system of a Kubernetes cluster. It manages and controls the cluster, making decisions about scheduling, scaling, and maintaining the desired state of the system.

When you deploy a Django app using a Deployment, the control plane’s scheduler determines which nodes have enough resources to run the pods. The API server processes your request, and the controller manager ensures the pods remain running as intended.

5. Services

A Service in Kubernetes is an abstraction that defines a logical set of pods and a policy by which to access them. Services provide a stable endpoint (IP address or DNS name) to access pods, even as the underlying pods might be changing due to scaling or updates.

Types of Services:

You have a Django app running in a Kubernetes cluster. The pods can come and go as you scale up or down. By creating a Service, you provide a stable IP or DNS name that clients can use to access your app, regardless of the underlying pods.

apiVersion: v1
kind: Service
metadata:
  name: django-service
spec:
  selector:
    app: django
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: LoadBalancer

6. Deployments

A Deployment in Kubernetes is a higher-level abstraction that manages a group of pods and ensures that your application runs correctly. It controls the creation and scaling of pods and handles updates to the application with rolling updates.

Suppose you have a Django app and you want to run three instances (pods) of it for high availability. You would create a Deployment that specifies this desired state. Kubernetes ensures that three pods are always running, even if one fails or is updated.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: django
  template:
    metadata:
      labels:
        app: django
    spec:
      containers:
        - name: django
          image: your-django-image
          ports:
            - containerPort: 8000

Deploying Applications with Kubernetes

1. Deployments

Rolling Updates and Rollbacks

2. Services and Networking

Kubernetes Services expose your pods to external traffic and other services within the cluster.

Exposing Applications with ClusterIP, NodePort, and LoadBalancer Services

Introduction to Ingress for HTTP Routing

3. Configuration Management

Managing configurations and secrets is crucial for deploying applications in different environments (development, staging, production).

Using ConfigMaps and Secrets for Application Configuration

Mounting Configurations into Pods

4. Scaling Applications

Scaling is one of Kubernetes’ most powerful features, allowing your application to handle varying amounts of traffic.

Manual and Auto-scaling of Applications

Building and Deploying a Scalable Django Project on Kubernetes

1. Setting Up a Kubernetes Cluster

Before deploying your Django application, you need to set up a Kubernetes cluster.

Using Minikube, K3s, or a Cloud Provider

Cluster Provisioning and Node Management

Once your cluster is up, you can manage nodes using kubectl:

2. CI/CD Pipeline Integration

Integrating a CI/CD pipeline automates your build, test, and deployment processes, making your deployments more reliable and repeatable.

Automating Docker Builds and Deployments

Deploying Applications on Kubernetes from a CI/CD Pipeline

3. Deploying Django and DRF on Kubernetes

Now, let’s create a full Django and DRF application, containerize it, and deploy it to your Kubernetes cluster.

Creating a Dockerfile for Django and DRF

Your Dockerfile should define the environment for your Django application. Here’s an example:

# Dockerfile
FROM python:3.10-slim

ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY requirements.txt /app/
RUN pip install -r requirements.txt

COPY . /app/

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi:application"]

Writing Kubernetes Manifests for Deployments, Services, and Ingress

Setting Up Auto-scaling and Load Balancing

4. Managing and Updating Your Application

Managing a live application in Kubernetes involves rolling updates, handling database migrations, and preparing for disaster recovery.

Rolling Updates and Handling Migrations

Backup and Disaster Recovery Strategies