Prometheus监控pgsql

1、首先我们需要部署pgsql

这里我为了方便直接用yum安装了

#yum源
[root@localhost postgres_exporter]# cat /etc/yum.repos.d/influxdb.repo 
[influxdb]
name = InfluxDB Repository - RHEL $releasever
baseurl = https://repos.influxdata.com/rhel/$releasever/$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key


yum install postgresql-server -y
#安装完初始化数据库
postgresql-setup initdb
#进入数据库测试
sudo -u postgres psql
vim /var/lib/pgsql/data/pg_hba.conf
vim /var/lib/pgsql/data/postgresql.conf
systemctl start postgresql
ALTER USER 用户名 WITH PASSWORD 'xxx';

2、部署postgres_export (ps:这里我们之间容器启动,也可以二进制看个人爱好)

docker run -d \
--restart=always \
--name postgres_exporter \
--net=host \
-e DATA_SOURCE_NAME="postgresql://postgres:123@172.16.149.216:5432/postgres?sslmode=disable" \
-v /etc/localtime:/etc/localtime:ro \
quay.io/prometheuscommunity/postgres-exporter

3、编写prometheus的配置文件

#配置根据文件自动发现   
      - job_name: 'pg_export'
        file_sd_configs:
          - files:
            - /prometheus/pgsql.yml
        metrics_path: /metrics
        relabel_configs:
          - source_labels: [__address__]
            regex: (.*)
            target_label: instance
            replacement: $1
          - source_labels: [__address__]
            regex: (.*):(.*)
            target_label: __address__
            replacement: $1:9187            
#添加一个job
[root@master prometheus]# cat pgsql.yml 
- targets: [172.16.149.216:5432]
  labels:
    instance_id: 216pg
    alias: "test"
    host: "172.16.149.216"
    instance: '172.16.149.216:5432'


- targets: [172.16.149.217:5432]
  labels:
    instance_id: 216pg
    alias: "test"
    host: "172.16.149.217"
    instance: '172.16.149.217:5432'

自此部署完成可以在prometheus页面查看部署情况

posted @ 2022-05-22 11:34  Throb_JL  阅读(496)  评论(0编辑  收藏  举报