简体   繁体   English

从docker容器进行主机监控

[英]Host monitoring from a docker container

While I believe the answer is no, I feel I should still ask: is it possible to monitor a host system from within a Docker container? 虽然我认为答案是否定的,但我觉得我仍然应该问:是否可以从Docker容器中监控主机系统? To make deployments and upgrades easier, I was hoping I could put some monitoring tools inside a container. 为了使部署和升级更容易,我希望我可以在容器中放置一些监视工具。 Specifically, I'm thinking tools like atop, sar, etc. 具体来说,我正在考虑像atop,sar等工具。

Thoughts? 思考?

Thanks. 谢谢。

The Docker philosophy of isolation can be circumvented by mounting host directories into the container (as Datadog client does, for example) or running a container in " privileged " container mode. Docker的隔离理念可以通过将主机目录挂载到容器中(例如,如Datadog客户端所做)或以“ 特权 ”容器模式运行容器来规避。 This prevents pid/network/ipc/disk/uts namespacing, allowing access to all devices and effectively launching the process as if it were on the host. 这可以防止pid / network / ipc / disk / uts命名空间,允许访问所有设备并有效地启动进程,就像它在主机上一样。

These tools are invaluable when running on an immutable host system such as CoreOS. 在不可变的主机系统(如CoreOS)上运行时,这些工具非常有用。

But priviledged mode is not necessary if you only want access to certain parts of the host machine. 但如果您只想访问主机的某些部分,则不需要特权模式。 For example Datadog currently launches its agent ("monitoring container") with these flags (specific to its monitoring requirements): 例如,Datadog当前使用这些标志(特定于其监视要求)启动其代理 (“监视容器”):

docker run -d --name dd-agent -h `hostname` \
  -v /var/run/docker.sock:/var/run/docker.sock -v /proc/:/host/proc/:ro \
  -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro -e API_KEY={your_api_key_here} \
  datadog/docker-dd-agent

(notice the volume mounts giving read-only access to the hosts proc and cgroup directories, as well as the docker socket [to monitor the daemon]) (注意卷安装提供对主机proccgroup目录的只读访问,以及docker socket [监视守护进程])

Sysdig Cloud requires privileged mode, because it has far deeper system introspection capabilities, whilst also mounting device, process, boot, modules and user directories: Sysdig Cloud需要特权模式,因为它具有更深入的系统内省功能,同时还可以安装设备,进程,引导,模块和用户目录:

docker run --name sysdig-agent --privileged --net host --pid host \
  -e ACCESS_KEY=[ACCESS_KEY] -e TAGS=[TAGS] \
  -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev \
  -v /proc:/host/proc:ro -v /boot:/host/boot:ro \
  -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro sysdig/agent

It is also possible to add and revoke individual capabilities using --cap-add and --cap-drop . 也可以使用--cap-add--cap-drop 添加和撤消各个功能

CoreOS provide a toolbox script (distinct from the new docker-toolbox ) to launch this style of container for you using systemd-nspawn instead of docker - they both run containers. CoreOS提供了一个toolbox脚本(来自新的唯一泊坞窗工具箱 )推出容器的这种风格你使用systemd-nspawn代替docker -它们都运行容器。

systemd-nspawn has different syntax to Docker, but the effect is still the same - the host system is shared with the container ( source ): systemd-nspawn与Docker的语法不同,但效果仍然相同 - 主机系统与容器( )共享:

sudo systemd-nspawn \
  --directory="${machinepath}" \
  --capability=all \
  --share-system \
  --bind=/:/media/root \
  --bind=/usr:/media/root/usr \
  --bind=/run:/media/root/run \
  --user="${TOOLBOX_USER}" "$@"

In summary, you can launch a container and install debugging tools that can inspect the host (and by extension, other containers) by using Docker with specific volume mounts and/or --privileged , or CoreOS's toolbox . 总之,您可以通过使用具有特定卷装入和/或 - --privileged--privileged 工具箱的 Docker来启动容器并安装可以检查主机(以及扩展名,其他容器)的调试工具。


nb my personal preference for debugging containers is Sysdig : "Think about sysdig as strace + tcpdump + htop + iftop + lsof + ...awesome sauce." 我个人对调试容器的偏好是Sysdig :“把sysdig想象成strace + tcpdump + htop + iftop + lsof + ......真棒酱。” - which currently looks like: - 目前看起来像:

docker run -i -t --name sysdig --privileged \
  -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev \
  -v /proc:/host/proc:ro -v /boot:/host/boot:ro \
  -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro sysdig/sysdig

Please take a look at cadvisor , a tool from google. 请看一下来自谷歌的工具cadvisor

cadvisor mounts /sys and /var/run/ and is therefore able to monitor the host. cadvisor mounts /sys/var/run/因此能够监视主机。

The Docker container should not be aware of the host, so this is against the Docker/process isolation philosophy. Docker容器不应该知道主机,因此这违反了Docker /进程隔离原则。 You may find some tricks in order to do so, but this is not recommended. 您可能会发现一些技巧,但不建议这样做。

One thing that I've found that you can do is capture file system events that happen in the host using inotify. 我发现你可以做的一件事是捕获使用inotify在主机中发生的文件系统事件。 I use that in my inotify-command container. 我在我的inotify-command容器中使用它。 But who knows if that will last... 但谁知道这会持续......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM