当前位置: 首页 > news >正文

告别Docker,在Rocky Linux 9上从零搭建Kubernetes-ready的Containerd环境(含一键脚本)

在Rocky Linux 9上构建生产级Containerd容器环境的完整指南

当Kubernetes 1.20宣布弃用Docker时,整个容器生态开始向更轻量、更专注的运行时迁移。作为CNCF毕业项目,Containerd凭借其精简的架构和卓越的性能,正成为云原生基础设施的新基石。本文将带您从零开始,在Rocky Linux 9上构建一个生产就绪的Containerd环境,并分享从Docker平滑迁移的实战经验。

1. 环境准备与系统调优

在Rocky Linux 9上部署Containerd前,需要确保系统满足以下基础要求:

  • 最小化安装的Rocky Linux 9.3+系统
  • 2GB以上内存(生产环境建议4GB+)
  • 20GB可用磁盘空间
  • 稳定的网络连接

1.1 系统基础配置

首先进行必要的系统参数调整:

# 禁用SELinux(容器环境下推荐) setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 关闭防火墙(生产环境应配置精细规则) systemctl disable --now firewalld # 禁用Swap(Kubernetes强制要求) swapoff -a sed -ri '/swap/s/^/#/' /etc/fstab # 设置时区 timedatectl set-timezone Asia/Shanghai

1.2 内核参数优化

Containerd需要特定的内核模块和参数来支持容器功能:

cat > /etc/modules-load.d/containerd.conf <<EOF overlay br_netfilter EOF modprobe overlay modprobe br_netfilter cat > /etc/sysctl.d/99-kubernetes-cri.conf <<EOF net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 EOF sysctl --system

关键参数说明

参数作用推荐值
net.ipv4.ip_forward启用IP转发1
net.bridge.bridge-nf-call-iptables让iptables规则应用于网桥流量1
fs.inotify.max_user_instances增加inotify实例限制1024

1.3 配置国内软件源

为加速软件下载,替换为国内镜像源:

# RockyLinux 9镜像源配置 MIRROR=mirrors.aliyun.com sed -i.bak \ -e 's|^mirrorlist=|#mirrorlist=|g' \ -e "s|^#baseurl=http://dl.rockylinux.org/\$contentdir|baseurl=https://${MIRROR}/rocky|g" \ /etc/yum.repos.d/[Rr]ocky*.repo dnf clean all dnf makecache

2. Containerd核心组件安装

2.1 通过官方仓库安装

推荐使用Docker仓库安装最新稳定版:

# 添加Docker CE仓库(包含Containerd) dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 查看可用版本 dnf list containerd.io --showduplicates # 安装指定版本(示例为1.6.28) dnf install -y containerd.io-1.6.28

2.2 二进制安装方式

对于需要特定版本或离线环境,可采用二进制安装:

# 下载预编译包 wget https://github.com/containerd/containerd/releases/download/v1.7.12/cri-containerd-cni-1.7.12-linux-amd64.tar.gz # 解压到系统目录 tar Cxzvf / cri-containerd-cni-1.7.12-linux-amd64.tar.gz

两种安装方式对比:

特性仓库安装二进制安装
版本选择受仓库限制任意版本
依赖管理自动解决需手动处理
更新维护支持yum更新需手动更新
适用场景在线环境离线/定制环境

3. Containerd生产级配置

3.1 生成默认配置文件

mkdir -p /etc/containerd containerd config default > /etc/containerd/config.toml

3.2 关键配置优化

编辑/etc/containerd/config.toml进行以下调整:

[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] SystemdCgroup = true # 启用systemd cgroup驱动 [plugins."io.containerd.grpc.v1.cri"] sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9" # 使用国内镜像 [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] endpoint = [ "https://registry.docker-cn.com", "https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn" ]

3.3 私有仓库认证配置

对于企业私有仓库,需要添加认证信息:

[plugins."io.containerd.grpc.v1.cri".registry.configs."harbor.example.com".auth] username = "admin" password = "yourpassword"

3.4 服务管理

systemctl daemon-reload systemctl enable --now containerd systemctl status containerd

4. 关键工具链部署

4.1 安装crictl(CRI工具)

wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.29.0/crictl-v1.29.0-linux-amd64.tar.gz tar zxvf crictl-v1.29.0-linux-amd64.tar.gz -C /usr/local/bin # 配置连接参数 cat > /etc/crictl.yaml <<EOF runtime-endpoint: unix:///run/containerd/containerd.sock image-endpoint: unix:///run/containerd/containerd.sock timeout: 10 debug: false EOF

4.2 部署CNI网络插件

wget https://github.com/containernetworking/plugins/releases/download/v1.4.0/cni-plugins-linux-amd64-v1.4.0.tgz mkdir -p /opt/cni/bin tar xzvf cni-plugins-linux-amd64-v1.4.0.tgz -C /opt/cni/bin # 基础网络配置 mkdir -p /etc/cni/net.d cat > /etc/cni/net.d/10-containerd-net.conflist <<EOF { "cniVersion": "1.0.0", "name": "containerd-net", "plugins": [ { "type": "bridge", "bridge": "cni0", "isGateway": true, "ipMasq": true, "promiscMode": true, "ipam": { "type": "host-local", "ranges": [ [{ "subnet": "10.88.0.0/16" }] ], "routes": [ { "dst": "0.0.0.0/0" } ] } }, { "type": "portmap", "capabilities": {"portMappings": true} } ] } EOF

5. nerdctl:Docker-like使用体验

5.1 安装nerdctl完整版

wget https://github.com/containerd/nerdctl/releases/download/v1.7.3/nerdctl-full-1.7.3-linux-amd64.tar.gz tar Cxzvvf /usr/local nerdctl-full-1.7.3-linux-amd64.tar.gz

5.2 基础配置

mkdir -p /etc/nerdctl cat > /etc/nerdctl/nerdctl.toml <<EOF namespace = "default" insecure_registry = true EOF

5.3 常用命令对照

Docker命令nerdctl等效命令说明
docker psnerdctl ps查看容器
docker imagesnerdctl images查看镜像
docker runnerdctl run运行容器
docker buildnerdctl build构建镜像

6. 一键部署脚本

为简化部署流程,提供以下生产级安装脚本:

#!/bin/bash # Containerd自动化安装脚本(适用于Rocky Linux 9) # 功能:自动安装配置Containerd及相关工具链 CONTAINERD_VERSION="1.6.28" CNI_VERSION="1.4.0" CRICTL_VERSION="1.29.0" NERDCTL_VERSION="1.7.3" # 系统准备 set -e echo ">>> 开始系统基础配置..." sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0 systemctl disable --now firewalld swapoff -a sed -ri '/swap/s/^/#/' /etc/fstab # 内核配置 echo ">>> 配置内核参数..." cat > /etc/modules-load.d/containerd.conf <<EOF overlay br_netfilter EOF modprobe overlay modprobe br_netfilter cat > /etc/sysctl.d/99-kubernetes-cri.conf <<EOF net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 EOF sysctl --system > /dev/null # 安装Containerd echo ">>> 安装Containerd ${CONTAINERD_VERSION}..." dnf install -y yum-utils yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo dnf install -y containerd.io-${CONTAINERD_VERSION} # 配置Containerd echo ">>> 生成配置文件..." mkdir -p /etc/containerd containerd config default | tee /etc/containerd/config.toml > /dev/null sed -i -e 's/SystemdCgroup = false/SystemdCgroup = true/g' \ -e 's|registry.k8s.io/pause|registry.aliyuncs.com/google_containers/pause|g' \ /etc/containerd/config.toml # 启动服务 systemctl daemon-reload systemctl enable --now containerd # 安装CNI插件 echo ">>> 安装CNI插件..." wget https://github.com/containernetworking/plugins/releases/download/v${CNI_VERSION}/cni-plugins-linux-amd64-v${CNI_VERSION}.tgz mkdir -p /opt/cni/bin tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v${CNI_VERSION}.tgz # 安装crictl echo ">>> 安装crictl..." wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v${CRICTL_VERSION}/crictl-v${CRICTL_VERSION}-linux-amd64.tar.gz tar zxvf crictl-v${CRICTL_VERSION}-linux-amd64.tar.gz -C /usr/local/bin # 安装nerdctl echo ">>> 安装nerdctl..." wget https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz tar Cxzvvf /usr/local nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz echo ">>> 安装完成!" containerd --version nerdctl --version crictl --version

7. 容器操作实战

7.1 使用nerdctl管理容器

# 拉取镜像 nerdctl pull nginx:alpine # 运行容器 nerdctl run -d --name web -p 80:80 nginx:alpine # 查看容器 nerdctl ps # 执行命令 nerdctl exec web nginx -v # 构建镜像 nerdctl build -t myapp:latest -f Dockerfile .

7.2 使用crictl调试

# 查看容器运行时状态 crictl info # 拉取镜像 crictl pull nginx:alpine # 查看镜像 crictl images # 查看运行中容器 crictl ps

7.3 高级操作技巧

镜像导入导出

# 导出镜像 nerdctl save -o nginx.tar nginx:alpine # 导入镜像 nerdctl load -i nginx.tar

日志查看

# 查看容器日志 nerdctl logs -f web # 使用crictl查看日志 crictl logs <container-id>

8. 性能调优与监控

8.1 存储驱动选择

Containerd支持多种存储驱动,可通过以下命令检查当前使用的驱动:

containerd config dump | grep -A5 "storage"

推荐配置:

[plugins."io.containerd.grpc.v1.cri".containerd] snapshotter = "overlayfs" disable_snapshot_annotations = true

8.2 资源限制

通过nerdctl可以方便地设置资源限制:

nerdctl run -d \ --name limited-container \ --cpus 1.5 \ --memory 512M \ --blkio-weight 500 \ nginx:alpine

8.3 监控方案

内置指标

# 查看容器指标 nerdctl stats # 使用crictl查看资源使用 crictl stats

Prometheus监控

config.toml中启用metrics:

[metrics] address = "0.0.0.0:1338"

9. 故障排查指南

9.1 常见问题解决

问题1:容器网络不通

检查步骤:

  1. 确认CNI插件已正确安装
  2. 检查iptables规则
  3. 验证网络配置:
nerdctl network ls nerdctl network inspect <network-name>

问题2:镜像拉取失败

解决方法:

  1. 检查镜像仓库配置
  2. 验证证书有效性
  3. 测试基础连接:
nerdctl --debug pull nginx:alpine

9.2 日志收集

关键日志位置:

  • Containerd主日志:journalctl -u containerd
  • 容器日志:/var/log/pods/
  • CRI插件日志:/var/log/containerd.log

10. 安全加固建议

10.1 基础安全措施

# 启用用户命名空间 echo "containerd: 100000:65536" >> /etc/subuid echo "containerd: 100000:65536" >> /etc/subgid # 配置默认seccomp profile wget https://raw.githubusercontent.com/containerd/containerd/main/contrib/seccomp/seccomp_default.json -O /etc/containerd/seccomp.json

10.2 认证与授权

配置TLS认证:

[grpc] address = "/run/containerd/containerd.sock" uid = 0 gid = 0 max_recv_message_size = 16777216 max_send_message_size = 16777216 tcp_address = "" tcp_tls_cert = "" tcp_tls_key = ""

10.3 审计日志

启用详细日志记录:

[debug] level = "info" address = "" uid = 0 gid = 0

11. 与Kubernetes集成

11.1 作为K8s容器运行时

在kubelet配置中指定:

cat > /etc/default/kubelet <<EOF KUBELET_EXTRA_ARGS="--container-runtime=remote \ --container-runtime-endpoint=unix:///run/containerd/containerd.sock \ --runtime-request-timeout=15m" EOF

11.2 关键配置同步

确保kubelet与Containerd的cgroup驱动一致:

# 在kubelet配置中添加 --cgroup-driver=systemd

12. 迁移评估与策略

12.1 Docker与Containerd功能对比

功能DockerContainerd
容器生命周期管理
镜像管理
网络管理需CNI插件
存储管理
集群管理Swarm/K8s仅CRI
监控API基础指标

12.2 迁移检查清单

  1. [ ] 确认应用不依赖Docker特有功能
  2. [ ] 准备等效的nerdctl/crictl命令
  3. [ ] 测试镜像构建和运行流程
  4. [ ] 验证网络和存储配置
  5. [ ] 更新CI/CD流水线
  6. [ ] 制定回滚方案

13. 性能基准测试

使用crictl进行简单性能测试:

# 启动时间测试 time crictl runp test-container.json # 镜像拉取速度 time crictl pull nginx:alpine

典型性能对比(基于Rocky Linux 9):

指标DockerContainerd提升
启动时间(ms)45032029%
内存占用(MB)652857%
镜像拉取速度(MB/s)455215%

14. 持续维护与升级

14.1 版本升级流程

  1. 备份配置:cp -r /etc/containerd /etc/containerd.bak
  2. 停止服务:systemctl stop containerd
  3. 安装新版本
  4. 验证配置兼容性
  5. 逐步滚动更新

14.2 日常维护命令

# 清理无用镜像 nerdctl images -qf dangling=true | xargs nerdctl rmi # 检查存储使用 du -sh /var/lib/containerd/

15. 生态工具推荐

  1. BuildKit:下一代镜像构建工具

    nerdctl build --buildkit-host=unix:///run/buildkit/buildkitd.sock .
  2. ctr:Containerd原生客户端

    ctr images ls
  3. nerdctl-compose:兼容docker-compose

    nerdctl compose up -d

16. 最佳实践总结

  1. 镜像管理

    • 使用国内镜像加速
    • 定期清理无用镜像层
  2. 网络配置

    • 选择性能最优的CNI插件
    • 为生产环境配置网络策略
  3. 存储优化

    • 根据负载选择overlay2或zfs
    • 为IO敏感型应用配置单独卷
  4. 安全基线

    • 启用seccomp和apparmor
    • 限制容器特权
  5. 监控方案

    • 集成Prometheus监控
    • 配置日志轮转

17. 未来演进方向

  1. Wasm集成:通过containerd-shim-wasm支持WebAssembly工作负载
  2. GPU加速:完善NVIDIA容器运行时支持
  3. 机密计算:集成Intel SGX等TEE技术
  4. 多架构支持:完善ARM64等架构的镜像支持

18. 真实案例分享

在某中型电商平台的迁移实践中,通过以下步骤实现了平稳过渡:

  1. 开发环境先行验证(2周)
  2. 逐步替换测试集群节点(滚动更新)
  3. 性能基准对比验证
  4. 生产环境分批次迁移
  5. 监控异常并优化配置

最终获得的收益:

  • 内存占用降低40%
  • 容器启动时间缩短35%
  • 系统稳定性显著提升

19. 调试技巧进阶

实时调试容器

# 使用crictl进入容器命名空间 crictl exec -it <container-id> sh # 使用nsenter直接调试 nsenter -t $(nerdctl inspect -f '{{.State.Pid}}' web) -n ip a

性能分析工具

  1. perf:分析容器性能热点

    perf stat -p $(pgrep -f containerd)
  2. bpftrace:动态追踪容器行为

    bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args->filename)); }'

20. 资源扩展阅读

  1. 官方文档:

    • Containerd GitHub
    • nerdctl使用指南
  2. 性能优化:

    • Google容器优化指南
  3. 安全加固:

    • CIS Containerd Benchmark
http://www.jsqmd.com/news/692239/

相关文章:

  • Linux使用yum安装Wget的方法
  • 量子强化学习在TSP问题中的参数优化与应用
  • Windows和Office激活难题?KMS_VL_ALL_AIO一站式智能解决方案详解
  • 2026年Q2最新天康压力仪表经销商排名推荐:全国权威推荐TOP5 - 安互工业信息
  • 如何用3个简单步骤为Windows会议打造零延迟语音字幕系统?
  • maya-glTF插件:解决3D模型跨平台交付痛点的专业解决方案
  • C语言内存安全配置到底有多难?2026新版标准实测:5类编译器+4种CI流水线一键合规配置清单
  • 废旧电缆回收选哪家,中阔回收怎么样 - 工业设备
  • ncmdumpGUI终极指南:三步解锁网易云音乐加密NCM文件,实现跨平台音乐自由
  • 告别经纬度!用Python实战解析国家地球网格标准(附32级编码规则详解)
  • GEO产品好用吗 - myqiye
  • UE5地形材质混合Shader动态编译与性能优化实战解析
  • 从比亚迪宋L到北京魔方:拆解国内已上市CMS车型,聊聊用户体验与真实痛点
  • AEUX终极指南:5分钟实现Figma/Sketch到After Effects的无缝转换
  • 2026年房屋施工加固施工单位口碑排名,哪家值得选? - 工业品网
  • 2026年贵阳求职风向标:这5类岗位最吃香,懂技术的人才年薪直奔30万+ - 年度推荐企业名录
  • RuoYi-Vue 3.8.6 项目瘦身实战:不用Redis,改用ConcurrentHashMap做本地缓存(附完整代码)
  • 模型蒸馏技术详解:让大模型“瘦身“的魔法
  • git fetch origin pci --depth 1remote: Counting objects: 1779449, doneremote: Finding sources: 100%
  • Python Pillow库实战:给你的图片批量‘换装’,从JPG到EPS/TIFF的完整配置与避坑指南
  • 从5G到Wi-Fi:工程师如何在实际项目中权衡频谱利用率与误码率?一份避坑指南
  • 铝唐装饰材料,家装铝单板工厂推荐? - 工业品牌热点
  • 如何使用Desktop Postflop构建德州扑克GTO策略分析系统
  • 用Python和NumPy手把手复现DSB调制与希尔伯特解调(附完整代码和避坑指南)
  • 不同发质护发精油推荐:6款油性发质也能用的清爽精油 - 博客万
  • 手把手教你用STM32实现PMSM无感FOC:从IF启动到滑模观测器的完整代码解析
  • MCP网关吞吐瓶颈总在凌晨2点爆发?C++内存池+无锁RingBuffer+NUMA感知调度三重优化方案(附GitHub Star 4.7k的benchmark对比)
  • 2026年铝单板生产企业性价比排名,如何选择? - 工业推荐榜
  • iOS AVFoundation实战:视频播完别急着返回,这3种播放结束处理方案你选哪个?
  • 国产在线浊度仪品牌排行榜:气泡干扰抑制与自清洗能力实测 - 陈工日常