Docker组件介绍


1602207109(1)


docker组件可以分以下三类

1、Docker 相关的组件:docker、dockerd、docker-init 和 docker-proxy

2、containerd 相关的组件:containerd、containerd-shim 和 ctr

3、容器运行时相关的组件:runc



docker:

       docker 是 Docker 客户端的一个完整实现,它是一个二进制文件,对用户可见的操作形式为 docker 命令,通过 docker 命令可以完成所有的 Docker 客户端与服务端的通信

      

       docker与dockerd通信的三种方式:

            1、通过UNIX套接字配置格式为unix://socket_path,默认 dockerd 生成的 socket 文件路径为 /var/run/docker.sock

               2、通过 TCP 与服务端通信:配置格式为tcp://host:port

               3、通过文件描述符的方式与服务端通信:配置格式为:fd://这种格式一般用于 systemd 管理的系统中。

            


dockerd

      dockerd 是 Docker 服务端的后台常驻进程,用来接收客户端发送的请求,执行具体的处理任务,处理完成后将结果返回给客户端



docker-init

     docker会启动inti进程充当容器中的1号进程,当子进程遇到一些问题(收留孤儿进程)的时候inti进程可以进行回收子进程


没有创建init的容器

[root@control-plane ~]# docker run -it busybox sh

/ # ps aux
PID   USER     TIME  COMMAND
     1 root      0:00 sh
     6 root      0:00 ps aux

创建了init进程的容器

[root@control-plane ~]# docker run -it --init busybox sh
/ # ps aux
PID   USER     TIME  COMMAND
    1 root      0:00 /sbin/docker-init -- sh
     6 root      0:00 sh
     7 root      0:00 ps aux



docker-proxy

        主要是用来做端口映射的。当我们使用 docker run 命令启动容器时, -p 参数


[root@control-plane ~]# docker run --name=nginx -d -p 8080:80 nginx


获取当前容器IP

[root@control-plane ~]# docker inspect --format '{{ .NetworkSettings.IPAddress }}' nginx
172.17.0.5


查看proxy进程

[root@control-plane ~]# ps aux|grep docker-proxy|grep -v grep
root      8745  0.0  0.1 217040  8864 ?        Sl   09:50   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.5 -container-port 80


底层通过iptables 做了DNAT转换

[root@control-plane ~]# iptables -L -nv -t nat|grep 172.17.0.5

1602208549(1)



containerd

          从docker1.11版正式从dockerd中剥离出来的完全遵循OCI标准,是容器表追后的产物。

          管理功能:

              1、容器周期管理

              2、镜像管理

              3、接受dockerd的请求,调用runc启动容器

              4、管理存储

               5、管理网络

          containerd 包含一个后台常驻进程,默认的 socket 路径为 /run/containerd/containerd.sock

          如果你不想使用 dockerd,也可以直接使用 containerd 来管理容器,由于 containerd 更加简单和轻量,生产环境中越来越多的人开始直接使用 containerd 来管理容器。


默认dockerd通过套接字调用containerd

[root@control-plane ~]# ps -ef|grep dockerd |grep -v grep
root     18717     1  1 9月14 ?       06:46:51 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock


containerd-shim

     containerd-shim 的意思是垫片,类似于拧螺丝时夹在螺丝和螺母之间的垫片。containerd-shim 的主要作用是将 containerd 和真正的容器进程解耦,使用 containerd-shim 作为容器进程的父进程,从而实现重启 containerd 不影响已经启动的容器进程。


ctr

           ctr 实际上是 containerd-ctr,它是 containerd 的客户端,主要用来开发和调试,在没有 dockerd 的环境中,ctr 可以充当 docker 客户端的部分角色,直接向 containerd 守护进程发送操作容器的请求。


runc

     runc 是一个标准的 OCI 容器运行时的实现,它是一个命令行工具,可以直接用来创建和运行容器。

[root@control-plane ~]# mkdir /tmp/runc
[root@control-plane ~]# cd /tmp/runc/

导出容器的文件目录
[root@control-plane runc]# mkdir rootfs && docker export $(docker create busybox) | tar -C rootfs -xvf -

生成runc配置文件

[root@control-plane runc]# runc spec
[root@control-plane runc]# ls
config.json  rootfs


{
        "ociVersion": "1.0.1-dev",
        "process": {
                "terminal": true,
                "user": {
                        "uid": 0,
                        "gid": 0
                },
                "args": [
                        "sh"
                ],
                "env": [
                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "TERM=xterm"
                ],
                "cwd": "/",
                "capabilities": {
                        "bounding": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "effective": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "inheritable": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "permitted": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "ambient": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ]
                },
                "rlimits": [
                        {
                                "type": "RLIMIT_NOFILE",
                                "hard": 1024,
                                "soft": 1024
                        }
                ],
                "noNewPrivileges": true
        },
        "root": {
                "path": "rootfs",
                "readonly": true
        },
        "hostname": "runc",
        "mounts": [
                {
                        "destination": "/proc",
                        "type": "proc",
                        "source": "proc"
                },
                {
                        "destination": "/dev",
                        "type": "tmpfs",
                        "source": "tmpfs",
                        "options": [
                                "nosuid",
                                "strictatime",
                                "mode=755",
                                "size=65536k"
                        ]
                },
                {
                        "destination": "/dev/pts",
                        "type": "devpts",
                        "source": "devpts",
                        "options": [
                                "nosuid",
                                "noexec",
                                "newinstance",
                                "ptmxmode=0666",
                                "mode=0620",
                                "gid=5"
                        ]
                },
                {
                        "destination": "/dev/shm",
                        "type": "tmpfs",
                        "source": "shm",
                        "options": [
                                "nosuid",
                                "noexec",
                                "nodev",
                                "mode=1777",
                                "size=65536k"
                        ]
                },
                {
                        "destination": "/dev/mqueue",
                        "type": "mqueue",
                        "source": "mqueue",
                        "options": [
                                "nosuid",
                                "noexec",
                                "nodev"
                        ]
                },
                {
                        "destination": "/sys",
                        "type": "sysfs",
                        "source": "sysfs",
                        "options": [
                                "nosuid",
                                "noexec",
                                "nodev",
                                "ro"
                        ]
                },
                {
                        "destination": "/sys/fs/cgroup",
                        "type": "cgroup",
                        "source": "cgroup",
                        "options": [
                                "nosuid",
                                "noexec",
                                "nodev",
                                "relatime",
                                "ro"
                        ]
                }
        ],
        "linux": {
                "resources": {
                        "devices": [
                                {
                                        "allow": false,
                                        "access": "rwm"
                                }
                        ]
                },
                "namespaces": [
                        {
                                "type": "pid"
                        },
                        {
                                "type": "network"
                        },
                        {
                                "type": "ipc"
                        },
                        {
                                "type": "uts"
                        },
                        {
                                "type": "mount"
                        }
                ],
                "maskedPaths": [
                        "/proc/acpi",
                        "/proc/asound",
                        "/proc/kcore",
                        "/proc/keys",
                        "/proc/latency_stats",
                        "/proc/timer_list",
                        "/proc/timer_stats",
                        "/proc/sched_debug",
                        "/sys/firmware",
                        "/proc/scsi"
                ],
                "readonlyPaths": [
                        "/proc/bus",
                        "/proc/fs",
                        "/proc/irq",
                        "/proc/sys",
                        "/proc/sysrq-trigger"
                ]
        }
}
config.json

config.json 文件定义了 runc 启动容器时的一些配置,如根目录的路径,文件挂载路径等配置。


启动容器

[root@control-plane runc]# runc run busybox

/ # ps aux
PID   USER     TIME  COMMAND
     1 root      0:00 sh
     6 root      0:00 ps aux


查看容器

[root@control-plane ~]# runc list
ID          PID         STATUS      BUNDLE      CREATED                          OWNER
busybox     19422       running     /tmp/runc   2020-10-09T02:28:53.505825491Z   root

















             





原文地址:https://www.cnblogs.com/menkeyi/p/13784429.html