📖 Scenario: You are working on a Kubernetes cluster for a simple web application. This application needs two containers running together in the same Pod: one container serves the web content, and the other container logs the web server activity.
🎯 Goal: Create a Kubernetes Pod manifest with two containers inside it. One container runs the nginx web server, and the other container runs a simple busybox container that logs messages to a shared volume.
📋 What You'll Learn
Create a Pod manifest named multi-container-pod.yaml.
Define two containers inside the Pod: web-server using the nginx image and logger using the busybox image.
Add a shared emptyDir volume named shared-logs mounted at /var/log in both containers.
The logger container should run the command sh -c 'while true; do echo Logging... >> /var/log/log.txt; sleep 5; done' to simulate logging.
The web-server container should expose port 80.
💡 Why This Matters
🌍 Real World
Multi-container Pods are used when containers need to work closely together, like a web server and a helper container for logging or data processing.
💼 Career
Understanding multi-container Pods is essential for Kubernetes administrators and DevOps engineers to design efficient and maintainable applications.
Progress0 / 4 steps
1
Create the basic Pod manifest with metadata
Create a YAML file named multi-container-pod.yaml with a Pod manifest. Add apiVersion: v1, kind: Pod, and metadata with name: multi-container-pod.
Kubernetes
Hint
Start by defining the Pod with its API version, kind, and give it a name under metadata.
2
Add two containers with images and ports
Under spec, add two containers: one named web-server using image nginx exposing port 80, and another named logger using image busybox. Do not add commands or volumes yet.
Kubernetes
Hint
Remember to indent containers properly under spec and list each container with its name and image.
3
Add shared volume and mount paths, and logger command
Add an emptyDir volume named shared-logs under spec.volumes. Mount this volume at /var/log in both containers. Add the command sh -c 'while true; do echo Logging... >> /var/log/log.txt; sleep 5; done' to the logger container.
Kubernetes
Hint
Use emptyDir: {} to create a shared volume. Mount it in both containers at /var/log. Add the command as a list of strings in the logger container.
4
Display the final Pod manifest
Print the complete content of multi-container-pod.yaml to verify your multi-container Pod manifest.
Kubernetes
Hint
Use print(open('multi-container-pod.yaml').read()) to display the file content if running locally, or simply output the YAML content as a string.
Practice
(1/5)
1. What is the main purpose of having multiple containers inside a single Kubernetes Pod?
easy
A. To isolate containers completely from each other
B. To run containers on different nodes for load balancing
C. To run containers with different Kubernetes versions
D. To allow containers to share network and storage resources closely
Solution
Step 1: Understand Pod resource sharing
Containers in the same Pod share network and storage, enabling close cooperation.
Step 2: Compare options
Only To allow containers to share network and storage resources closely correctly describes this sharing. Other options describe different Kubernetes features.
Final Answer:
To allow containers to share network and storage resources closely -> Option D
Quick Check:
Multi-container Pods share resources = C [OK]
Hint: Pods share network/storage among containers [OK]
Common Mistakes:
Thinking containers run on different nodes in one Pod
Believing containers in a Pod are isolated
Confusing Pod with Kubernetes version management
2. Which of the following is the correct way to define multiple containers inside a single Pod YAML spec?
easy
A. containers: [{name: 'app', image: 'app-image'}, {name: 'sidecar', image: 'sidecar-image'}]
B. container: [{name: 'app', image: 'app-image'}, {name: 'sidecar', image: 'sidecar-image'}]
C. containers: {name: 'app', image: 'app-image', name: 'sidecar', image: 'sidecar-image'}
D. pods: [{name: 'app', image: 'app-image'}, {name: 'sidecar', image: 'sidecar-image'}]
Solution
Step 1: Recall Pod spec syntax
The Pod spec uses a key 'containers' with a list of container objects.
Step 2: Validate options
containers: [{name: 'app', image: 'app-image'}, {name: 'sidecar', image: 'sidecar-image'}] correctly uses 'containers' as a list. The other options use wrong keys or wrong data structures.
A. Pod will fail because multiple containers are not allowed
B. Pod 'multi-container-pod' will be Running with two containers inside
C. Pod will be Pending indefinitely
D. Pod will run only the first container and ignore the second
Solution
Step 1: Analyze Pod spec
The Pod defines two containers: 'app' running nginx and 'sidecar' running busybox sleep command.
Step 2: Understand Kubernetes behavior
Kubernetes supports multiple containers in one Pod; both will run together. The Pod status will be Running if both containers start successfully.
Final Answer:
Pod 'multi-container-pod' will be Running with two containers inside -> Option B
Quick Check:
Multi-container Pod runs all containers = A [OK]
Hint: Multiple containers run together; Pod status shows Running if all start [OK]
Common Mistakes:
Thinking multiple containers are not allowed in one Pod
Assuming Pod stays Pending without reason
Believing only one container runs per Pod
4. You created a Pod with two containers, but the second container keeps restarting. What is the most likely cause?
medium
A. The second container's command or image is incorrect causing crash loop
B. Kubernetes does not support more than one container per Pod
C. The Pod spec is missing the 'containers' key
D. The first container is using all CPU resources
Solution
Step 1: Identify container restart reasons
Containers restart if their command fails or image is invalid, causing crash loops.
Step 2: Eliminate other options
Kubernetes supports multiple containers per Pod. A missing 'containers' key causes Pod creation failure. Resource starvation usually affects all containers but does not cause restart loops.
Final Answer:
The second container's command or image is incorrect causing crash loop -> Option A
5. You want to add a logging container to your Pod that shares the same volume with the main app container. Which configuration is correct to achieve this?
hard
A. Mount the volume only in the logging container
B. Create separate volumes for each container and do not mount them
C. Define a shared volume under 'volumes' in Pod spec and mount it in both containers under 'volumeMounts'
D. Use different Pods for logging and app containers without shared volumes
Solution
Step 1: Understand volume sharing in Pods
Volumes defined at Pod level can be mounted by multiple containers to share data.
Step 2: Check options for volume usage
Define a shared volume under 'volumes' in Pod spec and mount it in both containers under 'volumeMounts' correctly defines a shared volume and mounts it in both containers. Other options either separate volumes or do not mount properly, preventing sharing.
Final Answer:
Define a shared volume under 'volumes' in Pod spec and mount it in both containers under 'volumeMounts' -> Option C
Quick Check:
Shared volumes require Pod-level volume and mounts in containers [OK]
Hint: Share volumes by defining once and mounting in all containers [OK]
Common Mistakes:
Defining separate volumes for each container
Not mounting volumes in all containers needing access
Using separate Pods instead of multi-container Pod
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.