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
3. 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.
4. 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.
5. 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.
6. 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.
7. 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.
8. 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:
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:
Press i to enter insert mode and then type the YAML code below.
YAML Code for Creating Multiple Containers in a Pod
Note:
Pod Name:
testpod1First Container:
c00Second Container:
c01
After typing the code:
-
Press
ESC -
Type
:wqand press Enter to save and exit.
Step 3: Apply the Manifest File
Run the following command to create the pod:
You should see output like:
Step 4: Verify Pod Status
To check if the pod is running, execute:
Sample Output:
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:
Output:
Logs from Container c01:
Output:
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.
Comments
Post a Comment