Multi-container Pod Basics

In Kubernetes, a Pod can hold more than one container.

Because these containers share the same network IP, port space, and storage, they can easily talk to each other and share info. Each container in the Pod has its own purpose and works closely with the other containers in the same Pod.

Example: A Simple Multi-container Pod

Here’s a YAML file to define a multi-container Pod with a web server and a data filler container:

apiVersion: v1
kind: Pod
  name: my-multi-container-pod
  - name: web-server
    image: nginx
  - name: data-filler
    image: busybox
    command: ['sh', '-c', 'echo Hello > /usr/share/nginx/html/index.html']

To create the Pod, run:

kubectl apply -f my-multi-container-pod.yaml

Use Cases for Multi-container Pods

  1. Single Responsibility Principle: Each container can focus on a single task, making it easier to manage and scale each component.

  2. Resource Sharing: Containers in the same Pod can share storage volumes, reducing data duplication.

  3. Network Efficiency: Being on the same local network, the containers can efficiently communicate with each other.

Example: Shared Volume

You could have an HTML filler container that populates an HTML file which the web-server serves. They can share a volume:

  - name: web-server
    image: nginx
    - name: shared-data
      mountPath: /usr/share/nginx/html
  - name: html-filler
    image: busybox
    command: ['sh', '-c', 'echo Hello > /html/index.html']
    - name: shared-data
      mountPath: /html
  - name: shared-data
    emptyDir: {}

Design Patterns

  1. Sidecar: Enhances or extends the main container’s functionality. For example, a logging sidecar can handle logging for the main application.

  2. Ambassador: Used for proxying local connections to the world outside the Pod. This pattern can expose a database running in the Pod to other services.

  3. Adapter: Transforms the output of one container to make it understandable by another. Useful when integrating software components that expect different formats or data schemas.

Example: Sidecar Pattern

Using the Sidecar pattern, you can have a logging sidecar along with your application:

  - name: app
    image: my-app
  - name: logging-sidecar
    image: my-logger

Communication between Containers

  1. Inter-process Communication (IPC): Containers in the same Pod can communicate via IPC like shared memory or semaphores.

  2. Localhost Networking: Containers can reach each other through localhost as they share the network namespace.

  3. File Sharing: Containers in the same Pod can read and write to the same storage volumes.

Example: Localhost Networking

You could use curl inside one container to access another:

kubectl exec -it my-multi-container-pod -c data-filler -- curl localhost

Best Practices and Considerations

  1. Tight Coupling: Only group containers that are tightly coupled in functionality.

  2. Resource Allocation: Be cautious when allocating resources; one container should not starve others in the same Pod.

  3. Logging and Monitoring: Ensure logging and monitoring are configured to track the performance and errors of each container separately.

  4. Start-up Order: Use init containers if you need to ensure that certain containers start before others.

  5. Security: Containers in the same Pod share the security context, so ensure that your security policies account for this.

Example: Resource Allocation

Here’s how you could allocate CPU and memory to each container:

  - name: web-server
    image: nginx
        cpu: "0.5"
        memory: "128Mi"
  - name: data-filler
    image: busybox
        cpu: "0.2"
        memory: "64Mi"

Troubleshooting Exercise


We have a multi-container Pod with two containers: main-app and helper. main-app writes data to a shared volume, and helper is supposed to read this data and send it somewhere.


main-app is writing data, but helper is not able to read it.

Debug and Fix

  1. Check Logs: Check logs of both containers

    kubectl logs <pod_name> -c main-app
    kubectl logs <pod_name> -c helper
  2. Inspect Volume: Make sure that both containers mount the volume at the correct path.

  3. Check Permissions: Make sure helper has the correct permissions to read data. You might need to adjust the security context.

  4. Check Application Code: Ensure that main-app is writing to the correct location that helper is trying to read from.

  5. Resource Consumption: Use kubectl describe pod <pod_name> to check if any of the containers is consuming too many resources.

  6. Restart Container: Sometimes a simple restart helps.

    kubectl delete pod <pod_name> 

    Then reapply the YAML file.