1.9 Kubernetes Pods
一、Pod 基础概念
1. Pod 的本质
- 最小部署单元:Kubernetes 中最小的可调度和管理单元
- 逻辑主机:表示集群中的一个”逻辑虚拟机”
- 共享环境:一个 Pod 中的容器共享:
- 网络命名空间(同一 IP 和端口空间)
- 存储卷(Volumes)
- 内核命名空间(如 IPC)
2. Pod 生命周期
1 2
| kubectl describe pod <pod-name> | grep -A 10 "Events:"
|
典型阶段:
- Pending:调度阶段
- Running:至少一个容器运行中
- Succeeded:所有容器成功终止
- Failed:至少一个容器异常终止
- Unknown:状态无法获取
二、Pod 核心配置
1. 基本 YAML 结构
1 2 3 4 5 6 7 8 9 10 11 12
| apiVersion: v1 kind: Pod metadata: name: my-pod labels: app: my-app spec: containers: - name: main-container image: nginx:1.19 ports: - containerPort: 80
|
2. 多容器 Pod 示例
1 2 3 4 5 6 7 8 9 10 11 12
| apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: - name: web-server image: nginx - name: log-agent image: fluentd - name: metrics-collector image: prom/statsd-exporter
|
三、Pod 网络详解
1. 网络模型特性
1 2
| kubectl get pod <pod-name> -o wide
|
- IP-per-Pod:每个 Pod 获得唯一集群IP
- 跨节点通信:无需 NAT 直接互通
- 端口管理:容器端口通过
containerPort 暴露
2. 网络诊断命令
1 2 3 4 5
| kubectl exec <pod-name> -- curl -I <target-pod-ip>
kubectl exec <pod-name> -- nslookup kubernetes.default
|
四、Pod 存储管理
1. Volume 类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| spec: volumes: - name: data-volume emptyDir: {} - name: config-volume configMap: name: app-config containers: - name: app volumeMounts: - mountPath: /data name: data-volume - mountPath: /etc/config name: config-volume
|
2. 常用 Volume 类型对比
| 类型 |
用途 |
生命周期 |
| emptyDir |
临时存储 |
随 Pod 删除 |
| hostPath |
访问节点文件系统 |
持久 |
| configMap |
注入配置数据 |
可热更新 |
| secret |
注入敏感数据 |
加密存储 |
| persistentVolume |
持久化存储 |
独立于 Pod 生命周期 |
五、Pod 健康管理
1. 探针配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13
| containers: - name: web livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 15 periodSeconds: 20 readinessProbe: tcpSocket: port: 8080 initialDelaySeconds: 5 periodSeconds: 10
|
2. 探针类型对比
| 类型 |
用途 |
失败后果 |
| livenessProbe |
检测容器是否存活 |
重启容器 |
| readinessProbe |
检测容器是否就绪 |
从服务端点移除 |
| startupProbe |
保护慢启动容器 |
在成功前阻止其他探针 |
六、Pod 调度控制
1. 资源限制配置
1 2 3 4 5 6 7
| resources: requests: cpu: "250m" memory: "512Mi" limits: cpu: "1" memory: "1Gi"
|
2. 高级调度策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: gpu-type operator: In values: ["nvidia"] podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchLabels: app: database topologyKey: kubernetes.io/hostname
|
七、Pod 运维命令
1. 基础操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| kubectl apply -f pod.yaml
kubectl describe pod <pod-name>
kubectl logs <pod-name> [-c <container-name>]
kubectl exec -it <pod-name> -- /bin/bash
kubectl delete pod <pod-name>
|
2. 高级诊断
1 2 3 4 5 6 7 8
| kubectl get events --sort-by=.metadata.creationTimestamp --field-selector involvedObject.name=<pod-name>
kubectl top pod <pod-name>
kubectl cp <pod-name>:/path/to/file ./local-file
|
八、特殊 Pod 类型
1. Static Pod(静态 Pod)
1 2
| ps aux | grep kubelet | grep pod-manifest-path
|
- 特性:由 kubelet 直接管理,不经过 API Server
- 用途:用于运行集群核心组件(如 etcd、kube-apiserver)
2. Ephemeral Pod(临时 Pod)
1 2
| kubectl debug -it <target-pod> --image=busybox --target=<container-name>
|
- 特性:临时性,适合故障诊断
- 生命周期:随调试会话结束而终止
九、Pod 安全上下文
1. 安全配置示例
1 2 3 4 5 6 7
| securityContext: runAsUser: 1000 runAsGroup: 3000 fsGroup: 2000 capabilities: add: ["NET_ADMIN"] readOnlyRootFilesystem: true
|
2. 安全策略类型
| 配置项 |
作用 |
安全影响 |
| runAsNonRoot |
禁止 root 运行 |
减少权限提升风险 |
| readOnlyRootFilesystem |
根文件系统只读 |
防止文件系统篡改 |
| allowPrivilegeEscalation |
禁止特权提升 |
限制容器权限 |
| seccompProfile |
系统调用过滤 |
减少攻击面 |
十、Pod 设计模式
1. Sidecar 模式
- 特点:辅助容器增强主容器功能
- 用例:日志收集、监控代理、服务网格
2. Adapter 模式
- 特点:标准化容器输出
- 用例:指标格式转换、日志格式化
3. Ambassador 模式
- 特点:代理外部服务访问
- 用例:数据库连接代理、API 网关
Pod 作为 Kubernetes 的核心抽象,理解其设计理念和操作细节是掌握 Kubernetes 的关键。这些命令和配置示例覆盖了 Pod 管理的各个方面,可作为日常工作的实用参考。