Prometheus + Grafana 应用级监控方案(2)-安装及初始化
概述
在监控系统建设过程中,建议能使用Docker部署的服务,尽量用Docker部署,包括监控系统本身(Prometheus/Grafana/InfluxDB)及众多的采集器(export)。其带来的好处:
- 便利性:无需复杂的编译、安装过程,即下即用,支持离线安装
- 安全性:虚拟化后与宿主机及其它容器隔离
- 移植性:不同的Linux发行版、Windows跨系统基本无影响
- 持续集成: 为后续的持续集成、自动化部署带来较多的方便之处
注:本文以CentOS 7 为基础环境安装
Docker安装参考
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum makecache fast
sudo yum install docker-ce -y
sudo systemctl start docker
sudo docker info
Prometheus 安装参考
docker pull prom/prometheus
mkdir -p /data/docker-lv/prometheus/etc
mkdir -p /data/docker-lv/prometheus/data
docker run --name tmp-prometheus --rm prom/prometheus
#开一个新窗口,提取文件,其中配置与数据为宿主机映射
docker cp tmp-prometheus:/etc/prometheus/prometheus.yml /data/docker-lv/prometheus/etc/.
#中止tmp-prometheus(ctrl+C)
docker run -d --name prometheus \
-p 9090:9090 \
-v /data/docker-lv/prometheus/etc:/etc/prometheus/ \
-v /data/docker-lv/prometheus/data:/prometheus-data \
prom/prometheus
#查看启动日志
docker logs prometheus
#浏览器打开
http://host:9090
Grafana 安装参考
docker pull grafana/grafana
mkdir -p /data/docker-lv/grafana/data
mkdir -p /data/docker-lv/grafana/etc
#chmod 777 /data/docker-lv/grafana/data
chown 472:472 /data/docker-lv/grafana/*
docker run --name tmp-grafana --rm grafana/grafana
#开一个新窗口,提取文件,其中配置与数据为宿主机映射
docker cp tmp-grafana:/etc/grafana/grafana.ini /data/docker-lv/grafana/etc/.
chown 472:472 -R /data/docker-lv/grafana/*
#中止tmp-prometheus(ctrl+C)
docker run -d --name=grafana \
-p 3000:3000 \
-v /data/docker-lv/grafana/etc/grafana.ini:/etc/grafana/grafana.ini \
-v /data/docker-lv/grafana/data:/var/lib/grafana \
grafana/grafana
#查看启动日志
docker logs grafana
#浏览器打开
http://host:3000
admin/admin
Grafana 第一个Dashboard
- 新建数据源,类型为 Prometheus, 地址为 http://xxx.xxx.x.xx:9090
- 新建Dashboard,Prometheus已有数据,可用其制作图表
- 以下为样例Dashboard, 菜单 --> + --> Import --> JSON 粘贴以下JSON字符串
{"annotations":{"list":[{"builtIn":1,"datasource":"-- Grafana --","enable":true,"hide":true,"iconColor":"rgba(0, 211, 255, 1)","name":"Annotations & Alerts","type":"dashboard"}]},"editable":true,"gnetId":null,"graphTooltip":0,"id":1,"links":[],"panels":[{"cacheTimeout":null,"colorBackground":false,"colorValue":false,"colors":["#299c46","rgba(237, 129, 40, 0.89)","#d44a3a"],"datasource":"Prometheus","decimals":2,"format":"s","gauge":{"maxValue":100,"minValue":0,"show":false,"thresholdLabels":false,"thresholdMarkers":true},"gridPos":{"h":2,"w":6,"x":0,"y":0},"id":4,"interval":null,"links":[],"mappingType":1,"mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"options":{},"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","rangeMaps":[{"from":"null","text":"N/A","to":"null"}],"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":false,"lineColor":"rgb(31, 120, 193)","show":false,"ymax":null,"ymin":null},"tableColumn":"","targets":[{"expr":"time() - process_start_time_seconds","refId":"A"}],"thresholds":"","timeFrom":null,"timeShift":null,"title":"运行时间","type":"singlestat","valueFontSize":"80%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg"},{"cacheTimeout":null,"colorBackground":false,"colorPostfix":false,"colorValue":false,"colors":["#299c46","rgba(237, 129, 40, 0.89)","#d44a3a"],"datasource":"Prometheus","format":"decbytes","gauge":{"maxValue":100,"minValue":0,"show":false,"thresholdLabels":false,"thresholdMarkers":true},"gridPos":{"h":2,"w":5,"x":6,"y":0},"id":6,"interval":null,"links":[],"mappingType":1,"mappingTypes":[{"name":"value to text","value":1},{"name":"range to text","value":2}],"maxDataPoints":100,"nullPointMode":"connected","nullText":null,"options":{},"postfix":"","postfixFontSize":"50%","prefix":"","prefixFontSize":"50%","rangeMaps":[{"from":"null","text":"N/A","to":"null"}],"sparkline":{"fillColor":"rgba(31, 118, 189, 0.18)","full":false,"lineColor":"rgb(31, 120, 193)","show":true,"ymax":null,"ymin":null},"tableColumn":"","targets":[{"expr":"process_resident_memory_bytes","refId":"A"}],"thresholds":"","timeFrom":null,"timeShift":null,"title":"内存使用","type":"singlestat","valueFontSize":"80%","valueMaps":[{"op":"=","text":"N/A","value":"null"}],"valueName":"avg"},{"aliasColors":{},"bars":false,"dashLength":10,"dashes":false,"datasource":"Prometheus","decimals":0,"fill":1,"fillGradient":0,"gridPos":{"h":7,"w":24,"x":0,"y":2},"hiddenSeries":false,"id":2,"legend":{"alignAsTable":true,"avg":false,"current":true,"max":true,"min":false,"rightSide":true,"show":true,"total":false,"values":true},"lines":true,"linewidth":1,"nullPointMode":"null","options":{"dataLinks":[]},"percentage":false,"pointradius":2,"points":false,"renderer":"flot","seriesOverrides":[],"spaceLength":10,"stack":false,"steppedLine":false,"targets":[{"expr":"go_goroutines","legendFormat":"{{instance}}","refId":"A"}],"thresholds":[],"timeFrom":null,"timeRegions":[],"timeShift":null,"title":"协程数","tooltip":{"shared":true,"sort":0,"value_type":"individual"},"type":"graph","xaxis":{"buckets":null,"mode":"time","name":null,"show":true,"values":[]},"yaxes":[{"format":"short","label":null,"logBase":1,"max":null,"min":null,"show":true},{"format":"short","label":null,"logBase":1,"max":null,"min":null,"show":true}],"yaxis":{"align":false,"alignLevel":null}}],"schemaVersion":21,"style":"dark","tags":[],"templating":{"list":[]},"time":{"from":"now-6h","to":"now"},"timepicker":{},"timezone":"","title":"prometheus-view","uid":"HbfbwzBWk","version":5}
- 显示效果如下图所示
小结
- Prometheus + Grafana 安装&使用都比较方便,很快可以搭建一个看上去还不错的运行界面
- 上手很快,无需编码
- 下一节介绍一个典型的应用监控实现过程- Prometheus+Grafana+Redis监控