Back to Blog
Lesson 51 of the Kubernetes: Kubernetes Concepts & Your First Pod course
KubernetesSeptember 8, 20263 min read

Multi-Container Pods: Mastering Sidecar Patterns in Kubernetes

Learn to build multi-container pods using the sidecar pattern. Discover how shared volumes and networking enable powerful, modular application architecture.

KubernetesContainersSidecarsArchitecturePods
Colorful stacked shipping containers on a cargo ship traveling at sea, showcasing global trade.

Previously in this course, we explored using init containers to prepare environments before your main application starts. While Init Containers are designed for one-time setup, today we move to a more persistent architectural pattern: the Multi-Container Pod.

In Kubernetes, a Pod is not limited to a single process. By grouping multiple containers within a single Pod, you can implement Sidecars—an essential pattern for managing auxiliary tasks like log shipping, proxying, or config synchronization without bloating your main application code.

The Sidecar Architecture Defined

A sidecar is a secondary container that runs alongside your primary application container within the same Pod. Because they share the same lifecycle, they are scheduled on the same node, started together, and killed together.

The "magic" that makes this possible lies in two fundamental shared resources:

  1. Network Namespace: All containers in a Pod share the same IP address and port space. This means your application can communicate with a sidecar over localhost.
  2. Shared Volumes: You can mount the same volume into both containers, allowing them to read and write to the same filesystem paths.

Worked Example: A Simple Log Shipper

Imagine you have a web application that writes logs to a local file. You need to get these logs to a central server, but you don't want to add logging logic to your application code. A sidecar container can handle this.

YAML
apiVersion: v1
kind: Pod
metadata:
  name: sidecar-example
spec:
  volumes:
  - name: shared-logs
    emptyDir: {}
  containers:
  - name: app-container
    image: nginx
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx
  - name: log-shipper
    image: busybox
    command: ["/bin/sh", "-c", "tail -f /var/log/nginx/access.log"]
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx

In this example, both containers mount the shared-logs volume. The app-container writes to the log, and the log-shipper reads it. Because they share the volume, the sidecar sees exactly what the app produces.

Hands-on Exercise

To verify this architecture yourself:

  1. Create a file named pod-with-sidecar.yaml using the manifest above.
  2. Apply it with kubectl apply -f pod-with-sidecar.yaml.
  3. Verify both containers are running: kubectl get pod sidecar-example.
  4. Inspect the logs of the sidecar specifically: kubectl logs sidecar-example -c log-shipper.

You will see the Nginx access logs being streamed by the busybox container, even though the busybox image knows nothing about Nginx's internal logging format.

Common Pitfalls

  • Tight Coupling: Don't put two containers together if they don't need to share a lifecycle. If they can scale independently, they should be in separate Pods.
  • Resource Contention: Since both containers share the node's resources, one greedy sidecar can starve your primary application. Always define resource requests and limits for every container in the Pod.
  • Startup Ordering: Containers in a Pod start in parallel. If your app expects the sidecar to be ready (e.g., a proxy), your app might crash on startup. Use readiness probes to ensure the primary app only receives traffic once the sidecar is fully initialized.

FAQ

Can I run 10 containers in one Pod? Yes, but you shouldn't. Keep the number of containers in a Pod small (usually 1-3). Large Pods become difficult to manage and debug.

Do sidecars have their own IP addresses? No. All containers in a Pod share the localhost network interface. A sidecar listening on port 8080 is reachable by the main app via localhost:8080.

How do I update a sidecar? Like any other container, you update the image tag in your manifest and re-apply it. Kubernetes will replace the Pod to apply the change.

Recap

Multi-container Pods allow you to separate concerns by offloading secondary tasks to sidecars. By utilizing shared volumes and the shared network namespace, you create a modular, resilient architecture that keeps your primary application code clean and focused. This pattern is foundational for service identity and secure communication in complex systems.

Up next: We will discuss how to perform cluster maintenance safely using kubectl drain and node evacuation techniques.

Similar Posts