如何在命令行下優雅的管理 Container

containerd是一個高級容器運行時,又名容器管理器。簡單來說,它是一個守護進程,在單個主機上管理完整的容器生命週期:創建、啓動、停止容器、拉取和存儲鏡像、配置掛載、網絡等。

Containerd被設計成可以很容易地嵌入到更大的系統中。Docker使用containerd來運行容器。Kubernetes可以通過CRI使用containerd來管理單個節點上的容器。但是較小的項目也可以從與containerd集成的便利中獲益——例如,faasd使用 containerd在獨立的服務器上運行一個成熟的功能即服務解決方案。

但是,以編程方式使用 containerd 並不是唯一的選擇。它還可以通過可用客戶端之一從命令行使用。由此產生的容器 UX 可能不像docker客戶端提供的那樣全面和用戶友好,但它仍然是有用的,例如,用於調試或學習目的。

如何在 ctr 中使用 containerd

ctr是作爲 containerd 項目的一部分提供的命令行客戶端。如果您在一臺機器上運行了 containerd,那麼ctr二進制文件很可能也在那裏。

ctr界面 [顯然] 與 Docker CLI不兼容,乍一看,可能看起來不太用戶友好。顯然,它的主要受衆是測試守護進程的容器開發人員。但是,由於它最接近實際的 containerd API,因此它可以作爲一種很好的探索手段——通過檢查可用命令,您可以大致瞭解 containerd可以做什麼和不可以做什麼。

ctr也非常適合學習的能力低級別[OCI]容器的運行時間,因爲ctr + containerd是更接近實際的容器比docker + dockerd

使用 ctr 處理容器鏡像

當拉取鏡像,完全合格的參考似乎是必需的,所以你不能忽略鏡像倉庫或標籤部分:

$ 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不提供開箱即用的鏡像構建支持。然而,containerd 本身經常被更高級別的工具用來構建鏡像。

不使用ctr構建鏡像,您可以導入用docker build或其他oci兼容軟件構建的現有鏡像:

$ 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刪除圖像,請運行:

$ ctr images remove docker.io/library/nginx:1.21

使用 ctr 處理容器

有了一個本地鏡像,你可以通過ctr運行<image-ref> <container-id>來運行一個容器。例如:

$ ctr run --rm -t docker.io/library/debian:latest cont1

注意,與友好的docker運行生成唯一的容器ID不同,使用ctr,你必須自己提供唯一的容器IDctr運行命令也只支持一些常見的docker運行標誌:——env, -t,——tty, -d,——detach,——rm,等等。但是沒有端口發佈或使用——restart=總是開箱即用的自動容器重新啓動。

與鏡像類似,你可以列出現有的容器:

$ ctr containers ls

有趣的是,ctrl運行命令實際上是ctrl容器創建+ ctrl任務啓動的快捷方式:

$ 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

我喜歡這種容器和任務子命令的分離,因爲它反映了經常被遺忘的OCI容器的本質。儘管人們普遍認爲容器不是進程——對於進程來說,容器是隔離的和受限制的執行環境。

使用ctr任務連接,你可以重新連接到一個在容器中運行的現有任務的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,你可以在一個已有的容器中執行一個任務:

$ 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>
...

在移除一個容器之前,它的所有任務必須停止:

$ ctr task kill -9 nginx_1

或者,你可以使用——force標誌刪除正在運行的任務:

$ ctr task rm -f nginx_1

最後,要刪除容器,運行:

$ ctr container rm nginx_1

如何使用容器與 nerdctl

Nerdctl是一個相對較新的containerd命令行客戶端。與ctr不同,nerdctl的目標是用戶友好和docker兼容。在某種程度上,nerdctl + containerd可以無縫地替代docker + dockerd。然而,這似乎不是項目的目標:

nerdctl的目標是促進試驗Docker中沒有的最前沿的容器特性。這些特性包括但不限於lazy-pulling(stargz)和鏡像加密 (ocicrypt)。這些功能預計最終也會在Docker中實現,然而,這可能需要幾個月,甚至幾年的時間,因爲Docker目前只設計使用了容器子系統的一小部分。重構Docker以使用整個容器是可能的,但並不簡單。所以我們[NTT]決定創建一個完全使用containerCLI,但我們不打算用Docker來完成。我們一直在爲Docker/Moby以及容器做出貢獻,並將繼續這樣做。

從基本用法的角度來看,與ctr相比,nerdctl支持:

如何使用容器與 crictl

crictl是一個命令行客戶端,用於[Kubernetes] cri兼容的容器運行時。

引入 Kubernetes 容器運行時接口 (CRI) 以使 Kubernetes 容器運行時不可知。Kubernetes節點代理kubelet實現了 CRI客戶端 API,可以使用任何實現 CRI 服務器 API的容器運行時來管理其節點上的容器和 Pod

             Kubernetes CRI

1.1版開始,containerd就自帶了一個內置的CRI插件。因此,containerd是一個與cri兼容的容器運行時。因此,它可以與critl一起使用。

創建crictl是爲了檢查和調試Kubernetes節點上的容器運行時和應用程序。支持以下操作:

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是如何實際實現的。

有關如何crictl與 containerd一起使用的更多信息,請查看此文檔(containerd 項目的一部分)。

https://github.com/containerd/cri/blob/68b61297b59e38c1088db10fbd19807a4ffbad87/docs/crictl.md

參考資料

_
https://iximiuz.com/en/posts/containerd-command-line-clients/_

本文轉載自:「雲原生 CTO」,原文:https://tinyurl.com/sm5x829d,版權歸原作者所有。歡迎投稿,投稿郵箱: editor@hi-linux.com。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/38fBPfcItR0T77GTdd-46Q