Containerd【輕量級(jí)容器管理工具】
Kubernetes v1.24 之前的版本直接集成了 Docker Engine 的一個(gè)組件,名為 dockershim [用于調(diào)用Docker]。 這種特殊的直接整合不再是 Kubernetes 的一部分 (這次刪除被作為 v1.20 發(fā)行版本的一部分宣布)。 這意味Kubernetes從版本1.24開(kāi)始就棄用Docker作為容器運(yùn)行時(shí),取而代之的是更加輕量級(jí)的Containerd。
containerd可用作 Linux 和 Windows 的守護(hù)進(jìn)程。它管理其主機(jī)系統(tǒng)的完整容器生命周期,從圖像傳輸和存儲(chǔ)到容器執(zhí)行和監(jiān)督,再到低級(jí)存儲(chǔ)到網(wǎng)絡(luò)附件等等。
一、Containerd介紹
1、Containerd的由來(lái)
【Docker名噪一時(shí),捐出runC】2013年docker公司在推出docker產(chǎn)品后,由于其對(duì)全球技術(shù)產(chǎn)生了一定的影響力,Google公司明顯感覺(jué)到自己公司內(nèi)部所使用的Brog系統(tǒng)江湖地位受到的威脅,希望Docker公司能夠與自己聯(lián)合打造一款開(kāi)源的容器運(yùn)行時(shí)作為Docker核心依賴(lài),但Docker公司拒絕了;接著Google公司聯(lián)合RedHat、IBM等公司說(shuō)服Docker公司把其容器核心技術(shù)libcontainer
捐給OCI(Open Container Intiative),并更名為runC
。
【CNCF成立,kubernetes被迫開(kāi)源】為了進(jìn)一步遏制Docker在未來(lái)技術(shù)市場(chǎng)影響力,避免在容器市場(chǎng)上Docker一家獨(dú)大,Google公司帶領(lǐng)RedHat、IBM等成立了CNCF(Cloud Native Computing Fundation)基金會(huì),即云原生計(jì)算基金會(huì)。CNCF的目標(biāo)很明確,既然在容器應(yīng)用領(lǐng)域無(wú)法與Docker相抗衡,那就做Google更有經(jīng)驗(yàn)的技術(shù)市場(chǎng)——大規(guī)模容器編排應(yīng)用場(chǎng)景。Google公司把自己內(nèi)部使用的Brog系統(tǒng)開(kāi)源——Kubernetes,也就是我們今天所說(shuō)的云原生技術(shù)生態(tài)。
【Docker妥協(xié),貢獻(xiàn)出Containerd】2016年Docker公司推出了Docker Swarm,意在一統(tǒng)Docker生態(tài),讓Docker既可以實(shí)現(xiàn)容器應(yīng)用管理,也可以實(shí)現(xiàn)大規(guī)模容器編排,經(jīng)過(guò)近1年左右時(shí)間的市場(chǎng)驗(yàn)證后,發(fā)現(xiàn)在容器編排方面無(wú)法獨(dú)立抗衡kubernetes,所以Docker公司于2017年正式宣布原生支持Kubernetes。至此,Docker在大規(guī)模容器編排應(yīng)用市場(chǎng)敗下陣來(lái),但是Docker依然不甘心失敗,把Docker核心依賴(lài)Containerd捐給了CNCF,依此說(shuō)明Docker依舊是一個(gè)PaaS平臺(tái)。
【k8s宣布不支持Docker,Containerd成為CRI主角】2020年CNCF基金會(huì)宣布Kubernetes 1.20版本將不再僅支持Docker容器管理工具,此事的起因主要也與Docker捐給CNCF基金會(huì)的Containerd有關(guān),早期為了實(shí)現(xiàn)Kubernetes能夠使用Docker實(shí)現(xiàn)容器管理,專(zhuān)門(mén)在Kubernetes組件中集成一個(gè)shim
技術(shù),用來(lái)將Kubernetes 容器運(yùn)行時(shí)接口(CRI,Container Runntime Interface)調(diào)用翻譯成Docker的API,這樣就可以很好地使用Docker了。但是隨著Kubernetes在全球技術(shù)市場(chǎng)的廣泛應(yīng)用,有更多的容器管理工具的出現(xiàn),它們都想能夠借助于Kubernetes被用戶(hù)所使用,所以就提出標(biāo)準(zhǔn)化容器運(yùn)行時(shí)接口,只要適配了這個(gè)接口就可以集成到Kubernetes生態(tài)當(dāng)中,所以Kubernetes取消了對(duì)shim的維護(hù),并且由于Containerd技術(shù)的成功,可以實(shí)現(xiàn)無(wú)縫對(duì)接Kubernetes,所以接下來(lái)Kubernetes容器運(yùn)行時(shí)的主角是Containerd。
2、Containerd概念
早在2016年3月,Docker 1.11的Docker Engine里就包含了containerd,而現(xiàn)在則是把containerd從Docker Engine里徹底剝離出來(lái),作為一個(gè)獨(dú)立的開(kāi)源項(xiàng)目獨(dú)立發(fā)展,目標(biāo)是提供一個(gè)更加開(kāi)放、穩(wěn)定的容器運(yùn)行基礎(chǔ)設(shè)施。
和原先包含在Docker Engine里containerd相比,獨(dú)立的containerd將具有更多的功能,可以涵蓋整個(gè)容器運(yùn)行時(shí)管理的所有需求。另外獨(dú)立之后containerd的特性演進(jìn)可以和Docker Engine分開(kāi),專(zhuān)注容器運(yùn)行時(shí)管理,可以更穩(wěn)定。
Containerd是一個(gè)工業(yè)標(biāo)準(zhǔn)的容器運(yùn)行時(shí),重點(diǎn)是它簡(jiǎn)潔,健壯,便攜,在Linux和window上可以作為一個(gè)守護(hù)進(jìn)程運(yùn)行,它可以管理主機(jī)系統(tǒng)上容器的完整的生命周期:鏡像傳輸和存儲(chǔ),容器的執(zhí)行和監(jiān)控,低級(jí)別的存儲(chǔ)和網(wǎng)絡(luò)。
每個(gè)containerd只負(fù)責(zé)一臺(tái)機(jī)器,Pull鏡像,對(duì)容器的操作(啟動(dòng)、停止等),網(wǎng)絡(luò),存儲(chǔ)都是由containerd完成。具體運(yùn)行容器由runC負(fù)責(zé),實(shí)際上只要是符合OCI規(guī)范的容器都可以支持。
Containerd和docker不同,containerd重點(diǎn)是集成在大規(guī)模的系統(tǒng)中,例如kubernetes、Swarm、Mesos等【對(duì)于容器編排服務(wù)來(lái)說(shuō),運(yùn)行時(shí)只需要使用containerd+runC,更加輕量,容易管理?!?。Containerd 被設(shè)計(jì)成嵌入到一個(gè)更大的系統(tǒng)中,而不是直接由開(kāi)發(fā)人員或終端用戶(hù)使用。
Containerd的特點(diǎn):
- 簡(jiǎn)潔的基于 gRPC 的 API 和 client library。
- 完整的 OCI 支持(runtime 和 image spec)。
- 同時(shí)具備穩(wěn)定性和高性能的定義良好的容器核心功能。
- 一個(gè)解耦的系統(tǒng)(讓 image、filesystem、runtime 解耦合),實(shí)現(xiàn)插件式的擴(kuò)展和重用。
Containerd的作用:
- 管理容器的生命周期(從創(chuàng)建容器到銷(xiāo)毀容器)。
- 拉取/推送容器鏡像。
- 存儲(chǔ)管理(管理鏡像及容器數(shù)據(jù)的存儲(chǔ))。
- 調(diào)用 runC 運(yùn)行容器(與 runC 等容器運(yùn)行時(shí)交互)。
- 管理容器網(wǎng)絡(luò)接口及網(wǎng)絡(luò)。
使用 bucketbench 對(duì) Docker、crio 和 Containerd 的性能測(cè)試結(jié)果,包括啟動(dòng)、停止和刪除容器,以比較它們所耗的時(shí)間,可以發(fā)現(xiàn)Containerd 在各個(gè)方面都表現(xiàn)良好,總體性能優(yōu)于 Docker 和 crio。
3、Containerd架構(gòu)
Containerd 采用標(biāo)準(zhǔn)的 C/S 架構(gòu):服務(wù)端通過(guò) GRPC 協(xié)議提供穩(wěn)定的 API;客戶(hù)端通過(guò)調(diào)用服務(wù)端的 API 進(jìn)行高級(jí)的操作。
為了實(shí)現(xiàn)解耦,Containerd 將不同的職責(zé)劃分給不同的組件,每個(gè)組件就相當(dāng)于一個(gè)子系統(tǒng)(subsystem)。連接不同子系統(tǒng)的組件被稱(chēng)為模塊。
Containerd 被分為三個(gè)大塊: Storage 、 Metadata 和 Runtime。
Containerd 兩大子系統(tǒng)為:
Bundle : 在 Containerd 中,Bundle 包含了配置、元數(shù)據(jù)和根文件系統(tǒng)數(shù)據(jù),你可以理解為 容器的文件系統(tǒng)。而 Bundle 子系統(tǒng)允許用戶(hù)從鏡像中提取和打包 Bundles。
Runtime : Runtime 子系統(tǒng)用來(lái)執(zhí)行 Bundles,比如創(chuàng)建容器。其中,每一個(gè)子系統(tǒng)的行為都由一個(gè)或多個(gè)模塊協(xié)作完成(架構(gòu)圖中的 Core 部分)。
4、幾個(gè)概念區(qū)分
containerd
是一個(gè)高級(jí)容器運(yùn)行時(shí),又名容器管理器。簡(jiǎn)單來(lái)說(shuō),它是一個(gè)守護(hù)進(jìn)程,在單個(gè)主機(jī)上管理完整的容器生命周期:創(chuàng)建、啟動(dòng)、停止容器、拉取和存儲(chǔ)鏡像、配置掛載、網(wǎng)絡(luò)等。
ctr
是作為 containerd 項(xiàng)目的一部分提供的命令行客戶(hù)端。該ctr界面 [顯然] 與 Docker CLI不兼容,乍一看,可能看起來(lái)不太用戶(hù)友好。因?yàn)樗闹饕鼙娛菧y(cè)試守護(hù)進(jìn)程的容器開(kāi)發(fā)人員。ctr + containerd比docker + dockerd更接近實(shí)際的容器。
nerdctl
是一個(gè)相對(duì)較新的containerd命令行客戶(hù)端。與ctr不同,nerdctl的目標(biāo)是用戶(hù)友好和docker兼容。在某種程度上,nerdctl + containerd可以無(wú)縫地替代docker + dockerd。
crictl
是一個(gè)命令行客戶(hù)端,用于 [kubernetes] CRI兼容的容器運(yùn)行時(shí)。引入 Kubernetes 容器運(yùn)行時(shí)接口 (CRI)以使 Kubernetes 容器運(yùn)行時(shí)不可知。Kubernetes節(jié)點(diǎn)代理kubelet實(shí)現(xiàn)了 CRI客戶(hù)端 API,可以使用任何實(shí)現(xiàn) CRI 服務(wù)器 API的容器運(yùn)行時(shí)來(lái)管理其節(jié)點(diǎn)上的容器和 Pod。
二、Containerd安裝
containerd官網(wǎng):https://containerd.io/
containerd官方安裝步驟:https://github.com/containerd/containerd/blob/main/docs/getting-started.md
下載好相應(yīng)的安裝部署包【所有安裝包都可在github上進(jìn)行下載】如下:
$ ll
-rw-r--r-- 1 root root 118639 Sep 2 08:32 cni-1.1.2.tar.gz # cni工具源碼包[注意要與cni插件兼容]# cni工具1.1.2兼容cni插件1.0.0
-rw-r--r-- 1 root root 36336160 Sep 1 13:49 cni-plugins-linux-amd64-v1.0.0.tgz #cni插件
-rw-r--r-- 1 root root 44458241 Sep 1 13:20 containerd-1.6.8-linux-amd64.tar.gz #containerd
-rw-r--r-- 1 root root 10685899 Sep 1 14:36 nerdctl-0.22.2-linux-amd64.tar.gz #nerdctl
-rw-r--r-- 1 root root 9431456 Sep 1 13:58 runc.amd64 #runC
step1:安裝containerd
$ tar Cxzvf /usr/local containerd-1.6.8-linux-amd64.tar.gz
將containerd服務(wù)設(shè)置衛(wèi)開(kāi)機(jī)啟動(dòng),先配置為系統(tǒng)服務(wù)
$ vim /etc/systemd/system/containerd.service
內(nèi)容如下:
$ cat /etc/systemd/system/containerd.service
# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
#uncomment to enable the experimental sbservice (sandboxed) version of containerd/cri integration
#Environment="ENABLE_CRI_SANDBOXES=sandboxed"
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
將containerd服務(wù)設(shè)置衛(wèi)開(kāi)機(jī)啟動(dòng)。
$ systemctl enable --now containerd
# 驗(yàn)證
$ ctr version
Client:
Version: v1.6.8
Revision: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
Go version: go1.17.13
Server:
Version: v1.6.8
Revision: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
UUID: 87222662-9eb1-44cb-998a-689c9efce638
至此,containerd安裝完成。
step2:安裝runC
由于二進(jìn)制包中提供的runC默認(rèn)需要系統(tǒng)中安裝seccomp支持,需要單獨(dú)安裝,且不同版本runC 對(duì)seccomp版本要求一致,所以建議單獨(dú)下載runC二進(jìn)制包進(jìn)行安裝,里面包含了seccomp模塊支持。
$ install -m 755 runc.amd64 /usr/local/sbin/runc
# 驗(yàn)證
$ runc -v
runc version 1.1.4
commit: v1.1.4-0-g5fd4c4d1
spec: 1.0.2-dev
go: go1.17.10
libseccomp: 2.5.4
step3:安裝cni插件和cni工具包
$ mkdir -p /home/cni-plugins
$ tar Cxzvf /home/cni-plugins cni-plugins-linux-amd64-v1.0.0.tgz
## 該二進(jìn)制文件是靜態(tài)構(gòu)建的,可以在任何 Linux 發(fā)行版上運(yùn)行
# cni工具1.1.2兼容cni插件1.0.0
$ mkdir -p /home/cni-tools
$ tar Cxzvf /home/cni-tools cni-1.1.2.tar.gz
完成。
三、ctr命令行操作
ctr是作為 containerd 項(xiàng)目的一部分提供的命令行客戶(hù)端。
該ctr界面 [顯然] 與 Docker CLI不兼容,乍一看,可能看起來(lái)不太用戶(hù)友好。顯然,它的主要受眾是測(cè)試守護(hù)進(jìn)程的容器開(kāi)發(fā)人員。但是,由于它最接近實(shí)際的 containerd API,因此它可以作為一種很好的探索手段——通過(guò)檢查可用命令,可以大致了解 containerd可以做什么和不可以做什么。
$ ctr --help
NAME:
ctr -
__
_____/ /______
/ ___/ __/ ___/
/ /__/ /_/ /
\___/\__/_/
containerd CLI
USAGE:
ctr [global options] command [command options] [arguments...]
VERSION:
v1.6.8
DESCRIPTION:
ctr is an unsupported debug and administrative client for interacting
with the containerd daemon. Because it is unsupported, the commands,
options, and operations are not guaranteed to be backward compatible or
stable from release to release of the containerd project.
COMMANDS:
plugins, plugin provides information about containerd plugins
version print the client and server versions
containers, c, container manage containers
content manage content
events, event display containerd events
images, image, i manage images
leases manage leases
namespaces, namespace, ns manage namespaces
pprof provide golang pprof outputs for containerd
run run a container
snapshots, snapshot manage snapshots
tasks, t, task manage tasks
install install a new package
oci OCI tools
shim interact with a shim directly
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--debug enable debug output in logs
--address value, -a value address for containerd's GRPC server (default: "/run/containerd/containerd.sock") [$CONTAINERD_ADDRESS]
--timeout value total timeout for ctr commands (default: 0s)
--connect-timeout value timeout for connecting to containerd (default: 0s)
--namespace value, -n value namespace to use with commands (default: "default") [$CONTAINERD_NAMESPACE]
--help, -h show help
--version, -v print the version
1、Containerd鏡像管理
# 拉取鏡像 拉取鏡像,完全合格的參考似乎是必需的,所以不能忽略鏡像倉(cāng)庫(kù)或標(biāo)簽部分。
$ ctr images pull --all-platforms docker.io/library/nginx:alpine
# --all-platforms 指定所有平臺(tái)鏡像
# --platform 指定系統(tǒng)平臺(tái)
$ ctr images pull --platform linux/amd64 docker.io/library/nginx:latest
# 查看鏡像
$ ctr images ls
# 掛載鏡像
$ ctr images mount docker.io/library/nginx:alpine /mnt
$ ls /mnt
$ umount /mnt
# 鏡像導(dǎo)出,并保存為nginx.img文件
$ ctr i export --all-platforms nginx.img docker.io/library/nginx:alpine
$ ls -l
-rw-r--r-- 1 root root 70138368 Sep 2 16:33 nginx.img
# 鏡像導(dǎo)入
$ ctr images import nginx.img
# 鏡像打tag
$ ctr images tag docker.io/library/nginx:alpine nginx:alpine
# 鏡像對(duì)比
$ ctr images check
2、Containerd容器管理
# 創(chuàng)建【靜態(tài)】容器
# 該命令創(chuàng)建容器后,容器并沒(méi)有處于運(yùn)行狀態(tài),其只是一個(gè)靜態(tài)的容器。這個(gè)container對(duì)象只是包含了運(yùn)行一個(gè)容器所需的資源及配置的數(shù)據(jù)結(jié)構(gòu),例如: namespaces、rootfs 和容器的配置都已經(jīng)初始化成功了,只是用戶(hù)進(jìn)程(本案例為nginx)還沒(méi)有啟動(dòng)。需要使用`ctr tasks`命令才能獲取一個(gè)動(dòng)態(tài)容器。
$ ctr container create docker.io/library/nginx:alpine testnginx1
# 啟動(dòng)【動(dòng)態(tài)】容器 創(chuàng)建容器和任務(wù)子命令分離
$ ctr task start -d testnginx1
# 直接運(yùn)行一個(gè)動(dòng)態(tài)容器【容器的IP就是宿主機(jī)的IP】
# ctr運(yùn)行命令實(shí)際上是ctr容器創(chuàng)建+ ctr任務(wù)啟動(dòng)的快捷方式
$ ctr run -d --net-host docker.io/library/nginx:alpine testnginx2
# 查看【靜態(tài)】容器
$ ctr container ls
# 查看【動(dòng)態(tài)】容器
$ ctr task ps testnginx1
PID INFO
64574 -
64613 -
64614 -
64615 -
64616 -
# 物理機(jī)上查看
$ ps -ef | grep nginx
root 64574 64553 0 16:41 ? 00:00:00 nginx: master process nginx -g daemon off;
101 64613 64574 0 16:41 ? 00:00:00 nginx: worker process
101 64614 64574 0 16:41 ? 00:00:00 nginx: worker process
101 64615 64574 0 16:41 ? 00:00:00 nginx: worker process
101 64616 64574 0 16:41 ? 00:00:00 nginx: worker process
# 進(jìn)入容器
$ ctr task exec --exec-id 1 testnginx2 sh
# 查看網(wǎng)卡
ifconfig
ens33 Link encap:Ethernet HWaddr 00:0C:29:32:CC:B0
inet addr:192.168.168.201 Bcast:192.168.168.255 Mask:255.255.255.0
inet6 addr: fe80::63d5:da66:d3db:fa1c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:476940 errors:0 dropped:0 overruns:0 frame:0
TX packets:595381 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:175687016 (167.5 MiB) TX bytes:149770479 (142.8 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
# 為容器中運(yùn)行的網(wǎng)站添加網(wǎng)站文件
echo "hello nginx2" > /usr/share/nginx/html/index.html
# 在容器里訪問(wèn)
curl 127.0.0.1
# 退出容器
exit
# 在宿主機(jī)上訪問(wèn)
$ curl 192.168.168.201
hello nginx2
# 暫停容器
$ ctr task pause testnginx2
# 恢復(fù)容器
$ ctr task resume testnginx2
# 停止容器
$ ctr task kill testnginx2
# 刪除動(dòng)態(tài)容器【必須先停止】
$ ctr task delete testnginx2
# 刪除靜態(tài)容器
$ ctr container delete testnginx2
3、Containerd命名空間管理
containerd中namespace的作用為:隔離運(yùn)行的容器,可以實(shí)現(xiàn)運(yùn)行多個(gè)容器。
# 拉取鏡像
$ ctr namespace --help
# 創(chuàng)建命名空間
$ ctr ns create testns
# 查看命名空間
$ ctr ns ls
# 在命名空間中進(jìn)行鏡像、容器等相關(guān)操作
$ ctr -n testns images pull docker.io/library/nginx:latest
$ ctr -n testns images ls
$ ctr -n testns container create docker.io/library/nginx:latest
$ ctr -n testns container ls
# 刪除命名空間
$ ctr ns rm testns
4、Containerd網(wǎng)絡(luò)管理
默認(rèn)Containerd管理的容器僅有l(wèi)o網(wǎng)絡(luò),無(wú)法訪問(wèn)容器之外的網(wǎng)絡(luò),可以為其添加網(wǎng)絡(luò)插件,使用容器可以連接外網(wǎng),CNI(Container Network Interface)
cni插件和cni工具包已經(jīng)在前面第二章節(jié)已經(jīng)部署完畢。
# cni插件目錄
$ cd /home/cni-plugins
# cni工具包目錄
$ cd /home/cni-tools/cni-1.1.2
# cni工具1.1.2兼容cni插件1.0.0
- 下面先開(kāi)始準(zhǔn)備容器網(wǎng)絡(luò)配置文件,用于為容器提供網(wǎng)關(guān)、IP地址等。
$ vim /etc/cni/net.d/10-mynet.conf
$ vim /etc/cni/net.d/99-loopback.conf
$ cat /etc/cni/net.d/10-mynet.conf
{
"cniVersion": "1.0.0",
"name": "mynet",
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "10.66.0.0/16",
"routes": [
{
"dst": "0.0.0.0/0"
}
]
}
}
$ cat /etc/cni/net.d/99-loopback.conf
{
"cniVerion": "1.0.0",
"name": "lo",
"type": "loopback"
}
- 生成cni網(wǎng)絡(luò)
# 獲取epel源
$ wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#安裝jq【jq是一個(gè)'出色'的'針對(duì)-->JSON處理器'的命令行】
$ yum -y install jq
# 進(jìn)入cni工具目錄
$ cd /home/cni-tools/cni-1.1.2
# 執(zhí)行腳本文件,基于/etc/cni/net.d/目錄中的*.conf配置文件生成容器網(wǎng)絡(luò)
# CNI_PATH是cni插件安裝的目錄
$ CNI_PATH=/home/cni-plugins ./priv-net-run.sh echo "Hello World"
# cni插件和cni工具包版本要兼容,否則可能會(huì)報(bào)錯(cuò)如下:
mynet : error executing ADD: {
"code": 1,
"msg": "incompatible CNI versions",
"details": "config is \"1.0.0\", plugin supports [\"0.1.0\" \"0.2.0\" \"0.3.0\" \"0.3.1\" \"0.4.0\"]"
}
#安裝成功后,在宿主機(jī)上查看是否生成容器網(wǎng)絡(luò)名為cni0的網(wǎng)橋
$ ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
...
7: cni0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 6a:32:bc:eb:0f:23 brd ff:ff:ff:ff:ff:ff
inet 10.66.0.1/16 brd 10.66.255.255 scope global cni0
valid_lft forever preferred_lft forever
inet6 fe80::6832:bcff:feeb:f23/64 scope link
valid_lft forever preferred_lft forever
- 創(chuàng)建容器
$ ctr images pull docker.io/library/busybox:latest
$ ctr run -d docker.io/library/busybox:latest busybox
$ ctr tasks exec --exec-id $RANDOM -t busybox sh
/ # ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
#獲取容器進(jìn)程ID及其網(wǎng)絡(luò)命名空間
$ pid=$(ctr tasks ls | grep busybox | awk '{print $2}') && echo $pid
39287
$ netnspath=/proc/$pid/ns/net && echo $netnspath
/proc/39287/ns/net
# 進(jìn)入目錄為指定容器添加網(wǎng)絡(luò)配置
$ cd /home/cni-tools/cni-1.1.2/scripts/
$ CNI_PATH=/home/cni-plugins ./exec-plugins.sh add $pid $netnspath
- 驗(yàn)證容器網(wǎng)絡(luò)與宿主機(jī)網(wǎng)絡(luò)的互訪功能
# 驗(yàn)證
# 進(jìn)入容器確認(rèn)是否添加網(wǎng)卡信息
$ ctr tasks exec --exec-id $RANDOM -t busybox sh
/ # ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 5a:36:90:c1:33:b1 brd ff:ff:ff:ff:ff:ff
inet 10.66.0.4/16 brd 10.66.255.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::5836:90ff:fec1:33b1/64 scope link
valid_lft forever preferred_lft forever
# 在容器中ping容器宿主機(jī)IP地址
/ # ping -c 2 192.168.168.201
PING 192.168.168.201 (192.168.168.201): 56 data bytes
64 bytes from 192.168.168.201: seq=0 ttl=64 time=0.067 ms
64 bytes from 192.168.168.201: seq=1 ttl=64 time=0.070 ms
--- 192.168.168.201 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.067/0.068/0.070 ms
# 在容器中ping容器宿主機(jī)IP地址
/ # ping -c 2 192.168.168.2
PING 192.168.168.2 (192.168.168.2): 56 data bytes
64 bytes from 192.168.168.2: seq=0 ttl=127 time=0.141 ms
64 bytes from 192.168.168.2: seq=1 ttl=127 time=0.324 ms
--- 192.168.168.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.141/0.232/0.324 ms
# 在容器中ping容器宿主機(jī)IP地址
/ # ping -c 2 192.168.168.202
PING 192.168.168.201 (192.168.168.202): 56 data bytes
64 bytes from 192.168.168.202: seq=0 ttl=64 time=0.067 ms
64 bytes from 192.168.168.202: seq=1 ttl=64 time=0.070 ms
--- 192.168.168.202 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.067/0.068/0.070 ms
# 在容器中開(kāi)啟httpd服務(wù)
/ # echo "containerd net web test" > /tmp/index.html
/ # httpd -h /tmp
/ # wget -O - -q 127.0.0.1
containerd net web test
/ # exit
#在宿主機(jī)訪問(wèn)容器提供的httpd服務(wù)
$ curl 10.66.0.4
containerd net web test
5、Containerd數(shù)據(jù)持久化
實(shí)現(xiàn)把宿主機(jī)目錄掛載至Containerd容器中,實(shí)現(xiàn)容器數(shù)據(jù)持久化存儲(chǔ)。
# 創(chuàng)建一個(gè)靜態(tài)容器,實(shí)現(xiàn)宿主機(jī)目錄與容器掛載,src=/tmp 為宿主機(jī)目錄 dst=/hostdir 為容器中目錄
$ ctr container create docker.io/library/busybox:latest busybox3 --mount type=bind,src=/tmp,dst=/hostdir,options=rbind:rw
# 運(yùn)行用戶(hù)進(jìn)程
$ ctr tasks start -d busybox3 bash
$ ctr t ls
TASK PID STATUS
busybox 39287 RUNNING
busybox3 105003 RUNNING
# 進(jìn)入容器,查看是否掛載成功
$ ctr tasks exec --exec-id $RANDOM -t busybox3 sh
/ # ls /hostdir/
ks-script-GSRMK8 vmware-root_6389-1958486695
systemd-private-cc74dd8802f0428490924757e690c750-chronyd.service-9TDICh vmware-root_6511-1689719547
vmware-root_6359-1949639453 yum.log
# 向容器中掛載目錄中添加文件
/ # echo "hello world" > /hostdir/test.txt
/ # ls /hostdir/
ks-script-GSRMK8 vmware-root_6389-1958486695
systemd-private-cc74dd8802f0428490924757e690c750-chronyd.service-9TDICh vmware-root_6511-1689719547
test.txt yum.log
vmware-root_6359-1949639453
# 在宿主機(jī)上查看被容器掛載的目錄中是否添加了新的文件,已添加表明被容器掛載成功,并可以讀寫(xiě)此目錄中內(nèi)容。
$ cat /tmp/test.txt
hello world
6、Docker集成Container容器管理
目前Containerd主要任務(wù)還在于解決容器運(yùn)行時(shí)的問(wèn)題,對(duì)于其周邊生態(tài)還不完善,所以有時(shí)需要借助:Docker結(jié)合Containerd來(lái)實(shí)現(xiàn)Docker完整的功能應(yīng)用。
Docker安裝與使用教程:https://blog.csdn.net/qq_41822345/article/details/107123094文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-548871.html
docker運(yùn)行的容器默認(rèn)在moby命名空間下。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-548871.html
# 安裝docker
$ yum install docker
# 啟動(dòng)docker
$ systemctl start docker
# 運(yùn)行一個(gè)容器
$ docker run -d nginx:latest
$ docker ps
# 查看ctr命令下的namespace,發(fā)現(xiàn)多了一個(gè)moby命名空間。moby即為docker使用的命名空間。
$ ctr namespace ls
NAME LABELS
default
k8s.io
moby
# 查看moby命名空間下的容器和任務(wù)
$ ctr -n moby container ls
$ ctr -n moby tasks ls
到了這里,關(guān)于Containerd【輕量級(jí)容器管理工具】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!