0
0
Kubernetesdevops~5 mins

Multi-container Pods concept in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, you want to run two or more containers together on the same machine so they can share resources and work closely. Multi-container Pods let you do this by grouping containers that need to cooperate tightly in one unit.
When you want a helper container to provide files or data to the main app container.
When you need a logging or monitoring container to watch the main app inside the same pod.
When two containers must share the same network and storage space closely.
When you want to split responsibilities but keep containers tightly coupled for easier management.
When you want to run a sidecar container that adds features like proxy or security alongside your main app.
Config File - multi-container-pod.yaml
multi-container-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: main-app
    image: nginx:1.23.3
    ports:
    - containerPort: 80
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx
  - name: sidecar-logger
    image: busybox
    command: ["sh", "-c", "tail -f /var/log/nginx/access.log"]
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx
  volumes:
  - name: shared-logs
    emptyDir: {}

This YAML defines a Pod with two containers: main-app running Nginx and sidecar-logger running BusyBox to watch logs.

The emptyDir volume named shared-logs is shared between containers to let the logger read Nginx logs.

This setup shows how containers in one Pod share storage and network.

Commands
This command creates the Pod with two containers defined in the YAML file.
Terminal
kubectl apply -f multi-container-pod.yaml
Expected OutputExpected
pod/multi-container-pod created
Check that the Pod is running and ready with both containers inside.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE multi-container-pod 2/2 Running 0 10s
See detailed information about the Pod, including both containers and their shared volumes.
Terminal
kubectl describe pod multi-container-pod
Expected OutputExpected
Name: multi-container-pod Namespace: default Priority: 0 Node: <node-name>/192.168.1.100 Start Time: <time> Labels: <none> Annotations: <none> Status: Running IP: 10.244.1.5 Containers: main-app: Container ID: docker://<id> Image: nginx:1.23.3 State: Running Ready: True sidecar-logger: Container ID: docker://<id> Image: busybox State: Running Ready: True Volumes: shared-logs: Type: EmptyDir (a temporary directory that shares a pod's lifetime) Medium: QoS Class: BestEffort Node-Selectors: <none> Tolerations: <none> Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: containers in the same Pod share network and storage, so they can work closely as one unit.

Common Mistakes
Defining multiple containers but forgetting to share volumes for data exchange.
Containers cannot share files or logs without a shared volume, so they cannot cooperate properly.
Use a shared volume like emptyDir and mount it in all containers that need to share data.
Expecting containers in different Pods to share network or storage automatically.
Pods are isolated units; only containers inside the same Pod share network and storage.
Put tightly coupled containers inside the same Pod to share resources.
Summary
Create a Pod YAML with multiple containers listed under spec.containers.
Use shared volumes like emptyDir to let containers share files or logs.
Apply the YAML with kubectl apply and verify the Pod runs with kubectl get pods and kubectl describe pod.