欢迎访问我的 GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览
回顾操作
前文中,我们有三次操作:
执行以下命令,用于创建所有容器:
wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/prometheus.yml && \wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/docker-compose.yml && \docker-compose up -d
复制代码
wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/import_dashboard.sh && \chmod a+x import_dashboard.sh && \./import_dashboard.sh 192.168.1.101 xxxxxx
复制代码
prometheus 的配置文件 prometheus.yml
# my global configglobal: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s).
# Alertmanager configurationalerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: # - "first_rules.yml" # - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus'
# metrics_path defaults to '/metrics' # scheme defaults to 'http'.
static_configs: - targets: ['127.0.0.1:9090','node-exporterhost:9100','cadvisorhost:8080']
- job_name: 'proemtheusdemo'
# metrics_path defaults to '/metrics' # scheme defaults to 'http'. metrics_path: '/prometheus' static_configs: - targets: ['prometheusdemohost:8080']
复制代码
这是个普通的 prometheus 配置文件,除了监控自身的 9090 端口,还有 node-exporterhost、cadvisorhost、prometheusdemohost 这三个 host 的不同端口,分别对应着宿主机自身、docker 服务、业务 web 服务等三个监控数据源;
看了上面的配置,您可能会疑惑:node-exporterhost、cadvisorhost、prometheusdemohost 它们代表什么呢?配置项的那个位置应该填写具体的 IP 地址才对。
其实,这个 prometheus.yml 文件是给 Docker 容器中的 prometheus 服务使用的,而 node-exporter、cadvisor、业务 web 服务也分别运行在各自的 Docker 容器中,prometheus 服务想要访问这些容器,最好的方法就是 link 参数,因此 node-exporterhost、cadvisorhost、prometheusdemohost 都是给 prometheus 容器配置的 link 参数,这就相当于 docker 给 prometheus 容器的/etc/hosts 文件写入了上述三个容器的 ip,对应的 name 就是 node-exporterhost、cadvisorhost、prometheusdemohost,稍后在 docker-compose.yml 文件中可以看到;
容器编排文件 docker-compose.yml
version: '2'services: node-exporter: image: prom/node-exporter:v0.17.0-rc.0 container_name: node-exporter restart: unless-stopped ports: - '9100:9100' command: - '--path.procfs=/host/proc' - '--path.sysfs=/host/sys' - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)' - '--collector.textfile.directory=/node_exporter/prom' volumes: - /proc:/host/proc - /sys:/host/sys - /:/rootfs - ./etc/node_exporter/prom:/node_exporter/prom cadvisor: image: google/cadvisor:v0.28.0 container_name: cadvisor depends_on: - node-exporter volumes: - /:/rootfs:ro - /var/run:/var/run:rw - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro ports: - "8080:8080" restart: unless-stopped prometheusdemo: image: bolingcavalry/prometheusdemo:0.0.1-SNAPSHOT container_name: prometheusdemo ports: - "8081:8080" restart: unless-stopped prometheus: image: prom/prometheus:v2.8.0-rc.0 container_name: prometheus depends_on: - node-exporter links: - node-exporter:node-exporterhost - cadvisor:cadvisorhost - prometheusdemo:prometheusdemohost volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml ports: - "9090:9090" restart: unless-stopped grafana: image: grafana/grafana:5.4.2 container_name: grafana links: - prometheus environment: - GF_SERVER_ROOT_URL=http://grafana.server.name - GF_SECURITY_ADMIN_PASSWORD=secret - GF_USERS_ALLOW_SIGN_UP=false depends_on: - prometheus ports: - "3000:3000" restart: unless-stopped
复制代码
a. 为了采集到宿主机的数据,node-exporter、cadvisor 这两个容器通过数据卷参数将宿主机的目录映射到容器中,这在生产环境是要严格控制的,不要将重要的目录轻易暴露给未经校验的容器,例如一个恶意的镜像被 pull 到本地,然后通过 docker tag 命令把名称改成了 node-exporter、cadvisor;
b. prometheus 容器的配置中使用了 link 参数,这样就能用 node-exporterhost 这样的名称直接访问到 node-export 容器了;
c. prometheus 容器通过数据卷映射参数,将宿主机的 prometheus.yml 映射到容器中,这样我们只要配置好当前目录下的 prometheus.yml 文件,就可以直接在 prometheus 容器生效了(如果容器已经启动后再次修改了此文件,要使用 docker restart prometheus 命令重启容器才能生效);
d. prometheusdemo 是基于 springboot 开发的一个 web 服务,对外提供一个接口,通过 ports 参数将容器的 8080 和宿主机的 8081 端口映射;
e. grafana 容器的环境变量 GF_SECURITY_ADMIN_PASSWORD=secret,表示 Grafana 的 web 网页用 admin 账号登录时,密码是 secret;
通过 import_dashboard.sh 脚本在 Grafana 创建数据源和监控项
#!/bin/bash#第一个参数作为Grafana服务器的IP地址GRAFANA_HOST=$1#第二个参数作为身份鉴权的API KeyAPI_KEY=$2
echo "grafana host ["${GRAFANA_HOST}"]"echo "api key ["${API_KEY}"]"
echo "start create datasource"
#通过curl工具发起一个POST请求,用来创建数据源curl -X POST \ http://${GRAFANA_HOST}:3000/api/datasources \-H "Content-Type:application/json" \-H "Authorization: Bearer ${API_KEY}" \-d '{"name":"Prometheus","type":"prometheus","url":"http://prometheus:9090","access":"proxy","basicAuth":false}' \
echo ""echo "start create host dashboard"
#通过curl工具发起一个POST请求,用来创建一个dashboard,也就是前文中我们看到的反映宿主机CPU、磁盘等基本状况的监控页面curl -X POST \ http://${GRAFANA_HOST}:3000/api/dashboards/db \ -H 'Accept: application/json' \ -H "Authorization: Bearer ${API_KEY}" \ -H 'Content-Type: application/json' \ -H 'Postman-Token: 2d3c3d60-4c5a-4936-836f-1572d447f473' \ -H 'cache-control: no-cache' \ -d '{ "dashboard": { "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, ... ... ...
复制代码
以上只是部分内容,由于篇幅所限就不贴出整个脚本了,Grafana 提供了丰富的 http api 给我们调用,帮用户实现更多的自定义和自动化配置,详情请参考官方文档:http://docs.grafana.org/http_api
您可能会有疑问:上述脚本中,每个 dashboard 的配置信息多达数百行甚至上千行,是怎么做出来的?其实监控项一般都是网页上配置出来的,这些内容是将做好的监控项以 json 格式导出的内容,操作如下图:
得到上述 json 内容,我们就可以很方便的将自己配置好的监控项导入导出了;
至此,整个监控环境的编排和构建详情已经全部分析过了,相信您已经了解如何构建一个小而完整的监控系统,接下来的文章中,我们一起来实战自定义监控项的开发流程,这样就能通过 prometheus 和 Grafana 来展现个性化的业务数据了,请看《Docker 下 Prometheus 和 Grafana 三部曲之三:自定义监控项开发和配置》
欢迎关注 InfoQ:程序员欣宸
学习路上,你不孤单,欣宸原创一路相伴...
评论