docker-CAdvisor+InfluxDB+Granfana容器监控

一、前期准备:在Linux服务器上安装好docker,docker-compose

常用指令

列出运行中容器
docker ps 
列出所有容器,包括未运行的。
docker ps -a
进入容器
docker exec -it  /bin/bash
停止容器
docker stop  
后台运行docker-compose (第一次启动最好不用-d,因为可能有报错信息)
docker-compose up -d

二、使用docker安装influxdb

docker run -d --name influxdb -p 8086:8086  -v /data/influxdb:/var/lib/influxdb  --hostname=influxdb  influxdb
  1. 安装成功应显示
Unable to find image 'influxdb:latest' locally
latest: Pulling from library/influxdb
07471e81507f: Pull complete
c6cef1aa2170: Pull complete
13a51f13be8e: Pull complete
b6f021d1bd1c: Pull complete
ecf4e96afd5f: Pull complete
012b872e0ffa: Pull complete
fdfd321c83b8: Pull complete
2b9d42a4857a: Pull complete
3b93d6fb4c2d: Pull complete
Digest: sha256:ed884c6895724a4677de78e84df0246ced5d898216cd6d69332bebca2da22fd6  --hostname=influxdb  influxdb
Status: Downloaded newer image for influxdb:latest
f47c40a3ddbf444ddf873xxxxxxxxxxxxxxxxxxx47a33a427a3f03015894ced4

2.浏览器输入ip:8086即可看到一下页面


influxdb

2.1、 创建root用户和test用户(用于后期配置granfana)。
可以在页面上创建,也可以通过命令
docker exec -it /bin/bash
我这里容器名字就叫influxdb,所以运行一下命令即可进入docker里面

docker exec -it influxdb bin/bash 

create database "influxdb"
create user "root" with password 'rootroot' with all previleges
  1. cadvisor
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro --volume=/cgroup:/cgroup:ro \
--publish=8080:8080 --detach=true --privileged=true --name=cadvisor \
google/cadvisor:latest \
-storage_driver=influxdb-storage_driver_host=influxdb:8086 \
-storage_driver_db=cadvisor \
-storage_driver_user=root \
-storage_driver_password=root 

3.grafana

docker run -d -p 3000:3000 \
-v /data/grafana:/varlib/grafana \
--link=influxdb:influxdb \
--name grafana \
grafana/grafana 

三、使用docker-compoes安装CAdvisor+InfluxDB+Granfana

创建好docker-compose.yaml配置如下

version: '3.1'

volumes:
  grafana_data: {}

services:
 influxdb:
  image: tutum/influxdb:0.9
  #image: tutum/influxdb
  #image: influxdb
  restart: always
  #user: 
  environment:
    - PRE_CREATE_DB=cadvisor
  ports:
    - "8083:8083"
    - "8086:8086"
  expose:
    - "8090"
    - "8099"
  volumes:
    - ./data/influxdb:/data

 cadvisor:
  #image: google/cadvisor:v0.29.0
  image: google/cadvisor
  links:
    - influxdb:influxsrv
  command: -storage_driver=influxdb -storage_driver_db=cadvisor -storage_driver_host=influxsrv:8086
  restart: always
  ports:
    - "8080:8080"
  volumes:
    - /:/rootfs:ro
    - /var/run:/var/run:rw
    - /sys:/sys:ro
    - /var/lib/docker/:/var/lib/docker:ro

 grafana:
  #image: grafana/grafana:2.6.0
  user: "104"
  image: grafana/grafana
  user: "104"
  #user: "472"
  restart: always
  links:
    - influxdb:influxsrv
  ports:
    - "3000:3000"
  volumes:
    - grafana_data:/var/lib/grafana
  environment:
    - HTTP_USER=admin
    - HTTP_PASS=admin
    - INFLUXDB_HOST=influxsrv
    - INFLUXDB_PORT=8086
    - INFLUXDB_NAME=cadvisor
    - INFLUXDB_USER=root
    - INFLUXDB_PASS=root

验证配置是否正确
docker-compose config 
启动安装容器
docker-compose up
看到
 write to index of partition 1 took 5.399166ms
证明成功了
新开一个页面,运行
docker ps -a
应该可以看到
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS                      PORTS                                                                                                      NAMES
81984542671a   google/cadvisor      "/usr/bin/cadvisor -…"   8 seconds ago    Up 7 seconds                0.0.0.0:8080->8080/tcp, :::8080->8080/tcp                                                                  cadivsor-cadvisor-1
713ae988d3f4   grafana/grafana      "/run.sh"                8 seconds ago    Up 7 seconds                0.0.0.0:3000->3000/tcp, :::3000->3000/tcp                                                                  cadivsor-grafana-1
c6a712f78e8a   tutum/influxdb:0.9   "/run.sh"                8 seconds ago    Up 7 seconds                0.0.0.0:8083->8083/tcp, :::8083->8083/tcp, 8090/tcp, 0.0.0.0:8086->8086/tcp, :::8086->8086/tcp, 8099/tcp   cadivsor-influxdb-1

四、剩下的就是最麻烦的配置grafana界面了

请参考这篇文章:https://www.cnblogs.com/chanshuyi/p/03_grafana_chart_quick_start.html

最后附送几个查询的sql

上传下载流量(记得分开两个query写):
SELECT derivative(mean("value"), 1s) FROM "rx_bytes" WHERE ("container_name" = '/') AND $timeFilter GROUP BY time(1m) fill(null) 
SELECT derivative(mean("value"), 1s) FROM "tx_bytes" WHERE ("container_name" = '/') AND $timeFilter GROUP BY time(1m) fill(null) 

磁盘使用情况单位GB
SELECT mean("value") FROM "fs_usage" WHERE ("container_name" = '/') AND ("device" = '/dev/vda1') AND $timeFilter GROUP BY time($__interval) fill(null)

cpu占用率(16000000000 因为我是16核的cpu):
SELECT derivative(mean("value"), 1s)  / 16000000000 FROM "cpu_usage_total" WHERE ("container_name" = '/') AND $timeFilter GROUP BY time($__interval) fill(previous)

内存使用率百分比(32805847040 表示大概32G内存):
SELECT mean("value")  / 32805847040 FROM "memory_usage" WHERE ("container_name" = '/') AND $timeFilter GROUP BY time($__interval) fill(null)

最后poke一张效果图

效果图

当然你也可以在Grafana官网 -Grafana官网 - Dashboards 模块 找到你喜欢的现成模板。但能不能用就不一定了。

你可能感兴趣的:(docker-CAdvisor+InfluxDB+Granfana容器监控)