安装 crictl
下载 crictl
可以从 cri-tools 发布页面 下载一个压缩的 crictl 归档文件,用于几种不同的架构。下载与你的 kubernetes 版本相对应的版本。提取它并将其移动到系统路径上的某个位置,例如 /usr/local/bin/
。
配置 crictl
crictl 命令有几个子命令和运行时参数。 有关详细信息,请使用 crictl help
或 crictl <subcommand> help
获取帮助信息。
crictl 默认连接到 unix:///var/run/dockershim.sock
。 对于其他的运行时,你可以用多种不同的方法设置端点:
- 通过设置参数 –runtime-endpoint 和 –image-endpoint
- 通过设置环境变量 CONTAINER_RUNTIME_ENDPOINT 和 IMAGE_SERVICE_ENDPOINT
- 通过在配置文件中设置端点 –config=/etc/crictl.yaml
你还可以在连接到服务器并启用或禁用调试时指定超时值,方法是在配置文件中指定 timeout
或 debug
值,或者使用 --timeout
和 --debug
命令行参数。
创建 /etc/crictl.yaml 配置文件,内容如下:
1 | runtime-endpoint: unix:///var/run/containerd/containerd.sock |
crictl 命令示例
Pod 相关命令
打印所有 Pod 的清单
1
2
3# crictl pods
POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME
a49588e508020 17 minutes ago Ready calico-node-zhz7j kube-system 0 (default)根据名称打印 Pod 清单
1
2
3# crictl pods --name calico-node-zhz7j
POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME
a49588e508020 20 minutes ago Ready calico-node-zhz7j kube-system 0 (default)根据名称打印 Pod 清单
1
2
3
4# crictl pods --name metrics-server-64974786d5-2rw7z
POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME
a1314cfbf0fb3 27 hours ago Ready metrics-server-64974786d5-2rw7z kube-system 2 (default)
582728bf72211 3 days ago NotReady metrics-server-64974786d5-2rw7z kube-system 1 (default)根据标签打印 Pod 清单
1
2
3# crictl pods --label app=rook-discover
POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME
d6fa3afacf486 27 hours ago Ready rook-discover-5tdrg rook-ceph 0 (default)
镜像相关命令
打印所有镜像清单
1
2
3
4
5
6
7
8
9
10# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/calico/cni v3.21.2 4c5c325303915 81.7MB
docker.io/calico/node v3.21.2 f1bca4d4ced28 74.1MB
docker.io/calico/pod2daemon-flexvol v3.21.2 7778dd57e5063 9.17MB
docker.io/library/nginx latest f652ca386ed13 56.7MB
docker.io/rook/ceph v1.7.2 363d0efff0e17 450MB
docker.io/rook/ceph v1.8.0 7eba4989d560d 445MB
habor.china-snow.net/tms-backend-dev 0.0.90 89dbc1702519a 336MB
habor.china-snow.net/tms-frontend-dev 0.0.60 d51be42fc1d4c 55.6MB根据仓库打印镜像清单
1
crictl images nginx
只查看镜像 ID
1
2
3
4
5
6
7
8
9
10
11# crictl images -q
sha256:4c5c325303915ea5cad48921922fae6f7d877aa7b1bafde068225dc36e5e3468
sha256:f1bca4d4ced28afc1b38ad2003512a906912f939ebf49b6e9773bcbd29ea75bd
sha256:7778dd57e5063d7af353980a844c24d065e69c399fe36df886bca5a56cfe0399
sha256:f652ca386ed135a4cbe356333e08ef0816f81b2ac8d0619af01e2b256837ed3e
sha256:363d0efff0e17b32a2620e720f8fb86461a5a6f56c4fc114dd88a2968ee75d95
sha256:7eba4989d560d9d78ccf15c0d83450ab1155fac41b08a2586d0e8e04c3153b1d
sha256:89dbc1702519a3bc8b6b509dee332e14bca10195c7a661a15e2b05c71c6daa12
sha256:d51be42fc1d4c052aa5317ee5fa95d15ccfd20cfd4930119430e775a7d454ffd
sha256:ccf46f68f87a15b263415b678d8c55b432c1a872aedc923678d5c7ed021c7332
sha256:c51af0466f16fbc1ca8688bf0505cf3d53d95f07cfba90cbfc65db169a0de39b
容器相关命令
查看所有容器
1
2
3
4
5
6
7
8
9
10
11
12# crictl ps -a
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
af091e7b2c706 fe0f921f3c92a 28 seconds ago Running csi-provisioner 4 a533c47a5c06a
9e133c96d744e 1df30f0e25552 30 seconds ago Running csi-resizer 8 a533c47a5c06a
cc51ea8f3665f 33670232d7bc5 31 seconds ago Running csi-snapshotter 6 a533c47a5c06a
50c401560a90a 1df30f0e25552 2 minutes ago Exited csi-resizer 7 a533c47a5c06a
9bf732d2fa738 fe0f921f3c92a 5 minutes ago Exited csi-provisioner 3 a533c47a5c06a
d65915a1f8945 33670232d7bc5 6 minutes ago Exited csi-snapshotter 5 a533c47a5c06a
45adfb5d14d07 37f46af926da0 6 hours ago Running csi-attacher 8 a533c47a5c06a
5a24bc2076774 37f46af926da0 7 hours ago Exited csi-attacher 7 a533c47a5c06a
a547d2acc1cb4 c51af0466f16f 27 hours ago Running ceph-crash 0 70be4aaea54ad
e7e6a5201c5db c51af0466f16f 27 hours ago Exited chown-container-data-dir 0 70be4aaea54ad打印正在运行的容器清单
1
2
3
4
5
6
7
8
9# crictl ps
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
af091e7b2c706 fe0f921f3c92a 59 seconds ago Running csi-provisioner 4 a533c47a5c06a
9e133c96d744e 1df30f0e25552 About a minute ago Running csi-resizer 8 a533c47a5c06a
cc51ea8f3665f 33670232d7bc5 About a minute ago Running csi-snapshotter 6 a533c47a5c06a
45adfb5d14d07 37f46af926da0 6 hours ago Running csi-attacher 8 a533c47a5c06a
a547d2acc1cb4 c51af0466f16f 27 hours ago Running ceph-crash 0 70be4aaea54ad
c06a47539afe3 c51af0466f16f 27 hours ago Running mon 0 003533ee10b77
876ef3324b91d ef12af1d6a233 27 hours ago Running liveness-prometheus 0 a533c47a5c06a在运行的容器中执行命令
1
crictl exec -i -t 1f73f2d81bf98 ls
获取容器日志
1
crictl logs 87d3992f84f74
获取最近的 N 行日志
1
crictl logs --tail=1 87d3992f84f74