Multi-Container Pods Practical: Kubernetes Two Containers Pod Tutorial

Hands-on Kubernetes tutorial for creating and managing multi-container pods with Nginx and BusyBox using YAML configurations and kubectl commands.

Two Containers Pod Example

Download the YAML configuration file for the two containers Pod example

Download two-containers.yaml

Create the Pod

kubectl create -f two-containers.yaml

Creates the Pod with two containers using the YAML configuration file.

Get Pod Information

kubectl get pods -o wide
kubectl describe pod two-containers

Get detailed information about the running Pod and its containers.

Connect to BusyBox Container

kubectl exec -it two-containers --container mybox -- /bin/sh

Opens a shell inside the BusyBox container within the Pod.

Fetch Nginx HTML Page

wget -qO- localhost

Fetch the HTML page served by the Nginx container from within the BusyBox container.

Exit the Container

exit

Exit the shell and return to your local terminal.

Cleanup

kubectl delete -f two-containers.yaml --force --grace-period=0

Deletes the Pod and its containers immediately without waiting for graceful termination.

YAML Configuration File

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:
  restartPolicy: Always
  containers:
  - name: mynginx
    image: nginx
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi    
    ports:
      - containerPort: 80
  - name: mybox
    image: busybox
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi    
    ports:
      - containerPort: 81
    command:
      - sleep
      - "3600"

YAML Configuration Explanation:

Pod Structure:

  • apiVersion: v1 → Specifies the Kubernetes API version
  • kind: Pod → Defines this as a Pod resource
  • metadata.name: two-containers → Names the pod "two-containers"
  • restartPolicy: Always → Restarts containers if they fail

Nginx Container (mynginx):

  • image: nginx → Uses the official Nginx image
  • resources → Defines CPU and memory requests/limits
  • requests.cpu: 100m → Requests 0.1 CPU cores
  • requests.memory: 128Mi → Requests 128 MB of memory
  • limits.cpu: 250m → Limits to 0.25 CPU cores
  • limits.memory: 256Mi → Limits to 256 MB of memory
  • containerPort: 80 → Exposes port 80 for web traffic

BusyBox Container (mybox):

  • image: busybox → Uses a lightweight BusyBox image
  • resources → Similar resource constraints as Nginx container
  • containerPort: 81 → Exposes port 81 (though not used in this example)
  • command: ["sleep", "3600"] → Runs sleep command for 1 hour to keep container alive

How It Works:

This Pod runs two containers that share the same network namespace. The Nginx container serves web content on port 80, while the BusyBox container runs a sleep command to stay active. Since both containers are in the same Pod, they can communicate with each other using localhost. This allows the BusyBox container to access the Nginx server using wget -qO- localhost.

Use Case:

This pattern is useful when you need sidecar containers that assist the main application container. The BusyBox container could be used for debugging, monitoring, or performing auxiliary tasks alongside the main Nginx web server.