《Ubuntu 24.04 系统配置与 Containerd 容器运行时部署》

目录

  • 一、Ubuntu 24.04 安装与初始配置
  • 二、管理系统网络
  • 三、管理系统软件包
  • 四、Containerd 概述
  • 五、Containerd 部署
  • 六、ctr 工具使用
  • 七、nerdctl 工具使用

一、Ubuntu 24.04 安装与初始配置

1.1 安装注意事项

⚠️ 重要提示 :安装系统时建议断开网卡,防止安装过程中自动联网更新软件包。系统安装完成后再配置网络。

1.2 安装后基础配置

1.2.1 安装基础软件包
bash 复制代码
caojie@ubuntu2404:~$ sudo apt-get install -y vim bash-completion
1.2.2 配置静态 IP(Netplan)
bash 复制代码
caojie@ubuntu2404:~$ cd /etc/netplan/
caojie@ubuntu2404:/etc/netplan$ sudo vim 00-installer-config.yaml

配置文件内容:

yaml 复制代码
network:
  ethernets:
    ens32:
      dhcp4: no
      addresses:
        - 10.1.8.10/24
      routes:
        - to: default
          via: 10.1.8.2
      nameservers:
        addresses:
          - 223.5.5.5
  version: 2

应用配置:

bash 复制代码
caojie@ubuntu2404:/etc/netplan$ sudo chmod 600 00-installer-config.yaml
caojie@ubuntu2404:/etc/netplan$ sudo netplan apply
1.2.3 配置 root 密码及远程登录
bash 复制代码
# 设置 root 密码
caojie@ubuntu2404:~$ sudo passwd
[sudo] password for laoma: redhat
New password: redhat
Retype new password: redhat

# 开启 root 远程登录
caojie@ubuntu2404:~$ sudo vim /etc/ssh/sshd_config
# PermitRootLogin prohibit-password
PermitRootLogin yes

# 重启 SSH 服务
caojie@ubuntu2404:~$ sudo systemctl reload ssh
caojie@ubuntu2404:~$ sudo systemctl enable ssh
1.2.4 配置命令提示符

~/.bashrc 文件末尾添加:

bash

复制代码
# 带时间彩色提示符
PS1='[\[\e[91m\]\u\[\e[93m\]@\[\e[92;1m\]\h\[\e[0m\] \[\e[94m\]\W\[\e[0m\] \[\e[35m\]\t\[\e[0m\]]\[\e[93m\]\$\[\e[0m\] '

# 命令历史记录带时间戳
HISTTIMEFORMAT="%F %T "
1.2.5 配置时间同步
bash 复制代码
# 安装 chrony
[root@ubuntu2404 ~]# apt install -y chrony

# 设置时区
[root@ubuntu2404 ~]# timedatectl set-timezone Asia/Shanghai

# 验证 chrony 状态
[root@ubuntu2404 ~]# systemctl is-enabled chrony
enabled

二、管理系统网络

2.1 Netplan 网络配置

Netplan 是 Canonical 开发的网络配置工具,通过 YAML 文件定义网络配置,后端渲染器支持 NetworkManagerSystemd-networkd

配置文件位置/etc/netplan/*.yaml

示例模板/usr/share/doc/netplan/examples/

2.1.1 常用 Netplan 命令
bash 复制代码
# 应用配置
[root@ubuntu2404 ~]# netplan apply

# 获取当前配置
[root@ubuntu2404 ~]# netplan get

2.2 主机名管理

bash 复制代码
# 查看主机名状态
[root@ubuntu2404 ~]# hostnamectl status

# 设置主机名
[root@ubuntu2404 ~]# hostnamectl hostname my-server

2.3 名称解析机制

2.3.1 NSS 模块解析顺序

配置文件 /etc/nsswitch.conf 控制名称解析顺序:

bash 复制代码
[root@ubuntu2404 ~]# grep host /etc/nsswitch.conf
hosts:          files dns
模块 对应文件 特点
files /etc/hosts 本地静态解析,优先级最高,无需网络
dns systemd-resolved 网络 DNS 解析,优先级较低
2.3.2 systemd-resolved DNS 解析顺序
复制代码
本地缓存 → 链路级 DNS → 全局 DNS → Fallback DNS

常用命令

bash 复制代码
# 查看 DNS 状态
[root@ubuntu2404 ~]# resolvectl status

# 查询域名解析详情
[root@ubuntu2404 ~]# resolvectl query caojie.cloud

# 清空 DNS 缓存
[root@ubuntu2404 ~]# resolvectl flush-caches

三、管理系统软件包

3.1 DEB 软件包结构

DEB 包包含两部分:

  • DEBIAN 目录:控制文件(control、preinst、postinst、prerm、postrm 等)
  • 软件安装文件:etc、usr、opt、tmp 等目录
control 文件关键字段
字段 说明
Package 软件包名称
Version 版本号
Architecture 架构(amd64、i386 等)
Depends 依赖的软件包
Pre-Depends 安装前必须配置的依赖
Recommends 推荐安装的软件包
Suggests 建议安装的软件包

3.2 dpkg-query 命令

查询系统中已安装包信息。

选项 说明 示例
-l 列出已安装软件包 dpkg-query -l openssh-server
-s 查看软件包状态 dpkg-query -s openssh-server
-p 查看软件包详细信息 dpkg-query -p adduser
-W 查看软件包版本 dpkg-query -W openssh-server
-L 查看软件包文件清单 dpkg-query -L openssh-server
-S 查找文件所属软件包 dpkg-query -S 'bin/rz'
-c 查看控制文件路径 dpkg-query -c openssh-server

3.3 dpkg-deb 命令

分析 DEB 文件信息。

选项 说明 示例
-c 查看 DEB 文件内容清单 dpkg-deb -c package.deb
-W 查看 DEB 文件版本 dpkg-deb -W package.deb
-I 查看 DEBIAN 目录元数据 dpkg-deb -I package.deb
-f 查看 control 字段 dpkg-deb -f package.deb Maintainer
-e 提取 DEBIAN 目录 dpkg-deb -e package.deb
-x 提取软件文件 dpkg-deb -x package.deb ./dest
-X 提取并显示文件 dpkg-deb -X package.deb ./dest
-R 提取全部(DEBIAN + 文件) dpkg-deb -R package.deb ./dest

3.4 dpkg 命令

用于安装、卸载、验证 DEB 包。

选项 说明
-i 安装软件包
-r 卸载软件包(保留配置文件)
-P 彻底卸载(删除配置文件)
-V 验证软件包完整性
--configure 配置已解包的软件包
bash 复制代码
# 安装
[root@ubuntu2404 ~]# dpkg -i lrzsz_0.12.21-10_amd64.deb

# 验证
[root@ubuntu2404 ~]# dpkg -V lrzsz

# 卸载
[root@ubuntu2404 ~]# dpkg -r lrzsz

⚠️ 注意 :dpkg 无法解决依赖关系,推荐使用 apt。

3.5 apt 命令

apt 是 apt-get 和 apt-cache 的现代化替代品,命令更简洁、更易用。

3.5.1 apt vs apt-get 对照表
apt 命令 对应的 apt-get 命令 功能
apt install apt-get install 安装软件包
apt remove apt-get remove 移除软件包
apt purge apt-get purge 移除软件包及配置文件
apt update apt-get update 刷新软件源索引
apt upgrade apt-get upgrade 升级所有可升级软件包
apt autoremove apt-get autoremove 自动删除不需要的包
apt full-upgrade apt-get dist-upgrade 升级并处理依赖关系
apt search apt-cache search 搜索应用程序
apt show apt-cache show 显示软件包详情
3.5.2 apt 新增命令
命令 功能
apt list 列出符合条件的软件包
apt edit-sources 编辑软件源列表
3.5.3 常用 apt 命令示例
bash 复制代码
# 更新软件源
[root@ubuntu2404 ~]# apt update

# 查看软件包
[root@ubuntu2404 ~]# apt list openssh-server
[root@ubuntu2404 ~]# apt list --all-versions openssh-server

# 查看软件包详情
[root@ubuntu2404 ~]# apt show openssh-server

# 搜索软件包
[root@ubuntu2404 ~]# apt search --names-only apache2

# 安装软件包
[root@ubuntu2404 ~]# apt install apache2 -y

# 安装特定版本
[root@ubuntu2404 ~]# apt install apache2=2.4.52-1ubuntu4.5

# 重新安装
[root@ubuntu2404 ~]# apt reinstall apache2 -y

# 卸载软件包
[root@ubuntu2404 ~]# apt remove apache2 -y

# 自动清理依赖
[root@ubuntu2404 ~]# apt autoremove -y

# 升级系统
[root@ubuntu2404 ~]# apt upgrade
[root@ubuntu2404 ~]# apt full-upgrade

# 下载 DEB 包
[root@ubuntu2404 ~]# apt download openssh-server

# 编辑软件源
[root@ubuntu2404 ~]# export EDITOR=vim
[root@ubuntu2404 ~]# apt edit-sources

3.6 apt-file 命令

查找软件包中的文件。

bash 复制代码
# 更新文件索引
[root@ubuntu2404 ~]# apt-file update

# 查看软件包文件清单
[root@ubuntu2404 ~]# apt-file list openssh-server

# 搜索文件所属软件包(正则)
[root@ubuntu2404 ~]# apt-file -x search '.*bin/ifconfig$'
net-tools: /sbin/ifconfig

3.7 软件存储库管理

3.7.1 源格式
复制代码
deb 镜像url 版本代号 软件包分类
3.7.2 软件包分类
分类 说明
main 官方支持的自由软件
restricted 官方支持的非完全自由软件
universe 社区维护的自由软件
multiverse 非自由软件
3.7.3 配置国内镜像源
bash 复制代码
# 备份原源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 查看版本代号
lsb_release -c

# 编辑源(以 Ubuntu 24.04 noble 为例)
sudo vim /etc/apt/sources.list

阿里云镜像源配置:

bash 复制代码
deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
bash 复制代码
# 更新软件源
sudo apt update

四、Containerd 概述

4.1 什么是 Containerd

Containerd 是 CNCF 毕业的工业级容器运行时 ,强调简单性健壮性可移植性

  • 向上:为 Kubernetes、nerdctl、crictl 等提供标准化接口
  • 向下:对接 runc、crun 等 OCI 运行时

📌 核心定位:容器的「管家」,负责镜像管理、容器生命周期、存储/网络管理、运行时管理、事件上报。

4.2 为什么学习 Containerd

Kubernetes 1.24+ 已移除 Dockershim,默认无法使用 Docker Engine 作为容器运行时。

方案 说明
❌ Docker Engine (1.24-) 已废弃内置支持
✅ Containerd 推荐方案,1.1+ 原生支持 CRI
✅ cri-dockerd 独立维护的 dockershim 替代品

4.3 Containerd 架构

复制代码
┌─────────────────────────────────────────────┐
│           客户端层 (K8s/nerdctl/crictl)      │
├─────────────────────────────────────────────┤
│                   ↓ gRPC API                │
├─────────────────────────────────────────────┤
│           服务层 (containerd daemon)         │
├─────────────────────────────────────────────┤
│           核心组件层 (Plugins/Managers)      │
├─────────────────────────────────────────────┤
│         底层依赖层 (runc/CNI/CSI)           │
└─────────────────────────────────────────────┘
核心组件说明
组件 功能
Content Store 镜像数据存储和校验
Snapshotter 镜像分层存储(overlayfs 等)
Registry Client 镜像拉取/推送
Container Service 容器元数据管理
Task Service 容器进程生命周期管理
CRI Plugin Kubernetes 专用接口
Runtime Manager 对接 OCI 运行时(runc)

4.4 Containerd 工作流程(K8s 拉取镜像启动容器)

bash 复制代码
Kubelet → containerd → Registry (certs.d 加速)
                     → 校验镜像 → Content Store
                     → Snapshotter 创建文件系统
                     → runc 启动容器
                     → 返回容器 PID

4.5 Containerd 版本对比

维度 Ubuntu 官方仓库 containerd Docker CE 仓库 containerd.io
包名 containerd containerd.io
维护方 Ubuntu 安全团队 Docker Inc.
版本节奏 稳定滞后,LTS 半年一更新 紧跟上游,4 个月一次小版本
典型版本 (24.04) 1.7.x 1.7.x / 2.x
适用场景 追求极致稳定 快速迭代、K8s 新版本

选型建议

场景 推荐 理由
生产环境极致稳定 Ubuntu 官方 containerd 安全验证充分
K8s 集群需新特性 Docker CE containerd.io 版本新、兼容性好
轻量独立运行时 Ubuntu 官方 containerd 体积小、灵活

五、Containerd 部署

5.1 实验环境

  • VMware Workstation 17
  • Ubuntu 24.04 LTS
  • Containerd 1.7.12(示例版本)

5.2 安装 Containerd

bash 复制代码
# 安装 containerd
root@ubuntu2404:~# apt install -y containerd=1.7.12-0ubuntu4

5.3 配置镜像加速

5.3.1 不同版本配置方式对比
Containerd 版本 config 版本 推荐方式 旧方式 mirrors
1.5.x 及更早 v2 mirrors ✅ 支持
1.6.x v2 config_path + certs.d ✅ 兼容
1.7.x v2/v3 config_path + certs.d ⚠️ 兼容(弃用)
2.x v3 config_path + certs.d(强制) ❌ 废弃
5.3.2 新方式:config_path + certs.d(推荐)
bash 复制代码
# 1. 修改配置文件
root@ubuntu2404:~# vim /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".registry]
  config_path = "/etc/containerd/certs.d"

# ⚠️ containerd 2.2.1 需删除不存在的 /etc/docker/certs.d 路径

# 2. 配置 docker.io 加速
root@ubuntu2404:~# mkdir -p /etc/containerd/certs.d/docker.io
root@ubuntu2404:~# cat > /etc/containerd/certs.d/docker.io/hosts.toml << 'EOF'
server = "https://registry-1.docker.io"

[host."https://docker.m.daocloud.io"]
  capabilities = ["pull", "resolve"]

[host."https://09def58152000fc00ff0c00057bad7e0.mirror.swr.myhuaweicloud.com"]
  capabilities = ["pull", "resolve"]
EOF

# 3. 配置 registry.k8s.io 加速
root@ubuntu2404:~# mkdir -p /etc/containerd/certs.d/registry.k8s.io
root@ubuntu2404:~# cat > /etc/containerd/certs.d/registry.k8s.io/hosts.toml << 'EOF'
server = "https://registry.k8s.io"

[host."https://k8s.m.daocloud.io"]
  capabilities = ["pull", "resolve"]

[host."https://09def58152000fc00ff0c00057bad7e0.mirror.swr.myhuaweicloud.com"]
  capabilities = ["pull", "resolve"]
EOF

# 4. 重启服务
root@ubuntu2404:~# systemctl restart containerd
5.3.3 验证镜像加速
bash 复制代码
# 使用 ctr 验证(需加 --hosts-dir 参数)
root@ubuntu2404:~# ctr image pull --hosts-dir /etc/containerd/certs.d docker.io/library/busybox:latest

root@ubuntu2404:~# ctr image pull --hosts-dir /etc/containerd/certs.d registry.k8s.io/pause:3.8

root@ubuntu2404:~# ctr image ls | awk '{print $1}'
REF
docker.io/library/busybox:latest
registry.k8s.io/pause:3.8

六、ctr 工具使用

⚠️ 注意 :ctr 是 containerd 自带的底层调试工具,难用、不友好、不适合日常使用

6.1 查看帮助

bash 复制代码
root@ubuntu2404:~# ctr --help

6.2 命名空间管理

Containerd 支持命名空间隔离:

bash 复制代码
# 查看命名空间
root@ubuntu2404:~# ctr namespace ls
NAME   LABELS 
k8s.io

# 在指定命名空间操作
root@ubuntu2404:~# ctr -n k8s.io container ls

6.3 ctr 常用命令

命令 说明
ctr image pull 拉取镜像(需加 --hosts-dir 使用加速)
ctr image ls 列出镜像
ctr image rm 删除镜像
ctr container ls 列出容器
ctr task ls 列出任务
ctr run 运行容器

七、nerdctl 工具使用

推荐:nerdctl 命令 99% 与 Docker 一致,生产环境日常使用首选。

7.1 安装 nerdctl

bash 复制代码
# 1. 下载并安装 nerdctl
root@ubuntu2404:~# wget http://192.168.42.200/course-materials/softwares/stage03/nerdctl-1.7.7-linux-amd64.tar.gz
root@ubuntu2404:~# tar -xf nerdctl-1.7.7-linux-amd64.tar.gz -C /usr/bin/

# 2. 配置命令补全
root@ubuntu2404:~# apt install -y bash-completion
root@ubuntu2404:~# nerdctl completion bash > /etc/bash_completion.d/nerdctl
root@ubuntu2404:~# source /etc/bash_completion.d/nerdctl

# 3. 下载 CNI 插件
root@ubuntu2404:~# wget http://192.168.42.200/course-materials/softwares/stage03/cni-plugins-linux-amd64-v1.6.0.tgz
root@ubuntu2404:~# mkdir -p /opt/cni/bin
root@ubuntu2404:~# tar -xf cni-plugins-linux-amd64-v1.6.0.tgz -C /opt/cni/bin

# 4. 安装防火墙依赖
root@ubuntu2404:~# apt install -y iptables

# 5. 加载内核模块
root@ubuntu2404:~# modprobe -a overlay br_netfilter
root@ubuntu2404:~# cat > /etc/modules-load.d/k8s-net.conf << 'EOF'
br_netfilter
overlay
EOF

# 6. 配置内核参数
root@ubuntu2404:~# cat > /etc/sysctl.d/k8s.conf << 'EOF'
net.bridge.bridge-nf-call-iptables=1  
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
EOF
root@ubuntu2404:~# sysctl -p /etc/sysctl.d/k8s.conf

7.2 验证部署

bash 复制代码
root@ubuntu2404:~# nerdctl version
root@ubuntu2404:~# nerdctl info

7.3 nerdctl 配置文件

模式 配置文件路径
rootful(sudo/root) /etc/nerdctl/nerdctl.toml
rootless(普通用户) ~/.config/nerdctl/nerdctl.toml

配置示例:

toml

bash 复制代码
address = "unix:///run/containerd/containerd.sock"
hosts_dir = ["/etc/containerd/certs.d", "/etc/docker/certs.d"]
namespace = "default"

7.4 nerdctl 镜像管理命令

pull - 拉取镜像
bash 复制代码
root@ubuntu2404:~# nerdctl pull busybox
root@ubuntu2404:~# nerdctl pull docker.io/library/mysql:latest
images - 查看本地镜像
bash 复制代码
root@ubuntu2404:~# nerdctl images
REPOSITORY    TAG    IMAGE ID    CREATED    PLATFORM    SIZE    BLOB SIZE
busybox       latest 560af6915bfc 4 min ago linux/amd64 4.8 MiB 2.5 MiB
tag - 打标签
bash 复制代码
root@ubuntu2404:~# nerdctl tag busybox mage16196/busybox
push - 推送镜像
bash 复制代码
# 登录 Docker Hub
root@ubuntu2404:~# nerdctl login
Enter Username: mage16196
Enter Password: 

# 推送镜像
root@ubuntu2404:~# nerdctl push mage16196/busybox
save - 导出镜像
bash 复制代码
root@ubuntu2404:~# nerdctl save busybox -o busybox.tar
load - 导入镜像
bash 复制代码
root@ubuntu2404:~# nerdctl load -i busybox.tar
rm - 删除镜像
bash 复制代码
root@ubuntu2404:~# nerdctl image rm docker.io/library/mysql
history - 查看镜像构建历史
bash 复制代码
root@ubuntu2404:~# nerdctl history docker.io/library/mysql
inspect - 查看镜像详细信息
bash 复制代码
root@ubuntu2404:~# nerdctl inspect docker.io/library/mysql
prune - 清理未使用镜像
bash 复制代码
root@ubuntu2404:~# nerdctl image prune --all --force

7.5 镜像加速配置

nerdctl 不会读取 CRI 专属的 registry 配置 ,而是使用 config_path + certs.d 方式:

bash 复制代码
# 配置已经在 5.3.2 中完成
# nerdctl 会自动读取 /etc/containerd/certs.d 目录

# 验证加速
root@ubuntu2404:~# nerdctl pull hello-world

附录:常用命令速查

Netplan 网络

bash 复制代码
netplan apply          # 应用网络配置
netplan get            # 查看当前配置
resolvectl status      # 查看 DNS 状态
resolvectl query <域名> # 查询域名解析

apt 包管理

bash 复制代码
apt update             # 更新软件源
apt install <包名>     # 安装软件包
apt remove <包名>      # 卸载软件包
apt purge <包名>       # 彻底卸载
apt autoremove         # 清理依赖
apt search <关键词>    # 搜索软件包
apt show <包名>        # 查看详情
apt list --installed   # 列出已安装包
apt edit-sources       # 编辑软件源

containerd/nerdctl 容器

bash 复制代码
nerdctl pull <镜像>    # 拉取镜像
nerdctl images         # 查看镜像
nerdctl tag <源> <目标> # 打标签
nerdctl push <镜像>    # 推送镜像
nerdctl save -o <文件> <镜像>  # 导出镜像
nerdctl load -i <文件> # 导入镜像
nerdctl rm <镜像>      # 删除镜像
nerdctl prune          # 清理未使用镜像
nerdctl login          # 登录镜像仓库