1. 程式人生 > 資料庫 >PostgreSQL+Pgpool實現HA主備切換的操作

PostgreSQL+Pgpool實現HA主備切換的操作

PostgreSQL流複製實現HA主備切換

環境說明和主機規劃

作業系統 主機名 主機 角色
CentOS 7 master 10.0.0.11 PG-Master 54321
CentOS 7 slave 10.0.0.12 PG-Slave 54321
CentOS 7 pool 10.0.0.13 pgpool 54321

基礎環境配置(所有主機操作)

配置HOSTS

echo -e "10.0.0.11 master\n10.0.0.12 slave\n10.0.0.13 pool" >> /etc/hosts # 執行一次即可

配置統一的時間(若已配置,請忽略)

yum install -y ntpdate && ntpdate ntp1.aliyun.com
echo -e "# sync time from ntp1.aliyun.com\n5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1
" >> /var/spool/cron/root # 寫入定時任務,執行一次即可

建立postgres使用者

useradd postgres && echo "your_password" | passwd --stdin postgres

配置免金鑰登陸

su - postgres
ssh-keygen -t rsa -f /home/postgres/.ssh/id_rsa -P "" 
cd ~/.ssh/
ssh-copy-id postgres@master # 三臺主機執行
scp authorized_keys postgres@slave:~/.ssh # 只在master主機執行
scp authorized_keys postgres@pool:~/.ssh # 只在master主機執行

安裝Postgresql資料庫(PG9.6)

yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

yum install -y postgresql96-server postgresql96-contrib postgresql96 postgresql96-libs

建立統一的目錄結構

mkdir /data1/pg_{data,bin,logs} -p

chown -R postgres.postgres /data1/

修改系統變數

vi /etc/profile #增加以下內容
export PGHOME=/usr/pgsql-9.6/
export PGDATA=/data1/pg_data
export PGPORT=54321
export PATH=$PATH:$PGHOME/bin
# 生效
source /etc/profile

PostgreSQL流複製結構(master和slave主機操作)

master主機操作

初始化系統

/usr/pgsql-9.6/bin/postgresql96-setup initdb

vi /usr/lib/systemd/system/postgresql-9.6.service

修改postgresql-9.6.service

內容如下:

# Include the default config:
.include /usr/lib/systemd/system/postgresql-9.6.service

[Service]
Environment=PGDATA=/data1/pg_data

重啟PG服務

systemctl daemon-reload
su - postgres -c '/usr/pgsql-9.6/bin/initdb -D /data1/pg_data'
systemctl restart postgresql-9.6
systemctl enable postgresql-9.6.service

修改系統配置(以下用postgres使用者操作)

cp /data1/pg_data/pg_hba.conf{,.bak} 
cat >/data1/pg_data/pg_hba.conf<<EOF
local all    all            trust
host all    all      10.0.0.11/32   trust
host all    all      10.0.0.12/32   trust
host all    all      0.0.0.0/0    md5
host all    all      ::1/128     trust
host replication  stream_replication  0.0.0.0/0    md5
EOF
#host replication  stream_replication  0.0.0.0/0    md5 為流複製使用者

64G

cp /data1/pg_data/postgresql.conf{,.bak}
cat >/data1/pg_data/postgresql.conf<<EOF
listen_addresses = '*'
port = 54321
max_connections = 256
shared_buffers = 16GB
effective_cache_size = 48GB
work_mem = 64MB
maintenance_work_mem = 2GB
min_wal_size = 2GB
max_wal_size = 4GB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
wal_level = hot_standby
wal_log_hints = on
max_wal_senders = 1
hot_standby = on
logging_collector = on
log_directory = 'pg_log'
EOF
#操作完記得重啟 pg_ctl restart

128G

listen_addresses = '*'
port = 54321
max_connections = 256
shared_buffers = 32GB
effective_cache_size = 96GB
work_mem = 128MB
maintenance_work_mem = 2GB
min_wal_size = 2GB
max_wal_size = 4GB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
wal_level = hot_standby
wal_log_hints = on
max_wal_senders = 1
hot_standby = on
logging_collector = on
log_directory = 'pg_log'

在主庫中建立流複製使用者(stream_replication)和PGPool使用者(srcheck)

CREATE USER stream_replication replication LOGIN CONNECTION LIMIT 5 ENCRYPTED PASSWORD 'your_password';

CREATE USER srcheck replication LOGIN CONNECTION LIMIT 5 ENCRYPTED PASSWORD 'your_password';

修改主庫pg_hba.conf檔案(已操作見cat >/data1/pg_data/pg_hba.conf<<EOF)

host replication stream_replication 0.0.0.0/0 md5

slave主機操作

初始化系統

/usr/pgsql-9.6/bin/postgresql96-setup initdb

vi /usr/lib/systemd/system/postgresql-9.6.service

修改postgresql-9.6.service

內容如下:

# Include the default config:
.include /usr/lib/systemd/system/postgresql-9.6.service

[Service]
Environment=PGDATA=/data1/pg_data

重啟PG服務

systemctl daemon-reload

基礎備份複製到備庫伺服器

rm -rf /data1/pg_data # 如果沒有重要資料可操作,主要為同步主庫路徑

su - postgres -c 'pg_basebackup -D $PGDATA --format=p -h master -p 54321 -U stream_replication -W'

修改備庫配置資訊

cp $PGHOME/share/recovery.conf.sample $PGDATA/recovery.conf

vi $PGDATA/recovery.conf

增加以下內容

standby_mode='on'
primary_conninfo = 'host=master port=54321 user=stream_replication password=your_password'
restore_command = ''
recovery_target_timeline = 'latest'
# 重啟PG服務
systemctl restart postgresql-9.6
systemctl enable postgresql-9.6.service

驗證

主節點執行

create table test (id int4,create_time timestamp(0) without time zone);
insert into test values (1,now());
select * from test;

備節點執行

select * from test;

其他查詢

進入測試資料庫test,主庫上執行如下命令返回f,備庫上返回t。 select pg_is_in_recovery();

執行如下命令檢視快照,它返回主庫記錄點、備庫記錄點;主庫每增加一條寫入,記錄點的值就會加1。

select txid_current_snapshot();

執行如下命令可以檢視主備同步狀態。

select * from pg_stat_replication;

欄位state顯示的同步狀態有:startup(連線中)、catchup(同步中)、streaming(同步);欄位sync_state顯示的模式有:async(非同步)、sync(同步)、potential(雖然現在是非同步模式,但是有可能升級到同步模式)。

主備切換

假設主庫崩潰了,備庫如何從只讀狀態切換為讀寫狀態呢?只要把備庫的postgresql.conf中hot_standby修改為off,並且刪除recovery.conf,然後重啟庫就可以提供服務了。

PGPool2(pool主機操作)

安裝PGPool2

yum install -y http://www.pgpool.net/yum/rpms/3.6/redhat/rhel-7-x86_64/pgpool-II-release-3.6-1.noarch.rpm
yum -y install pgpool-II-pg96 pgpool-II-pg96-debuginfo pgpool-II-pg96-devel pgpool-II-pg96-extensions
systemctl enable pgpool.service #開啟自動啟動

新增Pgpool-II執行使用者

useradd postgres # 環境準備時已操作
chown -R postgres.postgres /etc/pgpool-II
chown -R postgres.postgres /var/run/pgpool/

配置pool_hba.conf

cp /etc/pgpool-II/pool_hba.conf{,.bak}

vi /etc/pgpool-II/pool_hba.conf

增加內容

host all all 0.0.0.0/0 md5

配置pcp.conf

主節點登陸後執行:

postgres=# select rolname,rolpassword from pg_authid;
  rolname  |    rolpassword    
--------------------+-------------------------------------
 pg_signal_backend | 
 srcheck   | md5662c10f61b27a9ab38ce69157186b25f
 postgres   | md5d3612d57ee8d4c147cf27b11e3a0974d
 stream_replication | md59279ef6b904bc483e4f85e6d44cfc0ed
(4 rows)

vi /etc/pgpool-II/pool_passwd

增加SQL執行結果的內容,形式為$rolname:$rolpassword例如:

srcheck:md5662c10f61b27a9ab38ce69157186b25f

或者:

pg_md5 -u postgres your_password

vi /etc/pgpool-II/pcp.conf ## 加入 postgres:上一命令的輸出

配置pgpool.conf

cp /etc/pgpool-II/pgpool.conf{,.bak}

vi /etc/pgpool-II/pgpool.conf

內容如下:

# CONNECTIONS

listen_addresses = '*'
port = 54321
socket_dir = '/var/run/pgpool'
pcp_listen_addresses = '*'
pcp_port = 9898
pcp_socket_dir = '/var/run/pgpool'

# - Backend Connection Settings -

backend_hostname0 = 'master'
backend_port0 = 54321
backend_weight0 = 1
backend_data_directory0 = '/data1/pg_data'
backend_flag0 = 'ALLOW_TO_FAILOVER'

backend_hostname1 = 'slave'
backend_port1 = 54321
backend_weight1 = 1
backend_data_directory1 = '/data1/pg_data'
backend_flag1 = 'ALLOW_TO_FAILOVER'

# - Authentication -

enable_pool_hba = on
pool_passwd = 'pool_passwd'

# FILE LOCATIONS

pid_file_name = '/var/run/pgpool/pgpool.pid'
logdir = '/data1/pg_logs'

replication_mode = off
load_balance_mode = on
master_slave_mode = on
master_slave_sub_mode = 'stream'

sr_check_period = 5
sr_check_user = 'srcheck'
sr_check_password = '123456'
sr_check_database = 'postgres'

# HEALTH CHECK 健康檢查

health_check_period = 10
health_check_timeout = 20
health_check_user = 'srcheck'
health_check_password = '123456'
health_check_database = 'postgres'

# FAILOVER AND FAILBACK

failover_command = '/data1/pg_bin/failover_stream.sh %H'

failover_stream.sh指令碼

vim /data1/pg_bin/failover_stream.sh
chmod 777 /data1/pg_bin/failover_stream.sh
chmod u+s /sbin/ifconfig 
chmod u+s /usr/sbin
pgpool -n -d -D > /data1/pg_logs/pgpool.log 2>&1 & ## 啟動
pgpool -m fast stop ## 關閉

failover_stream.sh內容:

#! /bin/sh 
# Failover command for streaming replication. 
# Arguments: $1: new master hostname. 

new_master=$1 
trigger_command="$PGHOME/bin/pg_ctl promote -D $PGDATA" 

# Prompte standby database. 
/usr/bin/ssh -T $new_master $trigger_command 

exit 0;

登陸設定

當執行pgpool -n -d -D > /data1/pg_logs/pgpool.log 2>&1 &後可檢視叢集狀態:

[postgres@pool pgpool-II]$ psql -p 54321 -h 10.0.0.13 -U srcheck -d postgres

postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay 
---------+----------+-------+--------+-----------+---------+------------+-------------------+-------------------
 0  | master | 54321 | up  | 0.500000 | primary | 0   | false    | 0
 1  | slave | 54321 | up  | 0.500000 | standby | 0   | true    | 0
(2 rows)

如果未發現叢集狀態,請在master和slave主機分別執行以下操作:

[postgres@pool ~]$ pcp_attach_node -d -U postgres -h pool -p 9898 -n 0
[postgres@pool ~]$ pcp_attach_node -d -U postgres -h pool -p 9898 -n 1
#詳情查詢命令pcp_attach_node

HA切換

模擬master主機宕機

Master端:

[postgres@master ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped

當前叢集狀態

[postgres@pool ~]$ psql -p 54321 -h 10.0.0.13 -U srcheck -d postgres
psql (9.6.1)
Type "help" for help.

postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay 
---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------
 0  | master | 5432 | down| 0.500000 | standby | 0    | false | 0
 1  | slave  | 5432 | up  | 0.500000 | primary | 0    | true | 0
(2 rows)

發現master已經是standby了,且down機了

修改master,啟動

當master主機宕機後,此時slave主機PG資料庫成為主庫,修改master成為slave的從庫即可

[postgres@master ~]$ vim recovery.conf
standby_mode='on'
primary_conninfo = 'host=slave port=54321 user=stream_replication password=your_password'
restore_command = ''
recovery_target_timeline = 'latest'

同步時間線

#如果報時間線衝突落後,先停掉pg服務,然後執行同步時間線,否知直接看狀態
[postgres@master ~]$ pg_rewind --target-pgdata=/data1/pg_data --source-server='host=slave port=54321 user=postgres dbname=postgres'
servers diverged at WAL position 0/5000098 on timeline 1
rewinding from last common checkpoint at 0/5000028 on timeline 1
Done!
# 重新啟動資料庫
[postgres@master ~]$ pg_ctl start

再次檢視當前狀態

[postgres@pool ~]$ psql -p 54321 -h 10.0.0.13 -U srcheck -d postgres
postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay 
---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------
 0  | master | 5432 | down| 0.500000 | standby | 0    | false | 0
 1  | slave  | 5432 | up  | 0.500000 | primary | 0    | true | 0
(2 rows)

#注意雖然master已經啟動了,但是還是down,需要手動將master節點新增進pgpool,master的node_id是0,所以-n 0
[postgres@pool ~]$ pcp_attach_node -d -U postgres -h pool -p 54321 -n 0
#提示輸入密碼,輸入pcp管理密碼
#檢視當前狀態
[postgres@pool ~]$ psql -p 54321 -h 10.0.0.13 -U srcheck -d postgres
postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay 
---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------
 0  | master | 5432 | up | 0.500000 | standby | 0    | false | 0
 1  | slave  | 5432 | up  | 0.500000 | primary | 0    | true | 0
(2 rows)

現在兩個節點都是up了。

主從兩節點pgpool健康檢查指令碼(pgpool_check.sh)

說明:此指令碼是基於PGpool只安裝到master和slave兩個主機上的情況下使用,在master主機有了pgpool程序後,可在slave主機執行sh pgpool_check.sh & 即可

#! /bin/bash
# Check Master host pgpool-process

while true
do
 pgcount=$(nmap 10.0.0.11|egrep '9898|9999'|wc -l)

 if [ $pgcount -eq 2 ] ; then
  echo 'Master host pgpool is GOOD!!!' > /dev/null 2>&1
 else
  echo -e "Master host pgpool is \033[31m BAD!!! \033[0m"
  echo -e "Master host pgpool is \033[31m BAD!!! \033[0m"
  echo -e "Master host pgpool is \033[31m BAD!!! \033[0m"
  echo -e "SYSTEM WILL DO THE SHELL : \033[34m su - postgres -c 'pgpool -n -d -D > /data1/pg_logs/pgpool.log 2>&1 &' \033[0m"
  su - postgres -c 'pgpool -n -d -D > /data1/pg_logs/pgpool.log 2>&1 &'
  pgport=$(netstat -lntup|egrep '9898|9999'|wc -l)
  [ $pgport -gt 0 ] && echo -e "Slave host pgpool is \033[32m RUNNING!!! \033[0m"
  exit 0
 fi
done

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。