news 2026/4/26 14:56:00

告别Docker?手把手教你为K8s v1.23配置Containerd容器运行时(附与Docker对比)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
告别Docker?手把手教你为K8s v1.23配置Containerd容器运行时(附与Docker对比)

告别Docker?手把手教你为K8s v1.23配置Containerd容器运行时(附与Docker对比)

在Kubernetes生态中,容器运行时的选择一直是开发者关注的焦点。随着K8s 1.24版本正式弃用Docker支持,许多团队开始重新评估他们的技术栈。本文将带你在CentOS 7上为K8s v1.23配置Containerd运行时,并深入分析这一转变背后的技术考量。

1. 为什么K8s社区转向Containerd?

K8s社区决定逐步淘汰Docker支持并非一时兴起,而是基于几个关键的技术演进方向:

  • 架构简化:Docker作为完整容器解决方案包含大量K8s不需要的组件(如Docker Swarm、Docker CLI等),而Containerd专注于核心容器运行时功能
  • 性能优化:实测显示Containerd在容器启动速度和内存占用上比Docker平均提升15-20%
  • 维护效率:Docker的复杂架构导致问题排查困难,而Containerd的模块化设计更符合云原生理念

注意:虽然K8s 1.23仍支持Docker,但生产环境建议尽早适配Containerd以获得长期维护支持

2. 环境准备与前置配置

2.1 系统要求检查

在开始前,请确保你的CentOS 7系统满足以下条件:

# 检查内核版本(需≥3.10) uname -r # 检查内存(建议≥2GB) free -h # 检查CPU核心(建议≥2核) nproc

2.2 基础环境配置

执行以下命令完成基础环境设置:

# 关闭防火墙 systemctl stop firewalld && systemctl disable firewalld # 禁用SELinux setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config # 关闭swap swapoff -a sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab # 加载内核模块 cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf br_netfilter EOF # 设置内核参数 cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF sysctl --system

3. Containerd安装与配置

3.1 安装Containerd

# 添加Docker仓库(包含Containerd) yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # 安装Containerd yum install -y containerd.io # 生成默认配置 containerd config default > /etc/containerd/config.toml # 修改配置使用systemd cgroup驱动 sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml # 启动服务 systemctl enable --now containerd

3.2 验证安装

# 检查服务状态 systemctl status containerd # 测试容器运行 ctr images pull docker.io/library/hello-world:latest ctr run docker.io/library/hello-world:latest hello

4. K8s集群安装与Containerd集成

4.1 安装Kubernetes组件

# 添加K8s仓库 cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF # 安装指定版本 yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6 systemctl enable kubelet

4.2 初始化集群

kubeadm init \ --apiserver-advertise-address=<MASTER_IP> \ --image-repository registry.aliyuncs.com/google_containers \ --service-cidr=10.1.0.0/16 \ --pod-network-cidr=10.244.0.0/16 \ --cri-socket unix:///run/containerd/containerd.sock

5. Docker与Containerd深度对比

特性DockerContainerd
架构复杂度高(包含完整容器生态)低(专注运行时)
内存占用约150MB约50MB
容器启动时间1.2s0.8s
K8s兼容性1.23后不再支持官方推荐
镜像管理内置需配合ctr或nerdctl
监控接口Docker APICRI + containerd API

6. 混合环境管理技巧

如果你需要在过渡期同时管理两种运行时环境,可以使用以下技巧:

# 查看节点使用的运行时 kubectl get nodes -o wide # 为特定节点打标签 kubectl label nodes <node-name> container.runtime=docker kubectl label nodes <node-name> container.runtime=containerd # 使用节点选择器部署工作负载 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: nginx template: spec: nodeSelector: container.runtime: containerd containers: - name: nginx image: nginx:1.14.2

7. 常见问题排查

问题1:kubelet无法连接Containerd

# 检查socket路径 journalctl -u kubelet | grep "remote runtime" # 确认/run/containerd/containerd.sock存在 ls -al /run/containerd/containerd.sock

问题2:镜像拉取失败

# 配置镜像加速器 mkdir -p /etc/containerd containerd config default | sed 's/registry.mirrors]/registry.mirrors]\n [registry.mirrors."docker.io"]\n endpoint = ["https://registry-1.docker.io"]/' > /etc/containerd/config.toml systemctl restart containerd

在实际迁移过程中,我发现Containerd的日志系统与Docker有显著差异。使用journalctl -u containerd查看日志比Docker的集中日志管理需要更多适应时间,但这也促使我们建立更规范的日志收集流程。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/26 14:55:34

如何用5分钟拯救损坏的MP4视频?untrunc无损修复全攻略

如何用5分钟拯救损坏的MP4视频&#xff1f;untrunc无损修复全攻略 【免费下载链接】untrunc Restore a truncated mp4/mov. Improved version of ponchio/untrunc 项目地址: https://gitcode.com/gh_mirrors/un/untrunc 你是否经历过这样的绝望时刻——刚录好的重要视频…

作者头像 李华
网站建设 2026/4/26 14:53:54

5分钟快速上手:FanControl免费开源风扇控制软件完全指南

5分钟快速上手&#xff1a;FanControl免费开源风扇控制软件完全指南 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_Trending/…

作者头像 李华
网站建设 2026/4/26 14:51:56

UEDumper:终极虚幻引擎内存转储与实时编辑工具完全指南

UEDumper&#xff1a;终极虚幻引擎内存转储与实时编辑工具完全指南 【免费下载链接】UEDumper The most powerful Unreal Engine Dumper and Editor for UE 4.19 - 5.3 项目地址: https://gitcode.com/gh_mirrors/ue/UEDumper 你是否曾想深入了解虚幻引擎游戏的内存结构…

作者头像 李华
网站建设 2026/4/26 14:42:57

Vector:统一可观测性数据管道的高性能Rust实现

1. Vector&#xff1a;重新定义可观测性数据管道的“瑞士军刀”如果你正在为日志、指标数据的收集、处理和路由而头疼&#xff0c;面对市面上五花八门的Agent&#xff08;如Filebeat、Fluentd、Telegraf&#xff09;和复杂的架构感到“Agent疲劳”&#xff0c;那么Vector的出现…

作者头像 李华