Kubernetes: Understanding Multi-Container Pods
Tags: tutorial, kubernetes, docker, devops
Published: 2024-07-03 16:19:37

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mxybzregka36yso03w3m.png)

#### 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']
```
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
```
**Step 3: Verify the Pod**
Check the status of the pod to ensure it's running correctly:

```
kubectl get pods myapp-pod
```
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
```
**Step 5: Verify the Services**
Ensure the services are running:

```
kubectl get svc
```
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:
{% embed https://www.youtube.com/watch?v=yRiFq1ykBxc&list=WL&index=21 %}