編輯:關於 Android 開發
PostgreSQL 的高可用方案有很多種。本文示範的是「一主多從 + 讀取負載均衡」的集群架構,但並不直接使用 Pacemaker 原生的 pgsql RA 腳本,而是採用作者修改與包裝後的腳本集 pha4pgsql:
以下動作需在所有節點執行。
時鐘同步:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ntpdate time.windows.com && hwclock -w
設定獨立主機名稱:
hostnamectl set-hostname node1
設定域名解析(/etc/hosts):
192.168.0.231 node1 192.168.0.232 node2 192.168.0.233 node3
停用 SELINUX:
setenforce 0 vi /etc/selinux/config # 將 SELINUX=disabled
停用防火牆:
systemctl disable firewalld.service systemctl stop firewalld.service
若必須啟用防火牆,至少要開放這三個連接埠:
安裝套件(所有節點):
yum install -y pacemaker corosync pcs ipvsadm
啟用 pcsd 服務(所有節點):
systemctl start pcsd.service systemctl enable pcsd.service
設定 hacluster 使用者密碼(所有節點):
echo hacluster | passwd hacluster --stdin
叢集認證(任一節點):
pcs cluster auth -u hacluster -p hacluster node1 node2 node3
同步設定(任一節點):
pcs cluster setup --last_man_standing=1 --name pgcluster node1 node2 node3
啟動叢集(任一節點):
pcs cluster start --all
需安裝 9.2 以上版本,本文以官方 yum 源在 CentOS 7.3 上安裝 PostgreSQL 9.6:https://yum.postgresql.org/
安裝(所有節點):
yum install -y https://yum.postgresql.org/9.6/redhat/rhel-7.3-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install -y postgresql96 postgresql96-contrib postgresql96-libs \
postgresql96-server postgresql96-devel
ln -sf /usr/pgsql-9.6 /usr/pgsql
echo 'export PATH=/usr/pgsql/bin:$PATH' >> ~postgres/.bash_profile
建立 Master 資料庫(僅在 node1 執行):
# 1. 建立資料目錄 mkdir -p /pgsql/data chown -R postgres:postgres /pgsql/ chmod 0700 /pgsql/data # 2. 初始化 db su - postgres initdb -D /pgsql/data/
3. 修改 postgresql.conf:
listen_addresses = '*' wal_level = hot_standby wal_log_hints = on synchronous_commit = on max_wal_senders = 5 wal_keep_segments = 32 hot_standby = on wal_sender_timeout = 5000 wal_receiver_status_interval = 2 max_standby_streaming_delay = -1 max_standby_archive_delay = -1 restart_after_crash = off hot_standby_feedback = on
其中 wal_log_hints = on 是必要設定,因為後續要用 pg_rewind 修復舊 Master。
4. 修改 pg_hba.conf:
local all all trust host all all 192.168.0.0/24 md5 host replication all 192.168.0.0/24 md5
# 5. 啟動 pg_ctl -D /pgsql/data/ start # 6. 建立複製使用者(加 -s 才能支援 pg_rewind) createuser --login --replication replication -P -s
建立 Slave 資料庫(在 node2 與 node3 執行):
mkdir -p /pgsql/data chown -R postgres:postgres /pgsql/ chmod 0700 /pgsql/data su - postgres pg_basebackup -h node1 -U replication -D /pgsql/data/ -X stream -P
最後在 node1 上停掉 PostgreSQL,把資源交給 Pacemaker 管理:
pg_ctl -D /pgsql/data/ stop
在任一節點執行:
# 1. 下載 cd /opt git clone git://github.com/ChenHuajun/pha4pgsql.git # 2. 複製設定檔範本 cd /opt/pha4pgsql cp template/config_muti_with_lvs.ini.sample config.ini
若不需要基於 LVS 的負載均衡,改用 config_muti.ini.sample 即可。
3. 修改 config.ini:
pcs_template=muti_with_lvs.pcs.template OCF_ROOT=/usr/lib/ocf RESOURCE_LIST="msPostgresql vip-master vip-slave" pha4pgsql_dir=/opt/pha4pgsql writer_vip=192.168.0.236 reader_vip=192.168.0.237 node1=node1 node2=node2 node3=node3 othernodes="" vip_nic=ens37 vip_cidr_netmask=24 pgsql_pgctl=/usr/pgsql/bin/pg_ctl pgsql_psql=/usr/pgsql/bin/psql pgsql_pgdata=/pgsql/data pgsql_pgport=5432 pgsql_restore_command="" pgsql_rep_mode=sync pgsql_repuser=replication pgsql_reppassord=replication
# 4. 安裝(install.sh 以 scp 分發檔案,會多次要求輸入其他節點的 root 密碼) sh install.sh ./setup.sh # 5. 設定環境變數 export PATH=/opt/pha4pgsql/bin:$PATH echo 'export PATH=/opt/pha4pgsql/bin:$PATH' >> /root/.bash_profile # 6. 啟動與檢查 cls_start cls_status
install.sh 執行完會產生 Pacemaker 的設定腳本 /opt/pha4pgsql/config.pcs,建議先檢視並依實際情況調優參數,再執行 setup.sh。
cls_status 的健康輸出範例:
Stack: corosync
Current DC: node1 (version 1.1.15-11.el7_3.2-e174ec8) - partition with quorum
Online: [ node1 node2 node3 ]
Full list of resources:
vip-master (ocf::heartbeat:IPaddr2): Started node1
vip-slave (ocf::heartbeat:IPaddr2): Started node2
Master/Slave Set: msPostgresql [pgsql]
Masters: [ node1 ]
Slaves: [ node2 node3 ]
lvsdr (ocf::heartbeat:lvsdr): Started node2
Clone Set: lvsdr-realsvr-clone [lvsdr-realsvr]
Started: [ node2 node3 ]
Stopped: [ node1 ]
Node Attributes:
* Node node1: + master-pgsql : 1000 + pgsql-data-status : LATEST
+ pgsql-status : PRI
* Node node2: + master-pgsql : 100 + pgsql-data-status : STREAMING|SYNC
+ pgsql-status : HS:sync
* Node node3: + master-pgsql : -INFINITY + pgsql-data-status : STREAMING|ASYNC
+ pgsql-status : HS:async
pgsql_REPL_INFO:node1|1|00000000050001B0
一個「完全健康」的叢集需同時滿足:
STREAMING|SYNC:同步複製 SlaveSTREAMING|POTENTIAL:候選同步複製 SlaveSTREAMING|ASYNC:異步複製 Slavepgsql_REPL_INFO 的三段內容分別是:當前 Master、上次提升前的事時間線、xlog 位置。
LVS 此時配置在 node2 上:
[root@node2 ~]# ipvsadm -L IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP node2:postgres rr -> node2:postgres Route 1 0 0 -> node3:postgres Route 1 0 0
在 node1 上停掉網卡模擬故障:
[root@node1 pha4pgsql]# ifconfig ens37 down
檢查叢集狀態,Pacemaker 已把 Master 與寫入 VIP 切換到 node2:
[root@node2 ~]# cls_status
resource msPostgresql is NOT running
Online: [ node2 node3 ]
OFFLINE: [ node1 ]
vip-master (ocf::heartbeat:IPaddr2): Started node2
vip-slave (ocf::heartbeat:IPaddr2): Started node3
Master/Slave Set: msPostgresql [pgsql]
Masters: [ node2 ]
Slaves: [ node3 ]
Stopped: [ node1 ]
* Node node2: + pgsql-status : PRI + pgsql-data-status : LATEST
* Node node3: + pgsql-status : HS:sync + pgsql-data-status : STREAMING|SYNC
pgsql_REPL_INFO:node2|2|00000000050008E0
LVS 與讀取 VIP 也一起移到了 node3:
[root@node3 ~]# ipvsadm -L TCP node3:postgres rr -> node3:postgres Route 1 0 0
修復舊 Master。此時 node1 上的 postgres 行程可能還在(這是 ifconfig down 模擬故障的特殊現象,實體機宕機或網卡故障時,故障節點會因失去 quorum 而被 Pacemaker 主動停止)。由於採用同步複製,資料無法寫入,因此不會發生腦裂。啟動網卡後 postgres 行程即被停止。
接著用 pg_rewind 把舊 Master 修成新 Slave 重新加入叢集:
[root@node1 pha4pgsql]# cls_repair_by_pg_rewind connected to server servers diverged at WAL position 0/50008E0 on timeline 2 rewinding from last common checkpoint at 0/5000838 on timeline 2 reading source file list reading target file list reading WAL in target need to copy 99 MB (total source directory size is 117 MB) 102359/102359 kB (100%) copied creating backup label and updating control file syncing target data directory Done! pg_rewind complete! wait for recovery complete..... slave recovery of node1 successed
此時 node1 已成為異步 Slave,叢集恢復 1 主 2 從:
Master/Slave Set: msPostgresql [pgsql]
Masters: [ node2 ]
Slaves: [ node1 node3 ]
* Node node1: + pgsql-data-status : STREAMING|ASYNC
* Node node2: + pgsql-data-status : LATEST
* Node node3: + pgsql-data-status : STREAMING|SYNC
LVS 在 node3 上,掛著兩個 real server:
[root@node3 ~]# ipvsadm -L TCP node3:postgres rr -> node2:postgres Route 1 0 0 -> node3:postgres Route 1 0 0
在 node1 上停網卡:
[root@node1 pha4pgsql]# ifconfig ens37 down
Pacemaker 自動更新 LVS 的 real server 清單,把故障節點摘除:
[root@node3 ~]# ipvsadm -L TCP node3:postgres rr -> node3:postgres Route 1 0 0
目前的架構是 1 主 2 從,兩個 Slave 透過「讀取 VIP + LVS」做讀取負載均衡。當讀取壓力變高時,可以加更多 Slave 來擴充。
直接把大量 Slave 都塞進 Pacemaker 叢集雖然可行,但節點數一多,Pacemaker + Corosync 的複雜度與通訊負擔都會上升(Corosync 的通訊是環路,節點越多延遲越大)。因此更好的做法是:額外的 Slave 不加入 Pacemaker,只加入 LVS 的 real server 清單,並讓 lvsdr 負責監控健康狀態、動態更新清單。
setenforce 0
# vi /etc/selinux/config → SELINUX=disabled
systemctl disable firewalld.service
systemctl stop firewalld.service
yum install -y https://yum.postgresql.org/9.6/redhat/rhel-7.3-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install -y postgresql96 postgresql96-contrib postgresql96-libs \
postgresql96-server postgresql96-devel
ln -sf /usr/pgsql-9.6 /usr/pgsql
echo 'export PATH=/usr/pgsql/bin:$PATH' >> ~postgres/.bash_profile
mkdir -p /pgsql/data
chown -R postgres:postgres /pgsql/
chmod 0700 /pgsql/data
# 從當前 Master(寫入 VIP)拉取備份
su - postgres
pg_basebackup -h 192.168.0.236 -U replication -D /pgsql/data/ -X stream -P
接著編輯 postgresql.conf,把由 RA 自動加入的這一行刪掉:
#include '/var/lib/pgsql/tmp/rep_mode.conf' # added by pgsql RA
編輯 recovery.conf,application_name 設為本節點 IP:
standby_mode = 'on'
primary_conninfo = 'host=192.168.0.236 port=5432 application_name=192.168.0.234
user=replication password=replication
keepalives_idle=60 keepalives_interval=5 keepalives_count=5'
restore_command = ''
recovery_target_timeline = 'latest'
pg_ctl -D /pgsql/data/ start
回到 Master 檢查 wal sender 行程,可看到新 Slave 已建立串流複製:
[root@node1 pha4pgsql]# ps -ef | grep '[w]al sender' postgres 32387 ... postgres: wal sender process replication 192.168.0.234(47894) streaming 0/7000220 postgres 116675 ... postgres: wal sender process replication 192.168.0.233(33652) streaming 0/7000220 postgres 117079 ... postgres: wal sender process replication 192.168.0.232(40088) streaming 0/7000220
在 node4 上調整核心參數,並把讀取 VIP 掛到 lo 網卡(LVS DR 模式的標準做法):
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce ip a add 192.168.0.237/32 dev lo:0
修改 lvsdr 資源的設定,讓它透過腳本取得 real server 清單:
[root@node2 ~]# pcs resource update lvsdr \ realserver_get_real_servers_script="/opt/pha4pgsql/tools/get_active_slaves \ /usr/pgsql/bin/psql \"host=192.168.0.236 port=5432 dbname=postgres \ user=replication password=replication connect_timeout=5\""
get_active_slaves 會透過寫入 VIP 連到 Master,把所有已串流連上的 Slave 的 application_name 當成 real server 清單。設定後新節點自動入列:
[root@node2 ~]# ipvsadm -L TCP node2:postgres rr -> node2:postgres Route 1 0 0 -> node3:postgres Route 1 0 0 -> 192.168.0.234:postgres Route 1 0 0
連續透過讀取 VIP 查詢 pg_postmaster_start_time(),可以看到請求被輪詢分配到不同 Slave:
[root@node1 pha4pgsql]# psql "host=192.168.0.237 port=5432 dbname=postgres \
user=replication password=replication" -tAc "select pg_postmaster_start_time()"
2017-01-14 12:01:48.068455+08
2017-01-14 12:01:12.222412+08
2017-01-14 12:15:19.614782+08
三個不同的啟動時間,對應三台不同的機器。
再測試 Slave 故障的情境:先連上其中一台 Slave,然後強制結束它的 postgres 行程:
[root@node4 ~]# killall postgres
lvsdr 探測到 node4 失效後會自動將其移出清單:
[root@node2 ~]# ipvsadm -L TCP node2:postgres rr -> node2:postgres Route 1 0 0 -> node3:postgres Route 1 0 0
psql 執行下一條 SQL 時,連線被中斷後會自動重連到其他 Slave:
postgres=# select pg_postmaster_start_time(); FATAL: terminating connection due to administrator command server closed the connection unexpectedly The connection to the server was lost. Attempting reset: Succeeded. postgres=# select pg_postmaster_start_time(); pg_postmaster_start_time ------------------------------- 2017-01-14 12:01:48.068455+08 (1 row)
並非所有連到 Master 的節點都適合參與讀取負載均衡——例如某台實際上是 pg_receivexlog 的歸檔機。這時可以用白名單排除。
方法一(推薦):仍用「查詢 Master 上的 Slave 清單」做健康檢查,但把不想參與的節點權重設為 0。
pcs resource update lvsdr default_weight="0" pcs resource update lvsdr weight_of_realservers="node1,1 node2,1 node3,1" pcs resource update lvsdr realserver_get_real_servers_script="/opt/pha4pgsql/tools/get_active_slaves \ /usr/pgsql/bin/psql \"host=192.168.0.236 port=5432 dbname=postgres \ user=replication password=replication connect_timeout=5\""
檢查後可看到 node4 的權重為 0,LVS 不會再往它轉發請求:
[root@node2 ~]# ipvsadm -L TCP node2:postgres rr -> node2:postgres Route 1 0 0 -> node3:postgres Route 1 0 0 -> 192.168.0.234:postgres Route 0 0 0
方法二:指定靜態清單,並用 check_active_slave 逐一連線檢查每個節點是否可連且確實為 Slave。
pcs resource update lvsdr default_weight="1" pcs resource update lvsdr weight_of_realservers="node1 node2 node3 192.168.0.234" pcs resource update lvsdr realserver_dependent_resource="" pcs resource update lvsdr realserver_get_real_servers_script="" pcs resource update lvsdr realserver_check_active_real_server_script="/opt/pha4pgsql/tools/check_active_slave \ /usr/pgsql/bin/psql \"port=5432 dbname=postgres user=replication \ password=replication connect_timeout=5\" -h"
一般建議採用方法一,因為每次健康檢查只需要一次連線,node 數量多時差異明顯。
Android GUI 系統之 SurfaceFlinger(五):Vsync 與合成流程解析
Android GUI 系統之 SurfaceFlinger(五):Vsync 與合成流程解析 9. Vsync 第二部分 上一篇提到,視圖的刷新需要經過許多步驟: vo
Android 新增聯絡人實作:透過 ContentProvider 寫入 raw_contacts 與 data
Android 新增聯絡人實作:透過 ContentProvider 寫入 raw_contacts 與 data 上一篇〈Android 獲取系統的聯繫人〉把讀取的路徑
Android加固教程:先學會破解,用IDA動態調試修改內存改寫程式走向
Android加固教程:先學會破解,用IDA動態調試修改內存改寫程式走向 【版權所有,轉載請註明出處。】 因公司專案需要對app進行加固,經過數月研究,我實現了一套完整的
Android 專案目錄結構詳解:以 HelloWorld 為例逐一剖析
Android 專案目錄結構詳解:以 HelloWorld 為例逐一剖析 初次接觸 Android 開發時,理解專案的目錄結構是必經的一課。本文以一個 HelloWorl