Use context: kubectl config use-context k8s-c1-H
Create a Pod named multi-container-playground
in Namespace default
with three containers, named c1
, c2
and c3
. There should be a volume attached to that Pod and mounted into every container, but the volume shouldn’t be persisted or shared with other Pods.
Container c1
should be of image nginx:1.17.6-alpine
and have the name of the node where its Pod is running available as environment variable MY_NODE_NAME
.
Container c2
should be of image busybox:1.31.1
and write the output of the date
command every second in the shared volume into file date.log
. You can use while true; do date >> /your/vol/path/date.log; sleep 1; done
for this.
Container c3
should be of image busybox:1.31.1
and constantly send the content of file date.log
from the shared volume to stdout. You can use tail -f /your/vol/path/date.log
for this.
Check the logs of container c3
to confirm correct setup.
Creating and Managing Multi-Container Pods in Kubernetes
In Kubernetes, multi-container Pods allow you to run multiple containers within the same Pod, sharing resources like volumes and network. This is useful for tightly coupled applications where containers need to communicate with each other or share data. In this guide, we’ll walk through creating a multi-container Pod that includes an NGINX server and two BusyBox containers that interact with each other.
Step 1: Create the Pod Template
We begin by creating a basic Pod template using the following command:
1 2 3 |
kubectl run multi-container-playground --image=nginx:1.17.6-alpine --dry-run=client -o yaml > 13.yaml |
This command generates a YAML file named 13.yaml
, which serves as the starting point for our multi-container Pod.
Step 2: Modify the YAML to Add Additional Containers
Next, open the 13.yaml
file in your text editor and modify it to include two additional BusyBox containers. The first BusyBox container writes timestamps to a shared volume, and the second reads and displays these timestamps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 13.yaml apiVersion: v1 kind: Pod metadata: name: multi-container-playground labels: run: multi-container-playground spec: containers: - image: nginx:1.17.6-alpine name: c1 env: - name: MY_NODE_NAME valueFrom: fieldRef: fieldPath: spec.nodeName volumeMounts: - name: vol mountPath: /vol - image: busybox:1.31.1 name: c2 command: ["sh", "-c", "while true; do date >> /vol/date.log; sleep 1; done"] volumeMounts: - name: vol mountPath: /vol - image: busybox:1.31.1 name: c3 command: ["sh", "-c", "tail -f /vol/date.log"] volumeMounts: - name: vol mountPath: /vol volumes: - name: vol emptyDir: {} |
In this YAML configuration:
- The
c1
container runs NGINX and includes an environment variableMY_NODE_NAME
that stores the name of the node the Pod is running on. - The
c2
container writes the current date and time to a log file every second. - The
c3
container continuously reads and outputs the content of the log file written byc2
. - A shared volume named
vol
is used to store the log file, accessible by all containers in the Pod.
Step 3: Create and Run the Pod
With the YAML file configured, apply it to your Kubernetes cluster:
1 2 3 |
kubectl apply -f 13.yaml |
Check the status of the Pod to ensure that all containers are running correctly:
1 2 3 |
kubectl get pod multi-container-playground |
You should see output indicating that all three containers are running:
1 2 3 4 5 6 |
NAME READY STATUS RESTARTS AGE multi-container-playground 3/3 Running 0 95s |
Step 4: Verify the Environment Variable
To confirm that the c1
container has the correct node name stored in the MY_NODE_NAME
environment variable, run the following command:
1 2 3 |
kubectl exec multi-container-playground -c c1 -- env | grep MY_NODE_NAME |
The output should display the name of the node where the Pod is running:
1 2 3 |
MY_NODE_NAME=cluster1-node2 |
Step 5: Check the Logs
Finally, let’s verify the logging output from the c3
container, which reads the log file created by the c2
container:
1 2 3 |
kubectl logs multi-container-playground -c c3 |
You should see output similar to this, showing the timestamps generated by the c2
container:
1 2 3 4 5 6 7 8 9 |
Sat Dec 7 16:05:10 UTC 2077 Sat Dec 7 16:05:11 UTC 2077 Sat Dec 7 16:05:12 UTC 2077 Sat Dec 7 16:05:13 UTC 2077 Sat Dec 7 16:05:14 UTC 2077 |
Conclusion
In this guide, we’ve successfully created a multi-container Pod in Kubernetes, demonstrating how containers can work together by sharing volumes and passing environment variables. This approach is powerful for applications where different containers need to collaborate closely, making full use of the Pod abstraction in Kubernetes.