如何在命令行下優(yōu)雅地管理 Containerd
Containerd被設(shè)計(jì)成可以很容易地嵌入到更大的系統(tǒng)中。Docker使用containerd來運(yùn)行容器。Kubernetes可以通過CRI使用containerd來管理單個節(jié)點(diǎn)上的容器。但是較小的項(xiàng)目也可以從與containerd集成的便利中獲益——例如,faasd使用containerd在獨(dú)立的服務(wù)器上運(yùn)行一個成熟的功能即服務(wù)解決方案。
但是,以編程方式使用 containerd 并不是唯一的選擇。它還可以通過可用客戶端之一從命令行使用。由此產(chǎn)生的容器 UX 可能不像docker客戶端提供的那樣全面和用戶友好,但它仍然是有用的,例如,用于調(diào)試或?qū)W習(xí)目的。
如何在 ctr 中使用 containerd
ctr是作為 containerd 項(xiàng)目的一部分提供的命令行客戶端。如果您在一臺機(jī)器上運(yùn)行了 containerd,那么ctr二進(jìn)制文件很可能也在那里。
該ctr界面 [顯然] 與 Docker CLI不兼容,乍一看,可能看起來不太用戶友好。顯然,它的主要受眾是測試守護(hù)進(jìn)程的容器開發(fā)人員。但是,由于它最接近實(shí)際的 containerd API,因此它可以作為一種很好的探索手段——通過檢查可用命令,您可以大致了解 containerd可以做什么和不可以做什么。
ctr也非常適合學(xué)習(xí)的能力低級別[OCI]容器的運(yùn)行時間,因?yàn)閏tr + containerd是更接近實(shí)際的容器比docker + dockerd。
使用 ctr 處理容器鏡像
當(dāng)拉取鏡像,完全合格的參考似乎是必需的,所以你不能忽略鏡像倉庫或標(biāo)簽部分:
$ ctr images pull docker.io/library/nginx:1.21
$ ctr images pull docker.io/kennethreitz/httpbin:latest
$ ctr images pull docker.io/kennethreitz/httpbin:latest
$ ctr images pull quay.io/quay/redis:latest
要列出本地鏡像,可以使用:
$ ctr images ls
令人驚訝的是,containerd不提供開箱即用的鏡像構(gòu)建支持。然而,containerd 本身經(jīng)常被更高級別的工具用來構(gòu)建鏡像。
不使用ctr構(gòu)建鏡像,您可以導(dǎo)入用docker build或其他oci兼容軟件構(gòu)建的現(xiàn)有鏡像:
$ docker build -t my-app .
$ docker save -o my-app.tar my-app
$ ctr images import my-app.tar
有了ctr,你也可以掛載鏡像
$ mkdir /tmp/httpbin
$ ctr images mount docker.io/kennethreitz/httpbin:latest /tmp/httpbin
$ ls -l /tmp/httpbin/
total 80
drwxr-xr-x 2 root root 4096 Oct 18 2018 bin
drwxr-xr-x 2 root root 4096 Apr 24 2018 boot
drwxr-xr-x 4 root root 4096 Oct 18 2018 dev
drwxr-xr-x 1 root root 4096 Oct 24 2018 etc
drwxr-xr-x 2 root root 4096 Apr 24 2018 home
drwxr-xr-x 3 root root 4096 Oct 24 2018 httpbin
...
$ ctr images unmount /tmp/httpbin
要使用ctrl刪除圖像,請運(yùn)行:
$ ctr images remove docker.io/library/nginx:1.21
使用ctr處理容器
有了一個本地鏡像,你可以通過ctr運(yùn)行<image-ref> <container-id>來運(yùn)行一個容器。例如:
$ ctr run --rm -t docker.io/library/debian:latest cont1
注意,與友好的docker運(yùn)行生成唯一的容器ID不同,使用ctr,你必須自己提供唯一的容器ID。ctr運(yùn)行命令也只支持一些常見的docker運(yùn)行標(biāo)志:——env, -t,——tty, -d,——detach,——rm,等等。但是沒有端口發(fā)布或使用——restart=總是開箱即用的自動容器重新啟動。
與鏡像類似,你可以列出現(xiàn)有的容器:
$ ctr containers ls
有趣的是,ctrl運(yùn)行命令實(shí)際上是ctrl容器創(chuàng)建+ ctrl任務(wù)啟動的快捷方式:
$ ctr container create -t docker.io/library/nginx:latest nginx_1
$ ctr container ls
CONTAINER IMAGE RUNTIME
nginx_1 docker.io/library/nginx:latest io.containerd.runc.v2
$ ctr task ls
TASK PID STATUS # Empty!
$ ctr task start -d nginx_1 # -d for --detach
$ ctr task list
TASK PID STATUS
nginx_1 10074 RUNNING
我喜歡這種容器和任務(wù)子命令的分離,因?yàn)樗从沉私?jīng)常被遺忘的OCI容器的本質(zhì)。盡管人們普遍認(rèn)為容器不是進(jìn)程——對于進(jìn)程來說,容器是隔離的和受限制的執(zhí)行環(huán)境。
使用ctr任務(wù)連接,你可以重新連接到一個在容器中運(yùn)行的現(xiàn)有任務(wù)的stdio流:
$ ctr task attach nginx_1
2021/09/12 15:42:20 [notice] 1#1: using the "epoll" event method
2021/09/12 15:42:20 [notice] 1#1: nginx/1.21.3
2021/09/12 15:42:20 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/09/12 15:42:20 [notice] 1#1: OS: Linux 4.19.0-17-amd64
2021/09/12 15:42:20 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:1024
2021/09/12 15:42:20 [notice] 1#1: start worker processes
2021/09/12 15:42:20 [notice] 1#1: start worker process 31
...
很像docker,你可以在一個已有的容器中執(zhí)行一個任務(wù):
$ ctr task exec -t --exec-id bash_1 nginx_1 bash
# From inside the container:
$ root@host:/# curl 127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
...
在移除一個容器之前,它的所有任務(wù)必須停止:
$ ctr task kill -9 nginx_1
或者,你可以使用——force標(biāo)志刪除正在運(yùn)行的任務(wù):
$ ctr task rm -f nginx_1
最后,要刪除容器,運(yùn)行:
$ ctr container rm nginx_1
如何使用容器與nerdctl
Nerdctl是一個相對較新的containerd命令行客戶端。與ctr不同,nerdctl的目標(biāo)是用戶友好和docker兼容。在某種程度上,nerdctl + containerd可以無縫地替代docker + dockerd。然而,這似乎不是項(xiàng)目的目標(biāo):
nerdctl的目標(biāo)是促進(jìn)試驗(yàn)Docker中沒有的最前沿的容器特性。這些特性包括但不限于lazy-pulling(stargz)和鏡像加密(ocicrypt)。這些功能預(yù)計(jì)最終也會在Docker中實(shí)現(xiàn),然而,這可能需要幾個月,甚至幾年的時間,因?yàn)镈ocker目前只設(shè)計(jì)使用了容器子系統(tǒng)的一小部分。重構(gòu)Docker以使用整個容器是可能的,但并不簡單。所以我們[NTT]決定創(chuàng)建一個完全使用container的CLI,但我們不打算用Docker來完成。我們一直在為Docker/Moby以及容器做出貢獻(xiàn),并將繼續(xù)這樣做。
從基本用法的角度來看,與ctr相比,nerdctl支持:
- 使用nerdctl構(gòu)建鏡像
- 容器網(wǎng)絡(luò)管理
- Docker Compose與nerdctl Compose up
- 最酷的部分是nerdctl試圖提供docker(和podman)命令行UX相同的功能。所以,如果你熟悉docker(或podman) CLI,你就已經(jīng)熟悉nerdctl了。
如何使用容器與crictl
crictl是一個命令行客戶端,用于[Kubernetes] cri兼容的容器運(yùn)行時。
引入 Kubernetes 容器運(yùn)行時接口 (CRI)以使 Kubernetes 容器運(yùn)行時不可知。Kubernetes節(jié)點(diǎn)代理kubelet實(shí)現(xiàn)了 CRI客戶端 API,可以使用任何實(shí)現(xiàn) CRI 服務(wù)器 API的容器運(yùn)行時來管理其節(jié)點(diǎn)上的容器和 Pod。
Kubernetes CRI
從1.1版開始,containerd就自帶了一個內(nèi)置的CRI插件。因此,containerd是一個與cri兼容的容器運(yùn)行時。因此,它可以與critl一起使用。
創(chuàng)建crictl是為了檢查和調(diào)試Kubernetes節(jié)點(diǎn)上的容器運(yùn)行時和應(yīng)用程序。支持以下操作:
attach: Attach to a running container
create: Create a new container
exec: Run a command in a running container
version: Display runtime version information
images, image, img: List images
inspect: Display the status of one or more containers
inspecti: Return the status of one or more images
imagefsinfo: Return image filesystem info
inspectp: Display the status of one or more pods
logs: Fetch the logs of a container
port-forward: Forward local port to a pod
ps: List containers
pull: Pull an image from a registry
run: Run a new container inside a sandbox
runp: Run a new pod
rm: Remove one or more containers
rmi: Remove one or more images
rmp: Remove one or more pods
pods: List pods
start: Start one or more created containers
info: Display information of the container runtime
stop: Stop one or more running containers
stopp: Stop one or more running pods
update: Update one or more running containers
config: Get and set crictl client configuration options
stats: List container(s) resource usage statistics
這里有趣的部分是,通過crictl + containerdbundle,人們可以了解pod是如何實(shí)際實(shí)現(xiàn)的。
有關(guān)如何crictl與 containerd一起使用的更多信息,請查看此文檔(containerd 項(xiàng)目的一部分)。