一、Pod 概述
Pod 是 Kubernetes 中部署应用的最小工作负载单元,每个工作负载(如 Deployment, StatefulSet 等)通常会创建一个或多个 Pod;Pod 内部进一步运行容器。理解 Pod 的结构与功能,对于掌握 Kubernetes 应用部署至关重要。
1. Pod 的定义
Pod 是由一个或多个容器组成的一组,这些容器共享网络和存储资源,并且总是一起调度运行,位于同一个节点上:contentReference[oaicite:2]{index=2}。
- 它们共享网络命名空间(network namespace)和存储卷(volumes):contentReference[oaicite:3]{index=3}。
- Pod 是 Kubernetes 中最小的可调度单位,控制平面直接管理 Pod 而非容器:contentReference[oaicite:4]{index=4}。
2. Pod 的 IP 地址
| 特性 | 说明 |
|---|---|
| IP 分配方式 | Pod 从 Pod 网络CIDR 获取一个唯一的 IP 地址,由网络插件负责分配:contentReference[oaicite:5]{index=5} |
| IP 性质 | 临时性(Ephemeral)——Pod 被删除重建时会分配新的 IP:contentReference[oaicite:6]{index=6} |
| 外部访问方式 | 访问 Pod 应使用 Pod IP 而非容器 IP(容器共享该 Pod IP) |
此外,Pod 内部的多个容器全部使用该 IP,并共享端口空间和网络接口:contentReference[oaicite:7]{index=7}。
3. 容器之间的网络通信
- Pod 内的容器可以通过
localhost(环回接口)直接通信,因为它们共享网络命名空间:contentReference[oaicite:8]{index=8}。 - 所有容器都在同一个网络堆栈中,使用同一个 IP 和端口空间:contentReference[oaicite:9]{index=9}。
4. 容器之间共享存储
- Pod 内的容器可以挂载同一个 Volume,从而实现文件级的数据共享:contentReference[oaicite:10]{index=10}。
- 建议将具有强关联关系的容器组合在同一个 Pod 内;避免将无关容器放入同 Pod,以免造成耦合和维护复杂化。
5. Pod 结构与网络模型
- Pod 内部启动一个 “Pause 容器” 用于初始化并持有其网络命名空间;其他主容器共享该网络堆栈:contentReference[oaicite:11]{index=11}。
- Pod 的网络模型支持跨 Pod 通信(通过 Pod IP)也支持 Pod 内通信(通过
localhost),体现了 Kubernetes 的一贯设计理念:contentReference[oaicite:12]{index=12}。