node1:192.168.1.121 CentOS 6.7 node2:192.168.1.122 CentOS 6.7 node3:192.168.1.123 CentOS 6.7 vip :192.168.1.80
三台節點的 hosts 必須一致,並建立彼此可免密登入的 SSH 通道:
# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.121 node1 192.168.1.122 node2 192.168.1.123 node3 # ssh-keygen -t rsa -P '' # ssh-copy-id -i ~/.ssh/id_rsa.pub node1 # ssh-copy-id -i ~/.ssh/id_rsa.pub node2 # ssh-copy-id -i ~/.ssh/id_rsa.pub node3
用 ansible 批量把設定推下去,省得一台台手動:
# rpm -ivh epel-release-latest-6.noarch.rpm # yum -y install ansible # cat /etc/ansible/hosts [ha] 192.168.1.121 192.168.1.122 192.168.1.123 # ansible ha -m copy -a 'src=/etc/hosts dest=/etc' # ansible ha -m shell -a 'ntpdate 192.168.1.62' # ansible ha -m cron -a 'minute="*/3" job="/usr/sbin/ntpdate 192.168.1.62" name="ntpdate"' # ansible ha -m copy -a 'src=/root/heartbeat2 dest=/root'
時間同步在高可用架構裡是硬性要求:heartbeat 靠心跳判斷節點存活性,時鐘漂移會直接造成誤判與腦裂。
node1、node2、node3 都要執行:
# yum -y install net-snmp-libs libnet PyXML perl-Time-Date
# ls heartbeat2/
heartbeat-2.1.4-12.el6.x86_64.rpm # 主程式包
heartbeat-debuginfo-2.1.4-12.el6.x86_64.rpm
heartbeat-devel-2.1.4-12.el6.x86_64.rpm
heartbeat-gui-2.1.4-12.el6.x86_64.rpm # 提供 hb-gui,可先不裝
heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm # 後端健康檢測工具
heartbeat-pils-2.1.4-12.el6.x86_64.rpm
heartbeat-stonith-2.1.4-12.el6.x86_64.rpm # 仲裁設備、節點隔離
# cd heartbeat2/
# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm \
heartbeat-pils-2.1.4-12.el6.x86_64.rpm \
heartbeat-stonith-2.1.4-12.el6.x86_64.rpm
[root@node1 ha.d]# cp /usr/share/doc/heartbeat-2.1.4/{ha.cf,haresources,authkeys} /etc/ha.d/
[root@node1 ha.d]# cd /etc/ha.d/
[root@node1 ha.d]# chmod 600 authkeys
authkeys:產生一組驗證金鑰,並把預設的 auth 1 註解掉、改用 sha1 的 auth 2。
[root@node1 ha.d]# openssl rand -base64 4 nuGXcw== #auth 1 #2 sha1 HI! # 改為 auth 2 2 sha1 nuGXcw==
ha.cf:主要改四處。
#logfile /var/log/ha-log 改為 logfile /var/log/ha-log logfacility local0 改為 #logfacility local0 #mcast eth0 225.0.0.1 694 1 0 改為 mcast eth0 225.23.190.1 694 1 0 # 在 node kathy(約 212 行)下新增 node node1 node node2 node node3 # 在 #ping 10.10.10.254(約 223 行)下新增 ping 192.168.1.1 # 仲裁設備 # 解除註解 compression bz2 compression_threshold 2
ping 那一行指向的仲裁節點是 heartbeat 判斷「自己是不是被網路孤立」的依據,務必填一個穩定可達的閘道位址,否則容易誤觸發資源搬移。
haresources:定義哪台當主、要管理哪些資源。
[root@node1 ha.d]# vim haresources # 在末尾添加 node1 192.168.1.80/16/eth0/192.168.255.255 httpd
這行的意思是:node1 為主,啟用 VIP 192.168.1.80(掩碼 16、綁 eth0、廣播 192.168.255.255),並啟動 httpd。
[root@node1 ha.d]# scp -p authkeys ha.cf haresources node2:/etc/ha.d [root@node1 ha.d]# scp -p authkeys ha.cf haresources node3:/etc/ha.d
[root@node1 ~]# vim /var/www/html/index.html <h1>node1.magedu.com</h1> [root@node1 ~]# service httpd stop [root@node1 ~]# chkconfig httpd off # node2、node3 同樣處理,內容分別改為 node2 / node3
關鍵觀念:httpd 一定要關掉並取消開機自啟,資源的啟動與切換完全交給 heartbeat,否則兩邊都跑 httpd 會衝突。
[root@node1 ha.d]# ansible ha -m service -a 'name=heartbeat state=started'
此後訪問 VIP 就能看到 node1 的頁面;把 node1 停掉,VIP 會自動漂到 node2。
# 使 node1 變成備用結點 [root@node1 heartbeat]# /usr/lib64/heartbeat/hb_standby # 使 node1 變回主結點 [root@node1 heartbeat]# /usr/lib64/heartbeat/hb_takeover
在 node3 上建立 NFS 分享:
[root@node3 ~]# mkdir /web/htdocs -pv [root@node3 ~]# vim /web/htdocs/index.html <h1>Page On NFS Server</h1> [root@node3 ~]# vim /etc/exports /web/htdocs 192.168.1.0/24(rw,no_root_squash) [root@node3 ~]# service nfs start
停掉兩台 heartbeat 後改 haresources,把掛載與 httpd 串在一起:
[root@node1 heartbeat]# service heartbeat stop;ssh node2 'service heartbeat stop'
[root@node1 ~]# cd /etc/ha.d/
[root@node1 ha.d]# vim haresources
# 在末尾添加
node1 192.168.1.80/16/eth0/192.168.255.255 \
Filesystem::192.168.1.123::/web/htdocs::/var/www/html::nfs \
httpd
[root@node1 ha.d]# service heartbeat start;ssh node2 'service heartbeat start'
這行的資源順序就是啟動順序:先掛 VIP,再掛 NFS 到 /var/www/html,最後起 httpd;關閉時則完全相反。順序寫錯是 heartbeat 資源啟動失敗最常見的原因——例如 httpd 寫在 Filesystem 之前,它會在網頁目錄還是空的情況下啟動。
另外提醒:正式環境更推薦 crmsh + corosync 這套新架構,heartbeat 2.x 已屬維護階段;而 NFS 單點本身也是瓶頸,進階做法會再引入 DRBD 或磁碟陣列來消除單點。