# Postgres Monitoring 目的: 用來 debug 解決 replication lag issue 先從 `slave` 開始監控,如果沒問題的話再加上 `master` New Components: `prometheus exporter`, `prometheus`, `Grafana` - `Grafana` 是一個常用的免費 GUI ,有很多別人做好的 dashboard 可以拿來改,背後可以接各種資料源,滿方便的 - `prometheus` 是一個 tsdb,是開源中滿多人在用的監控軟體,他會每 X 秒去抓取 metrics 並存下來,有很多別人寫好的 exporter (`flask` 也有) Alternative: `Datadog` 是需要付費使用的監控 service,非常好用!但缺點就是要錢 ### 步驟 (slave) 1. (opt.) 在 postgres 中開一個監控專用的 user ``` CREATE USER db_monitor WITH PASSWORD 'password'; GRANT pg_monitor TO db_monitor; ``` or 直接用現有的 `replication` user 來 monitor 2. 在 `/etc/postgresql/12/main/pg_hba.conf` 中加入 `db_monitor` 和 docker 的 entry ``` ... + host all db_monitor 172.18.0.4/16 md5 ``` 3. 讓 pg 吃到新的 config ``` postgres=# select pg_reload_conf(); ``` 4. 安裝 prometheus exporter 1. Create 一個 folder for prometheus exporter 並把 queries ``` > mkdir path/to/prometheus_exporter ``` 2. 修改 `queries.yml` 中的 query 讓 replication lag 的數值更精確 ``` pg_replication: query: "SELECT CASE WHEN (not pg_is_in_recovery()) or (pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn()) THEN 0 ELSE GREATEST (0, EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))) END AS lag" ``` 3. 用 docker 跑起 prometheus exporter ``` docker run \ -p 9187:9187 \ -d \ --name postgres_exporter \ -v /path/to/prometheus/queries.yml:/home/queries.yml \ -e PG_EXPORTER_EXTEND_QUERY_PATH="/home/queries.yml" \ -e DATA_SOURCE_NAME="postgresql://db_motnitor:password@$SLAVE_IP:5432/futures_web?sslmode=disable" \ quay.io/prometheuscommunity/postgres-exporter ``` 5. 安裝 prometheus 1. Create 一個 folder for prometheus ``` > mkdir path/to/prometheus ``` 2. 修改 `prometheus.yml` ``` 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). scrape_configs: - job_name: 'postgres_exp' static_configs: - targets: ["$EXPORTER_IP:9187"] ``` 3. 用 docker 跑起 ``` docker run \ -p 9090:9090 \ -d \ --name prometheus \ -v path/to/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus ``` 6. 安裝 Grafana ``` docker run -d --name=grafana -p 3000:3000 grafana/grafana ``` 7. 從 browser 查看 exporter/prometheus/grafana 有正常運作 8. 如果運行順利的話,可以開一個 repo 把 config 放進去 (或是放到現有的 repo 中) ### Metrics - 可以重點看 `replication lag` 的變化 - 看 `conflict` 發生的情況 - 看 `vacuum` 發生的情況
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up