検証した環境
Docker Server: CentOS 7
statsについて
- stats Display a live stream of one or more containers' resource usage statistics
1つ以上のコンテナリソース状況をライブ表示する - 1秒間隔でデータを取得する
感覚的にはUnixのtopコマンド
参考資料
- docker reference
https://docs.docker.com/reference/commandline/cli/#stats
https://docs.docker.com/reference/api/docker_remote_api_v1.17/
dockerコマンドで取得した場合
# docker stats cadvisor
CONTAINER CPU % MEM USAGE/LIMIT MEM % NET I/O
cadvisor 1.66% 25.3 MiB/3.704 GiB 0.67% 11.23 MiB/27.22 MiB
簡易的はビューには重宝しますね。
外部からAPIを使用して取得する場合
- docker confに外部接続許可設定を加える
今回使用したのは2375/tcp ポート。
/etc/sysconfig/docker
# Modify these options if you want to change the way the docker daemon runs
# OPTIONS=--selinux-enabled -H fd://
OPTIONS=--selinux-enabled -H fd:// -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp
docker 再起動
# systemctl restart docker.service
ポート開放
# firewall-cmd --add-port=2375/tcp
コンテナのステータスを取得する
$ curl -X GET http://127.0.0.1:2375/containers/[container id]/stats
上記で取得した場合、延々とステータス情報が垂れ流されるので、運用データとしての収集には不向きかなと。
そこで、タイムアウトを1秒としてjqを使用しjsonデータを加工してみた。
-
cpuのステータス情報を表示する場合
$ curl -X GET http://127.0.0.1:2375/containers/[container id]/stats --max-time 1 | jq '.cpu_stats' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1961 0 1961 0 0 1210 0 --:--:-- 0:00:01 --:--:-- 1210 curl: (28) Operation timed out after 1620 milliseconds with 1961 out of -1 bytes received { "throttling_data": { "throttled_time": 0, "throttled_periods": 0, "periods": 0 }, "system_cpu_usage": 1.01798489e+15, "cpu_usage": { "usage_in_usermode": 1.0411e+11, "usage_in_kernelmode": 5.916e+10, "percpu_usage": [ 99302252799, 101617606123 ], "total_usage": 200919858922 } }
必要に応じてこのデータを収集ツールに食べさせるスクリプトを組めば、運用がはかどりますね。
※ timed outの表示が不細工なので何とかしないと。まぁ、実データとしてはそっから下だし良いか。
追記(解決)
** -s オプションを加える事で簡単に消せました( 2015/02/18 ) **
# curl -s -X GET http://127.0.0.1:2375/containers/[container id]/stats --max-time 1 | jq '.cpu_stats'
{
"throttling_data": {
"throttled_time": 0,
"throttled_periods": 0,
"periods": 0
},
"system_cpu_usage": 2.03790306e+15,
"cpu_usage": {
"usage_in_usermode": 5.0961e+11,
"usage_in_kernelmode": 3.5196e+11,
"percpu_usage": [
445562955080,
439404240147
],
"total_usage": 884967195227
}
}