What is a Pod?
A Pod is a single instance of an application and is the smallest unit of a Kubernetes application. It provides an abstraction layer over containers. Normally, a pod runs only one application container inside it (one-to-one relationship). Key characteristics:- Each pod gets its own IP address. When a pod is recreated, a new IP address is assigned.
- Containers inside the pod can communicate with each other using
localhostas they share the same network space. - They can also share the same storage space (called a volume).
Pod Statuses
| Status | Description |
|---|---|
CrashLoopBackOff | The container is crashing repeatedly. The pod keeps trying to restart the container but it keeps failing on start. |
Commands
Pod YAML
Apply withkubectl apply -f pod.yaml.
| Kind | apiVersion |
|---|---|
| Pod | v1 |
| Service | v1 |
| ReplicaSet | apps/v1 |
| Deployment | apps/v1 |
pod.yaml
Restart Policy
Restart policy defines the behavior of the pod when the container crashes.| Policy | Description |
|---|---|
Always (default) | The container always restarts on any termination. |
OnFailure | The container restarts only on failure (non-zero exit status). |
Never | The container never restarts. |
pod.yaml
Init Containers
An init container is a special type of container that runs before the main container. It runs only once and must complete successfully before the main container starts. Use cases:- Wait for a database to be ready
- Prepare a configuration file
- Load data
init-container.yaml
If you specify multiple init containers, they will be run in order.
Multi-Container Pods
Multi-container pods act as helper containers. The containers can communicate directly via localhost as they share the same network space, the same storage space, and the same lifecycle (created and destroyed together).Design Patterns
Sidecar Pattern
Sidecar Pattern
A sidecar container is attached to the main container to extend or enhance it. For example, a sidecar can collect logs from the main container and forward them to a centralized logging system.Use cases: Logging, Monitoring, Security, Data synchronization
sidecar.yaml
Adapter Pattern
Adapter Pattern
An adapter container acts as a translator between the main container and external systems, transforming data into a standardized format before sending it out.Use cases: Data transformation, Protocol translation, API adaptation, Standardizing data format
adapter.yaml
Ambassador Pattern
Ambassador Pattern
An ambassador container acts as a proxy to external services. It hides the complexity of external services from the main container, managing all incoming and outgoing network traffic.Use case: Your application connects to different databases based on the environment (dev/staging/prod). The ambassador manages the connection so the main app always connects to
localhost.ambassador.yaml
ConfigMapstores database connections per environment.- The
ambassadorcontainer reads env variables and connects to the correct database. - The
backendcontainer connects to theambassadoraslocalhost.