Pods

Difficulty: Beginner

Question

What is a Pod in Kubernetes, and why don't we run containers directly?

Answer

A **Pod** is the smallest deployable object in Kubernetes. It represents a single instance of a running process in your cluster.

**Why Pods?**: - **Shared Resources**: Containers within a Pod share the same network IP and port space (localhost communication). - **Shared Storage**: Containers can share volumes mounted into the Pod. - **Lifecycle**: Pods provide a level of abstraction. Kubernetes manages Pods, not individual containers.

**Sidecar Pattern**: A common practice where a primary container runs the application, and a 'sidecar' container handles logging, proxying (like Envoy), or configuration sync.

Code examples

Multi-container Pod YAML

apiVersion: v1
kind: Pod
metadata:
  name: app-with-sidecar
spec:
  containers:
  - name: main-app
    image: my-app:v1
  - name: logger-sidecar
    image: fluentd
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log
  volumes:
  - name: shared-logs
    emptyDir: {}

Both containers share the 'shared-logs' volume and talk to each other via localhost.

Key points

Concepts covered

Pod, Container, Sidecar, Networking