postgresql高可用集群的安装步骤
admin
2023-05-21 10:02:52
0

一、hosts and topology structure of pg cluster

1.host infos

cluster01_node01 192.168.0.108
cluster01_node02 192.168.0.109
cluster02_node03 192.168.0.110

2.topology structure

                    sync                              async

primary(cls01_node01) -------》 standby01(cls01_node02) -------》standby02(cls01_node03)

二、安装配置

1.安装、初始化PG (cls01_node01,cls01_node02, cls01_node03)
1).install and init

--- install pg packages
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
yum install postgresql10-server
yum install -y postgresql10-contrib

--- init and auto boot
/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl enable postgresql-10
systemctl start postgresql-10

PGDATA:
/var/lib/pgsql/10/data/

2).mkdir pg archives

mkdir /pg_archive
chown postgres.postgres /pg_archive/
chmod 700 /pg_archive/

2.primary创建复制用户与数据库配置(cls01_node01)

1).create replication user

user/password: repuser/repuser

[root@pg_master ~]# su - postgres
Last login: Sun Apr 22 17:25:06 CST 2018 on pts/0
-bash-4.2$ createuser -U postgres repuser -P -c 5 --replication
Enter password for new role:
Enter it again:
-bash-4.2$

设置超级用户密码
-bash-4.2$ psql -h 127.0.0.1
psql (10.3)
Type "help" for help.
postgres=#
postgres=# alter user postgres with password '123456';

2).configuration file

a. postgresql.conf

#------------------------------------------------------------------------------
--# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
--# - Connection Settings -
listen_addresses = '*'
max_connections = 2000

--# - TCP Keepalives -
tcp_keepalives_idle = 60
tcp_keepalives_interval = 10
tcp_keepalives_count = 6

#------------------------------------------------------------------------------
--# RESOURCE USAGE (except WAL)
#------------------------------------------------------------------------------
--# - Memory -
shared_buffers = 256MB
maintenance_work_mem = 64MB

--# - Kernel Resource Usage -
shared_preload_libraries = 'pg_stat_statements'

#------------------------------------------------------------------------------
--# WRITE AHEAD LOG
#------------------------------------------------------------------------------
--# - Settings -
wal_level = logical
wal_log_hints = on

--# - Checkpoints -
max_wal_size = 10GB
checkpoint_completion_target = 0.9

--# - Archiving -
archive_mode = on
archive_command = 'test ! -f /pg_archive/%f && cp %p /pg_archive/%f'

#------------------------------------------------------------------------------
--# REPLICATION
#------------------------------------------------------------------------------
--# - Sending Server(s) -
wal_keep_segments = 5000

--# - Master Server -
synchronous_standby_names = '*'

--# - Standby Servers -
hot_standby_feedback = on

#------------------------------------------------------------------------------
--# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------
--# - When to Log -
log_min_duration_statement = 1000

--# - What to Log -
log_checkpoints = on
log_connections = on
log_disconnections =
log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
log_lock_waits = on
log_temp_files = 0

#------------------------------------------------------------------------------
AUTOVACUUM PARAMETERS
#------------------------------------------------------------------------------
log_autovacuum_min_duration = 0

b. pg_hba.conf

--# TYPE  DATABASE        USER            ADDRESS                 METHOD
--# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5
--# IPv6 local connections:
host    all             all             ::1/128                 md5
--# Allow replication connections
host     replication     repuser         192.168.0.108/32       md5
host     replication     repuser         192.168.0.109/32       md5
host     replication     repuser         192.168.0.110/32       md5

3).restart database

--# systemctl restart postgresql-10

3.sync slave配置
1).停止数据库
[root@cls01_node02 ~]# systemctl stop postgresql-10

2).clear old pgdata dir
[root@cls01_node02 ~]# su - postgres
Last login: Sun Apr 22 18:39:24 CST 2018 on pts/0
-bash-4.2$ cd 10/data/
-bash-4.2$ rm -rf *
-bash-4.2$ ll
total 0
-bash-4.2$
3). make sync standby

注意:从primary获取数据库数据

-bash-4.2$ cd
-bash-4.2$ pg_basebackup -h 192.168.0.108 -U repuser -p 5432 -D /var/lib/pgsql/10/data --wal-method=stream --checkpoint=fast --progress --verbose --write-recovery-conf > makeslave$(date +%Y%m%d).log 2>&1
Password:
-bash-4.2$ more make_slave_2018_04_22.log
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/4000060 on timeline 1
pg_basebackup: starting background WAL receiver
24448/24448 kB (100%), 1/1 tablespace                                        
pg_basebackup: write-ahead log end point: 0/4000130
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
-bash-4.2$

4).add sync flag(application_name) and trigger file (trigger_file)
-bash-4.2$ cd /var/lib/pgsql/10/data/
-bash-4.2$ vi recovery.conf  
standby_mode = 'on'
primary_conninfo = 'application_name=sync_slave user=repuser password=repuser host=192.168.0.108 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres target_session_attrs=any'
trigger_file = '/tmp/trigger_failover'
-bash-4.2$

5).start sync slave
-bash-4.2$ exit
logout
[root@cls01_node02 ~]# systemctl start postgresql-10
[root@cls01_node02 ~]#

4.async slave 配置
1).停止数据库
[root@cls01_node03 ~]# systemctl stop postgresql-10

2).清空pgdata目录
[root@cls01_node03 ~]# su - postgres  
-bash-4.2$ cd /var/lib/pgsql/10/data/
-bash-4.2$ rm -rf *
-bash-4.2$ ll
total 0
-bash-4.2$

3).make slave

注意:从sync standby获取数据库数据

-bash-4.2$ cd
-bash-4.2$ pg_basebackup -h 192.168.0.109 -U repuser -p 5432 -D /var/lib/pgsql/10/data --wal-method=stream --checkpoint=fast --progress --verbose --write-recovery-conf > makeslave$(date +%Y%m%d).log 2>&1
Password:
-bash-4.2$ ll
total 4
drwx------ 4 postgres postgres  51 Apr 22 17:17 10
-rw-r--r-- 1 postgres postgres 690 Apr 22 19:23 make_slave_2018_04_22.log
-bash-4.2$ more make_slave_2018_04_22.log
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/6000028 on timeline 1
pg_basebackup: starting background WAL receiver
32173/32173 kB (100%), 1/1 tablespace                                        
pg_basebackup: write-ahead log end point: 0/7000060
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
-bash-4.2$

4).add wal switch flag(recovery_target_timeline='latest')

-bash-4.2$ cd /var/lib/pgsql/10/data/
-bash-4.2$ vi recovery.conf  
standby_mode = 'on'
primary_conninfo = 'user=repuser password=repuser host=192.168.0.109 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres target_session_attrs=any'
recovery_target_timeline='latest'
-bash-4.2$

5).start async standby

-bash-4.2$ exit
logout
[root@cls01_node03 ~]#
[root@cls01_node03 ~]# systemctl start postgresql-10
[root@cls01_node03 ~]#

5.检查replication状态

1).primary

[root@cls01_node01 ~]# su - postgres
Last login: Sun Apr 22 19:16:19 CST 2018 on pts/0

-bash-4.2$
-bash-4.2$ psql -h 127.0.0.1
Password:
psql (10.3)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 9341
usesysid         | 16384
usename          | repuser
application_name | sync_slave
client_addr      | 192.168.0.109
client_hostname  |
client_port      | 34152
backend_start    | 2018-04-22 19:15:51.242261+08
backend_xmin     | 558
state            | streaming
sent_lsn         | 0/7000140
write_lsn        | 0/7000140
flush_lsn        | 0/7000140
replay_lsn       | 0/7000140
write_lag        |
flush_lag        |
replay_lag       |
sync_priority    | 1
sync_state       | sync

postgres=# create database tdb01;
CREATE DATABASE
postgres=# \c tdb01
You are now connected to database "tdb01" as user "postgres".
tdb01=# create table t1(id serial,user_name varchar(20));
CREATE TABLE
tdb01=# insert into t1(user_name) values('mia');
INSERT 0 1
tdb01=#
tdb01=#
tdb01=# select * from t1;
id | user_name
----+-----------
1 | mia
(1 row)

tdb01=# \q
-bash-4.2$

2).sync standby

[root@cls01_node02 ~]# su - postgres
Last login: Sun Apr 22 18:41:55 CST 2018 on pts/0
-bash-4.2$
-bash-4.2$ psql -h 127.0.0.1
Password:
psql (10.3)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 9086
usesysid         | 16384
usename          | repuser
application_name | walreceiver
client_addr      | 192.168.0.110
client_hostname  |
client_port      | 51408
backend_start    | 2018-04-22 19:29:17.659393+08
backend_xmin     | 563
state            | streaming
sent_lsn         | 0/7039290
write_lsn        | 0/7039290
flush_lsn        | 0/7039290
replay_lsn       | 0/7039290
write_lag        |
flush_lag        |
replay_lag       |
sync_priority    | 0
sync_state       | async

tdb01=# \x
Expanded display is off.
tdb01=#
tdb01=# \l
List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  
-----------+----------+----------+-------------+-------------+-----------------------
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
tdb01     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
(4 rows)

tdb01=# select * from t1;
id | user_name
----+-----------
1 | mia
(1 row)

tdb01=#

3).async standby

[root@cls01_node03 ~]# su - postgres
Last login: Sun Apr 22 19:18:55 CST 2018 on pts/0
-bash-4.2$
-bash-4.2$ psql -h 127.0.0.1
Password:
psql (10.3)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
(0 rows)

postgres=# \x
Expanded display is off.
postgres=# \l
List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  
-----------+----------+----------+-------------+-------------+-----------------------
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
tdb01     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=# \c tdb01
You are now connected to database "tdb01" as user "postgres".
tdb01=# select * from t1;
id | user_name
----+-----------
1 | mia
(1 row)

tdb01=# \q
-bash-4.2$

相关内容

热门资讯

印度大量驳回对华反倾销调查建议 【环球时报报道 记者 倪浩】印度正不断调整政策走向,释放出改善对华经贸关系的信号。《印度教徒报》8月...
原创 美... 文|追梦人 前言 7月31日,《国务院关于出境入境管理的规定》正式公布。 一条看似专业的内容很快引发...
泽连斯基签署总统令,解除多名乌... △斯特凡尼希娜(资料图)当地时间8月3日,乌克兰总统泽连斯基签署总统令,解除了乌克兰驻美国大使斯特凡...
10级美颜证件照火出圈的精神科... 极目新闻记者 王鹏近日,山西医科大学第一医院精神科医生郎小娥的证件照,因美颜效果突出火爆出圈,引发热...
全美25州提起诉讼,反对美政府... △美国白宫(资料图)央视记者当地时间8月3日获悉,法庭文件显示,美国有25个州提起诉讼,反对美政府最...
小米澎程72小时一线预售快报出... 来源:CNMO科技 【CNMO科技消息】8月3日,CNMO科技注意到,车fans创始人@孙少军发布了...
海评面:“中国商业航天正以惊人... 7月22日,引力一号遥四运载火箭在上海东部海域点火升空。(图/新华社) “中国商业航天领域正以惊人的...
第三届雷达学术前沿大会在长沙举... 中新网长沙8月2日电(王曼)8月2日,第三届雷达学术前沿大会在长沙启幕,雷达及相关领域8位院士,12...
埃及、卡塔尔和土耳其发表联合声... △加沙地带(资料图)当地时间3日,埃及、卡塔尔和土耳其三国发表联合声明,强烈谴责以色列在加沙地带持续...
鹤壁市汽车电子电气行业技术宣讲... 为助推我市汽车电子电器产业智能化、数字化和绿色化转型升级。7月30日,由市工商联、市汽车电子电器商会...