原则:
根据监控内容和场景,选择合适的工具
工具列表
Docker CLI
docker container stats命令显示容器资源的实时使用情况。
- 启动一个容器:docker container run –name db -d arungupta/couchbase
- 使用docker container stats db. 检查容器的状态,将显示如下信息
The output is continually updated. It shows:
- Container name
- Percent CPU utilization
- Total memory usage vs amount available to the container
- Percent memory utilization
- Network activity
- Disk activity
- PIDS??
3 : 再启动一个容器:docker container run -d –name web jboss/wildfly
4. 使用docker container stats db web. 命令,检查两个容器的统计信息。输出信息如下所示:
5. 查看所有容器的统计信息。docker container stats
请注意,在这种情况下显示容器ID,而不是容器的名称。
6. 仅使用命令docker container stats –format “{{.Container}}: {{.CPUPerc}}”:显示容器ID和百分比CPU利用率
88b04695855e: 0.14%
109d917d17e2: 4.83%
7: 格式化表中的输出。结果应包括容器名称,CPU利用率百分比和内存利用率。这可以使用命令来实现。docker container stats –format “table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}”:
NAME CPU % MEM USAGE / LIMIT
web 0.13% 266.1 MiB / 1.952 GiB
db 3.06% 398.9 MiB / 1.952 GiB
8.仅使用命令显示第一个结果 docker container stats –no-stream
Docker Remote API
Docker Remote API提供了有关容器运行状况的更多详细信息。可以使用以下格式调用它:
curl –unix-socket /var/run/docker.sock http://localhost/containers//stats
在Docker for Mac上,启用远程HTTP API仍然需要几个步骤。所以这个命令使用–unix-socket选项来调用Remote API。
使用特定的调用:
curl –unix-socket /var/run/docker.sock http://localhost/containers/db/stats。将会产生如下的输出:
{"read":"2017-02-28T02:40:29.595511475Z","preread":"0001-01-01T00:00:00Z","pids_stats":{"current":220},"blkio_stats":{"io_service_bytes_recursive":[{"major":254,"minor":0,"op":"Read","value":212992},{"major":254,"minor":0,"op":"Write","value":1339392},{"major":254,"minor":0,"op":"Sync","value":1257472},{"major":254,"minor":0,"op":"Async","value":294912},{"major":254,"minor":0,"op":"Total","value":1552384}],"io_serviced_recursive":[{"major":254,"minor":0,"op":"Read","value":3},{"major":254,"minor":0,"op":"Write","value":249},{"major":254,"minor":0,"op":"Sync","value":230},{"major":254,"minor":0,"op":"Async","value":22},{"major":254,"minor":0,"op":"Total","value":252}],"io_queue_recursive":[],"io_service_time_recursive":[],"io_wait_time_recursive":[],"io_merged_recursive":[],"io_time_recursive":[],"sectors_recursive":[]},"num_procs":0,"storage_stats":{},"cpu_stats":{"cpu_usage":{"total_usage":83724160991,"percpu_usage":[30641144914,10843586791,11798818901,30440610385],"usage_in_kernelmode":12390000000,"usage_in_usermode":15170000000},"system_cpu_usage":132730290000000,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"precpu_stats":{"cpu_usage":{"total_usage":0,"usage_in_kernelmode":0,"usage_in_usermode":0},"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"memory_stats":{"usage":419139584,"max_usage":426778624,"stats":{"active_anon":404185088,"active_file":20480,"cache":1589248,"dirty":12288,"hierarchical_memory_limit":9223372036854771712,"hierarchical_memsw_limit":9223372036854771712,"inactive_anon":0,"inactive_file":1568768,"mapped_file":122880,"pgfault":226379,"pgmajfault":2,"pgpgin":202886,"pgpgout":103818,"rss":404193280,"rss_huge":0,"swap":0,"total_active_anon":404185088,"total_active_file":20480,"total_cache":1589248,"total_dirty":12288,"total_inactive_anon":0,"total_inactive_file":1568768,"total_mapped_file":122880,"total_pgfault":226379,"total_pgmajfault":2,"total_pgpgin":202886,"total_pgpgout":103818,"total_rss":404193280,"total_rss_huge":0,"total_swap":0,"total_unevictable":0,"total_writeback":0,"unevictable":0,"writeback":0},"limit":2095898624},"name":"/db","id":"109d917d17e241713341b3d03470444c0144510f1e6de726eb72e1d6786a3e5d","networks":{"eth0":{"rx_bytes":3342,"rx_packets":57,"rx_errors":0,"rx_dropped":0,"tx_bytes":998,"tx_packets":13,"tx_errors":0,"tx_dropped":0}}}
如您所见,有关容器健康的更多细节如下所示。这些统计数据每隔一秒钟刷新一次。可以使用Ctrl + C终止连续刷新
Docker Events
docker system events,为Docker主机提供实时事件
- 在一个终端(T1),输入docker system events。他的命令不显示输出,并等待任何值得报告的事件发生。事件列表列在
https://docs.docker.com/engine/reference/commandline/events/#/extended-description - 在新的终端(T2)中,杀掉正在使用的容器。docker container rm -f web
- T1将更新的事件列表显示为:
2017-02-27T18:48:30.413053776-08:00 container kill 88b04695855ecf5390e57a6955a25f1ff507f7b066c2cd6397a5773a9e7e683f (build-date=20161214, image=jboss/wildfly, license=GPLv2, name=web, signal=9, vendor=CentOS)
2017-02-27T18:48:30.551760207-08:00 container die 88b04695855ecf5390e57a6955a25f1ff507f7b066c2cd6397a5773a9e7e683f (build-date=20161214, exitCode=137, image=jboss/wildfly, license=GPLv2, name=web, vendor=CentOS)
2017-02-27T18:48:30.954543362-08:00 network disconnect 83f14a590c9b8875cca8d050d47ec1e0dbff6db67180a56571496cadbe579e10 (container=88b04695855ecf5390e57a6955a25f1ff507f7b066c2cd6397a5773a9e7e683f, name=bridge, type=bridge)
2017-02-27T18:48:31.192092236-08:00 container destroy 88b04695855ecf5390e57a6955a25f1ff507f7b066c2cd6397a5773a9e7e683f (build-date=20161214, image=jboss/wildfly, license=GPLv2, name=web, vendor=CentOS)
输出显示事件列表,每行一个。这里显示的事件是container kill, container die, network disconnect and container destroy。每个事件的日期和时间戳记显示在行的开头。还会显示其他事件特定信息。
4. 在T2中,创建一个新的容器:docker container run -d –name web jboss/wildfly
5. T1中的输出更新为显示:
2017-02-27T18:49:24.218079500-08:00 container create 3cc3e2bf3c43e278e0e4bd2ea238a829610d0a620ab069010b4881c1bf8e096e (build-date=20161214, image=jboss/wildfly, license=GPLv2, name=web, vendor=CentOS)
2017-02-27T18:49:24.383788816-08:00 network connect 83f14a590c9b8875cca8d050d47ec1e0dbff6db67180a56571496cadbe579e10 (container=3cc3e2bf3c43e278e0e4bd2ea238a829610d0a620ab069010b4881c1bf8e096e, name=bridge, type=bridge)
2017-02-27T18:49:24.930142017-08:00 container start 3cc3e2bf3c43e278e0e4bd2ea238a829610d0a620ab069010b4881c1bf8e096e (build-date=20161214, image=jboss/wildfly, license=GPLv2, name=web, vendor=CentOS)
这里显示的事件列表是container create, network connect, and container start
在Event中 使用 filters
可以使用指定的过滤器限制事件列表,–filter或者是-f 选项。当前支持的过滤器有:
- container (container=)
- daemon (daemon=)
- event (event=)
- image (image=)
- label (label= or label==)
- network (network=)
- plugin (plugin=)
- type (type=)
- volume (volume=)
我们来使用这些过滤器:
- 按名称显示容器的事件
在T1中,输入: docker system events -f container=db.
在T2中 输入: docker container rm -f web 来终止web 这个容器
但是在T1中,并没有任何更新,因为他仅仅是监听了db这个容器
2 . 按照事件的类型显示事件
- 在 T1, 输入命令: docker system events -f event=create.
- In T2, 创建一个容器: docker container run -d –name web2
jboss/wildfly - T1 显示创建了一个容器的消息
2017-02-28T12:55:45.631795937-08:00 container create 4728dab7c27816351423d64e60adf21a0246c1006b1131655d8b66fc82e8b324 (build-date=20161214, image=jboss/wildfly, license=GPLv2, name=dreamy_lamport, vendor=CentOS)
- 在T2中终止一个容器:docker container rm -f web2
- T1不显示任何其他事件,因为它只寻找创建事件
- 更多的示例请移步:
https://docs.docker.com/engine/reference/commandline/events/#/filter-events-by-criteria.
未完,请移步下一篇博客
Docker实战系列——第三话–docker 监控(三)– how could we monitoring(2)?