• Containerd 的镜像和容器管理


    一.  镜像管理:

            1.  查看镜像列表:

    [root@node2 ~]# ctr images/image/i ls

            2.  拉取镜像:

    1. [root@node2 ~]# ctr images pull --platform linux/amd64 docker.io/library/busybox:latest
    2. --platform:指定要拉取得镜像的平台

            3.  镜像挂载,把已下载的容器镜像挂载至宿主机的目录中:

    1. 命令格式:ctr images mount 镜像名 挂载的目录
    2. [root@node2 ~]# ctr images mount docker.io/library/busybox:latest /mnt

            4.  卸载:

    [root@node2 ~]# umount /mnt

            5.  镜像导出:

    1. ## 下载镜像
    2. [root@node2 ~]# ctr images pull --all-platforms docker.io/library/busybox:latest
    3. --all-platforms:表示下载所有平台的busybox:latest镜像
    4. ## 导出镜像
    5. [root@node2 ~]# ctr images export --all-platforms nginx-alpine.img(导出的文件名)
    6. docker.io/library/nginx:alpine(要导出的镜像)

            6.  镜像导入:

    [root@node2 ~]# ctr images import nginx-alpine.img(要导入的镜像文件)

    注意:直接导入镜像可能会出现类似"ctr: content digest sha256:xxxxxxx not found"的错误,因此在拉取和导出镜像时,都要指定" --all-platforms "参数再导入镜像。

            7.  删除镜像:

    [root@node2 ~]# ctr images rm docker.io/library/busybox:latest

            8.  给镜像打标记tag:

    [root@node2 ~]# ctr images tag docker.io/library/busybox:latest busybox:latest

            再次查看镜像列表:

     二.  容器管理:

            1.  静态容器命令查询:

    1. [root@node2 ~]# ctr containerd --help
    2. [root@node2 ~]# ctr c -h

            2.  查看容器列表:

    [root@node2 ~]# ctr container/containers/c ls

            3.  查看任务列表:当容器运行起来的时候才会产生任务。

    [root@node2 ~]# ctr task/task/t ls

            4.  创建静态容器:

    1. [root@node2 ~]# ctr c create busybox:latest busybox
    2. [root@node2 ~]# ctr c ls
    3. CONTAINER IMAGE RUNTIME
    4. busybox busybox:latest io.containerd.runc.v2

            5.  启动静态容器,启动task,即表时在容器中运行了进程,即为动态容器。:

    1. [root@node2 ~]# ctr t start -d busybox(容器名)
    2. [root@node2 ~]# ctr t ls
    3. TASK PID STATUS
    4. busybox 1510 RUNNING
    5. -d:表示daemon或者后台的意思,否则会卡住终端

            6.  进入到动态容器:

    1. [root@node2 ~]# ctr -n default task exec --exec-id $RANDOM -t busybox sh
    2. -n:表示指定命名空间
    3. -t:表示提供一个终端

            7.  查看容器详细信息:

    [root@node2 ~]# ctr c info busybox

            8.  直接运行一个动态容器:

    1. [root@node2 ~]# ctr run -d --net-host busybox:latest busybox2
    2. -d:表示dameon,后台运行
    3. --net-host:表示容器的IP就是宿主机的IP(相当于docker里的host类型网络)
    4. ## 查看容器列表:
    5. [root@node2 ~]# ctr c ls
    6. CONTAINER IMAGE RUNTIME
    7. busybox busybox:latest io.containerd.runc.v2
    8. busybox2 busybox:latest io.containerd.runc.v2
    9. ## 查看任务列表:
    10. [root@node2 ~]# ctr t ls
    11. TASK PID STATUS
    12. busybox 1510 RUNNING
    13. busybox2 1637 RUNNING
    14. ## 进入到容器
    15. [root@node2 ~]# ctr -n default task exec --exec-id $RANDOM -t busybox2 sh

            9.  暂停容器:

    1. [root@node2 ~]# ctr t pause busybox2
    2. [root@node2 ~]# ctr t ls
    3. TASK PID STATUS
    4. busybox 1510 RUNNING
    5. busybox2 1637 PAUSED

            10.  恢复容器:

    1. [root@node2 ~]# ctr t resume busybox2
    2. [root@node2 ~]# ctr t ls
    3. TASK PID STATUS
    4. busybox2 1637 RUNNING
    5. busybox 1510 RUNNING

            11.  停止容器:

    1. ## 使用kill命令停止容器中运行的进程,既为停止容器
    2. [root@node2 ~]# ctr tasks kill busybox2
    3. ## 查看容器停止后状态,STATUS为STOPPED
    4. TASK PID STATUS
    5. busybox 10660 STOPPED
    6. busybox2 9823 RUNNING

            12.  删除容器

    1. [root@node2 ~]# ctr t delete busybox2 ## 删除已经停止的容器
    2. [root@node2 ~]# ctr t delete -f busybox2 ## 强制删除容器,包括正在运行的容器
    3. ## 查看任务列表:
    4. [root@node2 ~]# ctr t ls
    5. TASK PID STATUS
    6. busybox 1510 RUNNING
    7. ## 查看容器列表:
    8. [root@node2 ~]# ctr c ls
    9. CONTAINER IMAGE RUNTIME
    10. busybox busybox:latest io.containerd.runc.v2
    11. busybox2 busybox:latest io.containerd.runc.v2

            13.  查看命名空间列表:

    1. [root@node2 ~]# ctr ns ls
    2. NAME LABELS
    3. default

            14.  创建命名空间:

    1. [root@node2 ~]# ctr ns c it(命名空间名称)
    2. [root@node2 ~]# ctr ns ls
    3. NAME LABELS
    4. default
    5. it

            15.  删除命名空间:

    [root@node2 ~]# ctr ns rm it

  • 相关阅读:
    mysqlbinlog和binlog2sql的使用及mysql通过binlog日志恢复数据
    理解session,Cookie,token以及js-cookie在实际开发中的使用
    【数据结构——单链表】本篇文章通过图文结合的方式能让你轻松的掌握单链表
    Spring Boot中的 6 种API请求参数读取方式
    操作系统笔记——Linux实战、Windows(持续更新)
    linux_mysql安装教程带安装包(亲测有效)
    UWB精准定位方案,厘米级高精度技术应用,智能配对感应技术
    CDGA|工业互联网行业怎么做好数据治理?
    python运算符重载之构造函数和迭代器
    RabbitMQ面试篇
  • 原文地址:https://blog.csdn.net/NancyLCL/article/details/126905921