DEV Community

Naveen Jayachandran
Naveen Jayachandran

Posted on

Kubernetes – Creating Multiple Containers in a Pod

Introduction
Kubernetes is an open-source container orchestration platform developed by Google in 2014 and written in Golang. It automates container deployment, load balancing, scaling, and management across multiple environments — including physical, virtual, and cloud-based infrastructures.

All major cloud providers (like AWS, Azure, and GCP) support Kubernetes as a managed service. Kubernetes ensures your containers run efficiently and reliably through its powerful automation and scheduling capabilities.

Kubernetes Architecture Overview

  1. Kube-API Server
    The API Server is the main entry point to the Kubernetes control plane.
    It directly interacts with users through YAML or JSON configuration files and processes requests to manage cluster resources. It acts as the frontend of the control plane.

  2. ETCD
    etcd is a consistent, distributed key-value store that maintains the cluster state and metadata.
    It ensures high availability and reliability of data across nodes.

Key Features of etcd:

Secure and fault-tolerant

Fully replicated

High performance

Highly available

  1. Controller Manager
    The Controller Manager continuously ensures that the actual state of the cluster matches the desired state defined in your configuration files. It automates the lifecycle of pods, nodes, and other Kubernetes objects.

  2. Kube-Scheduler
    The Kube-Scheduler assigns newly created pods to suitable nodes based on resource availability and constraints. It decides where each pod should run within the cluster.

  3. Pod
    A Pod is the smallest deployable unit in Kubernetes. It can contain one or more containers that share the same network namespace (IP address and ports).

Although the best practice is to run one container per pod, sometimes multiple containers are necessary — for example, when they must share storage or communicate directly within the same environment.

  1. Container
    A container is the runnable instance of an image with all dependencies bundled together.
    It is lightweight, fast to start, and does not require pre-allocated memory.

  2. Kube-Proxy
    Kube-Proxy manages network communication inside and outside the cluster.
    It assigns a unique IP address to each pod and handles load balancing and network routing.

  3. Kubelet
    The Kubelet is an agent that runs on every node in the cluster.
    It receives instructions from the API Server and ensures that containers described in Pod specifications are running and healthy.

Creating Multiple Containers in a Single Pod
Let’s go step-by-step through how to create a pod with multiple containers.

Step 1: Open Your Kubernetes Environment
Ensure that Kubernetes is properly installed and configured on your machine or cluster.
You should be able to run:

kubectl version
Step 2: Create a Manifest File
All Kubernetes resources are created using manifest files written in YAML format.
We’ll create one for our multi-container pod.

Run the following command to create a file:

vi multicontainer.yml
Press i to enter insert mode and then type the YAML code below.

YAML Code for Creating Multiple Containers in a Pod
apiVersion: v1
kind: Pod
metadata:
name: testpod1
spec:
containers:
- name: c00
image: ubuntu
command: ["/bin/bash", "-c", "while true; do echo Hello-Coder; sleep 5; done"]
- name: c01
image: ubuntu
command: ["/bin/bash", "-c", "while true; do echo Hello-Programmer; sleep 5; done"]
After typing the code:

Press ESC

Type :wq and press Enter to save and exit.

Step 3: Apply the Manifest File
Run the following command to create the pod:

kubectl apply -f multicontainer.yml
You should see output like:

pod/testpod1 created
Step 4: Verify Pod Status
To check if the pod is running, execute:

kubectl get pods
Sample Output:

NAME READY STATUS RESTARTS AGE
testpod1 2/2 Running 0 30s
The READY column showing 2/2 confirms that both containers inside the pod are running successfully.

Step 5: View Container Logs
You can view the logs of each container individually using:

Logs from Container c00:
kubectl logs -f testpod1 -c c00
Output:

Hello-Coder
Hello-Coder
...
Logs from Container c01:
kubectl logs -f testpod1 -c c01
Output:

Hello-Programmer
Hello-Programmer
...
Summary
Pod Name: testpod1

Container 1: c00 → prints Hello-Coder

Container 2: c01 → prints Hello-Programmer

You have successfully created a multi-container pod in Kubernetes.
Although each pod typically contains a single container, multiple containers can coexist in the same pod when they share resources or need close coordination.

Top comments (0)