Kubernetes

How to create Multi-containers Kubernetes Pods

Damian Igbe
Feb. 26, 2022, 4:14 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

In this previous tutorial, we discussed how to create a single-container Kubernetes  pod from the CLI. You can only create a single container pod from the CLI using kubectl run command. Also, whenever you create a pod from the CLI, a Deployment is automatically created so if you want to:

  • create multi-containers Kubernetes pods
  • or a multi-containers Kubernetes pods with no Deployment,

you need to first create a Kubernetes manifest file in yaml or json and then use kubectl create command.

As best-practice, note that multi-containers Kubernetes pods does not apply to all situations and should be used only when truly needed. This is because:

  • Containers inside the same Pod are co-located and so cannot be spread to different hosts
  • Containers inside the same pod cannot be scaled separately even if required because containers  always share the same fate.
  • Containers are tightly-coupled, but  loosely-coupled may be preferable sometimes. For example, containers inside the same pod share the same network IP and volume.

The scenarios that are best for multi-containers Kubernetes pods are:

  • for performance reasons: for example, Redis cache and web-server can be tightly coupled in a pod for better performance
  • health-check: one of the pod can be used to monitor the other when tightly coupled
  • log-collecting processes-one pod tightly coupled with another can be used to collect the logs of the other

Here is a walkthrough of creating a multi-containers Kubernetes pod. You can watch the video below:

Creating the Kubernetes Manifest file

Here is a simple manifest that creates  2 containers.

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
  - name: alpine
    image: alpine
    command: ["watch", "wget", "-qO-", "localhost"]

Create multi-containers Kubernetes Pods with kubectl create command

Let’s use the manifest file to create a multi-containers Kubernetes pods.

$kubectl create -f two containers.yaml

Here we have 2 containers running

$kubectl get pods

NAME             READY     STATUS    RESTARTS   AGE

two-containers   2/2       Running   0          8s

 

Confirm that a Deployment is not created

As stated above, a deployment is not created, unlike using kubectl run that automatically creates a deployment.

$ kubectl get deployments
No resources found.

Conclusion

This tutorial shows how to create multi-containers Kubernetes pods using kubectl create command. We showed that a deployment was not created. As a general advice, it's not a best practice to create pods without monitoring them with a deployment. This will be addressed in the next tutorials.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role