A Pod in Kubernetes is the smallest deployable unit that contains one or more closely related containers. These containers share network and storage resources.
Pod Characteristics
-
Shared Network Namespace: Containers in the same Pod share the same IP address and port space, and can communicate with each other via localhost.
-
Shared Storage Volumes: Containers in a Pod can share mounted storage volumes, enabling data sharing and persistence.
-
Atomic Scheduling: The Pod is scheduled as a whole to run on the same Node.
-
Ephemeral: Pods are temporary and replaceable. When a Pod is deleted or a Node fails, the Pod does not automatically recover.
Pod Lifecycle
The Pod lifecycle includes the following phases:
-
Pending: The Pod has been created, but the containers have not started yet, possibly because the image is downloading or resources are insufficient.
-
Running: All containers in the Pod have been created, and at least one container is running.
-
Succeeded: All containers in the Pod have terminated successfully.
-
Failed: All containers in the Pod have terminated, but at least one container terminated in a failed state.
-
Unknown: Unable to get the Pod's status, usually because communication with the Node where the Pod is located has failed.
Pod Restart Policies
Kubernetes supports three Pod restart policies:
-
Always: Always restart the container when it fails. This is the default policy.
-
OnFailure: Only restart when the container fails with a non-zero exit code.
-
Never: Do not restart when the container fails.
Pod and Container Relationship
A Pod is a wrapper for containers. A Pod can contain:
- A single main container (most common)
- One main container plus one or more sidecar containers (Sidecar pattern)
- Multiple cooperating containers
Best Practices
-
One Pod One Container: For most applications, it is recommended that a Pod contains only one container, which makes management and scaling easier.
-
Use Sidecar Pattern: When multiple closely cooperating containers are needed, you can use the Sidecar pattern, such as log collection, monitoring agents, etc.
-
Avoid Running Multiple Unrelated Containers in a Pod: This increases management complexity and is not conducive to scaling and troubleshooting.
-
Set Resource Limits Reasonably: Set CPU and memory requests and limits for Pods to avoid resource contention.
-
Use Health Checks: Configure livenessProbe and readinessProbe to ensure the health status of the Pod.