Skip to main content

Workloads & Scheduling

How to Run Multiple Containers in One Kubernetes Pod

The DevOpscube team is testing a workload that requires multiple processes to run independently within the same Kubernetes Pod. Each container must perform a different task while remaining available for inspection.

Your task is to create the Pod busybox-multi-container with three busybox containers named container-1, container-2, and container-3.

The containers must be configured to:

  • container-1 : execute ls and remain running for 3600 seconds.
  • container-2 : print This is devopscube and remain running for 1000 seconds.
  • container-3 : print the current date and remain running for 3600 seconds.

After deploying the Pod, verify that all three containers are running and collect the output generated by each container. Save the logs for each container to separate files named container1.log, container2.log, and container3.log.

📚
For more details about Multi-Container Pod, refer to the official Kubernetes documentation.

Generate the Pod Manifest

You can use kubectl run with --dry-run=client to generate a basic Pod manifest:

$ kubectl run busybox-multi-container \
  --image=busybox \
  --dry-run=client \
  -o yaml \
  --command -- /bin/sh -c "ls; sleep 3600" \
  > busybox-multi-container.yaml

The --dry-run=client option generates the YAML without creating the Pod.

The -o yaml option outputs the resource definition in YAML format.

The --command option allows you to specify the command that the container should execute.

Edit the Pod Manifest

Open busybox-multi-container.yaml and configure the three containers.

The final manifest should look like this:

busybox-multi-container.yaml
apiVersion: v1 kind: Pod metadata: name: busybox-multi-container spec: containers: - name: container-1 image: busybox command: - /bin/sh - -c - ls; sleep 3600 - name: container-2 image: busybox command: - /bin/sh - -c - echo This is devopscube; sleep 1000 - name: container-3 image: busybox command: - /bin/sh - -c - date; sleep 3600

The Pod contains three containers, all using the busybox image.

Understand the Container Commands

Container 1 runs ls first and then keeps running for 3600 seconds using the sleep command.

Container 2 prints This is devopscube and then keeps running for 1000 seconds.

Container 3 prints the current date and time and then keeps running for 3600 seconds.

Apply the Manifest

Create the Pod:

$ kubectl apply -f busybox-multi-container.yaml
pod/busybox-multi-container create

You should output confirmation that the Pod was created successfully

Verify the Pod Status

Check the Pod:

$ kubectl get pod busybox-multi-container

NAME                      READY   STATUS    RESTARTS   AGE
busybox-multi-container   3/3     Running   0          3m

The important values are READY: 3/3 and STATUS: Running. The 3/3 value confirms that all three containers in the Pod are ready and running successfully.

Check the Logs of Container 1

Because the Pod contains multiple containers, specify the container with the -c option:

kubectl logs busybox-multi-container -c container-1

Container 1 should produce output from the ls command.

Save Container Logs to Files

Save the logs from each container to the required files.

Container 1

kubectl logs busybox-multi-container -c container-1 > container1.log

Container 2

kubectl logs busybox-multi-container -c container-2 > container2.log

Container 3

kubectl logs busybox-multi-container -c container-3 > container3.log

The three files are now:

$ ls

container2.log container1.log container3.log  

Verify the Log Files

Check each log file using the cat command. The first file should contain the output from ls, the second should contain This is devopscube, and the third should contain the output from date.

Why Is -c Required?

A Pod can contain multiple containers, so Kubernetes needs to know which container's logs you want to retrieve. The -c or --container option specifies the container name.

For example, kubectl logs busybox-multi-container -c container-1 retrieves logs from container-1, while using -c container-2 or -c container-3 retrieves logs from the respective containers.

Why Does the Pod Need sleep?

Commands such as ls and date finish immediately. If a container runs only these commands, it exits once the command completes. The sleep command keeps each container's main process running for a specified duration.

In this example, container-1 runs ls followed by sleep 3600, container-2 runs echo followed by sleep 1000, and container-3 runs date followed by sleep 3600. This allows all three containers to remain in the Running state.

CKA Exam Tips

These tips will help you quickly identify key Kubernetes concepts and avoid common mistakes during the CKA exam and CKA multi-container exam practice

Tip 2: Kubernetes multi-container Pods Use One containers List

All containers belong under:

spec:
  containers:

For example:

spec:
  containers:
  - name: container-1
    image: busybox

  - name: container-2
    image: busybox

  - name: container-3
    image: busybox

Tip 2: Every Container Needs a Unique Name

The three containers must have different names:

container-1
container-2
container-3

You cannot give multiple containers in the same Pod the same name.

Tip 3: A Pod Can Be Running Without All Containers Being Ready

The STATUS column and READY column provide different information.

For this task, you want:

READY    3/3
STATUS   Running

This confirms that all three containers are running and ready.