- 可口可樂的秘方:異地備份
所以可口可樂的秘方需要做的是「異地備份」。
***就是每隔一段時間找個地方把資料另外存一份,NBU?
- 我一秒鐘幾十萬上下:即時備援
如果一秒鐘幾十萬上下,需要的是一個「生意不停頓」Business non-stop系統,知道嗎!
***資料要能同步,而且在Master掛了後馬上能接手。
- 副總統的責任:異地備援
正副總統不但不可以同時搭一班飛機,而且正總統一秒鐘何只幾十萬上下,所以副總統只能「遠遠地」老實的待著,乖乖地做正總統的「異地備援」。
***資料同步能接受delay,但資料所在位置不能在同一棟大樓或區域。另外在原有的機器掛的時後也能接手工作。
回到正題:
為了模擬即時備援+異地備援,我利用VirtualBox實作了Synchronous Hot Standby 的PostgreSQL,在這裡把過程記錄下來。
環境:
==============================================
OS : CentOS 6.3
DB : Postgres Plus Advanced Server (9.2.4.8)
Network : EDBNode1 (172.16.1.124) - Master Node
EDBNode2 (172.16.1.125) - Slave Node
==============================================
1.安裝PPAS。這Google上很多,而且有GUI很容易裝,所以就跳過囉!!只記錄一下重要的:
a.因為安裝好後會有postgres這個user,我們要利用這個user來做xlog的複製,所以我們要先改一下postgres的home目錄
# vi /etc/passwd
---------------------------------------------------------------------------------------------------
postgres:x:501:501:Postgres Plus Advanced Server:/opt/PostgresPlus/9.2AS:/bin/bash
改為
postgres:x:501:501:Postgres Plus Advanced Server:/home/postgres:/bin/bash
---------------------------------------------------------------------------------------------------
b.建立postgres的home目錄,並給予適當權限
# mkdir /home/postgres
# chown -R postgres.postgres /home/postgres
# chmod -R 700 /home/postgres
2.設定rsync (兩台都要做):
# su - postgres
# mkdir /home/postgres/pg_archive
# chmod -R 775 /home/postgres/pg_archive
# ssh-keygen -t rsa #過程中一直按enter就可以
# scp ~/.ssh/id_rsa.pub root@172.16.1.125:/tmp/ #記得ip要換,在124那台這裡要打125囉,反過來亦同。以上步驟在兩台都做完後再往下做才不會有問題喔!!
# cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
# chmod 700 ~/.ssh/authorized_keys
3.設定Master Node (172.16.1.124)
# cd /opt/PostgresPlus/9.2AS/data
# vi postgresql.conf
---------------------------------------------------------------------------------------------------
wal_level = hot_standby
archive_mode = on
archive_command = 'rsync %p postgres@172.16.1.125:/home/postgres/pg_archive/%f'
max_wal_senders = 1
synchronous_commit = on
synchronous_standby_names = 'edb_standby1'
---------------------------------------------------------------------------------------------------
4.設定Slave Node (172.16.1.125)
# cd /opt/PostgresPlus/9.2AS
# mv data data.bak
# cd /opt/PostgresPlus/9.2AS/bin
# ./pg_basebackup -h 172.16.1.124 -p 5432 -U postgres -D /opt/PostgresPlus/9.2AS/data
# chown -R postgres.postgres /opt/PostgresPlus/9.2AS/data
# cd /opt/PostgresPlus/9.2AS/data
# vi postgresql.conf
---------------------------------------------------------------------------------------------------
hot_standby = on
---------------------------------------------------------------------------------------------------
# vi recovery.conf
---------------------------------------------------------------------------------------------------
standby_mode = on
restore_command = 'cp /home/postgres/pg_archive/%f %p'
primary_conninfo = 'host=172.16.1.124 port=5432 user=postgres password=你的密碼 application_name=edb_standby1'
---------------------------------------------------------------------------------------------------
5.啟動Master and Slave Node後你可以登到Master Node去新增DB, Table, Data看看同步的結果.
# /etc/init.d/ppas-9.2 start | restart
Node2
6. Fail-Over的作法(當172.16.1.124掛掉時的做法,可利用shell script配合HA的機制
a.在Slave Node (172.16.1.125) 執行以下指令
# vi postgresql.conf
---------------------------------------------------------------------------------------------------
wal_level = hot_standby
archive_mode = on
archive_command = 'rsync %p postgres@172.16.1.124:/home/postgres/pg_archive/%f'
max_wal_senders = 1
synchronous_commit = on
synchronous_standby_names = 'edb_standby1'
---------------------------------------------------------------------------------------------------
# pg_ctl promote -D /opt/PostgresPlus/9.2AS/data #會看到原來的recovery.conf變成recovery.done
# /etc/init.d/ppas-9.2 restart
b.在原來的 Master Node (172.16.1.124) 執行以下動作
# cd /opt/PostgresPlus/9.2AS
# mv data data.bak
# cd /opt/PostgresPlus/9.2AS/bin
# ./pg_basebackup -h 172.16.1.125 -p 5432 -U postgres -D /opt/PostgresPlus/9.2AS/data
# chown -R postgres.postgres /opt/PostgresPlus/9.2AS/data
# cd /opt/PostgresPlus/9.2AS/data
# vi postgresql.conf
---------------------------------------------------------------------------------------------------
hot_standby = on
---------------------------------------------------------------------------------------------------
# vi recovery.conf
---------------------------------------------------------------------------------------------------
standby_mode = on
restore_command = 'cp /home/postgres/pg_archive/%f %p'
primary_conninfo = 'host=172.16.1.125 port=5432 user=postgres password=你的密碼 application_name=edb_standby1'
---------------------------------------------------------------------------------------------------
# /etc/init.d/ppas-9.2 start
完成上述動作後兩台就反過來囉125變Master,124變Slave。
n.與上述不同的另一種做法(一次做三台,一台Master兩台Slave,其中一台Slave模擬在異地,我家老大提出來的,大家可以參考看看):
a.先將所有機器(include Master and slaves)的postgres.conf設定如下:
# vi postgresql.conf
---------------------------------------------------------------------------------------------------
hot_standby = on
wal_level = hot_standby
max_wal_senders = 1
synchronous_commit = on
synchronous_standby_names = 'edb_standby1, edb_standby2'
---------------------------------------------------------------------------------------------------
b.除了Master Node外每台Slave都加上recovery.conf,內容如下:
# vi recovery.conf
---------------------------------------------------------------------------------------------------
standby_mode = on
primary_conninfo = 'host=MasterNodeIP port=5432 user=postgres password=你的密碼 application_name=edb_standby1'
recovery_target_timeline='latest'
---------------------------------------------------------------------------------------------------
c.如果第一次建置,可以在Master Node啟動後做一次BaseBackup,在建立recovery.conf
# ./pg_basebackup -h MasterNodeIP -p 5432 -U postgres -D /opt/PostgresPlus/9.2AS/data
d.Fail-over:
$Promote 1st Slave Node to Master.
# ./pg_ctl promote -D /opt/PostgresPlus/9.2AS/data
$接著將所有的Slave(不包含原來的Master)做以下的更動:
# vi recovery.conf
---------------------------------------------------------------------------------------------------
standby_mode = on
primary_conninfo = 'host=改為新的MasterNodeIP port=5432 user=postgres password=你的密碼 application_name=edb_standby1'
recovery_target_timeline='latest'
---------------------------------------------------------------------------------------------------
$把新MasterNode下的tx log全部同步到每一台Slave
# rsync -ar 新的MasterNode:/opt/PostgresPlus/9.2AS/data/pg_xlog/ SlaveNode:/opt/PostgresPlus/9.2AS/data/pg_xlog/
$重啟所有的Slave
# /etc/init.d/ppas-9.2 restart
***我個人對以上這些方案覺得怪,怪的原因在於,當Master掛掉後,雖然Promote第一台Slave很容易(可透過RHCS之類的HA機制來handle),但仍然要對所有的Slave再進行一次同步tx log跟restart覺得很不罩,為什麼不能有類似PGPOOL-II的機制,能在Slave接手後,其它都仍會自動再同步呢??還是我對PGP有誤解(它其實不能?),有人能跟我說的話我會很感恩。
沒有留言:
張貼留言