Containerd-1.6.5 之Task任务

一、Task任务

1 概念

在containerd中有一个task任务的概念,使用containerd create创建的容器,并没有处于running状态,只是一个静态的容器。

在docker中可以直接run容器,但是在containerd是需要先create在通过task启动容器。

create 容器并不会启动容器,可以理解只是声明了一个container,并不会启动和执行相关操作。

在task也可以管理容器的网络,以及容器的监控等。实际上就是增强版的docker ps

 

2 task可以操作的相关命令

[root@ecs-65685 ~]# ctr task
NAME:
   ctr tasks - manage tasks

USAGE:
   ctr tasks command [command options] [arguments...]

COMMANDS:
   attach                   attach to the IO of a running container
   checkpoint               checkpoint a container
   delete, del, remove, rm  delete one or more tasks
   exec                     execute additional processes in an existing container
   list, ls                 list tasks
   kill                     signal a container (default: SIGTERM)
   pause                    pause an existing container
   ps                       list processes for container
   resume                   resume a paused container
   start                    start a container that has been created
   metrics, metric          get a single data point of metrics for a task with the built-in Linux runtime

OPTIONS:
   --help, -h  show help

3 查看正在运行的容器

[root@ecs-65685 ~]# ctr -n xyz task ls
TASK    PID    STATUS

4 使用task启动容器

[root@ecs-65685 ~]# ctr -n xyz task start -d nginx
[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID    STATUS    
nginx    913    RUNNING

5 通过ps -ef就可以看到进程了

[root@ecs-65685 ~]#  ps -ef| grep nginx
root       893     1  0 01:01 ?        00:00:00 /usr/local/bin/containerd-shim-runc-v2 -namespace xyz -id nginx -address /run/containerd/containerd.sock
root       913   893  0 01:01 ?        00:00:00 nginx: master process nginx -g daemon off;
101        952   913  0 01:01 ?        00:00:00 nginx: worker process
101        953   913  0 01:01 ?        00:00:00 nginx: worker process
101        954   913  0 01:01 ?        00:00:00 nginx: worker process
101        955   913  0 01:01 ?        00:00:00 nginx: worker process
root      1347 20536  0 01:01 pts/1    00:00:00 grep --color=auto nginx

6 进入容器

  • exec task进入容器操作
  • --exec-id  id 设置一个id,可以随便写,唯一即可
  • -t --tty为container分配一个tty
  • nginx 容器名称
  • sh && bash即可
# 查看当前运行容器
[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID    STATUS    
nginx    913    RUNNING

# 进入容器内部和docker exec基本上相同
[root@ecs-65685 ~]# ctr -n xyz task exec --exec-id 1 -t nginx sh
/ # ps -ef|grep nginx
    1 root      0:00 nginx: master process nginx -g daemon off;
   32 nginx     0:00 nginx: worker process
   33 nginx     0:00 nginx: worker process
   34 nginx     0:00 nginx: worker process
   35 nginx     0:00 nginx: worker process
   43 root      0:00 grep nginx
/ # ls
bin                   home                  proc                  sys
dev                   lib                   root                  tmp
docker-entrypoint.d   media                 run                   usr
docker-entrypoint.sh  mnt                   sbin                  var
etc                   opt                   srv
/ # exit

7 暂停容器,和 docker pause 类似的功能。暂停后容器状态变成了 PAUSED

[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID    STATUS    
nginx    913    RUNNING

# 停止容器
[root@ecs-65685 ~]# ctr -n xyz task pause nginx

# 查看状态
[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID    STATUS    
nginx    913    PAUSED

8 使用 resume 命令来恢复容器:

[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID    STATUS    
nginx    913    PAUSED

# 恢复容器
[root@ecs-65685 ~]# ctr -n xyz task resume nginx

[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID    STATUS    
nginx    913    RUNNING

注意:需要注意暂停和恢复容器不等于重启容器 。

9 停止容器,只能通过kill来进行停止,然后在重新start。在containerd中没有stop和restart参数

# kill停止task任务
[root@ecs-65685 ~]# ctr -n xyz task kill nginx
[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID    STATUS    
nginx    913    RUNNING

# 删除task并不会删除container
[root@ecs-65685 ~]# ctr -n xyz c ls
CONTAINER    IMAGE                             RUNTIME                  
nginx        docker.io/library/nginx:alpine    io.containerd.runc.v2

[root@ecs-65685 ~]# ctr -n xyz task ls
TASK    PID    STATUS
nginx   913   STOPPED

10 删除容器

[root@ecs-65685 ~]# ctr c rm nginx
[root@ecs-65685 ~]# ctr c ls
CONTAINER    IMAGE    RUNTIME

11 获取容器的 cgroup 相关信息,可以使用 task metrics 命令用来获取容器的内存、CPU 和 PID 的限额与使用量。

[root@ecs-65685 ~]# ctr -n xyz task ls
TASK     PID      STATUS    
nginx    19068    RUNNING

# 使用metrics查看资源使用情况
[root@ecs-65685 ~]# ctr -n xyz task metrics nginx
ID       TIMESTAMP                                  
nginx    2022-10-13 17:26:36.355142677 +0000 UTC    

METRIC                   VALUE                                 
memory.usage_in_bytes    5144576                               
memory.limit_in_bytes    9223372036854771712                   
memory.stat.cache        0                                     
cpuacct.usage            30850201                              
cpuacct.usage_percpu     [3416878 11180203 8691209 7561911]    
pids.current             5                                     
pids.limit               0

12 task ps可以看到在宿主机上容器的进程

[root@ecs-65685 ~]# ctr task ls
TASK     PID      STATUS    
nginx    19068    RUNNING

# PID为宿主机上的PID
[root@ecs-65685 ~]# ctr task ps nginx
PID      INFO
19068    -
19108    -
19109    -
19110    -
19111    -
[root@ecs-65685 ~]# ps -ef |grep 19068
root     19068 19048  0 01:25 ?        00:00:00 nginx: master process nginx -g daemon off;
101      19108 19068  0 01:25 ?        00:00:00 nginx: worker process
101      19109 19068  0 01:25 ?        00:00:00 nginx: worker process
101      19110 19068  0 01:25 ?        00:00:00 nginx: worker process
101      19111 19068  0 01:25 ?        00:00:00 nginx: worker process
root     21332 20536  0 01:28 pts/1    00:00:00 grep --color=auto 19068

 

posted @ 2022-10-14 00:38  娇小赤雅  阅读(636)  评论(0编辑  收藏  举报