CentOS_6.5 postgresql 故障切換實現

   pgpool-II(http://pgpool.projects.postgresql.org/ )是一箇中間 件,工作在PostgreSQL多服 務 器和PostgreSQL數據 庫 客 戶 端之 間。

它提供了以下功能

連 接池: pgpool -Ⅱ保存 連 接到PostgreSQL服 務 器,並重複利用具有相同屬性的新的 連 接(即用 戶 名,數據 庫 , 協議 的版本),減少 連 接的開 銷 ,並提高了系 統 的整體吞吐量。     複製: pgpool - II可以管理多個PostgreSQL服務 器。 使用複製功能,可以 實時備份在 2個或多個物理磁 盤 上,因此即使在硬 盤出故障的時候也不用停止服務。  

負載 平衡: 如果數據 庫 是複製,任何服 務 器上 執 行一個SELECT 查 詢 將返回相同的 結 果。 pgpool -Ⅱ採用一個複製功能 優 勢 是,以減少多個服 務 器之 間 分配上的SELECT 查 詢 每個PostgreSQL服 務 器的 負載 ,提高系 統 的整體吞吐量。在最好的,性能的提高比例的PostgreSQL服 務 器的數量。在同一 時間有 大量用 戶 的 查 詢的時候,負載 平衡的情況下有最佳的 執 行。  

 連接超 過限制 : 有一個關於與 PostgreSQL 的最大並 發連 接數限制,最大 連接數超過後 的 連 接被拒 絕 。 設 置最大 連 接數,但是增加的 資 源消耗和影響系 統 性能。 pgpool - II 也有 對 最大 連 接數的限制,但 額 外的 連 接將被排 隊 ,而不是立即返回 錯誤 。  

   並行查 詢 : 使用並行 查 詢 功能,數據可分佈在多個服 務 器中,以便 查 詢 可以 執 行所有服 務 器上同 時 減少 總 體 執 行 時間 。 並行 查 詢 的工作 時 候 , 尋 找最佳的大 規 模的數據。

 
進行pgpool搭建前需要配置好postgresql的流複製,操作步驟參考http://xiajie.blog.51cto.com/6044823/1662222

一、安裝

 wget http://www.pgpool.net/download.php?f=pgpool-II-3.4.0.tar.gz
 tar -zxvf pgpool-3.4.0.tar.gz
 cd pgpool-II-3.4.0/
 ./configure --prefix=/usr/local/pgpool --with-pgsql=path --with-pgsql=/usr/local/pgsql
 make
 make install
 chown postgres.postgres /usr/local/pgpool/ -R
 chown postgres.postgres /usr/src/pgpool-II-3 -R
 mkdir /var/run/pgpool
 chown postgres.postgres /var/run/pgpool/
 #切換postgres 用戶安裝一些函數
 su - postgres
 
 cd /usr/src/pgpool-II-3.4.0/src/sql/
 make
 make install
 cd pgpool-recovery/
 make install
 cd ../pgpool-regclass/
 make install

 
二、配置

 cd /usr/local/pgpool/etc
 cp pcp.conf.sample pcp.conf
 pg_md5 postgres
 e8a48653851e28c69d0506508fb27fc5
 echo "postgres:e8a48653851e28c69d0506508fb27fc5" >> pcp.conf
 echo "postgres:e8a48653851e28c69d0506508fb27fc5" >> pool_passwd
 cp pool_hba.conf.sample pool_hba.conf
 vim pool_hba.conf
 host    all         postgres    db2                   md5
 
 
 listen_addresses = '*'                    #允許所有主機監聽
 port = 9999                            #訪問端口
 backend_hostname0 = 'db1'                #DBmaster ip
 backend_port0 = 5432                    #DBmaster postgresql 端口
 backend_weight0 = 1                    #權重
 backend_data_directory0 = '/opt/data'    #DBmaster 數據庫目錄
 backend_flag0 = 'ALLOW_TO_FAILOVER'    #允許切換
 
 backend_hostname0 = 'db2'
 backend_port0 = 5432
 backend_weight0 = 1
 backend_data_directory0 = '/opt/data'
 backend_flag0 = 'ALLOW_TO_FAILOVER'
 
 enable_pool_hba = on   #隨意,自由定製,使用 pool_hba.conf 對client的驗證
 pool_passwd = 'pool_passwd' #md5驗證文件
 sr_check_user = 'postgres'  #用來故障切換的用戶
 
 failover_command = '/usr/local/pgsql/bin/failover_command.sh %d %H /tmp/trigger_file'

故障切換腳本
 
 

 vim /usr/local/pgsql/bin/failover_command.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;
chmod +x /usr/local/pgsql/bin/failover_command.sh

三、調試

 啓動命令,帶有日誌輸出
 

[postgres@db1 etc]$ pgpool -nd >/tmp/pgpool.log 2>&1 &
[postgres@db1 etc]$ netstat -ntlp
 (Not all processes could be identified, non-owned process info
  will not be shown, you would have to be root to see it all.)
 Active Internet connections (only servers)
 Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
 tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      -                   
 tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      -                   
 tcp        0      0 0.0.0.0:9898                0.0.0.0:*                   LISTEN      16664/pgpool        
 tcp        0      0 0.0.0.0:9999                0.0.0.0:*                   LISTEN      16664/pgpool        
 tcp        0      0 :::22                       :::*                        LISTEN      -                   
 tcp        0      0 ::1:25                      :::*                        LISTEN      -                   
 tcp        0      0 :::9999                     :::*                        LISTEN      16664/pgpool

 
 登錄

 [postgres@db1 etc]$ psql -U postgres -h db1 -p 9999
 psql (9.2.1)
 Type "help" for help.
 postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight |  role   
---------+----------+------+--------+-----------+---------
 0       | db1      | 5432 | 2      | 0.500000  | primary
 1       | db2      | 5432 | 2      | 0.500000  | standby
(2 rows)
 postgres=# create database db0;
 CREATE DATABASE

2:啓動
3:死啦

測試可以登錄,可以讀寫

四、故障切換

首先停止DBmaster
[postgres@db1 etc]$ pg_ctl -m fast stop

登錄查看

[postgres@db1 etc]$ psql -U postgres -h db1 -p 9999
 postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight |  role   
---------+----------+------+--------+-----------+---------
 0       | db1      | 5432 | 3      | 0.500000  | standby
 1       | db2      | 5432 | 2      | 0.500000  | primary
(2 rows)

此時DBslave顯示的日誌信息

[postgres@db2 data]$ FATAL:  replication terminated by primary server
LOG:  record with zero length at 0/10000FE0
LOG:  trigger file found: /tmp/trigger_file
LOG:  redo done at 0/10000F80
LOG:  last completed transaction was at log time 2015-06-17 11:05:44.379009+08
LOG:  selected new timeline ID: 2
LOG:  archive recovery complete
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started


DBmaster 已經死啦,狀態切換爲standby;DBslave切換爲primary;測試可讀寫
日誌提示,發現 trigger_file文件,進行切換
DBslave 切換爲 主服務器
recover.conf自動更改爲recover.done

[postgres@db2 data]$ ll /opt/data/recovery.done 
recovery.done

故障切換成功

五、恢復DBmaster

1、同步DBmaster至DBslave

pg_basebackup -D $PGDATA -Fp -Xs -v  -h db1 -p 5432 -U postgres

2、修改配置文件

listen_addresses = '*' 
port = 5432
hot_standby = on

3、修改recover文件

mv recover.done  recover.conf
vim recover.conf
primary_conninfo = 'host=172.16.0.133 port=5432 user=postgres'

4、啓動DBslave
5、添加node

pcp_attach_node -d 5 db1 9898 postgres postgres 0
pcp_attach_node -d 5 db1 9898 postgres postgres 1

登錄查看

postgres=# show pool_nodes;
node_id | hostname | port | status | lb_weight |  role   
---------+----------+------+--------+-----------+---------
 0       | db1      | 5432 | 2      | 0.500000  | primary
 1       | db2      | 5432 | 2      | 0.500000  | standby
(2 rows)


恢復正常
Game Over !




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章