k3s容器中使用docker、ctr、crictl命令的方法
概述
在使用k3s過程中,經(jīng)常需要使用ctr
和crictl
兩個命令,本文記錄一下。
ctr
類似docker
命令是docker-shim容器運行時的客戶端工具,ctr
是Containerd的客戶端工具。一個簡單的CLI接口,用作Containerd本身的一些調(diào)試用途,投入生產(chǎn)使用時還是應(yīng)該配合docker或cri-containerd。
安裝Containerd作為容器運行時后,會自動安裝ctr
;安裝k3s后,也會自動安裝ctr
。
命名空間
ctr
與docker
命令一個較大的區(qū)別,ctr有命名空間的概念,自帶一個moby
命名空間:
如果執(zhí)行過ctr i pull
、ctr i import
、ctr i export
等命令,又沒有通過-n
參數(shù)指定命名空間,則會自動創(chuàng)建default
命名空間:
當Containerd結(jié)合k8s(或k3s)使用時,相關(guān)鏡像一般存放到k8s.io
,相關(guān)操作需要加-n k8s.io
,避免與默認空間混淆,且將參數(shù)放到最前面,即:ctr -n=k8s.io i pull
。
ctr
命令輸出:
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 sandboxes, sandbox, sb, s Manage sandboxes info Print the server info deprecations 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
crictl
crictl,GitHub,一個兼容CRI的命令行接口,可用于檢查和調(diào)試k8s/kubelet節(jié)點上的容器運行時和應(yīng)用程序。只有一個k8s.io
命名空間,無需-n
參數(shù)。
安裝
VERSION="v1.33.0" wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz sudo tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin rm -f crictl-$VERSION-linux-amd64.tar.gz
crictl默認連接到unix:///var/run/dockershim.sock
。對于其它運行時,可通過多種方式設(shè)置端點:
- 設(shè)置
--runtime-endpoint
和--image-endpoint
選項; - 設(shè)置
CONTAINER_RUNTIME_ENDPOINT
和IMAGE_SERVICE_ENDPOINT
環(huán)境變量; - 在配置文件
--config=/etc/crictl.yaml
設(shè)置端點。
在連接到服務(wù)器時,配置文件可指定超時值(timeout
或–timeout
),啟用或禁用調(diào)試(debug
或–debug
)。
比如在k3s環(huán)境下,執(zhí)行cat /var/lib/rancher/k3s/agent/etc/crictl.yaml
輸出:
runtime-endpoint: unix:///run/k3s/containerd/containerd.sock
crictl
命令輸出:
VERSION: v1.31.0-k3s2 COMMANDS: attach Attach to a running container checkpoint Checkpoint one or more running containers completion Output shell completion code config Get and set crictl client configuration options create Create a new container events, event Stream the events of containers exec Run a command in a running container imagefsinfo Return image filesystem info images, image, img List images info Display information of the container runtime inspect Display the status of one or more containers inspecti Return the status of one or more images inspectp Display the status of one or more pods logs Fetch the logs of a container metricsp List pod metrics. Metrics are unstructured key/value pairs gathered by CRI meant to replace cAdvisor's /metrics/cadvisor endpoint. pods List pods port-forward Forward local port to a pod ps List containers pull Pull an image from a registry rm Remove one or more containers rmi Remove one or more images rmp Remove one or more pods run Run a new container inside a sandbox runp Run a new pod runtime-config Retrieve the container runtime configuration start Start one or more created containers stats List container(s) resource usage statistics statsp List pod statistics. Stats represent a structured API that will fulfill the Kubelet's /stats/summary endpoint. stop Stop one or more running containers stopp Stop one or more running pods update Update one or more running containers update-runtime-config Update the runtime configuration version Display runtime version information help, h Shows a list of commands or help for one command GLOBAL OPTIONS: --config value, -c value Location of the client config file. If not specified and the default does not exist, the program's directory is searched as well (default: "/etc/crictl.yaml") [$CRI_CONFIG_FILE] --debug, -D Enable debug mode (default: false) --enable-tracing Enable OpenTelemetry tracing. (default: false) --image-endpoint value, -i value Endpoint of CRI image manager service (default: uses 'runtime-endpoint' setting) [$IMAGE_SERVICE_ENDPOINT] --runtime-endpoint value, -r value Endpoint of CRI container runtime service (default: uses in order the first successful one of [unix:///run/k3s/containerd/containerd.sock unix:///var/run/dockershim.sock unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]). Default is now deprecated and the endpoint should be set instead. [$CONTAINER_RUNTIME_ENDPOINT] --timeout value, -t value Timeout of connecting to the server in seconds (e.g. 2s, 20s.). 0 or less is set to default (default: 2s) --tracing-endpoint value Address to which the gRPC tracing collector will send spans to. (default: "127.0.0.1:4317") --tracing-sampling-rate-per-million value Number of samples to collect per million OpenTelemetry spans. Set to 1000000 or -1 to always sample. (default: -1) --help, -h Show help (default: false) --version, -v Print the version (default: false)
crictl pull
可以明確的是:
- 哪怕當前環(huán)境已經(jīng)下載過docker鏡像,使用yaml文件通過vLLM啟動大模型時,還是會去下載crictl鏡像;
- 通過crictl下載鏡像,和docker下載的鏡像,其名字一致;
- yaml文件里使用
dustynv/vllm:0.7.4-r36.4.0-cu128-24.04
鏡像,等價于docker.io/dustynv/vllm:0.7.4-r36.4.0-cu128-24.04
; - 上面的
docker.io
就是部署在國外的Docker Hub官方鏡像源,國內(nèi)下載速度非常慢; - 可考慮使用國內(nèi)鏡像源,如
docker.m.daocloud.io/dustynv/vllm
、docker.1ms.run/dustynv/vllm
; - 通過
crictl pull
下載鏡像并沒有進度條提示,狠是惡心,只能干等,有時候等了10分鐘給你來個提示,只能重試。
如上圖所示,沒有任何進度提示。
下載國外鏡像,會遇到各種奇奇怪怪的報錯:
使用國內(nèi)的鏡像源地址:
反復(fù)重試,還是上面的報錯提示。
Qwen等ChatGPT給出的解決方法是先登錄。
好,遇到問題解決問題;打開網(wǎng)站毫秒鏡像,使用個人手機號注冊。按照Qwen給出的幻覺答復(fù),crictl login
:
crictl pull
鏡像可以執(zhí)行docker login
,可見crictl底層使用的還是docker鏡像那一套機制:
不過又遇到另一個超時報錯,那就再換一個國內(nèi)鏡像源吧。
實踐下來,通過docker.m.daocloud.io
鏡像源下載crictl鏡像速度還能接受。
crictl鏡像和ctr鏡像
部分網(wǎng)絡(luò)資料說crictl image list
等效于ctr -n=k8s.io image list
。
經(jīng)過各種ChatGPT以及命令嘗試:
crictl image
等效于crictl images
,等效于crictl image list
和crictl images list
。
在k3s環(huán)境下,crictl image
等效于k3s ctr -n=k8s.io image list
。
k3s
命令前綴不能省去。k3s默認使用-n=k8s.io
命名空間,因此crictl image
等效于k3s ctr images list
。
個數(shù)
crictl images | grep vllm
輸出2個:
而k3s ctr images list | grep vllm
輸出4個:
分析:
- crictl是CRI客戶端工具,crictl輸出是CRI提供給kubelet的簡化視圖,只顯示標簽信息,不展示digest表示;
k3s ctr image
輸出,表明containerd實際存儲四個鏡像引用;分別是tag和digest;
crictl鏡像導(dǎo)出
輸入命令crictl images
和k3s crictl images
的截圖如下:
可見,在k3s環(huán)境下,crictl
命令等價于k3s crictl
,也就是說,k3s
命令前綴可以省去。
問題在于,一個vLLM鏡像動輒5~6個G。
vLLM這篇博客也提到過,部署大模型時,通過k8s調(diào)度到從節(jié)點,則從節(jié)點也需要對應(yīng)的模型文件和crictl鏡像。
那crictl鏡像能不能導(dǎo)出為tar包,通過scp命令傳輸?shù)綇墓?jié)點,然后在從節(jié)點導(dǎo)入tar包為crictl鏡像呢?
答案當然是可以,docker鏡像就完全可以這么玩,crictl和docker存在很多相似之處。
但crictl并沒有提供save命令:
需要通過ctr export
命令執(zhí)行導(dǎo)出操作。
導(dǎo)出鏡像的前提是找到鏡像,要不然就會出現(xiàn)如下not found報錯:
k3s ctr image | grep vllm:0.8.6
輸出2行,下面這一行非常非常長,:
docker.io/dustynv/vllm:0.8.6-r36.4-cu128-24.04 application/vnd.docker.distribution.manifest.v2+json sha256:b2210294b661acc6e94bc302ce5111d6d0cb01a3815ada0132c105f540fb7a63 5.7 GiB linux/arm64 io.cri-containerd.image=managed docker.io/dustynv/vllm@sha256:b2210294b661acc6e94bc302ce5111d6d0cb01a3815ada0132c105f540fb7a63 application/vnd.docker.distribution.manifest.v2+json sha256:b2210294b661acc6e94bc302ce5111d6d0cb01a3815ada0132c105f540fb7a63 5.7 GiB linux/arm64 io.cri-containerd.image=managed
導(dǎo)出命令:k3s ctr -n k8s.io image export vllm-0.8.6.tar docker.io/dustynv/vllm:0.8.6-r36.4-cu128-24.04
。如果
檢查:
docker鏡像轉(zhuǎn)換成crictl鏡像
TODO
docker、crictl及ctr
三個命令行的對比
命令 | docker | crictl(推薦) | ctr |
---|---|---|---|
查看容器列表 | docker ps | crictl ps | ctr -n k8s.io c ls |
查看容器詳情 | docker inspect | crictl inspect | ctr -n k8s.io c info |
查看容器日志 | docker logs | crictl logs | 無 |
容器內(nèi)執(zhí)行命令 | docker exec | crictl exec | 無 |
掛載容器 | docker attach | crictl attach | 無 |
容器資源使用 | docker stats | crictl stats | 無 |
創(chuàng)建容器 | docker create | crictl create | ctr -n k8s.io c create |
啟動容器 | docker start | crictl start | ctr -n k8s.io run |
停止容器 | docker stop | crictl stop | 無 |
刪除容器 | docker rm | crictl rm | ctr -n k8s.io c del |
查看鏡像列表 | docker images | crictl images | ctr -n k8s.io i ls |
查看鏡像詳情 | docker inspect | crictl inspect | 無 |
拉取鏡像 | docker pull | crictl pull | ctr -n k8s.io i pull |
推送鏡像 | docker push | 無 | ctr -n k8s.io i push |
刪除鏡像 | docker rmi | crictl rmi | ctr -n k8s.io i rm |
查看Pod列表 | 無 | crictl pods | 無 |
查看Pod詳情 | 無 | crictl inspectp | 無 |
啟動Pod | 無 | crictl runp | 無 |
停止Pod | 無 | crictl stopp | 無 |
導(dǎo)入鏡像 | docker load | 無 | ctr -n k8s.io i import |
導(dǎo)出鏡像 | docker save | 無 | ctr -n k8s.io i export |
到此這篇關(guān)于docker、ctr、crictl命令簡介與使用的文章就介紹到這了,更多相關(guān)docker ctr crictl命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
多云環(huán)境下的Docker部署策略實現(xiàn)
在多云環(huán)境下,Docker容器技術(shù)為應(yīng)用程序的部署提供了高度的靈活性和可擴展性,本文就來介紹一下多云環(huán)境下的Docker部署策略實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01Docker快速部署MinIO對象存儲服務(wù)的最新實戰(zhàn)指南
MinIO作為高性能的云原生對象存儲服務(wù),結(jié)合Docker容器技術(shù)可以快速搭建企業(yè)級存儲系統(tǒng),本文介紹了從零開始的Docker部署全流程,有需要的可以了解下2025-05-05Docker NFS volume創(chuàng)建與使用方式
這篇文章主要介紹了Docker NFS volume創(chuàng)建與使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Docker中conda環(huán)境的導(dǎo)出和導(dǎo)入
現(xiàn)在很多的應(yīng)用程序系統(tǒng)都會選擇使用docker容器進行部署,本文主要介紹了Docker中conda環(huán)境的導(dǎo)出和導(dǎo)入,具有一定的參考價值,感興趣的可以了解一下2024-02-02Docker部署RabbitMQ的實現(xiàn)方法(圖文并茂)
本文詳細介紹了如何使用Docker部署RabbitMQ的步驟,包括拉取RabbitMQ鏡像,創(chuàng)建與授權(quán)文件夾,創(chuàng)建RabbitMQ容器,查看啟動日志,訪問控制臺和登錄等,全程詳細的參數(shù)解釋和操作步驟,讓Docker部署RabbitMQ變得簡單易行2024-10-10docker配置skywalking 監(jiān)控springcloud應(yīng)用的詳細步驟
本文分步驟給大家講解docker配置skywalking 監(jiān)控springcloud應(yīng)用的方法,感興趣的朋友一起看看吧2025-04-04Docker跨主機網(wǎng)絡(luò)(overlay)的實現(xiàn)
這篇文章主要介紹了Docker跨主機網(wǎng)絡(luò)(overlay)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12docker鏡像的拉取登陸上傳及保存等相關(guān)使用命令
這篇文章主要為大家介紹了docker鏡像的拉取登陸上傳及保存等相關(guān)使用命令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04Docker安裝RabbitMQ AMQP協(xié)議及重要角色
這篇文章主要為大家介紹了Docker安裝RabbitMQ AMQP協(xié)議和主要角色詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05