6

containerd系列(三):containerd 的容器管理

 1 year ago
source link: https://blog.51cto.com/flyfish225/5366113
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

标签(空格分隔):containerd 系列


一:获取创建静态容器命令

1.1 创建静态容器命令查询

ctr containerd --help 
containerd系列(三):containerd 的容器管理_容器管理
使用`ctr container create `命令创建容器后,容器并没有处于运行状态,其只是一个静态的容器。
这个 container 对象只是包含了运行一个容器所需的资源及配置的数据结构,例如: namespaces、rootfs 和容器的配置都已经初始化成功了,
只是用户进程(本案例为nginx)还没有启动。需要使用`ctr tasks`命令才能获取一个动态容器。

1.2 获取动态容器命令帮助

# ctr run --help
使用`ctr run`命令可以创建一个静态容器并使其运行。一步到位运行容器。
containerd系列(三):containerd 的容器管理_containerd_02

1.3 查看容器

ctr container ls 可以简写成 ctr c ls 
containerd系列(三):containerd 的容器管理_容器管理_03

1.4 查看任务

ctr task ls  可以简写成ctr t ls 
containerd系列(三):containerd 的容器管理_containerd_04

1.5 创建一个静态的容器

ctr c create docker.io/library/nginx:alpine nginx1
ctr c ls 
containerd系列(三):containerd 的容器管理_containerd_05
查看容器详细信息
# ctr container info nginx1
containerd系列(三):containerd 的容器管理_containerd_06

1.6 静态容器启动为动态容器

复制containerd连接runC垫片工具至系统
# ls usr/local/bin/
containerd  containerd-shim  containerd-shim-runc-v1  containerd-shim-runc-v2  containerd-stress  crictl  critest  ctd-decoder  ctr
[root@localhost ~]# cp usr/local/bin/containerd-shim-runc-v2 /usr/bin/
containerd系列(三):containerd 的容器管理_容器管理_07
启动task,即表时在容器中运行了进程,即为动态容器。
# ctr task start -d nginx1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

###
说明:
-d表示daemon或者后台的意思,否则会卡住终端

containerd系列(三):containerd 的容器管理_容器管理_08

containerd系列(三):containerd 的容器管理_containerd_09
查看容器所在宿主机进程,是以宿主机进程的方式存在的。
# ctr task ls
TASK      PID     STATUS
nginx1    9823    RUNNING

查看容器的进程(都是物理机的进程)
# ctr task ps nginx1
PID     INFO
9823    -
9867    -
9868    -
9869    -
9870    -

物理机查看到相应的进程
# ps -ef | grep 9823
root       9823   9799  0 21:49 ?        00:00:00 nginx: master process nginx -g daemon off;
101        9867   9823  0 21:49 ?        00:00:00 nginx: worker process
101        9868   9823  0 21:49 ?        00:00:00 nginx: worker process
101        9869   9823  0 21:49 ?        00:00:00 nginx: worker process
101        9870   9823  0 21:49 ?        00:00:00 nginx: worker process
root       9968   9304  0 21:54 pts/1    00:00:00 grep --color=auto 9823

containerd系列(三):containerd 的容器管理_containerd_10

1.7 进入容器操作

ctr -n default task exec --exec-id $RANDOM -t nginx1 sh
containerd系列(三):containerd 的容器管理_容器管理_11
containerd系列(三):containerd 的容器管理_容器管理_12

1.8 直接运行一个动态的容器

# ctr run -d --net-host docker.io/library/nginx:alpine nginx2
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

说明:
* -d 代表dameon,后台运行
* --net-host 代表容器的IP就是宿主机的IP(相当于docker里的host类型网络)

containerd系列(三):containerd 的容器管理_容器管理_13

查看已运行容器
# ctr container ls
CONTAINER    IMAGE                             RUNTIME
nginx2       docker.io/library/nginx:alpine    io.containerd.runc.v2

查看已运行容器中运行的进程,既tasks
# ctr tasks ls
TASK      PID     STATUS
nginx2    4061    RUNNING


进入容器
ctr -n default task exec --exec-id $RANDOM -t nginx2 sh
containerd系列(三):containerd 的容器管理_containerd_14

containerd系列(三):containerd 的容器管理_containerd_15

containerd系列(三):containerd 的容器管理_容器管理_16
为容器中运行的网站添加网站文件
/ # echo "nginx2" > /usr/share/nginx/html/index.html
/ # exit

在宿主机上访问网站
[root@flyfishsrvs01 ~]# curl 172.16.10.11
nginx2
containerd系列(三):containerd 的容器管理_containerd_17

1.9 暂停容器

查看容器状态
# ctr tasks ls
TASK      PID     STATUS
nginx2    4061    RUNNING

暂停容器
# ctr tasks pause nginx2

再次查看容器状态,看到其状态为PAUSED,表示停止。
# ctr tasks ls
TASK      PID     STATUS
nginx2    4061    PAUSED

[root@localhost ~]# curl http://172.16.10.11
在宿主机访问,发现不可以访问到网站
containerd系列(三):containerd 的容器管理_containerd_18

1.10 恢复容器

使用resume命令恢复容器
# ctr tasks resume nginx2
查看恢复后状态
# ctr tasks ls
TASK      PID     STATUS
nginx2    4061    RUNNING

在宿主机上访问容器中提供的服务
# curl http://172.16.10.11
nginx2
containerd系列(三):containerd 的容器管理_containerd_19

1.11 停止容器

使用kill命令停止容器中运行的进程,既为停止容器
# ctr tasks kill nginx2

查看容器停止后状态,STATUS为STOPPED
TASK      PID      STATUS
nginx2    10660    STOPPED
nginx1    9823     RUNNING

1.12 删掉容器

ctr tasks delete nginx2
必须先停止tasks或先删除task,再删除容器
查看静态容器,确认其还存在于系统中
# ctr container ls
CONTAINER    IMAGE                             RUNTIME
nginx2       docker.io/library/nginx:alpine    io.containerd.runc.v2

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK