11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker1.5の新機能[stats]を使用して、外部からリソース状況を拾ってみる。

Last updated at Posted at 2015-02-12

検証した環境

Docker Server: CentOS 7

statsについて

  • stats Display a live stream of one or more containers' resource usage statistics
    1つ以上のコンテナリソース状況をライブ表示する
  • 1秒間隔でデータを取得する
    感覚的にはUnixのtopコマンド

参考資料

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
  }
}
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?