16.3.2 Pod规格与常用字段
Pod 规格定义了容器运行所需的镜像、资源、网络、存储与调度约束等,是理解与编排应用的核心。常见字段集中在 metadata 与 spec 中,其中 spec 决定 Pod 的运行行为。
1. 基本结构与必填项#
apiVersion、kind、metadata、spec为基本结构。metadata.name为 Pod 名称,metadata.labels为标签,常用于选择器匹配与服务发现。
示例:最小可运行 Pod(/root/pod-min.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-min
labels:
app: demo
spec:
containers:
- name: web
image: nginx:1.25
应用并验证:
kubectl apply -f /root/pod-min.yaml
kubectl get pod demo-min -o wide
kubectl describe pod demo-min | sed -n '1,80p'
命令解释:
- apply -f:创建/更新资源
- get pod -o wide:查看节点、IP 等信息
- describe:查看事件、镜像拉取与调度细节
2. 容器定义(containers)#
containers:容器列表(至少一个)。name:容器名称,Pod 内唯一。image:镜像地址与标签(如nginx:1.25)。command、args:覆盖镜像默认启动命令与参数。env:环境变量,支持value与valueFrom(ConfigMap/Secret/FieldRef)。ports:容器端口声明(仅用于说明,不等同于服务暴露)。imagePullPolicy:镜像拉取策略(Always/IfNotPresent/Never)。workingDir、stdin、tty:工作目录与交互参数。
示例:带命令与环境变量的容器(/root/pod-cmd.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-cmd
spec:
containers:
- name: app
image: busybox:1.36
command: ["/bin/sh", "-c"]
args: ["echo POD_NAME=$POD_NAME; sleep 3600"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
运行与查看日志:
kubectl apply -f /root/pod-cmd.yaml
kubectl logs demo-cmd
3. 资源与限制(resources)#
resources.requests:调度依据,影响 Pod 被分配的最小资源。resources.limits:限制容器最大资源,防止超用。- 常见字段:
cpu、memory,单位如500m、1Gi。
示例:限制资源(/root/pod-res.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-res
spec:
containers:
- name: web
image: nginx:1.25
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
排错要点:
- Pending 且事件提示 Insufficient cpu/memory:降低 requests 或扩容节点
- 被 OOMKilled:提高 memory limit 或排查内存泄漏
查看事件与资源:
kubectl describe pod demo-res | grep -A5 -E "Limits|Requests|Events"
kubectl top pod demo-res
4. 卷与挂载(volumes / volumeMounts)#
volumes:定义 Pod 级别存储。volumeMounts:挂载到容器内路径。- 常见类型:
emptyDir、hostPath、configMap、secret、pvc。
示例:emptyDir + ConfigMap(/root/pod-vol.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-vol
spec:
volumes:
- name: cache
emptyDir: {}
- name: conf
configMap:
name: nginx-conf
containers:
- name: web
image: nginx:1.25
volumeMounts:
- name: cache
mountPath: /cache
- name: conf
mountPath: /etc/nginx/conf.d
先创建 ConfigMap:
kubectl create configmap nginx-conf \
--from-literal=default.conf='server { listen 80; location / { return 200 "OK\n"; } }'
kubectl apply -f /root/pod-vol.yaml
kubectl exec -it demo-vol -- ls /etc/nginx/conf.d
5. 重启策略与终止行为#
restartPolicy:Always/OnFailure/Never(控制器通常为 Always)。terminationGracePeriodSeconds:优雅终止等待时间。lifecycle:postStart与preStop钩子。
示例:优雅停止(/root/pod-life.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-life
spec:
terminationGracePeriodSeconds: 10
containers:
- name: app
image: nginx:1.25
lifecycle:
preStop:
exec:
command: ["/bin/sh","-c","echo preStop > /usr/share/nginx/html/stop.txt; sleep 5"]
删除验证:
kubectl apply -f /root/pod-life.yaml
kubectl delete pod demo-life --grace-period=10
6. 健康检查(probes)#
livenessProbe:存活探针,失败后重启容器。readinessProbe:就绪探针,控制流量是否进入。startupProbe:启动探针,适用于启动慢的应用。- 支持
httpGet、tcpSocket、exec。
示例:HTTP 探针(/root/pod-probe.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-probe
spec:
containers:
- name: web
image: nginx:1.25
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 10
排错命令:
kubectl describe pod demo-probe | grep -A5 -E "Liveness|Readiness|Events"
kubectl get pod demo-probe -w
7. 调度与亲和性#
nodeSelector:简单节点选择。affinity:节点亲和性与 Pod 亲和/反亲和。tolerations:容忍污点,配合taints使用。priorityClassName:优先级调度。
示例:nodeSelector + taint/toleration(/root/pod-aff.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-aff
spec:
nodeSelector:
disktype: ssd
tolerations:
- key: "dedicated"
operator: "Equal"
value: "ops"
effect: "NoSchedule"
containers:
- name: web
image: nginx:1.25
准备节点标签与污点:
kubectl label node node1 disktype=ssd
kubectl taint node node1 dedicated=ops:NoSchedule
kubectl apply -f /root/pod-aff.yaml
kubectl get pod demo-aff -o wide
8. 网络与安全上下文#
hostNetwork:使用宿主机网络(谨慎使用)。dnsPolicy、dnsConfig:DNS 解析策略。securityContext:用户/组、只读根文件系统、特权模式等。serviceAccountName:Pod 使用的服务账户。
示例:非 root 运行(/root/pod-sec.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-sec
spec:
securityContext:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 2000
containers:
- name: app
image: busybox:1.36
command: ["sh","-c","id; sleep 3600"]
securityContext:
readOnlyRootFilesystem: true
验证:
kubectl apply -f /root/pod-sec.yaml
kubectl logs demo-sec
9. 多容器与共享#
- 一个 Pod 内可包含多个容器,共享网络命名空间与卷。
- 常见模式:Sidecar(日志/代理)、Init 容器预处理。
initContainers:在应用容器前依次执行。
示例:Init + Sidecar(/root/pod-multi.yaml)
apiVersion: v1
kind: Pod
metadata:
name: demo-multi
spec:
volumes:
- name: data
emptyDir: {}
initContainers:
- name: init
image: busybox:1.36
command: ["sh","-c","echo init > /data/ready.txt"]
volumeMounts:
- name: data
mountPath: /data
containers:
- name: app
image: nginx:1.25
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
- name: sidecar
image: busybox:1.36
command: ["sh","-c","tail -f /data/ready.txt"]
volumeMounts:
- name: data
mountPath: /data
查看启动顺序:
kubectl apply -f /root/pod-multi.yaml
kubectl get pod demo-multi -w
kubectl logs demo-multi -c sidecar
10. 常见编写要点#
- 明确资源
requests/limits,避免调度失败或资源争抢。 - 关键应用配合探针与优雅终止,避免滚动更新失败。
- 标签设计与命名规范化,便于选择器与运维管理。
常见问题排查清单:
- 镜像拉取失败:kubectl describe pod | grep -A3 "Failed to pull image",检查镜像名/仓库凭证
- 容器不断重启:kubectl logs --previous 查看上次退出原因
- 未就绪:检查 readinessProbe、应用端口、配置文件挂载
练习:
1) 编写一个 Pod,使用 hostNetwork: true 并解释其风险与场景。
2) 将 demo-res 的 memory limit 调小到 64Mi,观察是否 OOMKilled。
3) 为 demo-probe 添加 startupProbe,模拟慢启动应用。