容器运行时:Containerd容器管理

云计算 云原生
Nginx 指定容器名称 使用 ctr container create 命令创建容器后,容器并没有处于运行状态,其只是一个静态的容器。

容器基本操作

容器基本操作主要是 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 href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">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
责任编辑:赵宁宁 来源: 云原生运维圈
相关推荐

2021-09-11 15:38:23

容器运行镜像开放

2019-07-12 09:30:12

DashboardDockerDNS

2021-09-02 05:37:22

Containerd Kubernetes 容器

2023-08-29 08:20:35

Kubernete跨云容器

2023-01-03 09:10:21

2020-08-11 08:59:20

容器虚拟化技术

2021-10-22 00:09:16

Kubernetes容器接口

2021-03-24 06:26:00

kubeadmK8Scontainerd

2021-08-18 06:40:54

KubernetesDocker Containerd

2021-12-23 07:58:06

Kubelet容器运行

2017-03-20 07:21:32

Docker

2023-04-03 13:01:14

UbuntuCRI-O

2015-07-20 15:44:46

Swift框架MJExtension反射

2024-03-21 09:15:58

JS运行的JavaScrip

2024-04-15 05:00:00

kubernete网络容器

2022-09-13 12:03:39

cri-dockerKubernetesdockershim

2022-04-01 12:51:44

命令Containerd

2021-09-07 07:48:37

kubeletKubernetesContainerd

2022-02-16 20:04:08

容器KubernetesShim

2013-11-26 16:49:55

Android开发运行时KitKat
点赞
收藏

51CTO技术栈公众号