赞
踩
转载说明:如果您喜欢这篇文章并打算转载它,请私信作者取得授权。感谢您喜爱本文,请文明转载,谢谢。
相关文章:
如果PostgreSQL有两层nginx代理,会发生什么事?
之前在《PostgreSQL学习之部署与简单使用》记录了PostgreSQL的搭建和简单使用,本文记录PostgreSQL集群的搭建,接入Prometheus监控(Prometheus监控部署在k8s集群内)。
IP | hostname | 角色 |
10.99.88.64 | test64 | master |
10.99.88.65 | test65 | slave |
10.99.88.66 | test65 | slave |
- ## 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)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
- #删除数据目录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------
- [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=#
在master节点创建一个名为sre的database,然后在slave节点查看,sre库已经存在。
master节点:
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------,导致无法启动
- postgres=# drop database sre;
- ERROR: cannot execute DROP DATABASE in a read-only transaction
- postgres=#
因为从库不能执行删库操作,误在从库执行了删库操作
Prometheus是之前部署在k8s集群内部的监控系统,因此将PostgreSQL接入Prometheus监控,采用在PostgreSQL服务器部署postgres_exporter,将PostgreSQL节点通过endpoint方式挂载到Prometheus系统内部做监控。
- $ 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
- [postgres@test64 postgres_exporter]$ cat start.sh
- #!/bin/bash
- #配置 数据库数据源信息
- export DATA_SOURCE_NAME=postgresql://postgres:postgres@10.99.88.64:5432/postgres?sslmode=disable
- #启动监控命令
- ./postgres_exporter &
-
- [postgres@test64 postgres_exporter]$
- [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]$
- [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]#
DashboardID:9628
Gashboard链接:https://grafana.com/grafana/dashboards/9628-postgresql-database/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。