Pod 深度解析
Pod 是什么
Pod 是 Kubernetes 中最小的调度单元,是一组共享网络和存储的容器集合。同一 Pod 内的容器:
- 共享同一个网络命名空间(相同 IP,可通过 localhost 通信)
- 可以共享存储卷(Volume)
- 共享 IPC 命名空间(可通过共享内存通信)
Pod
├── Pause 容器(网络命名空间持有者)
├── Init 容器 1(顺序执行,完成后退出)
├── Init 容器 2
├── 业务容器 A
├── 业务容器 B(Sidecar)
└── 共享 VolumePod 完整 Spec
yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: default
labels:
app: my-app
version: v1
annotations:
description: "示例 Pod"
spec:
# 调度相关
nodeName: node1 # 直接指定节点(跳过调度器)
nodeSelector:
disktype: ssd
serviceAccountName: my-sa # 使用指定 ServiceAccount
# 安全上下文(Pod 级别)
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
runAsNonRoot: true
# Init 容器(按顺序执行,全部成功后才启动业务容器)
initContainers:
- name: init-db
image: busybox:1.35
command: ['sh', '-c', 'until nc -z db-service 5432; do sleep 2; done']
# 业务容器
containers:
- name: app
image: my-app:v1.0
ports:
- containerPort: 8080
protocol: TCP
# 资源限制
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
# 环境变量
env:
- name: DB_HOST
value: "postgres-service"
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-secret
key: password
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name # 注入 Pod 元数据
# 挂载 Volume
volumeMounts:
- name: config-vol
mountPath: /etc/config
readOnly: true
- name: data-vol
mountPath: /data
# 健康检查
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
periodSeconds: 5
# 生命周期钩子
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo started > /tmp/started"]
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 5"] # 优雅退出等待
# 安全上下文(容器级别)
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
add: ["NET_BIND_SERVICE"]
# Sidecar 容器
- name: log-collector
image: fluentd:v1.16
volumeMounts:
- name: log-vol
mountPath: /var/log/app
# Volume 定义
volumes:
- name: config-vol
configMap:
name: my-config
- name: data-vol
persistentVolumeClaim:
claimName: my-pvc
- name: log-vol
emptyDir: {}
# 重启策略
restartPolicy: Always # Always | OnFailure | Never
# 优雅终止时间
terminationGracePeriodSeconds: 30
# DNS 配置
dnsPolicy: ClusterFirst
dnsConfig:
options:
- name: ndots
value: "2"
# 主机网络(直接使用节点网络)
hostNetwork: false
hostPID: false
hostIPC: falsePod 生命周期
Pending → Running → Succeeded/Failed
│
├── Pending:等待调度或镜像拉取
├── Running:至少一个容器在运行
├── Succeeded:所有容器成功退出(Job 场景)
├── Failed:所有容器退出,至少一个失败
└── Unknown:无法获取 Pod 状态容器状态
bash
# 查看 Pod 详情
kubectl describe pod my-pod
# 容器状态:
# Waiting:等待启动(原因:ContainerCreating/ImagePullBackOff/CrashLoopBackOff)
# Running:正在运行
# Terminated:已终止(exitCode)常见问题排查
bash
# Pod 一直 Pending
kubectl describe pod my-pod | grep -A5 Events
# 常见原因:资源不足、节点选择器不匹配、PVC 未绑定
# Pod CrashLoopBackOff
kubectl logs my-pod --previous # 查看上次崩溃日志
kubectl logs my-pod -c container-name # 多容器时指定容器
# 进入运行中的容器
kubectl exec -it my-pod -- /bin/bash
kubectl exec -it my-pod -c sidecar -- sh
# 临时调试容器(不修改原 Pod)
kubectl debug -it my-pod --image=busybox --target=appDownward API
将 Pod 自身信息注入到容器:
yaml
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: CPU_LIMIT
valueFrom:
resourceFieldRef:
containerName: app
resource: limits.cpu