Grafana监控PostgreSQL
1、下载postgres_exporter部署包
curl -L -O https://github.com/prometheus-community/postgres_exporter/releases/download/v0.15.0/postgres_exporter-0.15.0.linux-amd64.tar.gz
2、解压并拷贝命令
temdir=$(mktemp -d)
tar xvf postgres_exporter-0.15.0.linux-amd64.tar.gz -C ${temdir}
sudo cp ${temdir}/postgres_exporter-0.15.0.linux-amd64/postgres_exporter /usr/local/bin/
3、启动postgres_exporter服务
cat <<'EOF' | sudo tee /usr/lib/systemd/system/postgres_exporter.service >> /dev/null
[Unit]
Description=postgres_exporter
Documentation=https://github.com/prometheus-community/postgres_exporter
Wants=network.service
After=network.service
[Service]
Type=simple
User=postgres
Environment=DATA_SOURCE_USER=postgres
Environment=DATA_SOURCE_PASS=123456
Environment=DATA_SOURCE_URI=localhost:5432/postgres?sslmode=disable
ExecStart=/usr/local/bin/postgres_exporter --web.listen-address=:9187 --collector.postmaster --collector.stat_statements
ExecStop=/bin/kill -s SIGTERM $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable postgres_exporter.service --now
4、清理文件
sudo rm -rf ${temdir}
postgresql配置扩展
1、验证是否有编译pg_stat_statements扩展
ls /app/postgresql/lib/pg_stat_statements.so
2、编译pg_stat_statements扩展
cd ./postgresql-14.10/contrib/pg_stat_statements/
make && sudo make install
3、修改postgresql配置文件
grep shared_preload_libraries $PGDATA/postgresql.confshared_preload_libraries = 'pg_stat_statements' # (change requires restart)
sudo systemctl restart postgresql
4、验证是否有pg_stat_statements扩展
psql -h localhost -p 5432 -U postgres -W -c "select name from pg_available_extensions where name = 'pg_stat_statements';"
Password:
name
--------------------
pg_stat_statements
(1 row)
5、添加pg_stat_statements扩展
psql -h localhost -p 5432 -U postgres -W -c "CREATE EXTENSION pg_stat_statements;"
Prometheus和grafana配置
1、Prometheus采集postgres数据
kubectl -n kube-system edit cm prometheus
- job_name: "postgres-exporter"
static_configs:
- targets:
- "172.139.20.17:9187"
- "172.139.20.81:9187"
- "172.139.20.177:9187"
2.验证是否采集成功
curl -s $(kubectl -n kube-system get svc prometheus -ojsonpath='{.spec.clusterIP}:{.spec.ports[0].port}')/prometheus/api/v1/query --data-urlencode 'query=up{job="postgres-exporter"}'
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","instance":"172.139.20.17:9187","job":"postgres-exporter"},"value":[1731314524.901,"1"]},{"metric":{"__name__":"up","instance":"172.139.20.81:9187","job":"postgres-exporter"},"value":[1731314524.901,"1"]},{"metric":{"__name__":"up","instance":"172.139.20.177:9187","job":"postgres-exporter"},"value":[1731314524.901,"1"]}]}}
原文地址:https://blog.csdn.net/weixin_45916098/article/details/143932998
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!