容器運(yùn)行時(shí):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
創(chuàng)建靜態(tài)容器
create:
[root@localhost ~]# ctr container create docker.io/library/nginx:alpine nginx
nginx 指定容器名稱 使用 ctr container create 命令創(chuàng)建容器后,容器并沒有處于運(yùn)行狀態(tài),其只是一個(gè)靜態(tài)的容器。這個(gè) container 對(duì)象只是包含了運(yùn)行一個(gè)容器所需的資源及配置的數(shù)據(jù)結(jié)構(gòu),例如:namespaces、rootfs 和容器的配置都已經(jīng)初始化成功了,只是用戶進(jìn)程(本案例為nginx)還沒有啟動(dòng)。需要使用ctr tasks命令才能獲取一個(gè)動(dòng)態(tài)容器。
查看容器
[root@localhost ~]# ctr container ls
CONTAINER IMAGE RUNTIME
nginx docker.io/library/nginx:alpine io.containerd.runc.v2
加上 -q 選項(xiàng) 僅查看名字:
[root@localhost ~]# ctr container ls -q
nginx
也可以簡(jiǎn)寫:
[root@localhost ~]# ctr c ls -q
nginx
查看容器詳細(xì)配置,類似于 docker inspect 功能。
[root@localhost ~]# ctr container info nginx
刪除容器
[root@localhost ~]# ctr container rm nginx
[root@localhost ~]# ctr container ls
CONTAINER IMAGE RUNTIME
容器任務(wù)
上面我們通過 container create 命令創(chuàng)建的容器,并沒有處于運(yùn)行狀態(tài),只是一個(gè)靜態(tài)的容器。一個(gè) container 對(duì)象只是包含了運(yùn)行一個(gè)容器所需的資源及相關(guān)配置數(shù)據(jù),表示 namespaces、rootfs 和容器的配置都已經(jīng)初始化成功了,只是用戶進(jìn)程還沒有啟動(dòng)。一個(gè)容器真正運(yùn)行起來是由 Task 任務(wù)實(shí)現(xiàn)的,Task 可以為容器設(shè)置網(wǎng)卡,還可以配置工具來對(duì)容器進(jìn)行監(jiān)控等。我們操作容器實(shí)際上是對(duì)容器進(jìn)程操作。
1.靜態(tài)容器啟動(dòng)為動(dòng)態(tài)容器
將靜態(tài)容器啟動(dòng)為動(dòng)態(tài)容器 ,使用 ctr task 命令 Task 相關(guān)操作可以通過 ctr task 獲取,如下我們通過 Task 來啟動(dòng)容器:
[root@localhost ~]# ctr task start -d nginx
-d是一個(gè)命令行選項(xiàng),它的全稱是--detach。這個(gè)選項(xiàng)告訴ctr task start命令在啟動(dòng)任務(wù)后立即返回,讓任務(wù)在后臺(tái)運(yùn)行。
2.查看容器進(jìn)程
通過 task ls 查看正在運(yùn)行的容器進(jìn)程:
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 RUNNING
通過ps 查看,其中第一個(gè) PID 23181 就是我們?nèi)萜髦械?1 號(hào)進(jìn)程。
[root@localhost ~]# ctr task ps nginx
PID INFO
23181 -
23208 -
查看物理機(jī)進(jìn)程,可以看到相應(yīng)的進(jìn)程ID:23181 、23208 可以對(duì)應(yīng)的上:
[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參數(shù) 為 exec 進(jìn)程設(shè)定一個(gè)id,可以隨意輸入,只要保證唯一即可,也可使用$RANDOM變量。
4.運(yùn)行一個(gè)動(dòng)態(tài)容器
[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,后臺(tái)運(yùn)行
- --net-host 代表容器的IP就是宿主機(jī)的IP(相當(dāng)于docker里的host類型網(wǎng)絡(luò))
5.進(jìn)入容器
[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>
暫停容器進(jìn)程
和 docker pause 類似的功能:
[root@localhost ~]# ctr task pause nginx
暫停后容器狀態(tài)變成了 PAUSED:
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 PAUSED
恢復(fù)容器進(jìn)程
使用 resume 命令來恢復(fù)容器:
[root@localhost ~]# ctr task resume nginx
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 RUNNING
殺死容器進(jìn)程
ctr 沒有 stop 容器的功能,只能暫停或者殺死容器進(jìn)程,然后在刪除容器殺死容器進(jìn)程可以使用 task kill 命令:
[root@localhost ~]# ctr task kill nginx
[root@localhost ~]# ctr task ls
TASK PID STATUS
nginx 22945 STOPPED
刪除進(jìn)程
殺掉容器后可以看到容器的狀態(tài)變成了 STOPPED。同樣也可以通過 task rm 命令刪除 Task:
[root@localhost ~]# ctr task rm nginx
[root@localhost ~]# ctr task ls
TASK PID STATUS
刪除進(jìn)程之后才可以刪除容器:
[root@localhost ~]# ctr c rm nginx
查看容器進(jìn)程資源
除此之外我們還可以獲取容器的 cgroup 相關(guān)信息,可以使用 task metrics 命令用來獲取容器的內(nèi)存、CPU 和 PID 的限額與使用量。
# 重新啟動(dòng)容器
[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