当 containerd 出现 “io timeout” 如何调优 pool size?
解读
在国内生产环境中,containerd 作为 Kubernetes 默认的 CRI 运行时,其底层 gRPC 连接池(pool)默认配置往往偏小(默认 10 并发)。当镜像仓库位于公网(Docker Hub、Quay、gcr.io 的国内镜像源)或跨可用区拉取大镜像时,网络 RTT 高、丢包重传频繁,极易触发“context deadline exceeded (Client.Timeout exceeded while awaiting headers)”或“io timeout”。面试官想考察的是:你能否把“超时”现象与“连接池饥饿”建立因果链,并给出可落地的调优路径,而不是简单重启 containerd。
知识点
- containerd 的 gRPC 连接池由 CRI 插件的
max_concurrent_downloads与max_concurrent_uploads控制,对应配置项在 /etc/containerd/config.toml 的[plugins."io.containerd.grpc.v1.cri".registry]段落。 - 连接池耗尽时,新的 Pull 请求会排队等待,若镜像层数多或大文件分片多,排队时间 > 默认 30 s 就会报 io timeout。
- 调优前需先确认瓶颈:
- 用 ctr -n k8s.io images pull –debug 复现,观察是否卡在“fetching blob”。
- 用 ss -antp | grep EST | grep containerd 统计并发连接数,若≈ pool size 即证实池满。
- 国内云厂商(阿里云 ACR、腾讯云 TCR、华为云 SWR)对单 IP 并发限流 20~50,pool size 盲目调到 100 反而会被限速 429,需阶梯压测后取最优值。
- 调大 pool size 必须同步调大 containerd 的 ulimit nofile(建议 65535)与 kubelet 的 image-pull-progress-deadline(默认 1 m 可提到 5 m),否则文件句柄耗尽或 kubelet 提前取消拉取。
答案
- 定位:在 Node 上执行
journalctl -u containerd -n 500 | grep -i 'io timeout'
若伴随“failed to do request: context deadline exceeded”且并发连接数≈10,可判定 pool size 不足。 - 备份并修改 /etc/containerd/config.toml:
数值需结合仓库限流策略,阿里云 ACR 推荐 32,Docker Hub 国内镜像源推荐 16。[plugins."io.containerd.grpc.v1.cri".registry] max_concurrent_downloads = **32** max_concurrent_uploads = **10** - 联动调优:
- 在 /etc/systemd/system/containerd.service.d/override.conf 追加
执行[Service] LimitNOFILE=65535systemctl daemon-reload && systemctl restart containerd。 - 修改 kubelet 启动参数
--image-pull-progress-deadline=5m
防止大镜像层因进度卡住被 kubelet 强制取消。
- 在 /etc/systemd/system/containerd.service.d/override.conf 追加
- 灰度验证:
选一台 Node 调大 pool,用crictl pull <1.5 GB 镜像>计时,对比调优前拉取耗时下降 ≥ 40 % 且不再出现 io timeout,再滚动到全集群。
拓展思考
如果调大 pool size 后仍偶发 timeout,需继续下沉:
- 使用 containerd 的镜像加速插件(如阿里云 overlaybd、Dragonfly P2P)把回源流量打散到边缘节点,减少单点 RTT。
- 在 CNI 层启用 imagePullPolicy: IfNotPresent 并结合 Harbor 预热,让业务镜像提前分发到 Node 本地存储,彻底跳过公网拉取。
- 对跨洋镜像可配置 containerd 的 mirror 代理(registry.mirrors)指向国内云厂商加速域名,并在 /etc/containerd/config.toml 增加
实现故障自动切换,避免单加速源超时导致整体失败。[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] endpoint = ["https://registry.docker-cn.com", "https://xxx.mirror.aliyuncs.com"]