Docker-服务进程管理

docker服务进程

通过查看docker进程,了解docker的运行及工作方式

查看宿主机进程树
[root@ubuntu1804 ~]#docker version
Client: Docker Engine - Community
Version:           19.03.5
API version:       1.40
Go version:        go1.12.12
Git commit:        633a0ea838
Built:             Wed Nov 13 07:29:52 2019
OS/Arch:           linux/amd64
Experimental:      false
Server: Docker Engine - Community
Engine:
Version:          19.03.5
API version:      1.40 (minimum version 1.12)
Go version:       go1.12.12
Git commit:       633a0ea838
Built:            Wed Nov 13 07:28:22 2019
OS/Arch:          linux/amd64
Experimental:     false
containerd:
Version:          1.2.10
GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version:          1.0.0-rc8+dev
GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version:          0.18.0
GitCommit:        fec3683
[root@ubuntu1804 ~]#pstree -p
systemd(1)─┬─VGAuthService(796)
├─accounts-daemon(800)─┬─{accounts-daemon}(805)
│                      └─{accounts-daemon}(807)
├─agetty(866)
├─atd(799)
├─blkmapd(502)
├─containerd(801)─┬─containerd-shim(2497)─┬─sh(2520)
│                 │                       ├─{containerd-shim}(2499)
│                 │                       ├─{containerd-shim}(2500)
│                 │                       ├─{containerd-shim}(2501)
│                 │                       ├─{containerd-shim}(2502)
│                 │                       ├─{containerd-shim}(2503)
│                 │                       ├─{containerd-shim}(2504)
│                 │                       ├─{containerd-shim}(2505)
│                 │                       ├─{containerd-shim}(2550)
│                 │                       └─{containerd-shim}(2551)
│                 ├─containerd-shim(2627)─┬─nginx(2649)───nginx(2699)
│                 │                       ├─{containerd-shim}(2628)
│                 │                       ├─{containerd-shim}(2629)
│                 │                       ├─{containerd-shim}(2630)
│                 │                       ├─{containerd-shim}(2631)
│                 │                       ├─{containerd-shim}(2632)
│                 │                       ├─{containerd-shim}(2633)
│                 │                       ├─{containerd-shim}(2634)
│                 │                       ├─{containerd-shim}(2636)
│                 │                       └─{containerd-shim}(2678)
│                 ├─containerd-shim(2756)─┬─nginx(2776)───nginx(2823)
│                 │                       ├─{containerd-shim}(2757)
│                 │                       ├─{containerd-shim}(2758)
│                 │                       ├─{containerd-shim}(2759)
│                 │                       ├─{containerd-shim}(2760)
│                 │                       ├─{containerd-shim}(2761)
│                 │                       ├─{containerd-shim}(2762)
│                 │                       ├─{containerd-shim}(2763)
│                 │                       ├─{containerd-shim}(2765)
│                 │                       └─{containerd-shim}(2805)
│                 ├─{containerd}(906)
│                 ├─{containerd}(907)
│                 ├─{containerd}(909)
│                 ├─{containerd}(930)
│                 ├─{containerd}(931)
│                 ├─{containerd}(933)
│                 ├─{containerd}(934)
│                 ├─{containerd}(948)
│                 ├─{containerd}(2498)
│                 └─{containerd}(2827)
├─cron(794)
├─dbus-daemon(781)
├─dockerd(2207)─┬─docker-proxy(2622)─┬─{docker-proxy}(2623)
│               │                    ├─{docker-proxy}(2624)
│               │                    ├─{docker-proxy}(2625)
│               │                    └─{docker-proxy}(2626)
│               ├─docker-proxy(2750)─┬─{docker-proxy}(2751)
│               │                    ├─{docker-proxy}(2752)
│               │                    ├─{docker-proxy}(2753)
│               │                    ├─{docker-proxy}(2754)
│               │                    └─{docker-proxy}(2755)
│               ├─{dockerd}(2209)
│               ├─{dockerd}(2210)
│               ├─{dockerd}(2211)
│               ├─{dockerd}(2215)
│               ├─{dockerd}(2220)
│               ├─{dockerd}(2221)
│               ├─{dockerd}(2222)
│               ├─{dockerd}(2514)
│               └─{dockerd}(2540)
├─iscsid(837)
├─iscsid(839)
├─lvmetad(513)
├─lxcfs(780)─┬─{lxcfs}(783)
│            ├─{lxcfs}(784)
│            └─{lxcfs}(1512)
├─networkd-dispat(795)───{networkd-dispat}(925)
├─polkitd(808)─┬─{polkitd}(809)
│              └─{polkitd}(816)
├─rpc.idmapd(690)
├─rpc.mountd(751)
├─rpcbind(693)
├─rsyslogd(788)─┬─{rsyslogd}(791)
│               ├─{rsyslogd}(792)
│               └─{rsyslogd}(793)
├─snapd(798)─┬─{snapd}(823)
│            ├─{snapd}(826)
│            ├─{snapd}(827)
│            ├─{snapd}(828)
│            ├─{snapd}(873)
│            ├─{snapd}(882)
│            └─{snapd}(883)
├─sshd(881)───sshd(912)───bash(1240)───pstree(2900)
├─systemd(947)───(sd-pam)(958)
├─systemd-journal(481)
├─systemd-logind(797)
├─systemd-network(700)
├─systemd-resolve(730)
├─systemd-timesyn(691)───{systemd-timesyn}(746)
├─systemd-udevd(508)
└─vmtoolsd(489)
[root@ubuntu1804 ~]#ps aux|grep  -E "containerd|docker"
root        801  0.0  4.4 776680 43972 ?        Ssl  12:30   0:03 /usr/bin/containerd
root       2207  0.0  8.8 839016 86712 ?        Ssl  16:48   0:02 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root       2497  0.0  0.5 107696  5564 ?        Sl   17:33   0:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/d4741f815199a35c7e802662206160342d56125b47ec46d48a5f580759d86a6e -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runc
root       2622  0.0  0.4 405532  4128 ?        Sl   17:34   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8888 -container-ip 172.17.0.3 -container-port 80
root       2627  0.0  0.6 109104  6196 ?        Sl   17:34   0:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/5dee9be9afdbab8c2f6c4c5eb0f956c9579efe93110daf638f8fd15f43d961e2 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runc
root       2750  0.0  0.4 479264  4148 ?        Sl   17:38   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 6666 -container-ip 172.17.0.4 -container-port 80
root       2756  0.0  0.6 109104  6204 ?        Sl   17:38   0:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/d9e7f75cdb9d7d30f8febae30d72cdc4b6e96b0408fa998af6deb3937d5271ed -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runc
root       2899  0.0  0.1  14428  1100 pts/0    S+   17:51   0:00 grep --color=auto -E containerd|docker

18.06及之前的docker版本,进程关系:

Docker-服务进程管理插图
Docker-服务进程管理插图(1)

docker的进程关系

有四个进程:

dockerd:被client直接访问,其父进程为宿主机的systemd守护进程。

docker-proxy:每个进程docker-proxy实现对应一个需要网络通信的容器,管理宿主机和容器的之间端口映射,其父进程为dockerd,如果容器不需要网络则无需启动

containerd:被dockerd进程调用以实现与runc交互

containerd-shim:真正运行容器的载体,每个容器对应一个containerd-shim进程,其父进程为containerd

containerd-shim命令使用
[root@ubuntu1804 ~]#containerd-shim -h
Usage of containerd-shim:
-address string
grpc address back to main containerd
-containerd-binary containerd publish
path to containerd binary (used for containerd publish) (default "containerd")
-criu string
path to criu binary
-debug
enable debug output in logs
-namespace string
namespace that owns the shim
-runtime-root string
root directory for the runtime (default "/run/containerd/runc")
-socket string
abstract socket path to serve
-systemd-cgroup
set runtime to use systemd-cgroup
-workdir string
path used to storge large temporary data
容器的创建与管理过程

通信流程:

Docker-服务进程管理插图(2)

1.dockerd通过grpc和 containerd模块通信,dockerd由libcontainerd负责和containerd进行交换,dockerd和containerd 通信socket文件:/run/containerd/containerd.sock

2.containerd在dockerd启动时被启动,然后containerd启动grpc请求监听,containerd处理grpc请求,根据请求做相应动作

3.若是start或是exec 容器,containerd 拉起一个container-shim , 并进行相应的操作

4.container-shim别拉起后,start/exec/create拉起runC进程,通过exit、control文件和containerd通信,通过父子进程关系和SIGCHLD监控容器中进程状态

5.在整个容器生命周期中,containerd通过 epoll 监控容器文件,监控容器事件

grpc简介

Docker-服务进程管理插图(3)

gRPC是Google开发的一款高性能、开源和通用的 RPC 框架,支持众多语言客户端

官网:https://www.grpc.io/

podman 的进程结构

podman没有dockerd服务进程,所以当无容器启动时,无需启动任何进程,而容器启动时,会做为conmon的子进程

[root@centos8 ~]#podman version
Version:            1.4.2-stable2
RemoteAPI Version:  1
Go Version:         go1.12.8
OS/Arch:            linux/amd64
[root@centos8 ~]#podman run -d -p 80:80 docker.io/library/nginx
d8877293635c599a82ab5cb82c942cd86baf7c5810dd824154b15b0a88e76be8
[root@centos8 ~]#ss -tlnp
State   Recv-Q   Send-Q      Local Address:Port       Peer Address:Port                    LISTEN  0        128               0.0.0.0:80              0.0.0.0:*       users:(("conmon",pid=5173,fd=5))   
LISTEN  0        128               0.0.0.0:22              0.0.0.0:*       users:(("sshd",pid=687,fd=4))      
LISTEN  0        128                  [::]:22                 [::]:*       users:(("sshd",pid=687,fd=6))   
[root@centos8 ~]#pstree -p
systemd(1)─┬─NetworkManager(660)─┬─{NetworkManager}(680)
│                     └─{NetworkManager}(682)
├─VGAuthService(663)
├─agetty(805)
├─anacron(2793)
├─atd(799)
├─auditd(616)───{auditd}(617)
├─automount(816)─┬─{automount}(821)
│                ├─{automount}(822)
│                ├─{automount}(829)
│                └─{automount}(837)
├─conmon(5173)─┬─nginx(5183)───nginx(5194)
│              └─{conmon}(5175)
├─crond(797)
├─dbus-daemon(658)
├─polkitd(665)─┬─{polkitd}(679)
│              ├─{polkitd}(683)
│              ├─{polkitd}(694)
│              ├─{polkitd}(695)
│              └─{polkitd}(750)
├─rngd(661)───{rngd}(673)
├─rsyslogd(814)─┬─{rsyslogd}(818)
│               └─{rsyslogd}(820)
├─sshd(687)─┬─sshd(1166)───sshd(1243)───bash(1244)
│           └─sshd(1306)───sshd(1308)───bash(1309)───pstree(5198)
├─sssd(659)─┬─sssd_be(722)
│           └─sssd_nss(749)
├─systemd(1234)───(sd-pam)(1237)
├─systemd-journal(543)
├─systemd-logind(794)
├─systemd-udevd(575)
├─tuned(692)─┬─{tuned}(1080)
│            ├─{tuned}(1089)
│            └─{tuned}(1097)
└─vmtoolsd(664)───{vmtoolsd}(762)
[root@centos8 ~]#

本文链接:http://www.yunweipai.com/34769.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/52645.html

(0)
上一篇 2021年8月6日 16:42
下一篇 2021年8月6日 16:42

相关推荐

发表回复

登录后才能评论