DEV Community

Cover image for Kubernetes: Understanding Multi-Container Pods
Jensen Jose
Jensen Jose

Posted on

Kubernetes: Understanding Multi-Container Pods

Welcome back to our Kubernetes series! In this installment, we'll dive into the concept of multi-container pods and explore related concepts through a hands-on demo. By the end of this post, you'll have a solid understanding of how multi-container pods work and how to set them up in your Kubernetes cluster.

What is a Multi-Container Pod?

A multi-container pod is a Kubernetes pod that runs more than one container. These containers share the same network namespace, meaning they can communicate with each other directly via localhost. This setup is useful when you have tightly coupled applications that need to share resources and data.

There are two main types of containers in a multi-container pod:

  1. Init Containers: These run and complete before the main application containers start. They are typically used for setup tasks.
  2. Sidecar Containers: These run alongside the main application container and provide support, such as logging, monitoring, or proxying.

Image description

Example Scenario

Let's imagine you have a Kubernetes pod running an NGINX application container. You might also have an init container that sets up necessary configurations before the NGINX container starts. Additionally, you could have a sidecar container that continuously monitors logs and metrics.

Setting Up a Multi-Container Pod

We'll now walk through creating a multi-container pod with an init container and a main application container.

Step 1: Define the Pod Manifest
We'll start by creating a YAML file for our pod. Open your preferred code editor and create a new file called multi-container-pod.yaml. Here's the content for the file:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
  - name: app-container
    image: busybox
    command: ['sh', '-c', 'echo The app is running; sleep 3600']
  initContainers:
  - name: init-service
    image: busybox
    command: ['sh', '-c', 'until nslookup myservice.default.svc.cluster.local; do echo waiting for service to be up; sleep 2; done']
  - name: init-db
    image: busybox
    command: ['sh', '-c', 'until nslookup mydb.default.svc.cluster.local; do echo waiting for db service to be up; sleep 2; done']
Enter fullscreen mode Exit fullscreen mode

In this file:

  • The app-container runs a simple command to echo a message and sleep.
  • The init-service and init-db containers wait for their respective services to be available before the main container starts.

Step 2: Apply the Manifest
Apply the manifest to your Kubernetes cluster using the following command:

kubectl apply -f multi-container-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the Pod
Check the status of the pod to ensure it's running correctly:

kubectl get pods myapp-pod
Enter fullscreen mode Exit fullscreen mode

You should see the pod in a Running state once both init containers have completed their tasks.

Step 4: Create the Services
Next, we need to create the services that our init containers are waiting for. We'll create two deployments and expose them as services.

kubectl create deployment nginx-deploy --image=nginx
kubectl expose deployment nginx-deploy --name=myservice --port=80

kubectl create deployment redis-deploy --image=redis
kubectl expose deployment redis-deploy --name=mydb --port=6379
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify the Services
Ensure the services are running:

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

You should see myservice and mydb listed among the services.

Conclusion

In this blog post, we've explored the concept of multi-container pods in Kubernetes. We've seen how init containers can be used to perform setup tasks before the main application container starts. We've also learned how to create and expose services that the init containers depend on.

Stay tuned for more in our Kubernetes series, where we'll continue to dive deeper into various aspects of Kubernetes and container orchestration. Don't forget to share your learnings on social media and help spread the word!

Happy learning!

For further reference, check out the detailed YouTube video here:

Top comments (0)