postgresql高可用叢集安裝

來源:互聯網
上載者:User

標籤:postgresql

一、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

[[email protected]_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).停止資料庫
[[email protected]_node02 ~]# systemctl stop postgresql-10

2).clear old pgdata dir
[[email protected]_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
[[email protected]_node02 ~]# systemctl start postgresql-10
[[email protected]_node02 ~]#

4.async slave 配置
1).停止資料庫
[[email protected]_node03 ~]# systemctl stop postgresql-10

2).清空pgdata目錄
[[email protected]_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
[[email protected]_node03 ~]#
[[email protected]_node03 ~]# systemctl start postgresql-10
[[email protected]_node03 ~]#

5.檢查replication狀態

1).primary

[[email protected]_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

[[email protected]_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

[[email protected]_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$

postgresql高可用叢集安裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.