Files
the_information_nexus/tech_docs/kubernetes.md
2024-06-26 23:40:00 +00:00

3.6 KiB

Certainly! I'll simplify the project and provide clearer documentation for both the Docker Compose and Kubernetes approaches. We'll use a simple web application with a Redis cache as our example.

Let's start with the Docker Compose version:

version: '3'

services:
  # Web application service
  web:
    image: nginx:alpine  # Using the official Nginx image
    ports:
      - "80:80"  # Mapping host port 80 to container port 80
    environment:
      - REDIS_HOST=redis  # Environment variable to connect to Redis
    depends_on:
      - redis  # Ensures redis service is started before web

  # Redis cache service
  redis:
    image: redis:alpine  # Using the official Redis image
    ports:
      - "6379:6379"  # Exposing Redis port (usually not needed in production)

# No volumes defined for simplicity

Now, let's create the equivalent setup using Kubernetes resources:

---
# Web application deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: nginx:alpine
        ports:
        - containerPort: 80
        env:
        - name: REDIS_HOST
          value: redis  # Points to the Redis service name

---
# Web application service (for external access)
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
  - port: 80
    targetPort: 80
  type: LoadBalancer  # Exposes the service externally

---
# Redis deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:alpine
        ports:
        - containerPort: 6379

---
# Redis service (for internal access)
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
  - port: 6379

Now, let me explain the key differences and similarities between these two approaches:

  1. Structure:

    • Docker Compose: All services are defined in a single YAML file.
    • Kubernetes: Resources are typically split into separate YAML files (combined here for simplicity).
  2. Service Definition:

    • Docker Compose: Services are defined under the services key.
    • Kubernetes: Services are split into Deployments (for container specs) and Services (for networking).
  3. Networking:

    • Docker Compose: Uses the ports key to map container ports to host ports.
    • Kubernetes: Uses Service resources to expose deployments, with LoadBalancer type for external access.
  4. Environment Variables:

    • Docker Compose: Directly specified under the environment key.
    • Kubernetes: Defined in the container spec under the env key.
  5. Dependencies:

    • Docker Compose: Uses depends_on to specify service dependencies.
    • Kubernetes: Doesn't have a direct equivalent; often handled through readiness probes or init containers (not shown in this simple example).
  6. Scaling:

    • Docker Compose: Typically runs on a single host, limiting scaling options.
    • Kubernetes: Can easily scale by changing the replicas field in Deployments.

Both configurations achieve the same basic setup: a web service connected to a Redis cache. The Kubernetes version offers more flexibility for scaling and managing the application across multiple nodes, while the Docker Compose version is simpler for local development or single-host deployments.

Would you like me to elaborate on any specific aspect of these configurations?