Kubernetes
Pod Configuration
Containers
Multi-Container Pods
DevOps

How to write a kubernetes pod configuration to start two containers

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding Kubernetes Pods and Their Configuration

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. At the core of Kubernetes' functionality is the concept of a "Pod." A Pod is the smallest deployable unit in Kubernetes and can run one or multiple containers that share the same network namespace.

When you need different containers to work closely together, they are usually defined within the same Pod. This article will guide you through how to configure a Kubernetes Pod to run two containers, providing a technical explanation and examples to enhance your understanding.


Why Use Multiple Containers in a Pod?

There are various scenarios where multiple containers in a single Pod make sense, such as:

  • Sidecar Pattern: The main application runs in one container, while a helper service runs in another. For example, a log collector or a proxy container.
  • Ambassador Pattern: A helper container that acts as an adapter between an external service and the main application.
  • Adapter/Anti-Pattern: For legacy systems where coupling components is necessary for compatibility.

Step-by-Step Guide to Create a Pod with Two Containers

Kubernetes Pod YAML Specification

A Pod specification in Kubernetes is defined using a YAML file. This file describes all the necessary configurations for the Pod, such as the containers to be used, the image, resources, ports, and more.

Here's a basic example YAML configuration for a Pod running two containers:

  • name: container1
    • containerPort: 80
  • name: container2
  • apiVersion: v1: Indicates the API version of the Kubernetes object.
  • kind: Pod: Declares the type of Kubernetes object being created is a Pod.
  • metadata: Provides data to identify the Pod such as name and labels.
  • spec: Describes the desired state of the Pod.
    • containers: This lists the containers within the Pod:
      • First Container (nginx):
        • name: Sets the identifier for the container.
        • image: Specifies the container image (nginx).
        • ports: Opens port 80 for HTTP requests.
      • Second Container (busybox):
        • name: Sets the identifier for the container.
        • image: Uses the "busybox" image, which is lightweight.
        • command: Executes a simple command in the shell and persists using sleep.
  • Ports and Network Policies: Define and manage network policies to secure communication between Pods.
  • Health Checks: Use livenessProbe and readinessProbe to ensure that your containers are running and ready.
  • Logging and Monitoring: Ensure logging is centralized for easier troubleshooting and use monitoring systems like Prometheus.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.