容器的运行状态获取

2016/10/26 12:28 pm posted in  Docker

获得运行状态的两种方式

获得容器的运行状态主要有两种方法,一种是通过docker cli提供的stats命令查看容器的状态。另一种就是通过docker api

docker stats

通过docker stats可以获得所有的容器的状态:

docker api

默认情况下,Docker daemon监听unix://var/run/docker.sock,并且客户端必须有root权限用来与daemon交互。

为了使用Docker REST API,可以修改docker配置,添加-H标记开启远程访问:

DOCKER_OPTS="$DOCKER_OPTS -H=0.0.0.0:4232"

此处开启远程访问只是方便测试,但会因为没有添加权限验证留下安全隐患

然后就可以用过docker api与docker daemon交互:

root@ubuntu:~# curl localhost:4232/version | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   199  100   199    0     0  19638      0 --:--:-- --:--:-- --:--:-- 19900
{
    "ApiVersion": "1.23",
    "Arch": "amd64",
    "BuildTime": "2016-06-01T21:47:50.269346868+00:00",
    "GitCommit": "b9f10c9",
    "GoVersion": "go1.5.4",
    "KernelVersion": "3.16.0-30-generic",
    "Os": "linux",
    "Version": "1.11.2"
}

而通过remote api获得容器状态比较简单,只需要通过 /containers/<id or name>/stats 便可以获得容器状态。而且这个api会每秒一次的返回最新状态。

当然,如果只是想获得一次状态也是很简单的,这个api支持一个stream参数,支持 True/true/1False/false/0 两种只,前者会让这个api每秒返回一次状态,后者只会让这个api返回一次。默认是前者。

root@ubuntu:~# curl localhost:4232/containers/d81984a3c60d/stats?stream=0 | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1592  100  1592    0     0   1018      0  0:00:01  0:00:01 --:--:--  1018
{
    "blkio_stats": {
        "io_merged_recursive": [],
        "io_queue_recursive": [],
        "io_service_bytes_recursive": [],
        "io_service_time_recursive": [],
        "io_serviced_recursive": [],
        "io_time_recursive": [],
        "io_wait_time_recursive": [],
        "sectors_recursive": []
    },
    "cpu_stats": {
        "cpu_usage": {
            "percpu_usage": [
                348564780
            ],
            "total_usage": 348564780,
            "usage_in_kernelmode": 10000000,
            "usage_in_usermode": 0
        },
        "system_cpu_usage": 3000540000000,
        "throttling_data": {
            "periods": 0,
            "throttled_periods": 0,
            "throttled_time": 0
        }
    },
    "memory_stats": {
        "failcnt": 0,
        "limit": 1041981440,
        "max_usage": 6668288,
        "stats": {
            "active_anon": 6610944,
            "active_file": 4096,
            "cache": 32768,
            "hierarchical_memory_limit": 18446744073709551615,
            "inactive_anon": 12288,
            "inactive_file": 0,
            "mapped_file": 0,
            "pgfault": 1133,
            "pgmajfault": 0,
            "pgpgin": 669,
            "pgpgout": 584,
            "rss": 6594560,
            "rss_huge": 6291456,
            "total_active_anon": 6610944,
            "total_active_file": 4096,
            "total_cache": 32768,
            "total_inactive_anon": 12288,
            "total_inactive_file": 0,
            "total_mapped_file": 0,
            "total_pgfault": 1133,
            "total_pgmajfault": 0,
            "total_pgpgin": 669,
            "total_pgpgout": 584,
            "total_rss": 6594560,
            "total_rss_huge": 6291456,
            "total_unevictable": 0,
            "total_writeback": 0,
            "unevictable": 0,
            "writeback": 0
        },
        "usage": 6627328
    },
    "networks": {
        "eth0": {
            "rx_bytes": 2592,
            "rx_dropped": 0,
            "rx_errors": 0,
            "rx_packets": 32,
            "tx_bytes": 648,
            "tx_dropped": 0,
            "tx_errors": 0,
            "tx_packets": 8
        }
    },
    "pids_stats": {},
    "precpu_stats": {
        "cpu_usage": {
            "percpu_usage": [
                347925623
            ],
            "total_usage": 347925623,
            "usage_in_kernelmode": 10000000,
            "usage_in_usermode": 0
        },
        "system_cpu_usage": 2999540000000,
        "throttling_data": {
            "periods": 0,
            "throttled_periods": 0,
            "throttled_time": 0
        }
    },
    "read": "2016-10-08T00:05:40+08:00"
}