目录
1. 前言
当prometheus对各类exporter(如node_exporter、mysql_exporter等)进行监控指标采集时,可可以通过手动配置 static_configs,一个一个实例进行添加配置(不适于大规模主机等环境,太累了),也可以通过prometheus的自动发现(sd_config),如consul、eureka、zookeeper、kubernetes、aws等方式实现动态发现服务。
为什么要用自动发现的功能呢?这里有一个场景,我们在阿里云上有100多台ecs,如果通过手动添加targets的话,需要一个一个录入,而且,当ecs不用时,还需要手动去删除,所以,这里需要一个功能,能自动发现阿里云上的所有ecs实例,随着ecs的增减而自动识别,但是查看官方prometheus源码发现,目前对公有云的自动发现,官方只支持aws和azure,并不支持国内的云
下面说说阿里云prometheus自动发现ecs的解决方案
2. 安装阿里云重编译后的Prometheus
由于官方prometheus没有针对国内的云做适配版本,这里用的是阿里云重编译的版本,安装方法如下
2.1 通过二进制方式安装
# 下载二进制
wget 'http://arms-public.oss-cn-hangzhou.aliyuncs.com/prometheus-community/ecs_sd/prometheus' -O prometheus
# 添加执行权限
chmod 755 ./prometheus
# 启动服务
./prometheus --config.file="/root/test/prometheus.yaml"
# 实测无法下载,403错误,这种方法略过,阿里云也不再维护了
2.2 通过Docker容器方式安装
# 创建配置文件目录
mkdir /root/aliprometheus
# 启动容器
docker run -d -p9090:9090 -v /root/aliprometheus:/etc/prometheus/ registry.cn-hangzhou.aliyuncs.com/public-community/prometheus-alibaba:v0.2
3. 配置Prometheus自动发现阿里云 ecs
配置如下
# 需要先创建prometheus.yml配置文件,否则容器启动会报错,没有配置文件
# vim /root/aliprometheus/prometheus.yml
# my global config
global:
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 configuration
#alerting:
# alertmanagers:
# - static_configs:
# - targets: ['localhost:9093']
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
#rule_files:
# - rules/*.yml
# - "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: aliyun-ecs-sd
honor_timestamps: true
scrape_interval: 30s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
aliyun_sd_configs:
- port: 9100
# user_id: 11111 # 可以不填
refresh_interval: 30s
region_id: cn-shenzhen # 可用区,一个Job只能获取一个可用区
access_key: '********' # accesskey,根据实际填写
access_key_secret: '********'
# tag_filters: # 标签过滤只想采集的ecs
# - key: 'test'
# values: ['prometheus']
#limit: 60 # 限制发现数量 ,不填表示所有
4. 配置说明
- region_id 可用区id,详细可参考 ECS地域和可用区列表:地域和可用区 - 云服务器 ECS - 阿里云
- tag_filters 标签过滤,比如只想采集指定的ecs,可以给ecs打标签,只采集标签为:prometheus="test"的ecs实例
这样就可以根据实例用途等功能进行分组标记,后续也可以根据分组标签进行路由分发告警
配置完成后重载下prometheus配置,打开http://localhost:9090/targets,就能看到自动发现的ecs实例了
5. 总结
通过promehteus自动发现功能,可以动态实现对ecs实例的数据采集,但是,在实际使用中也发现了一些bug
- 通过标签过滤的方式的,最多只能获取50个实例的ecs
- 阿里云已不再维护经阿里重编译后的prometheus版本,建议用官方saas产品
- 已停止更新的版本较低,为20191216-02:15:04
经过和官方客服的友好反馈后,官方把之前可以手动安装prometheus的文档连夜下架了
参考文档:Prometheus实例 for VPC - Prometheus监控 - 阿里云
原文章是关于怎么在自己的实例安装阿里云编译后的prometheus的方法,现在已经被删了,跳转的链接是阿里云saas prometheus产品
所以,由于阿里云官方不再支持这种安装方式,建议使用consul自动注册的方式或直接使用saas版的阿里云,这个看个人需求决定
有更好的方法的,欢迎一起研究探讨~