Project-Stage-Academy / UA1244_beta

1 stars 0 forks source link

Configure Kubernetes #57

Open mehalyna opened 3 weeks ago

mehalyna commented 3 weeks ago

Task Description:

We need to configure Kubernetes for deploying and managing our Django project, ensuring it runs smoothly in a containerized environment. The configuration will handle container orchestration, scaling, networking, and service discovery. This will involve setting up Kubernetes YAML files for deployment, service, and persistent storage for the project.


Objectives:

  1. Containerization:

    • Set up a Dockerfile and docker-compose.yml (if needed) to containerize the Django project for local development and production.
  2. Kubernetes Deployment:

    • Create a k8s/ directory in the project root for Kubernetes YAML configuration files.
    • Define a Deployment resource for running the Django application, ensuring scalability.
    • Define a Service resource for exposing the application (either as a ClusterIP, NodePort, or LoadBalancer).
    • Define Ingress resources to route external traffic to the Django app through a single entry point.
  3. Kubernetes Configuration Files:

    • Deployment: Create a YAML file for the Deployment that defines how the application containers are created, scaled, and updated.
    • Service: Create a YAML file for the Service that exposes the Django application to other services or external traffic.
    • ConfigMap & Secrets: Set up configuration data and environment variables (such as database connection strings and environment-specific settings) using ConfigMaps and Secrets for secure data.
    • Persistent Volume Claims (PVC): Create a PVC for storing media and static files (if necessary) persistently across pods.
  4. Database Configuration:

    • Set up a PostgreSQL/MySQL database in Kubernetes using the appropriate deployment and service configurations.
    • Create a Persistent Volume and Persistent Volume Claim to store the database data.
  5. Load Balancing and Scaling:

    • Configure Kubernetes Horizontal Pod Autoscaler (HPA) to automatically scale the number of Django application pods based on CPU/memory usage.
    • Set up a LoadBalancer or Ingress for external access.

Steps to Implement:

  1. Step 1: Set up Dockerfile and docker-compose:

    • Write a Dockerfile that installs Python dependencies, sets up the Django app, and exposes the appropriate ports.
    • Optionally, set up docker-compose.yml to orchestrate the Django app with the database in a local environment for testing.

    Example Dockerfile:

    FROM python:3.9-slim
    
    WORKDIR /app
    
    # Install dependencies
    COPY requirements.txt .
    RUN pip install -r requirements.txt
    
    # Copy the project files
    COPY . .
    
    # Expose the port
    EXPOSE 8000
    
    CMD ["gunicorn", "projectname.wsgi:application", "--bind", "0.0.0.0:8000"]
  2. Step 2: Create Kubernetes Deployment YAML (e.g., k8s/deployment.yaml):

    • Define the deployment for the Django application with the required container image, environment variables, and replicas.

    Example Deployment:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
     name: django-app
    spec:
     replicas: 3
     selector:
       matchLabels:
         app: django-app
     template:
       metadata:
         labels:
           app: django-app
       spec:
         containers:
         - name: django-container
           image: your-docker-image:latest
           ports:
           - containerPort: 8000
           envFrom:
           - configMapRef:
               name: django-config
           - secretRef:
               name: django-secrets
  3. Step 3: Create Kubernetes Service YAML (e.g., k8s/service.yaml):

    • Define the service to expose the Django application to other services or externally.

    Example Service:

    apiVersion: v1
    kind: Service
    metadata:
     name: django-service
    spec:
     type: LoadBalancer
     selector:
       app: django-app
     ports:
     - protocol: TCP
       port: 80
       targetPort: 8000
  4. Step 4: Create Kubernetes Ingress YAML (e.g., k8s/ingress.yaml):

    • Set up ingress rules to route HTTP(S) traffic to the Django application.

    Example Ingress:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
     name: django-ingress
     annotations:
       nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
     rules:
     - host: your-domain.com
       http:
         paths:
         - path: /
           pathType: Prefix
           backend:
             service:
               name: django-service
               port:
                 number: 80
  5. Step 5: Set up ConfigMap and Secrets (e.g., k8s/configmap.yaml, k8s/secret.yaml):

    • Define environment variables and sensitive data (like database credentials) using ConfigMap and Secrets.

    Example ConfigMap:

    apiVersion: v1
    kind: ConfigMap
    metadata:
     name: django-config
    data:
     DJANGO_SETTINGS_MODULE: "projectname.settings"
     DATABASE_URL: "postgres://user:password@db-service:5432/dbname"

    Example Secret:

    apiVersion: v1
    kind: Secret
    metadata:
     name: django-secrets
    type: Opaque
    data:
     SECRET_KEY: "<your_base64_encoded_secret_key>"
  6. Step 6: Create Persistent Volume Claims (if necessary, k8s/pvc.yaml):

    • Use PVC for media/static file storage or database persistence.

    Example PVC:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
     name: media-storage
    spec:
     accessModes:
       - ReadWriteOnce
     resources:
       requests:
         storage: 5Gi
  7. Step 7: Set up Horizontal Pod Autoscaler (HPA, optional k8s/hpa.yaml):

    • Configure autoscaling based on the load (e.g., CPU or memory usage).

    Example HPA:

    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
     name: django-hpa
    spec:
     scaleTargetRef:
       apiVersion: apps/v1
       kind: Deployment
       name: django-app
     minReplicas: 2
     maxReplicas: 10
     targetCPUUtilizationPercentage: 50
  8. Step 8: Test the Configuration Locally and in the Kubernetes Cluster:

    • Test the Kubernetes setup locally using minikube or directly in the Kubernetes cluster.
    • Ensure that the Django application, database, and ingress routes are functioning correctly.

Deliverables:

  1. Dockerfile and docker-compose.yml (optional):

    • A working Dockerfile to build the Django app container, and optionally docker-compose.yml for local orchestration.
  2. Kubernetes Configuration:

    • Kubernetes YAML files for deployment, services, ingress, and optional PVCs located in a new k8s/ directory.
    • k8s/deployment.yaml
    • k8s/service.yaml
    • k8s/ingress.yaml
    • k8s/configmap.yaml
    • k8s/secret.yaml
    • k8s/pvc.yaml (if necessary)
    • k8s/hpa.yaml (if scaling is required)
  3. Database Configuration:

    • Deployment and service YAML for the database (PostgreSQL/MySQL) if applicable.

Notes: