Deployment Architectures (Pod Patterns) 🏗️

When we talk about Pods in Kubernetes, we sometimes forget that a Pod can have multiple containers. Here we explore how and why to do this.

The Roommate Analogy 🏠

To understand a multi-container Pod, imagine a Department (Pod) inside a Building (Node).

  • The Pod is the Department: It has a unique address (IP), shared resources (electricity, water) and isolation from neighbors.
  • Containers are Roommates: They live together within the same apartment.
    • Share Network: You can talk shouting from one room to another (localhost). They do not need to go outside (Internet/Service Mesh) to communicate.
    • Share Disk (Volumes): You can leave notes on the refrigerator (shared files) that both can read.
    • Die Together: If the apartment catches fire (Pod delete/crash), all the roommates are left homeless.

1. Sidecar Pattern (The Helper) 🏍️

Concept: You have a “Main” container (your application) and a “Sidecar” that helps you with peripheral tasks without touching the main code.

Practical Example: Logs to S3 📦

  1. Main (Node.js App): Write logs to /var/log/app.log. He doesn’t know what S3 is, he only knows how to write to disk.
  2. Sidecar (Log Agent): Reads /var/log/app.log (via shared volume) and uploads it to an S3 bucket.
  3. Benefit: If you switch from S3 to Azure Blob, you only update the Sidecar. Your main App doesn’t know.
apiVersion: v1
kind: Pod
metadata:
  name: sidecar-pattern
spec:
  containers:
    # 1. Main App
    - name: app
      image: my-app:v1
      volumeMounts:
        - name: shared-logs
          mountPath: /var/log
 
    # 2. Sidecar
    - name: log-agent
      image: agent:latest
      volumeMounts:
        - name: shared-logs
          mountPath: /var/log
 
  volumes:
    - name: shared-logs
      emptyDir: {}

2. Other Common Patterns

  • Ambassador (The Ambassador): A sidecar that acts as a proxy to connect the application with the outside world.
    • Example: Your App connects to localhost:5432 and the Ambassador takes care of the complex and secure connection to the real database in the cloud.
  • Adapter (The Translator): A sidecar that standardizes the output.
    • Example: Your App spits out logs in ugly and old format. The Adapter reads them and reformats them to pretty JSON so that Prometheus understands them.