系统选型
实际线上环境在运行中对于开发人员基本上是一个黑盒,线上服务是什么情况都无从知晓,急需一个监控系统对系统的关键数据进行采集,存储,展示。结合实际情况,需要有一个轻量级,可以全平台通用,查阅相关资料最终定为Graphite+Grafana作为监控组件。
Graphite(石墨)是一个企业级的监控工具。有两种作用,存储数字时间序列数据,按需呈现此数据的图表,它不做数据收集。
github地址:https://github.com/graphite-project
Graphite由3部分组成:
- carbon --> 守护进程,监听时序数据
- Whisper --> 存储时间序列的简单数据库(在设计上类似于RPD)
- graphite webapp --> 用django开发的UI,用于图表的渲染,提供API被调用者做页面层集成
架构图:
Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知,主要有以下特点:
- 1、展示方式: 快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图,折线图、图表等多种展示方式
- 2、数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等
- 3、通知提醒: 以可视化定义最重要指标的警报规则,Grafana将不断计算并发送通知,在数据达到阈值通过Slack、PagerDuty等获得通知
- 4、混合展示: 在同一图表混合使用不同的数据源,可以基于每个查询指定数据源,甚至自定义数据源
- 5、注释: 使用来自不同数据源的丰富事件注释图表,将鼠标悬停在事件上会显示完整的事件元数据和标记
- 6、过滤器: Ad-hoc过滤器容许动态创建新的键/值过滤器,这些过滤器会自动应用于使用该数据源的所有查询
安装graphite
为了避免环境的问题,这里直接采用Docker方式进行安装
1、 下载镜像 https://github.com/graphite-project/docker-graphite-statsd
docker pull graphiteapp/graphite-statsd
2、此镜像包含了 Nginx、Graphite 、 Carbon 、 Statsd
3、端口映射
Linux端口 | 容器端口 | 服务 |
---|---|---|
80 | 80 | nginx端口 |
2003 | 2003 | carbon 接收端口 明文 |
2004 | 2004 | carbon 接收端口 批量 明文 (pickle协议是对明文协议的一种更有效的处理,支持一次性向Carbon发送成批的度量) |
2023 | 2023 | carbon 聚合器 明文 |
2024 | 2024 | carbon 聚合器 批量 明文 |
8080 | 8080 | Graphite端口 (没有nginx代理) |
8125 | 8125 | |
8126 | 8126 |
4、挂载卷
Linux | 容器 | 备注 |
---|---|---|
配置 | /opt/graphite/conf | graphite config |
配置 | /opt/graphite/storage | graphite stats storage |
配置 | /opt/graphite/webapp/graphite/functions/custom | graphite custom functions dir |
配置 | /etc/nginx | nginx config |
配置 | /opt/statsd/config | statsd config |
配置 | /etc/logrotate.d | logrotate config |
配置 | /var/log | log files |
配置 | /var/lib/redis | Redis TagDB data (optional) |
5、 启动容器
docker run -d\
--name graphite\
--restart=always\
-v /opt/graphite/conf:/opt/graphite/conf\
-v /opt/graphite/storage:/opt/graphite/storage\
-v /opt/graphite/log:/var/log
-p 80:80\
-p 2003-2004:2003-2004\
-p 2013-2014:2023-2024\
-p 8125:8125/udp\
-p 8126:8126\
graphiteapp/graphite-statsd
注: -v Linux本地路劲:容器内部路劲
6、启动验证
让我们用一个随机计数器来伪造一些统计数据,以证明一切正常。
while true; do echo -n "example:$((RANDOM % 100))|c" | nc -w 1 -u 127.0.0.1 8125; done
打开连接:
http://localhost/dashboard
http://localhost/render?from=-10mins&until=now&target=stats.example
管理员用户密码:
username: root
password: root
安装Grafana
https://grafana.com/grafana/download
wget https://dl.grafana.com/oss/release/grafana-6.3.5-1.x86_64.rpmsudo yum localinstall grafana-6.3.5-1.x86_64.rpm
Systemctl start grafana-server
#开放防火墙3000端口
配置Grafana
- 1、登陆Grafana后台 ,初始化帐户密码 admin:admin
- 2、添加数据源
-
3、选择Graphite数据源
-
4、添加数据源