一、简介
Kubernetes 中的特殊容器包括 Init Container 、Sidecar Container 和 Ephemeral Container,它们在 Pod 生命周期中扮演不同角色。
表格
| 类型 | 作用 | 生命周期 | K8s 版本 |
|---|---|---|---|
| Init Container | 初始化准备 | Pod 创建时,先于主容器 | 1.6+ |
| Sidecar Container | 辅助增强 | 与主容器同期运行 | 1.28+ 原生 |
| Ephemeral Container | 临时调试 | 按需创建 | 1.25+ Beta |
二、Init Container(初始化容器)
2.1 核心作用与区别
Init Container 在主容器启动前执行,用于等待依赖、初始化配置、执行迁移。
plaintext
┌──────────────────────────────────────────────────────────────┐
│ Init vs 普通容器 │
├──────────────────────────────────────────────────────────────┤
│ Init: 串行执行 → 必须 exit 0 → 无探针支持 │
│ 普通: 并行启动 → 失败影响健康 → 支持探针 │
└──────────────────────────────────────────────────────────────┘
2.2 工作原理
plaintext
┌────────────────────────────────────────────────────────────┐
│ Init Container 执行流程 │
├────────────────────────────────────────────────────────────┤
│ │
│ Pod 创建 │
│ ↓ │
│ [Init-1: 等待 DB] → exit 0 → [Init-2: 迁移数据] │
│ ↓ │
│ [Main Container 启动] │
│ │
│ ⚠️ 任一 Init 失败 → restartPolicy → 重试 │
└────────────────────────────────────────────────────────────┘
2.3 YAML 配置示例
等待依赖服务就绪
yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myapp:v1.0
ports:
- containerPort: 8080
initContainers:
- name: wait-for-mysql
image: busybox:1.36
command:
- sh
- -c
- |
echo "Waiting for MySQL..."
until nc -z mysql.default.svc.cluster.local 3306; do
echo "MySQL not ready, retrying..."
sleep 5
done
echo "MySQL is ready"
多 Init 容器顺序执行
yaml
apiVersion: v1
kind: Pod
metadata:
name: app-with-migration
spec:
containers:
- name: app
image: myapp:v2.0
initContainers:
- name: wait-dependencies
image: busybox:1.36
command: ['sh', '-c', 'echo "Wait..." && sleep 10']
- name: init-config
image: busybox:1.36
command: ['sh', '-c', 'echo "Init config done"']
- name: db-migration
image: myapp-migrate:v1.0
command: ['/migrate.sh']
三、Sidecar Container(边车容器)
3.1 核心作用
Sidecar 与主容器并行运行,通过共享网络/存储增强功能:
- 日志收集(Filebeat、Fluentd)
- 代理转发(Envoy、Nginx sidecar)
- 监控指标暴露
K8s 1.28+ 原生支持 :restartPolicy: Always 的容器自动获得 Sidecar 语义。
3.2 工作原理
plaintext
┌────────────────────────────────────────────────────────────┐
│ Sidecar 容器架构 │
├────────────────────────────────────────────────────────────┤
│ │
│ Pod │
│ ┌──────────────────────────────────────────────────┐ │
│ │ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Main App │ │ Sidecar │ │ │
│ │ │ localhost │←──→│ localhost │ │ │
│ │ │ :8080 │ │ :80 │ │ │
│ │ └──────┬──────┘ └──────┬──────┘ │ │
│ │ └──────────┬───────┘ │ │
│ │ ↓ │ │
│ │ ┌────────────────┐ │ │
│ │ │ shared Volume │ ← 日志目录挂载共享 │ │
│ │ │ /var/log/app/ │ │ │
│ │ └────────────────┘ │ │
│ └──────────────────────────────────────────────────┘ │
└────────────────────────────────────────────────────────────┘
3.3 YAML 配置示例
日志收集 Sidecar
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-with-log-sidecar
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: main-app
image: myapp:v1.0
volumeMounts:
- name: log-volume
mountPath: /var/log/myapp
- name: log-sidecar
image: fluent/fluent-bit:2.1
volumeMounts:
- name: log-volume
mountPath: /var/log/myapp
- name: fluent-bit-config
mountPath: /fluent-bit/etc
volumes:
- name: log-volume
emptyDir: {}
- name: fluent-bit-config
configMap:
name: fluent-bit-config
Nginx 代理 Sidecar
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-with-nginx-sidecar
spec:
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: main-app
image: python-app:v1.0
ports:
- containerPort: 8080
- name: nginx-proxy
image: nginx:1.25-alpine
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx/conf.d
readOnly: true
volumes:
- name: nginx-conf
configMap:
name: nginx-proxy-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-proxy-config
data:
default.conf: |
server {
listen 80;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
}
}
四、Ephemeral Container(临时容器)
4.1 核心作用
Ephemeral Container 是 K8s 1.25+ 的临时调试容器,用于诊断运行中 Pod 问题,无需重启。
典型场景:
- Pod 进入 CrashLoopBackOff 无法排查
- 容器缺少调试工具
- 需要查看进程/网络信息
4.2 kubectl debug 用法
bash
# 在现有 Pod 中创建临时容器
kubectl debug myapp-pod -it --image=busybox:1.36 --share-processes
# 使用网络调试工具
kubectl debug myapp-pod -it --image=nicolaka/netshoot:latest -- sh
# 复制 Pod 并添加临时容器(不改变原 Pod)
kubectl debug myapp-pod --image=busybox:1.36 -it --copy-to=myapp-debug -- sh
# 调试进入后执行排查命令
# ps aux, netstat -tlnp, ls /proc/1/root 等
五、常见问题与排查
问题 1:Init Container 一直处于 Running
bash
# 排查步骤
kubectl describe pod <pod> | grep -A 10 "Init"
kubectl logs <pod> -c init-container-name
# 解决:添加超时控制
initContainers:
- name: wait-service
image: busybox:1.36
command: ['sh', '-c']
args:
- |
for i in $(seq 1 12); do
nc -z dependency-svc 80 && exit 0
sleep 5
done
exit 1 # 超时失败
问题 2:Sidecar 日志收集不生效
bash
# 排查步骤
kubectl describe pod <pod> | grep -A 5 Volumes
kubectl exec -c main-app -- ls -la /var/log/myapp/
kubectl logs <pod> -c log-sidecar
问题 3:Ephemeral Container 无法创建
bash
# 原因:K8s 版本 < 1.25 或 feature gate 未启用
# 检查版本
kubectl version --short
# 确保启用
--feature-gates=EphemeralVolumeSource=true
六、最佳实践
-
Init Container 快速失败:添加超时机制,避免无限等待
-
Sidecar 避免启动顺序依赖:使用 wait script 或 sharedVolume 协调
-
Ephemeral Container 仅用于调试:生产环境慎用,调试后删除临时 Pod
-
Init 容器数量控制:建议不超过 3 个,过多影响启动速度
-
Sidecar 镜像版本锁定:使用固定 tag 或 SHA256,避免自动更新
-
资源限制必填 :
yaml
resources: limits: memory: "128Mi" cpu: "100m" -
合理使用共享命名空间 :
yaml
spec: shareProcessNamespace: true # 允许查看主容器进程 hostNetwork: true # 按需启用