深入了解 containerd 容器執行時,Kubernetes 和 Docker 的核心元件
專案簡介
containerd 是一個工業級的容器執行時,負責容器的生命週期管理。是 Docker 和 Kubernetes 的核心元件,現為 CNCF 畢業專案。
GitHub Stars: 20K+
主要功能
- 容器執行 - 建立和管理容器
- 映像管理 - 拉取、儲存映像
- 快照管理 - 高效檔案系統層
- 網路/儲存 - 可插拔介面
- OCI 相容 - 標準容器格式
架構
1
2
3
4
5
6
7
8
9
| ┌─────────────────────────────────────────┐
│ containerd │
├──────────────┬──────────────┬───────────┤
│ Content │ Metadata │ Runtime │
│ Store │ Store │ (runc) │
├──────────────┴──────────────┴───────────┤
│ Snapshotter │
│ (overlayfs, btrfs, zfs) │
└─────────────────────────────────────────┘
|
安裝
Ubuntu/Debian
1
2
| sudo apt-get update
sudo apt-get install containerd
|
從 Release
1
2
| wget https://github.com/containerd/containerd/releases/download/v1.7.0/containerd-1.7.0-linux-amd64.tar.gz
sudo tar Cxzvf /usr/local containerd-1.7.0-linux-amd64.tar.gz
|
設定 systemd
1
2
3
4
5
6
7
8
9
10
11
| # /etc/systemd/system/containerd.service
[Unit]
Description=containerd container runtime
After=network.target
[Service]
ExecStart=/usr/local/bin/containerd
Restart=always
[Install]
WantedBy=multi-user.target
|
1
2
| sudo systemctl daemon-reload
sudo systemctl enable --now containerd
|
設定
產生預設設定
1
| containerd config default | sudo tee /etc/containerd/config.toml
|
基本設定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # /etc/containerd/config.toml
version = 2
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.k8s.io/pause:3.9"
[plugins."io.containerd.grpc.v1.cri".containerd]
default_runtime_name = "runc"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
|
Registry 鏡像
1
2
3
4
| [plugins."io.containerd.grpc.v1.cri".registry]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://mirror.gcr.io"]
|
ctr 命令
映像操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 拉取映像
ctr images pull docker.io/library/nginx:latest
# 列出映像
ctr images list
# 刪除映像
ctr images remove docker.io/library/nginx:latest
# 匯出映像
ctr images export nginx.tar docker.io/library/nginx:latest
# 匯入映像
ctr images import nginx.tar
|
容器操作
1
2
3
4
5
6
7
8
| # 建立容器
ctr containers create docker.io/library/nginx:latest nginx
# 列出容器
ctr containers list
# 刪除容器
ctr containers delete nginx
|
任務操作
1
2
3
4
5
6
7
8
9
10
11
12
| # 啟動容器
ctr task start nginx
# 列出任務
ctr task list
# 執行命令
ctr task exec --exec-id myexec nginx /bin/sh
# 停止任務
ctr task kill nginx
ctr task delete nginx
|
nerdctl
安裝
1
2
| wget https://github.com/containerd/nerdctl/releases/download/v1.7.0/nerdctl-1.7.0-linux-amd64.tar.gz
sudo tar Cxzvf /usr/local/bin nerdctl-1.7.0-linux-amd64.tar.gz
|
Docker 相容命令
1
2
3
4
5
6
7
8
9
10
11
| # 運行容器
nerdctl run -d -p 80:80 nginx
# 列出容器
nerdctl ps
# 建置映像
nerdctl build -t myapp .
# 推送映像
nerdctl push myapp:latest
|
Compose
1
2
| nerdctl compose up -d
nerdctl compose down
|
Kubernetes 整合
CRI 設定
1
2
3
4
| [plugins."io.containerd.grpc.v1.cri"]
[plugins."io.containerd.grpc.v1.cri".containerd]
snapshotter = "overlayfs"
default_runtime_name = "runc"
|
kubelet 設定
1
2
| # /var/lib/kubelet/config.yaml
containerRuntimeEndpoint: unix:///run/containerd/containerd.sock
|
crictl 命令
1
2
3
4
5
6
7
8
9
10
11
| # 列出容器
crictl ps
# 列出 Pod
crictl pods
# 檢視日誌
crictl logs <container-id>
# 執行命令
crictl exec -it <container-id> /bin/sh
|
快照管理
支援的 Snapshotter
| Snapshotter | 說明 |
|---|
| overlayfs | 預設,效能好 |
| btrfs | Btrfs 檔案系統 |
| zfs | ZFS 檔案系統 |
| native | 簡單複製 |
| devmapper | Device Mapper |
設定 Snapshotter
1
2
| [plugins."io.containerd.grpc.v1.cri".containerd]
snapshotter = "overlayfs"
|
命名空間
使用命名空間
1
2
3
4
5
6
7
8
| # 指定命名空間
ctr -n k8s.io containers list
# 建立命名空間
ctr namespaces create myns
# 列出命名空間
ctr namespaces list
|
常見命名空間
| 命名空間 | 用途 |
|---|
| default | 預設 |
| k8s.io | Kubernetes |
| moby | Docker |
安全設定
Rootless 模式
1
2
| containerd-rootless-setuptool.sh install
containerd-rootless-setuptool.sh install-buildkit
|
Seccomp
1
2
| [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SeccompDefault = true
|
AppArmor
1
2
| [plugins."io.containerd.grpc.v1.cri"]
restrict_oom_score_adj = true
|
監控
Prometheus 指標
1
2
| [metrics]
address = "127.0.0.1:1338"
|
健康檢查
1
2
| ctr version
containerd healthcheck
|
疑難排解
日誌
1
| journalctl -u containerd -f
|
Debug 模式
1
2
| [debug]
level = "debug"
|
常見問題
1
2
3
4
5
| # 檢查 socket
ls -la /run/containerd/containerd.sock
# 檢查服務狀態
systemctl status containerd
|
相關連結
延伸閱讀