Prometheus对于不同的数据库,有各种专门的Exporter进行监控,本文将介绍基于Prometheus监控postgresql数据库的解决方案。
Postgresql数据库是一款热门的开源关系型数据库,目前已在不少企业场景中被广泛使用。
本文将介绍基于Prometheus监控postgresql数据库的解决方案。

一. Exporter安装与监控
Prometheus对于不同的数据库,有各种专门的Exporter进行监控。Exporter可以理解为监控的agent端,PrometheusServer端通过Exporter可获取监控目标的数据。
对于Postgresql数据库,目前使用的Exporter名称为Postgres-Exporter,GitHub仓库地址为:https://github.com/prometheus-community/postgres_exporter 。
该Exporter支持二进制和容器两种安装方式,此处我们使用Docker容器的方式进行安装。当容器启动时,需要以环境变量的方式传入对应的目标地址、以及账号和密码,如下:
docker run -d \ --name postgres-exporter \ --net=host \ --restart=always \ -e DATA_SOURCE_URI="localhost:5432/postgres?sslmode=disable" \ -e DATA_SOURCE_USER=postgres \ -e DATA_SOURCE_PASS=password \ quay.io/prometheuscommunity/postgres-exporter
Exporter的访问端口为9187,在部署完成后,Prometheus即可配置获取监控信息,如下:
scrape_configs: - job_name: postgres static_configs: - targets: ["127.0.0.1:9187"]
二. 多节点监控
目前Postgre-Exporters可支持多节点监控,即可以通过一个Exporter获取到多个Postgresql的监控信息。
1. 创建配置文件
该文件包含目标Postgresql实例的账号密码,为了配置方便,最好是对所有Postgresql实例创建统一的监控账号。
# postgres_exporter.ymlauth_modules: psql: # 自定义的认证模块名称 type: userpass userpass: username: postgres password: password options: sslmode: disable
2. 启动Exporter容器
docker run -d \ --name postgres-exporter \ --net=host \ --restart=always \ -v $(pwd)/postgres_exporter.yml:/postgres-exporter/config/postgres_exporter.yml \ quay.io/prometheuscommunity/postgres-exporter \ --config.file=/postgres-exporter/config/postgres_exporter.yml
3. Prometheus配置
scrape_configs: - job_name: 'postgres' static_configs: - targets: - postgresql-1:5432 #postgresql地址 - postgresql-2:5432 metrics_path: /probe params: auth_module: [psql] relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 127.0.0.1:9187 # Postgresql Exporter
