Skip to content

Kubernetes Pod

A Pod is the smallest and most basic unit in the Kubernetes object model, representing a single instance of an application^[400-devops__06-Kubernetes__k8s-ithelp__Day3__README.md]. While it is often used to manage a single container, a Pod can encapsulate one or more containers that must run together on the same host^[400-devops__06-Kubernetes__k8s-ithelp__Day3__README.md]1.

Logically, a Pod acts as the "host" for its containers, managing shared resources such as storage ([Volumes]) and networking environments^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md].

Core Characteristics

Resource Sharing

The primary function of a Pod is to group containers that need to be tightly coupled. All containers within a single Pod share the same Network Namespace, meaning they share the same IP address and port space^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]. They can also be configured to share storage volumes, allowing data to be exchanged between containers (e.g., an app container writing logs and a sidecar container shipping them)^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md].

Scheduling and Lifecycle

Pods are ephemeral and scheduled onto Nodes. Once a Pod is scheduled to a Node, it remains there until termination (by deletion, eviction, or error)^[400-devops__06-Kubernetes__k8s-ithelp__Day3__README.md]. The control plane component kube-scheduler assigns new Pods to suitable Worker Nodes based on resource requirements and policies^[400-devops__06-Kubernetes__k8s-ithelp__Day3__README.md].

Definition and Structure

A Pod is typically defined using a YAML configuration file. Key fields include:

  • Metadata: Contains name and labels. Labels are key-value pairs used to organize and select Pods^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md].
  • Spec: Defines the desired state of the Pod, including the list of containers to run^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md].
    • containers: Specifies the container image (e.g., from Docker Hub), ports (containerPort), and environment variables^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md].

Lifecycle Hooks

Pods can define lifecycle hooks to manage container startup and shutdown: * postStart: Executes immediately after the container is created^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]. * preStop: Executes immediately before the container is terminated^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md].

Pod Phases

A Pod's status indicates where it is in its lifecycle^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]:

  1. Pending: The Pod has been accepted by the cluster, but one or more containers have not been created. This often happens while waiting for scheduling or image downloading.
  2. Running: The Pod has been bound to a Node, and all containers have been created. At least one container is still running or is in the process of starting.
  3. Succeeded: All containers in the Pod have terminated successfully, and will not be restarted.
  4. Failed: All containers in the Pod have terminated, and at least one terminated in failure.
  5. Unknown: The state of the Pod could not be obtained (usually a communication error between the node and the control plane).

Networking and Exposure

By default, a Pod's IP address is only accessible within the Kubernetes cluster. To interact with a running Pod locally, kubectl port-forward can be used to map a local port to the Pod's port^[400-devops-06-kubernetes-k8s-ithelp-day6-readme.md].

For stable network access and load balancing within the cluster, Pods are typically abstracted using a Service.

Management Concepts

While Pods can be managed directly, they are often managed by higher-level controllers: * ReplicaSet: Ensures a specified number of Pod replicas are running at any given time^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]. * Deployment: Manages ReplicaSets and facilitates "Rolling Updates" by gradually replacing old Pods with new ones^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md].

Sources

  • 400-devops__06-Kubernetes__k8s-ithelp__Day3__README.md
  • 400-devops__06-Kubernetes__k8s-ithelp__Day6__README.md
  • 400-devops-06-kubernetes-k8s-ithelp-day6-readme.md
  • 400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md

  1. Source text distinguishes between "Node" as the smallest host unit and "Pod" as the smallest scheduling unit. A Node hosts one or more Pods^[400-devops__06-Kubernetes__k8s-ithelp__Day3__README.md].