Pod 基础

Pod 基础

什么是 Pod

Pod 是 Kubernetes 中最小的可部署单元。你可以把 Pod 理解为一个"容器组"。

为什么需要 Pod?

在 Docker 中,容器是最小单元。为什么 Kubernetes 引入了 Pod 这个概念?

原因:有些容器需要紧密协作,共享资源。

例如:

  • Web 应用容器 + 日志收集容器
  • 应用容器 + 数据同步容器
  • 应用容器 + 监控代理容器

这些容器需要:

  • 共享同一个网络(localhost 互访)
  • 共享存储卷
  • 同时调度到一个节点

Pod 的特点

1. 共享网络

Pod 内的所有容器共享同一个 IP 地址和端口空间。

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
  - name: sidecar
    image: busybox
    command: ['sh', '-c', 'while true; do wget -O- http://localhost:80; sleep 10; done']

在这个例子中:

  • nginx 监听 80 端口
  • sidecar 容器可以通过 localhost:80 访问 nginx

2. 共享存储

apiVersion: v1
kind: Pod
metadata:
  name: shared-volume-pod
spec:
  containers:
  - name: writer
    image: busybox
    command: ['sh', '-c', 'echo "Hello" > /data/message.txt; sleep 3600']
    volumeMounts:
    - name: shared-data
      mountPath: /data
  - name: reader
    image: busybox
    command: ['sh', '-c', 'cat /data/message.txt; sleep 3600']
    volumeMounts:
    - name: shared-data
      mountPath: /data
  volumes:
  - name: shared-data
    emptyDir: {}

两个容器共享 /data 目录,writer 写入的文件,reader 可以读取。

3. 生命周期一致

Pod 内的容器:

  • 同时创建
  • 同时销毁
  • 调度到同一个节点

创建 Pod

方法 1:命令行创建

# 运行一个 nginx Pod
kubectl run nginx --image=nginx

# 查看 Pod
kubectl get pods

# 查看详细信息
kubectl describe pod nginx

# 查看日志
kubectl logs nginx

# 进入容器
kubectl exec -it nginx -- bash

# 删除 Pod
kubectl delete pod nginx

方法 2:YAML 配置文件

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
    env: dev
spec:
  containers:
  - name: nginx
    image: nginx:1.21
    ports:
    - containerPort: 80
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

应用配置:

kubectl apply -f pod.yaml

Pod 的生命周期

Pod 有以下几个阶段:

1. Pending(等待中)

Pod 已创建,但容器还未运行。可能原因:

  • 正在下载镜像
  • 等待调度到节点
  • 资源不足

2. Running(运行中)

Pod 已绑定到节点,至少一个容器正在运行。

3. Succeeded(成功)

Pod 中所有容器成功终止,且不会重启。

4. Failed(失败)

Pod 中所有容器终止,至少一个容器失败。

5. Unknown(未知)

无法获取 Pod 状态,通常是节点通信问题。

查看 Pod 状态:

kubectl get pods

NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m

容器重启策略

通过 restartPolicy 控制容器失败后的行为:

apiVersion: v1
kind: Pod
metadata:
  name: restart-demo
spec:
  restartPolicy: Always  # Always | OnFailure | Never
  containers:
  - name: app
    image: myapp
  • Always(默认):总是重启
  • OnFailure:失败时重启
  • Never:从不重启

健康检查

Kubernetes 提供三种探针检查容器健康:

1. Liveness Probe(存活探针)

检查容器是否存活,失败则重启容器。

apiVersion: v1
kind: Pod
metadata:
  name: liveness-pod
spec:
  containers:
  - name: app
    image: myapp
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 3
      periodSeconds: 3

2. Readiness Probe(就绪探针)

检查容器是否准备好接收流量,失败则从 Service 中移除。

readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 5

3. Startup Probe(启动探针)

检查容器是否启动成功,适用于启动慢的应用。

startupProbe:
  httpGet:
    path: /startup
    port: 8080
  failureThreshold: 30
  periodSeconds: 10

资源管理

为容器指定资源需求和限制:

resources:
  requests:    # 最小需求
    memory: "64Mi"
    cpu: "250m"
  limits:      # 最大限制
    memory: "128Mi"
    cpu: "500m"
  • requests:调度器用于选择节点,保证分配
  • limits:容器运行时的硬限制,超出会被限制

单位说明:

  • CPU:1 = 1 核心,250m = 0.25 核心
  • 内存:Mi = MiB,Gi = GiB

实战练习

练习 1:创建简单 Pod

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: app
    image: nginx
    ports:
    - containerPort: 80
kubectl apply -f pod.yaml
kubectl get pods
kubectl port-forward my-app 8080:80
# 访问 http://localhost:8080

练习 2:多容器 Pod

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
  - name: debian
    image: debian
    command: ["sleep", "3600"]
kubectl apply -f pod.yaml
kubectl exec -it two-containers -c nginx -- bash
kubectl exec -it two-containers -c debian -- bash

常见问题

Pod 一直 Pending?

kubectl describe pod <pod-name>

可能原因:

  • 镜像拉取失败
  • 资源不足
  • 节点污点(Taint)限制

Pod 频繁重启?

kubectl logs <pod-name> --previous

可能原因:

  • 应用崩溃
  • 健康检查失败
  • OOMKilled(内存超限)

小结

Pod 是 Kubernetes 的核心概念:

  • 最小单元:Pod 是最小的可部署单元
  • 容器组:一个 Pod 可以包含多个容器
  • 共享资源:Pod 内容器共享网络和存储
  • 短暂性:Pod 是临时的,不应该期望 Pod 永久存在
  • 管理工具:实践中通常不直接创建 Pod,而是使用 Deployment 等控制器

下一章我们将学习如何使用 Deployment 管理 Pod。