postgresql流複製+pgpool2高可用
1 Pgpool部署前準備
1.1 下載所需軟體安裝包
1.1.1 Postgresql安裝包:postgresql-9.6.7.tar.gz
1.1.2 Pgpool安裝包:pgpool-II-3.7.2.tar.gz
1.2 建立所需的資料夾並授權
1.2.1 建立資料夾
a) 軟體安裝目錄:/app/pgpool和/app/pgsql
b) 資料目錄:/data/postgresql
1.2.2 授權資料夾
1. 所有目錄使用
sudo chown –R 使用者名稱:使用者名稱 路徑
命令授權資料夾使用者使用許可權
2. 在使用者目錄建立soft資料夾,將軟體包複製到資料夾內。
1.3 配置hosts檔案
1.3.1 編輯並修改hosts檔案
1. 分別在主機1,2上執行sudo vi /etc/hosts
2. 在檔案中新增
主機1的IP 主機的名稱
主機2的IP 從機的名稱
虛擬IP VIP
如:192.168.1.1 postgres1
1.4 配置ssh互信
1.4.1 在要配置互信的機器上,生成各自的經過認證的key檔案
[使用者名稱@主機 ~]$ mkdir .ssh
[使用者名稱@主機~]$ chmod 755 .ssh
[使用者名稱@主機~]$ /usr/bin/ssh-keygen -t rsa
[使用者名稱@從機 ~]$ mkdir .ssh
[
[使用者名稱@從機~]$ /usr/bin/ssh-keygen -t rsa
1.4.2 將所有的key檔案彙總到一個總的認證檔案中
[使用者名稱@主機~]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[使用者名稱@主機~]$ ssh 使用者名稱@主機主機2 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
1.4.3 分發認證檔案
[使用者名稱@主機1 ~]$ rcp ~/.ssh/authorized_keys 主機2:~/.ssh/authorized_keys
1.4.4 驗證互信
個主機間使用ssh 主機名或ssh 主機IP驗證無密登入
2 PostgreSQL安裝與流複製配置
2.1 安裝並配置postgresql
2.1.1 安裝postgresql
3. tar -zxvf postgresql-9.6.7.tar.gz
4. 進入postgresql-9.6.7資料夾
5. 執行./configure --prefix=/app/pgsql
6. 執行make & make install 直到安裝完畢
2.1.2 配置postgresql
1. 在使用者目錄下執行vi .bashrc,新增以下語句:
export PGHOME=/app/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
7. 執行source .bashrc使之生效
8. 執行下列命令
sudo cp -r /app/pgsql/bin/* /usr/local/bin/
sudo cp -r /app/pgsql/include/* /usr/local/include/
sudo cp -r /app/pgsql/lib/* /usr/local/lib/
sudo cp -r /app/pgsql/share/* /usr/local/share/
2.2 流複製配置
2.2.1 主機初始化資料庫並配置引數
1. 主機使用pg_ctl initdb -D /data/postgresql命令初始化資料庫
9. 這時在/data/postgresql資料夾內會初始化出資料庫檔案,編輯其中的postgresql.conf檔案進行引數配置,這裡只展示可以達成流複製和pgpool高可用部署的必備配置,更多其他配置可自行除錯。
listen_addresses = *
wal_level = hot_standby
max_wal_senders = 1 從機個數,按自己情況填寫
wal_keep_segments = 64
10. 編輯pg_hba.conf檔案,內容如下
host replication repl 0.0.0.0/0 password
host all all 0.0.0.0/0 password
host all all 0/0 md5
2.2.2 從機初始化及配置
1. 在主機上使用pg_ctl start -D /data/postgresql命令開啟主機服務
11. 然後使用psql -U 使用者名稱 -d postgres登入資料庫
12. 使用create user repl superuser password ‘repl’;建立帶密碼超級使用者repl
13. 使用alter user 使用者名稱 with password ‘要設定的密碼’;命令給使用者建立密碼
14. 從機上使用pg_basebackup -h 主機IP -U repl -F p -x -P -R -D /data/postgresql/ -l replbackup命令生成從機資料檔案。
15. 編輯資料資料夾中的postgresql.conf檔案
hot_standby = on
max_standby_archive_delay = 30s
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on
16. 編輯資料資料夾中的recovery.conf檔案
新增trigger_file = '/tmp/trigger_file0'
17. 編輯pg_hba.conf檔案,修改如下
host replication repl 0.0.0.0/0 password
host all all 0.0.0.0/0 password
host all all 0/0 md5
2.2.3 驗證流複製
1. 使用相同命令pg_ctl start -D /data/postgresql開啟從機服務
18. 登入主機資料庫建立表並插入資料,然後在從機登入查詢該表,如果查詢得到則流複製配置成功。
3 Pgpool的安裝及配置
3.1 Pgpool軟體安裝與配置
3.1.1 Pgpool安裝
1. 使用tar -zxvf pgpool-II-3.7.2.tar.gz命令解壓軟體包
2. 進入pgpool-II-3.7.2目錄執行./configure --prefix=/app/pgpool -with-pgsql=path -with-pgsql=/app/pgpool
3. 執行make & make install 安裝軟體
4. 進入pgpool-II-3.7.2/src資料夾執行make & make install
5. 進入pgpool-II-3.7.2/src/sql資料夾執行make & make install
6. 執行psql -f insert_lock.sql命令安裝pgpool相關函式,非強制,可選安裝,提高穩定性。
3.2 Pgpool配置
3.2.1 執行環境配置
1. 進入使用者目錄執行vi .bashrc,新增下面語句
export PGPOOLHOME=/app/pgpool
export PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin:$PGPOOLHOME/bin
2. 使用source .bashrc使之生效
3.3 Pgpool部署配置
3.3.1 配置pool_hba.conf
1. 進入/app/pgpool/etc資料夾
2. 執行cp pool_hba.conf.sample pool_hba.conf
3. 編輯pool_hba.conf,修改如下
host replication repl 0.0.0.0/0 password
host all all 0.0.0.0/0 password
host all all 0/0 md5
3.3.2 配置pcp.conf
1. 執行cp pcp.conf.sample pcp.conf
2. 執行pg_md5 使用者密碼,得到密碼的MD5加密值
3. 編輯pcp.conf新增如下
使用者名稱:密碼MD5加密值,如postgres:6b07583ba8af8e03043a1163147faf6a
19. 使用pg_md5 -p -m -u 使用者名稱 pool_passwd命令,提示輸入密碼,密碼不能輸錯,否則無法進入資料庫,之後會在同一目錄生成pool_passwd檔案
3.3.3 配置系統命令許可權
1. 執行sudo cp /usr/share/bash-completion/completions/arping /usr/sbin
2. 執行sudo chmod u+s /sbin/ifconfig 和sudo chmod u+s /usr/sbin命令
3.3.4 配置pgpool.conf
1. 使用ifconfig命令檢視網絡卡名稱,並記錄備用
2. 配置主機pgpool.conf
使用命令cp pgool.conf.sample pgpool.conf
編輯pgpool.conf,內容如下
# CONNECTIONS
listen_addresses = '*'
port = 9999
pcp_listen_addresses = '*'
pcp_port = 9898
# - Backend Connection Settings -
backend_hostname0 = '主機'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/data/postgresql '
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = '從機'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/data/postgresql '
backend_flag1 = 'ALLOW_TO_FAILOVER'
# - Authentication -
enable_pool_hba = on
pool_passwd = 'pool_passwd'
# FILE LOCATIONS
pid_file_name = '/app/pgpool/pgpool.pid'
replication_mode = off
load_balance_mode = on
master_slave_mode = on
master_slave_sub_mode = 'stream'
sr_check_period = 5
sr_check_user = 'repl'
sr_check_password = 'repl'
sr_check_database = 'postgres'
#------------------------------------------------------------------------------
# HEALTH CHECK 健康檢查
#------------------------------------------------------------------------------
health_check_period = 10 # Health check period
# Disabled (0) by default
health_check_timeout = 20 # Health check timeout
# 0 means no timeout
health_check_user = '資料庫使用者名稱' # Health check user
health_check_password = '資料庫密碼' #資料庫密碼 # Password for health check user
health_check_database = 'postgres'
#必須設定,否則primary資料庫down了,pgpool不知道,不能及時切換。從庫流複製還在連線資料,報連線失敗。
#只有下次使用pgpool登入時,發現連線不上,然後報錯,這時候,才知道掛了,pgpool進行切換。
#主備切換的命令列配置
#------------------------------------------------------------------------------
# FAILOVER AND FAILBACK
#------------------------------------------------------------------------------
failover_command = '/app/pgpool/failover_stream.sh %d %H /tmp/trigger_file0 '
#------------------------------------------------------------------------------
# WATCHDOG
#------------------------------------------------------------------------------
# - Enabling -
use_watchdog = on
# - Watchdog communication Settings -
wd_hostname = '主機' # Host name or IP address of this watchdog
# (change requires restart)
wd_port = 9000 # port number for watchdog service
# (change requires restart)
# - Virtual IP control Setting -
delegate_IP = 'VIP' # delegate IP address
# If this is empty, virtual IP never bring up.
# (change requires restart)
if_cmd_path = '/sbin' # path to the directory where if_up/down_cmd exists
# (change requires restart)
if_up_cmd = 'ifconfig ens33:0 inet $_IP_$ netmask 255.255.255.0'
# startup delegate IP command
# (change requires restart)
# ens33根據現場機器改掉
if_down_cmd = 'ifconfig ens33:0 down' # shutdown delegate IP command
# (change requires restart)
# ens33根據現場機器改掉
# -- heartbeat mode --
wd_heartbeat_port = 9694 # Port number for receiving heartbeat signal
# (change requires restart)
wd_heartbeat_keepalive = 2 # Interval time of sending heartbeat signal (sec)
# (change requires restart)
wd_heartbeat_deadtime = 30 # Deadtime interval for heartbeat signal (sec)
# (change requires restart)
heartbeat_destination0 = '從機' # Host name or IP address of destination 0
# for sending heartbeat signal.
# (change requires restart)
heartbeat_destination_port0 = 9694 # Port number of destination 0 for sending
# heartbeat signal. Usually this is the
# same as wd_heartbeat_port.
# (change requires restart)
heartbeat_device0 = 'ens33' # Name of NIC device (such like 'eth0')
# used for sending/receiving heartbeat
# signal to/from destination 0.
# This works only when this is not empty
# and pgpool has root privilege.
# (change requires restart)
# ens33根據現場機器改掉
# - Other pgpool Connection Settings -
other_pgpool_hostname0 = '從機' #對端
# Host name or IP address to connect to for other pgpool 0
# (change requires restart)
other_pgpool_port0 = 9999 # Port number for othet pgpool 0
# (change requires restart)
other_wd_port0 = 9000 # Port number for othet watchdog 0
# (change requires restart)
20. 配置從機pgpool.conf
# CONNECTIONS listen_addresses = '*' port = 9999 pcp_listen_addresses = '*' pcp_port = 9898 # - Backend Connection Settings - backend_hostname0 = '主機' backend_port0 = 5432 backend_weight0 = 1 backend_data_directory0 = '/data/postgresql' backend_flag0 = 'ALLOW_TO_FAILOVER' backend_hostname1 = '從機' backend_port1 = 5432 backend_weight1 = 1 backend_data_directory1 = '/data/postgresql ' backend_flag1 = 'ALLOW_TO_FAILOVER' # - Authentication - enable_pool_hba = on pool_passwd = 'pool_passwd' # FILE LOCATIONS pid_file_name = '/app/pgpool/pgpool.pid' replication_mode = off load_balance_mode = on master_slave_mode = on master_slave_sub_mode = 'stream' sr_check_period = 5 sr_check_user = 'repl' sr_check_password = 'repl' sr_check_database = 'postgres' #------------------------------------------------------------------------------ # HEALTH CHECK 健康檢查 #------------------------------------------------------------------------------ health_check_period = 10 # Health check period # Disabled (0) by default health_check_timeout = 20 # Health check timeout # 0 means no timeout health_check_user = '資料庫使用者名稱' # Health check user health_check_password = '資料庫密碼' #資料庫密碼 # Password for health check user health_check_database = 'postgres' #必須設定,否則primary資料庫down了,pgpool不知道,不能及時切換。從庫流複製還在連線資料,報連線失敗。 #只有下次使用pgpool登入時,發現連線不上,然後報錯,這時候,才知道掛了,pgpool進行切換。 #主備切換的命令列配置 #------------------------------------------------------------------------------ # FAILOVER AND FAILBACK #------------------------------------------------------------------------------ failover_command = '/app/pgpool/failover_stream.sh %d %H /tmp/trigger_file0 ' #------------------------------------------------------------------------------ # WATCHDOG #----------------------------------------------------------------------------- # - Enabling - use_watchdog = on # - Watchdog communication Settings - wd_hostname = '從機' #本端 # Host name or IP address of this watchdog # (change requires restart) wd_port = 9000 # port number for watchdog service # (change requires restart) # - Virtual IP control Setting - delegate_IP = 'VIP' # delegate IP address # If this is empty, virtual IP never bring up. # (change requires restart) if_cmd_path = '/sbin' # path to the directory where if_up/down_cmd exists # (change requires restart) if_up_cmd = 'ifconfig ens33:0 inet $_IP_$ netmask 255.255.255.0' # startup delegate IP command # (change requires restart) # ens33根據現場機器改掉 if_down_cmd = 'ifconfig ens33:0 down' # shutdown delegate IP command # (change requires restart) # ens33根據現場機器改掉 # -- heartbeat mode -- wd_heartbeat_port = 9694 # Port number for receiving heartbeat signal # (change requires restart) wd_heartbeat_keepalive = 2 # Interval time of sending heartbeat signal (sec) # (change requires restart) wd_heartbeat_deadtime = 30 # Deadtime interval for heartbeat signal (sec) # (change requires restart) heartbeat_destination0 = '主機' #對端 # Host name or IP address of destination 0 # for sending heartbeat signal. # (change requires restart) heartbeat_destination_port0 = 9694 # Port number of destination 0 for sending # heartbeat signal. Usually this is the # same as wd_heartbeat_port. # (change requires restart) heartbeat_device0 = 'ens33' # Name of NIC device (such like 'eth0') # used for sending/receiving heartbeat # signal to/from destination 0. # This works only when this is not empty # and pgpool has root privilege. # (change requires restart) # ens33根據現場機器改掉 # - Other pgpool Connection Settings - other_pgpool_hostname0 = '主機' #對端 # Host name or IP address to connect to for other pgpool 0 # (change requires restart) other_pgpool_port0 = 9999 # Port number for othet pgpool 0 # (change requires restart) other_wd_port0 = 9000 # Port number for othet watchdog 0 # (change requires restart) |
21. 進入/app/pgpool資料夾,使用touch failover_stream.sh 建立failover_stream.sh檔案
22. 編輯failover_stream.sh檔案內容如下:
#! /bin/sh # Failover command for streaming replication. # This script assumes that DB node 0 is primary, and 1 is standby. # # If standby goes down, do nothing. If primary goes down, create a # trigger file so that standby takes over primary node. # # Arguments: $1: failed node id. $2: new master hostname. $3: path to # trigger file. failed_node=$1 new_master=$2 trigger_file=$3
# Do nothing if standby goes down. if [ $failed_node = 1 ]; then exit 0; fi
# Create the trigger file. /usr/bin/ssh -T $new_master /bin/touch $trigger_file
exit 0; |
23. 使用sudo chown –R 使用者名稱:使用者名稱 /app/pgpool
sudo chmod 777 /app/pgpool/failover_stream.sh
命令給與執行許可權。
4 Pgpool叢集管理
4.1 啟動叢集
4.1.1 資料庫啟動的命令
在主機,從機上先後執行下面命令,開啟postgresql資料庫
pg_ctl start -D 資料資料夾路徑
4.1.2 Pgpool啟動的命令
在主機,從機上先後執行下面命令,開啟pgpool
pgpool -n -d -D > /app/pgpool/pgpool.log 2>&1 & (該命令為silent啟動方式)
pgpool -n& (該命令會顯示log資訊)
4.1.3 快速停止pgpool的命令
pgpool -m fast stop
psql -d postgres -p 9999 -h ip地址
4.1.4 叢集啟動後檢視狀態
使用資料庫客戶端用虛擬VIP登入資料庫,執行show pool_nodes;命令
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay
---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------
0 | master | 5432 | up | 0.500000 | primary | 0 | false | 0
1 | slave | 5432 | up | 0.500000 | standby | 0 | true | 0
(2 rows)
發現當前主備節點都是正常的up狀態