由於 crictl 的說明文件過於簡單,一邊查詢一邊輸入指令實在沒有效率,因此整理此crictl CLI說明文件並提供一些範例指令碼,以方便查詢與使用。
crictl.exe [global options] command [command options] [arguments...]
COMMANDS:
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
statsp List pod resource usage statistics
completion Output shell completion code
help, h Shows a list of commands or help for one command
Attach to a running container
crictl.exe attach [command options] CONTAINER-ID
Create a new container
crictl.exe create [command options] POD container-config.[json|yaml] pod-config.[json|yaml]
CLI範例
crictl create $POD_ID container-config.json pod-config.json
Run a command in a running container
crictl.exe exec [command options] CONTAINER-ID COMMAND [ARG...]
CLI範例
crictl exec $CONTAINER_ID ipconfig
crictl exec -it $CONTAINER_ID cmd
Display runtime version information
crictl.exe version [command options] [arguments...]
CLI範例
PS C:\> crictl.exe version
Version: 0.1.0
RuntimeName: containerd
RuntimeVersion: v1.6.6
RuntimeApiVersion: v1
List images
crictl.exe images [command options] [REPOSITORY[:TAG]]
CLI範例
crictl.exe ima
crictl.exe image
crictl.exe images
前二個是縮寫。
Display the status of one or more containers
crictl.exe inspect [command options] CONTAINER-ID [CONTAINER-ID...]
CLI範例
crictl.exe inspect $CONTAINER_ID
Return the status of one or more images
crictl.exe inspecti [command options] IMAGE-ID [IMAGE-ID...]
CLI範例
crictl.exe img
crictl.exe inspecti 2a67292b6e8ba e3acc2fa6fe12
Return image filesystem info
crictl.exe imagefsinfo [command options] [arguments...]
CLI範例
crictl.exe imagefsinfo e3acc2fa6fe12
inspecti
顯示的資訊與imagefsinfo
不同哦。
Display the status of one or more pods
crictl.exe inspectp [command options] POD-ID [POD-ID...]
CLI範例
crictl.exe inspectp $POD_ID
Fetch the logs of a container
crictl.exe logs [command options] CONTAINER-ID
CLI範例
crictl.exe logs $CONTAINER_ID
Forward local port to a pod
crictl.exe port-forward [command options] POD-ID [LOCAL_PORT:]REMOTE_PORT
CLI範例
crictl.exe port-forward $POD_ID 8080:80
List containers
crictl.exe ps [command options] [arguments...]
CLI範例
crictl.exe ps
crictl.exe ps -a
還有許多過濾用的參數。當容器多的時候,再查詢使用即可。
Pull an image from a registry
crictl.exe pull [command options] NAME[:TAG|@DIGEST]
OPTIONS:
--auth AUTH_STRING Use AUTH_STRING for accessing the registry. AUTH_STRING is a base64 encoded 'USERNAME[:PASSWORD]' [%CRICTL_AUTH%]
--creds USERNAME[:PASSWORD] Use USERNAME[:PASSWORD] for accessing the registry [%CRICTL_CREDS%]
--pod-config pod-config.[json|yaml] Use pod-config.[json|yaml] to override the the pull context
CLI範例
crictl.exe pull k8s.gcr.io/pause:3.7
如果像存取Azure ACR需要憑證,就要帶認證參數。
crictl.exe pull --creds USERNAME[:PASSWORD] youregistry.azurecr.io/blabla:latest
Note:
在網路獨立環境,有時需要透過ctr.exe
來匯出與匯入.tar
,在匯入時需要加上-n="k8s.io"
命名空間,這樣 crictl.exe
才看得到匯入的映象檔。
ctr image pull k8s.gcr.io/pause:3.7
ctr images export pause37.tar k8s.gcr.io/pause:3.7
ctr -n="k8s.io" images import .\pause37.tar
crictl images
Run a new container inside a sandbox
crictl.exe run [command options] container-config.[json|yaml] pod-config.[json|yaml]
CLI範例
crictl.exe run .\container.json .\pod.json
crictl.exe pods
crictl.exe ps
run
執行之後顯示的是 CONTAINER ID。run
等於把runp
、create
、start
整合在一起。
Run a new pod
crictl.exe runp [command options] pod-config.[json|yaml]
CLI範例
$POD_ID=(crictl runp .\pod-config.json)
$CONTAINER_ID=(crictl create $POD_ID .\container-config.json .\pod-config.json)
crictl start $CONTAINER_ID
一般會分開執行
runp
、create
、start
是為了取得各自的ID,方便後續 CLI 操作執行。
Remove one or more containers
crictl.exe rm [command options] CONTAINER-ID [CONTAINER-ID...]
OPTIONS:
--all, -a Remove all containers (default: false)
--force, -f Force removal of the container, disregarding if running (default: false)
CLI範例
crictl.exe img
crictl.exe stop 61837f3d17b3b
crictl.exe rm 61837f3d17b3b
Remove one or more images
crictl.exe rmi [command options] IMAGE-ID [IMAGE-ID...]
OPTIONS:
--all, -a Remove all images (default: false)
--prune, -q Remove all unused images (default: false)
CLI範例
crictl.exe rmi c764b8ebd8645
crictl.exe rmi --prune
如果系統目前沒在執行中的容器,那麼
--prune
等於清除所有映像檔。
Remove one or more pods
crictl.exe rmp [command options] POD-ID [POD-ID...]
OPTIONS:
--all, -a Remove all pods (default: false)
--force, -f Force removal of the pod sandbox, disregarding if running (default: false)
CLI範例
PS C:\> crictl.exe rmp -a
pod sandbox "46c169974805cb12f3cfedc34e48444c6710e118af93b42ce303ae3cbbd8d304" is running, please stop it first
PS C:\> crictl.exe pods
POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME
46c169974805c 23 minutes ago Ready dotnet-sample-sandbox default 1 (default)
PS C:\> crictl.exe stopp 46c169974805c
Stopped sandbox 46c169974805c
PS C:\> crictl.exe rmp -a
Removed sandbox 46c169974805cb12f3cfedc34e48444c6710e118af93b42ce303ae3cbbd8d304
List pods
crictl.exe pods [command options] [arguments...]
CLI範例
crictl.exe pods
Start one or more created containers
crictl.exe start [command options] CONTAINER-ID [CONTAINER-ID...]
CLI範例
crictl.exe start $CONTAINER_ID
Display information of the container runtime
crictl.exe info [command options] [arguments...]
CLI範例
crictl.exe info
Stop one or more running containers
crictl.exe stop [command options] CONTAINER-ID [CONTAINER-ID...]
CLI範例
crictl.exe stop $CONTAINER_ID
Stop one or more running pods
crictl.exe stopp [command options] POD-ID [POD-ID...]
CLI範例
crictl.exe stopp $POD_ID
Update one or more running containers
crictl.exe update [command options] CONTAINER-ID [CONTAINER-ID...]
OPTIONS:
--cpu-count value (Windows only) Number of CPUs available to the container (default: 0)
--cpu-maximum value (Windows only) Portion of CPU cycles specified as a percentage * 100 (default: 0)
--cpu-period value CPU CFS period to be used for hardcapping (in usecs). 0 to use system default (default: 0)
--cpu-quota value CPU CFS hardcap limit (in usecs). Allowed cpu time in a given period (default: 0)
--cpu-share value CPU shares (relative weight vs. other containers) (default: 0)
--cpuset-cpus value CPU(s) to use
--cpuset-mems value Memory node(s) to use
--memory value Memory limit (in bytes) (default: 0)
CLI範例
crictl.exe update --cpu-count 1 $CONTAINER_ID
Get and set crictl client configuration options
crictl.exe config [command options] [<crictl options>]
Windows 通常會設定在 ~\.crictl\crictl.yaml
。
mkdir -Force "$home\.crictl"
@"
runtime-endpoint: npipe://./pipe/containerd-containerd
image-endpoint: npipe://./pipe/containerd-containerd
timeout: 10
#debug: true
"@ | Set-Content "$home\.crictl\crictl.yaml" -Force
如果你是第一次在
crictl.exe
執行建立Pod的工作,它會在背景下載k8s.gcr.io/pause
,而這個下載作業會受到上面的timeout
參數影響。因此建議第一次執行可以加大至90
~120
。等正常下載完成後再縮減回來即可。
List container(s) resource usage statistics
crictl.exe stats [command options] [ID]
CLI範例
crictl.exe stats
crictl.exe stats -a
crictl.exe stats $CONTAINER_ID
List pod resource usage statistics
crictl.exe statsp [command options] [ID]
CLI範例
crictl.exe statsp
crictl.exe statsp $POD_ID
Output shell completion code
crictl.exe completion [command options] SHELL
提供Shell自動完成功能,支援bash、zsh、fish,Windows不支援。