dockerstats

package module
v0.0.0-...-a749207 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2020 License: MIT Imports: 10 Imported by: 0

README

dockerstats

The easy way to collect docker stats.

Q: why dockerstats?

A: 通常来讲,如果我们需要知道 docker 运行时的一些指标,如 CPU/MEM/IO 等信息,可以通过 docker stats 命令查看,对于 kubernetes 用户来讲,可能是 kubectl top pods。Google 为此开源了一个容器运行指标采集器 google/cadvisor,不过 cadvisor 跑起来相对占资源,且可能大部分指标用户其实都是不关心的,所以我觉得需要一个更轻量级的指标采集器。That's dockerstats.

指标说明

Name Desc
container_id 容器 ID
container_name 容器名称
cpu_usage_percentage CPU 使用率
memory_usage_in_bytes 已使用内存
memory_usage_percentage 内存占用百分比
memory_limit_in_bytes 内存限制
network_rx_in_bytes 网络接收
network_tx_in_bytes 网络发送
block_read_in_bytes 磁盘读
block_write_in_bytes 磁盘写
kubernetes_labels kubernetes 相关指标
kubernetes_labels.kubernetes_container_name k8s 容器名称
kubernetes_labels.kubernetes_pod_name k8s Pod 名称
kubernetes_labels.kubernetes_pod_namespace k8s Pod 命名空间

使用

本地开发构建

# go path
$ go get -u github.com/chenjiandongx/dockerstats/...

# go module => go.mod
require (
  github.com/chenjiandongx/dockerstats
)

Docker case

$ docker run -v /var/run/docker.sock:/var/run/docker.sock -p 8099:8099 -d chenjiandongx/dockerstats:latest
# 获取指标
$ curl -s http://localhost:8099/stats | jq

Kubernetes case

在 kubernetes 中情况可能稍微复杂了,由于 Service 本身是个负载均衡,所以要采集所有 Node 节点的话需要获取 Service 对应的 Endpoints 列表,然后循环遍历发起请求,或者可以根据自身业务情况,在 Pod 中主动上报数据到监控中心(如果有的话 🐶)。

Glances

License

MIT ©chenjiandongx

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultDockerClientOpts = []client.Opt{client.WithVersion("1.39")}

	// Kubernetes 往 docker label 注入的相关变量
	KubernetesLabels = map[string]string{
		"io.kubernetes.pod.namespace":  "kubernetes_pod_namespace",
		"io.kubernetes.pod.name":       "kubernetes_pod_name",
		"io.kubernetes.container.name": "kubernetes_container_name",
	}
)

Functions

This section is empty.

Types

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

Exporter 负责采集 docker 运行的实时相关指标

func NewExporter

func NewExporter(opts ...client.Opt) *Exporter

NewExporter 生成并返回 Exporter 实例

func (*Exporter) List

func (e *Exporter) List() ([]*StatsEntry, error)

List 通过 docker.ContainerStats 接口采集 docker stats 指标

func (*Exporter) Watch

func (e *Exporter) Watch()

Watch 监听 Docker 错误事件,并重新生成新实例

type StatsEntry

type StatsEntry struct {
	ContainerID      string  `json:"container_id"`
	ContainerName    string  `json:"container_name"`
	CPUPercentage    float64 `json:"cpu_usage_percentage"`
	Memory           float64 `json:"memory_usage_in_bytes"`
	MemoryPercentage float64 `json:"memory_usage_percentage"`
	MemoryLimit      float64 `json:"memory_limit_in_bytes"`
	NetworkRx        float64 `json:"network_rx_in_bytes"`
	NetworkTx        float64 `json:"network_tx_in_bytes"`
	BlockRead        float64 `json:"block_read_in_bytes"`
	BlockWrite       float64 `json:"block_write_in_bytes"`
	// Kubernetes 相关 labels 变量,仅在 k8s 集中中运行才能采集到
	KubernetesLabels map[string]string `json:"kubernetes_labels,omitempty"`
}

StatsEntry 定义了 Stats 的详细内容

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL