容器運行時:Containerd容器管理
容器基本操作
容器基本操作主要是 ctr image 命令,查看命令幫助:
[root@localhost ~]# ctr containers -h
NAME:
ctr containers - Manage containers
USAGE:
ctr containers command [command options] [arguments...]
COMMANDS:
create Create container
delete, del, remove, rm Delete one or more existing containers
info Get info about a container
list, ls List containers
label Set and clear labels for a container
checkpoint Checkpoint a container
restore Restore a container from checkpoint
OPTIONS:
--help, -h show help
創建靜態容器
create:
[root@localhost ~]# ctr container create docker.io/library/nginx:alpine nginx
nginx 指定容器名稱 使用 ctr container create 命令創建容器后,容器并沒有處于運行狀態,其只是一個靜態的容器。這個 container 對象只是包含了運行一個容器所需的資源及配置的數據結構,例如:namespaces、rootfs 和容器的配置都已經初始化成功了,只是用戶進程(本案例為nginx)還沒有啟動。需要使用ctr tasks命令才能獲取一個動態容器。
查看容器
[root@localhost ~]# ctr container ls
CONTAINER IMAGE RUNTIME
nginx docker.io/library/nginx:alpine io.containerd.runc.v2
加上 -q 選項 僅查看名字:
[root@localhost ~]# ctr container ls -q
nginx
也可以簡寫:
[root@localhost ~]# ctr c ls -q
nginx
查看容器詳細配置,類似于 docker inspect 功能。
[root@localhost ~]# ctr container info nginx
刪除容器
[root@localhost ~]# ctr container rm nginx
[root@localhost ~]# ctr container ls
CONTAINER IMAGE RUNTIME
容器任務
上面我們通過 container create 命令創建的容器,并沒有處于運行狀態,只是一個靜態的容器。一個 container 對象只是包含了運行一個容器所需的資源及相關配置數據,表示 namespaces、rootfs 和容器的配置都已經初始化成功了,只是用戶進程還沒有啟動。一個容器真正運行起來是由 Task 任務實現的,Task 可以為容器設置網卡,還可以配置工具來對容器進行監控等。我們操作容器實際上是對容器進程操作。
1.靜態容器啟動為動態容器
將靜態容器啟動為動態容器 ,使用 ctr task 命令 Task 相關操作可以通過 ctr task 獲取,如下我們通過 Task 來啟動容器:
[root@localhost ~]# ctr task start -d nginx
-d是一個命令行選項,它的全稱是--detach。這個選項告訴ctr task start命令在啟動任務后立即返回,讓任務在后臺運行。
2.查看容器進程
通過 task ls 查看正在運行的容器進程:
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 RUNNING
通過ps 查看,其中第一個 PID 23181 就是我們容器中的 1 號進程。
[root@localhost ~]# ctr task ps nginx
PID INFO
23181 -
23208 -
查看物理機進程,可以看到相應的進程ID:23181 、23208 可以對應的上:
[root@localhost ~]# ps -aux|grep nginx
root 23159 0.0 2.1 722644 20916 ? Sl 13:01 0:00 /usr/local/bin/containerd-shim-runc-v2 -namespace default -id nginx -address /run/containerd/containerd.sock
root 23181 0.0 0.5 8904 5120 ? Ss 13:01 0:00 nginx: master process nginx -g daemon off;
101 23208 0.0 0.2 9400 2256 ? S 13:01 0:00 nginx: worker process
root 23266 0.0 0.2 112836 2332 pts/3 S+ 13:15 0:00 grep --color=auto nginx
3.exec終端操作
[root@localhost ~]# ctr task exec --exec-id 0 -t nginx sh
/ # ls
bin docker-entrypoint.d etc lib mnt proc run srv tmp var
dev docker-entrypoint.sh home media opt root sbin sys usr
/ # pwd
/
這里要注意 --exec-id參數 為 exec 進程設定一個id,可以隨意輸入,只要保證唯一即可,也可使用$RANDOM變量。
4.運行一個動態容器
[root@localhost ~]# ctr run -d --net-host docker.io/library/nginx:alpine nginx2
[root@localhost ~]# ctr c ls
CONTAINER IMAGE RUNTIME
nginx docker.io/library/nginx:alpine io.containerd.runc.v2
nginx2 docker.io/library/nginx:alpine io.containerd.runc.v2
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 23181 RUNNING
nginx2 23339 RUNNING
- -d 代表dameon,后臺運行
- --net-host 代表容器的IP就是宿主機的IP(相當于docker里的host類型網絡)
5.進入容器
[root@localhost ~]# ctr task exec --exec-id 1 -t nginx2 /bin/sh
/ # ifconfig
eno16777736 Link encap:Ethernet HWaddr 00:0C:29:AD:FC:E9
inet addr:192.168.36.137 Bcast:192.168.36.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fead:fce9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2304427 errors:0 dropped:0 overruns:0 frame:0
TX packets:462774 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3259139229 (3.0 GiB) TX bytes:182005861 (173.5 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:696 (696.0 B) TX bytes:696 (696.0 B)
/ # curl 192.168.36.137
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
暫停容器進程
和 docker pause 類似的功能:
[root@localhost ~]# ctr task pause nginx
暫停后容器狀態變成了 PAUSED:
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 PAUSED
恢復容器進程
使用 resume 命令來恢復容器:
[root@localhost ~]# ctr task resume nginx
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 RUNNING
殺死容器進程
ctr 沒有 stop 容器的功能,只能暫停或者殺死容器進程,然后在刪除容器殺死容器進程可以使用 task kill 命令:
[root@localhost ~]# ctr task kill nginx
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 STOPPED
刪除進程
殺掉容器后可以看到容器的狀態變成了 STOPPED。同樣也可以通過 task rm 命令刪除 Task:
[root@localhost ~]# ctr task rm nginx
[root@localhost ~]# ctr task ls
TASK PID STATUS
刪除進程之后才可以刪除容器:
[root@localhost ~]# ctr c rm nginx
查看容器進程資源
除此之外我們還可以獲取容器的 cgroup 相關信息,可以使用 task metrics 命令用來獲取容器的內存、CPU 和 PID 的限額與使用量。
# 重新啟動容器
[root@localhost ~]# ctr task start -d nginx
[root@localhost ~]# ctr task metrics nginx
ID TIMESTAMP
nginx seconds:1701925304 nanos:694970440
METRIC VALUE
memory.usage_in_bytes 2592768
memory.limit_in_bytes 9223372036854771712
memory.stat.cache 258048
cpuacct.usage 21976291
cpuacct.usage_percpu [21976291 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
pids.current 2
pids.limit 0