K8s – Question13

Use context: kubectl config use-context k8s-c1-H

Create a Pod named multi-container-playground in Namespace default with three containers, named c1c2 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:

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.

In this YAML configuration:

  • The c1 container runs NGINX and includes an environment variable MY_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 by c2.
  • 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:

Check the status of the Pod to ensure that all containers are running correctly:

You should see output indicating that all three containers are running:

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:

The output should display the name of the node where the Pod is running:

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:

You should see output similar to this, showing the timestamps generated by the c2 container:

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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *