Postgresql的集群搭建与监控方案

转载说明:如果您喜欢这篇文章并打算转载它,请私信作者取得授权。感谢您喜爱本文,请文明转载,谢谢。


 相关文章:

PostgreSQL学习之部署与简单使用

PostgreSQL的常见错误和解决方法

如果PostgreSQL有两层nginx代理,会发生什么事?

前言

之前在《PostgreSQL学习之部署与简单使用》记录了PostgreSQL的搭建和简单使用,本文记录PostgreSQL集群的搭建,接入Prometheus监控(Prometheus监控部署在k8s集群内)。

1. PostgreSQL集群搭建

1.1 服务器规划

IP  hostname 角色
10.99.88.64 test64 master
10.99.88.65 test65 slave
10.99.88.66 test65 slave

1.2 在3个节点安装pgsql

## 1. 编译安装
# adduser postgres  #创建pgsql管理员账号postgres
# su postgres  #切换到postgres用户下操作后边步骤
$ mkdir /home/postgres/app -p
$ mkdir /home/postgres/data -p
$ tar xf postgresql-14.4.tar.gz  #上传安装包到服务器并解压
$ cd postgresql-14.4/ $$ ./configure --prefix=/home/postgres/app
$ make && make install   
$ /home/postgres/app/bin/initdb -D /home/postgres/data/  -E UTF8 --locale=zh_CN.UTF8   #初始化数据



## 2. 修改配置:
#1). change the bind IP:
#编辑 /home/postgres/data/postgresql.conf  ,约61行添加这行,增加5432端口绑定IP:
listen_addresses = '*'  #or listen_addresses = '${local IP}'

#2. add IPv4 local connections:
#编辑 /home/postgres/data/pg_hba.conf,约92行“IPv4 local connections”模块添加这行,授权远程连接:
host    all             all             0.0.0.0/0               md5



## 3. 修改环境变量
#在/etc/profile文件添加环境变量:
export PGSQL=/home/postgres/app/
export PATH=$PATH:$PGSQL/bin
source /etc/profile  #环境变量生效



## 4. 创建启停脚本:
[postgres@test64 data]$ cat pgsql.sh 
#!/bin/bash
case $1 in
start)
  /home/postgres/app/bin/pg_ctl -D /home/postgres/data/ -l /home/postgres/data/logfile start;;
stop)
  /home/postgres/app/bin/pg_ctl -D /home/postgres/data/ stop;;
restart)
  /home/postgres/app/bin/pg_ctl restart -D /home/postgres/data/  -m fast;;
*)
  echo "useage:$0 [start, stop, restart]";;  
esac
[postgres@test64 data]$ 



## 5. 启动服务:
[postgres@test64 data]$ ./pgsql.sh start



## 6. 登录pgsql并给管理员postgres设置密码:
[postgres@test64 data]$ psql 
psql (14.4)
Type "help" for help.

postgres=# password postgres
Enter new password for user "postgres": 
Enter it again: 
postgres=#

1.3 主从配置

1.3.1 master节点配置

1)master创建主从账号:

CREATE ROLE rep login replication encrypted password 'rep';

2)修改master节点pg_hba.conf,在# IPv4 local connections部分加入以下几行:

host replication rep 10.99.88.65 trust
host replication rep 10.99.88.66 trust

3)修改master节点postgresql.conf如下参数:

max_connections = 2000 #100
wal_level = replica
full_page_writes = on
wal_log_hints = on          # also do full page writes of non-critical updates
max_wal_size = 1GB
min_wal_size = 80MB
archive_mode = on           # enables archiving; off, on, or always
archive_command = 'cd ./'   # command to use to archive a logfile segment
max_wal_senders = 10        # max number of walsender processes
hot_standby = on            # "off" disallows queries during recovery

4)重启master

[postgres@test64 data]$ ./pgsql.sh restart

1.3.2 重建slave节点的数据

#删除数据目录pgsql
rm -rf /home/postgres/data/   

#重新从master节点同步数据到slave节点
pg_basebackup -h 10.99.88.64 -p 5432 -U rep -Fp -Xs -Pv -R -D /home/postgres/data/

注意:删除数据目录后,不要手动mkdir创建pgsql,会引起权限问题。手动mkdir pgsql,pgsql的权限是drwxrwxr-x ,而执行上面第二条数据同步命令生成的pgsql目录是drwx------

1.3.3 登录master节点,查看集群状态

[postgres@test64 data]$ psql
psql (14.4)
Type "help" for help.
postgres=# select client_addr,sync_state from pg_stat_replication;
 client_addr | sync_state
-------------+------------
 10.99.88.65 | async
 10.99.88.66 | async
(2 rows)
postgres=#

1.3.4 测试集群同步

在master节点创建一个名为sre的database,然后在slave节点查看,sre库已经存在。

master节点:

slave节点:

至此集群搭建完毕。

1.4 遇到的问题

1.4.1 slave数据目录权限异常,启动失败

创建主从时,从库从主库同步数据后启动失败,报错:

2023-11-15 22:06:00.096 CST [118982] FATAL: data directory "/home/postgres/data" has invalid permissions
2023-11-15 22:06:00.096 CST [118982] DETAIL: Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).

因为在同步数据前,slave删除了数据目录/home/postgres/data后。又手动mkdir pgsql创建了一个数据目录pgsql,pgsql的权限是drwxrwxr-x ,而执行下面复制命令生成的pgsql目录是drwx------,导致无法启动

1.4.2 删除database失败

postgres=# drop database sre;
ERROR: cannot execute DROP DATABASE in a read-only transaction
postgres=#

因为从库不能执行删库操作,误在从库执行了删库操作

2. PostgreSQL接入Prometheus

2.1 方案

Prometheus是之前部署在k8s集群内部的监控系统,因此将PostgreSQL接入Prometheus监控,采用在PostgreSQL服务器部署postgres_exporter,将PostgreSQL节点通过endpoint方式挂载到Prometheus系统内部做监控。

2.2 操作

2.2.1 下载postgres_exporter并解压

$ wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.11.1/postgres_exporter-0.11.1.linux-amd64.tar.gz
$ tar xf postgres_exporter-0.11.1.linux-amd64.tar.gz 
$ mv postgres_exporter-0.11.1.linux-amd64 postgres_exporter

2.2.2 创建启动文件

[postgres@test64 postgres_exporter]$ cat start.sh 
#!/bin/bash
#配置 数据库数据源信息
export  DATA_SOURCE_NAME=postgresql://postgres:[email protected]:5432/postgres?sslmode=disable
#启动监控命令
./postgres_exporter &

[postgres@test64 postgres_exporter]$

2.2.3 启动服务

[postgres@test64 postgres_exporter]$ ./start.sh 
ts=2023-12-29T08:54:56.694Z caller=main.go:135 level=info msg="Listening on address" address=:9187
ts=2023-12-29T08:54:56.694Z caller=tls_config.go:195 level=info msg="TLS is disabled." http2=false
[postgres@test64 postgres_exporter]$

2.2.4 创建service、servicemonitor和endpoint

[root@rancher-0001 pgsql]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  labels:   #此处label要和上一步创建的servicemonitor对象的seletor匹配
    app: pgsql-exporter-service-test
  name: pgsql-exporter-service-test
  namespace: monitoring
spec:
  type: ClusterIP
  ports:
  - name: pgsql-exporter-port
    port: 9187
    protocol: TCP
    targetPort: 9187
[root@rancher-0001 pgsql]# 

[root@rancher-0001 pgsql]# cat servicemonitor.yaml 
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: pgsql-exporter-test
  name: pgsql-exporter-test
  namespace: monitoring
spec:
  endpoints:
  - interval: 15s
    port: pgsql-exporter-port
    path: /metrics
  namespaceSelector:
    matchNames:
    - monitoring
  selector:
    matchLabels:
      app: pgsql-exporter-service-test
[root@rancher-0001 pgsql]# 



[root@rancher-0001 pgsql]# cat endpoint.yaml 
apiVersion: v1
kind: Endpoints
metadata:
  name: pgsql-exporter-service-test
  namespace: monitoring
  labels:
    app: pgsql-exporter-endpoints
subsets:
- addresses:
  - ip: 10.99.88.64
  - ip: 10.99.88.65
  - ip: 10.99.88.66
  ports:
    - name: pgsql-exporter-port
      port: 9187
      protocol: TCP
[root@rancher-0001 pgsql]#

2.2 5. 在Grafana导入Dashboard

DashboardID:9628

Gashboard链接:https://grafana.com/grafana/dashboards/9628-postgresql-database/