本文詳細記錄如何在 CentOS 6.6 環境下,利用 Heartbeat 搭配 Nginx、MySQL(MariaDB)與 NFS 共享存儲,構建一套 Web 與 SQL 服務的高可用集群。架構採用雙節點主備模式,透過虛擬 IP (VIP) 漂移實現故障自動切換,並引入 NFS 解決數據一致性問題,最終加入 MariaDB 達到 Web+SQL 全棧高可用。
| 角色 | 主機名 | IP 地址 | 說明 |
|---|---|---|---|
| Node1 (主) | ZhongH100.wxjr.com.cn | 172.16.6.100 | 運行 Heartbeat、Nginx、MariaDB |
| Node2 (備) | ZhongH101.wxjr.com.cn | 172.16.6.101 | 運行 Heartbeat、Nginx、MariaDB |
| NFS Server | ZhongH102.wxjr.com.cn | 172.16.6.102 | 提供共享存儲 /home/wwwroot/data 及 /data |
| VIP | 172.16.7.200 | 對外服務虛擬 IP,掛載於 eth0 | |
基礎條件: 兩節點主機名互解、SSH 互信、時間同步(crontab ntpdate)、關閉 SELinux 及防火牆、配置好 yum/epel 源。

在 Node1、Node2 分別編輯 /etc/hosts 加入對方記錄,並建立 crontab 每 5 分鐘同步一次時間:
[root@ZhongH100 ~]# echo "172.16.6.101 ZhongH101.wxjr.com.cn" >> /etc/hosts [root@ZhongH100 ~]# crontab -l */5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
Node2 同理,指向 Node1。
雙向生成密鑰並分發公鑰,實現無密碼登錄:
[root@ZhongH100 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' [root@ZhongH100 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@ZhongH101.wxjr.com.cn # Node2 執行相同操作指向 Node1
[root@ZhongH ~]# yum -y install heartbeat*
[root@ZhongH ~]# wget https://nginx.org/download/nginx-1.9.1.tar.gz
[root@ZhongH ~]# tar xf nginx-1.9.1.tar.gz && cd nginx-1.9.1
# 建立 www 用戶/組(UID/GID 1000-1500 區間自動選取)
[root@ZhongH nginx-1.9.1]# Username="www" && for i in `seq 1000 1500`; do [ -z "$(awk -F: '{print$3,$4}' /etc/passwd | grep "$i")" -a -z "$(awk -F: '{print$3}' /etc/group | grep "$i")" ] && UGID=$i && break; done && groupadd -g $UGID $Username && useradd -M -u $UGID -g $UGID -s /sbin/nologin $Username
[root@ZhongH nginx-1.9.1]# yum install pcre-devel pcre -y
[root@ZhongH nginx-1.9.1]# mkdir -p /tmp/nginx /var/run/nginx /var/lock
[root@ZhongH nginx-1.9.1]# ./configure --prefix=/usr/local/nginx/ --user=www --group=www \
--error-log-path=/tmp/nginx/error.log --http-log-path=/tmp/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock \
--with-pcre --with-http_ssl_module --with-http_flv_module \
--with-http_spdy_module --with-http_gzip_static_module \
--with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client/ \
--http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi
[root@ZhongH nginx-1.9.1]# make -j $(grep -c processor /proc/cpuinfo) && make install && echo $?
[root@ZhongH nginx-1.9.1]# echo "export PATH=/usr/local/nginx/sbin:\$PATH" > /etc/profile.d/nginx1.9.1.sh
[root@ZhongH nginx-1.9.1]# . /etc/profile.d/nginx1.9.1.sh
# 配置開機不自啟,改由 Heartbeat 托管
[root@ZhongH ~]# wget https://www.dwhd.org/script/Nginx-init-CentOS -O /etc/rc.d/init.d/nginx
[root@ZhongH ~]# chmod +x /etc/rc.d/init.d/nginx && chkconfig --add nginx && chkconfig nginx on && chkconfig nginx off
[root@ZhongH ~]# sed -i '$ a \ include vhost/*.conf;' /usr/local/nginx/conf/nginx.conf
[root@ZhongH ~]# mkdir -p /usr/local/nginx/conf/vhost
[root@ZhongH100 ~]# mkdir -p /home/wwwroot/ZhongH100.wxjr.com.cn
[root@ZhongH100 ~]# cat > /usr/local/nginx/conf/vhost/ZhongH100.wxjr.com.cn.conf <<'EOF'
server {
listen 80;
server_name ZhongH100.wxjr.com.cn;
root /home/wwwroot/ZhongH100.wxjr.com.cn;
index index.html index.php index.htm;
access_log /tmp/nginx/ZhongH100.wxjr.com.cn_nginx.log combined;
}
EOF
[root@ZhongH100 ~]# echo "Hello node1" > /home/wwwroot/ZhongH100.wxjr.com.cn/index.html
[root@ZhongH100 ~]# /etc/init.d/nginx configtest && /etc/init.d/nginx restart
[root@ZhongH101 ~]# mkdir -p /home/wwwroot/ZhongH101.wxjr.com.cn
[root@ZhongH101 ~]# cat > /usr/local/nginx/conf/vhost/ZhongH101.wxjr.com.cn.conf <<'EOF'
server {
listen 80;
server_name ZhongH101.wxjr.com.cn;
root /home/wwwroot/ZhongH101.wxjr.com.cn;
index index.html index.php index.htm;
access_log /tmp/nginx/ZhongH101.wxjr.com.cn_nginx.log combined;
}
EOF
[root@ZhongH101 ~]# echo "Hello node2" > /home/wwwroot/ZhongH101.wxjr.com.cn/index.html
[root@ZhongH101 ~]# /etc/init.d/nginx configtest && /etc/init.d/nginx restart
分別驗證兩節點 80 端口監聽正常,瀏覽器訪問各自域名可見對應歡迎頁。

[root@ZhongH100 ~]# cp /usr/share/doc/heartbeat-*/{authkeys,ha.cf,haresources} /etc/ha.d/
[root@ZhongH100 ~]# echo -e "auth 1\n1 md5 $(cat /dev/urandom | head | openssl md5 | awk '{print $2}')" >> /etc/ha.d/authkeys
[root@ZhongH100 ~]# chmod 600 /etc/ha.d/authkeys
[root@ZhongH100 ~]# cat > /etc/ha.d/ha.cf <<'EOF' logfacility local0 mcast eth0 225.172.16.1 694 1 0 auto_failback on node ZhongH100.wxjr.com.cn node ZhongH101.wxjr.com.cn EOF
[root@ZhongH100 ~]# echo "ZhongH100.wxjr.com.cn IPaddr::172.16.7.200/16/eth0 nginx" > /etc/ha.d/haresources
[root@ZhongH100 ~]# scp /etc/ha.d/{authkeys,ha.cf,haresources} ZhongH101.wxjr.com.cn:/etc/ha.d/
[root@ZhongH100 ~]# /etc/init.d/heartbeat start
[root@ZhongH100 ~]# ssh ZhongH101.wxjr.com.cn "/etc/init.d/heartbeat start"
啟動後 VIP 掛載於 Node1,Nginx 運行。模擬 Node1 故障:
[root@ZhongH100 ~]# /etc/init.d/heartbeat stop
觀察 VIP 已漂移至 Node2(ip addr show 可見 172.16.7.200/16 secondary),Nginx 於 Node2 啟動。瀏覽器訪問 VIP 顯示 Node2 頁面,證明 Web 服務無縫切換。

痛點: 無共享存儲時,用戶上傳文件寫入本地磁盤,主節點故障導致文件「丟失」,用戶體驗極差。
兩大解決路徑:
[root@ZhongH102 ~]# mkdir -p /home/wwwroot/data [root@ZhongH102 ~]# echo "/home/wwwroot/data 172.16.0.0/8(ro,async)" >> /etc/exports [root@ZhongH102 ~]# echo 'Hello This is NFS Server
' > /home/wwwroot/data/index.html [root@ZhongH102 ~]# /etc/init.d/portreserve start && /etc/init.d/nfs start [root@ZhongH102 ~]# showmount -e 172.16.6.102
[root@ZhongH100 ~]# mount -t nfs 172.16.6.102:/home/wwwroot/data/ /usr/local/nginx/html [root@ZhongH101 ~]# mount -t nfs 172.16.6.102:/home/wwwroot/data/ /usr/local/nginx/html
[root@ZhongH100 ~]# cat > /etc/ha.d/haresources <<'EOF' ZhongH100.wxjr.com.cn IPaddr::172.16.7.200/8/eth0 Filesystem::172.16.6.102:/home/wwwroot/data::/usr/local/nginx/html::nfs nginx EOF [root@ZhongH100 ~]# scp /etc/ha.d/haresources 172.16.6.101:/etc/ha.d/ [root@ZhongH100 ~]# /etc/init.d/heartbeat restart && ssh 172.16.6.101 "/etc/init.d/heartbeat restart"
停止 Node1 Heartbeat,觀察 Node2 掛載 NFS、接管 VIP、啟動 Nginx,頁面正常顯示 NFS Server 內容,日誌顯示資源遷移成功。

[root@ZhongH102 ~]# echo "/data 172.16.0.0/16(rw,all_squash,anonuid=1500,anongid=1500)" >> /etc/exports [root@ZhongH102 ~]# groupadd -g 1500 mysql && useradd -g mysql -u 1500 -s /sbin/nologin -M mysql [root@ZhongH102 ~]# chown -R mysql.mysql /data/ [root@ZhongH102 ~]# /etc/init.d/nfs restart
[root@ZhongH100 ~]# yum groupinstall "Development tools" "Server Platform Development" -y [root@ZhongH100 ~]# cd /tmp && wget https://www.cmake.org/files/v3.2/cmake-3.2.2.tar.gz && tar xf cmake-3.2.2.tar.gz && cd cmake-3.2.2 && ./bootstrap && make && make install [root@ZhongH100 ~]# wget "https://downloads.mariadb.org/interstitial/mariadb-10.0.19/source/mariadb-10.0.19.tar.gz/from/http%3A//mirrors.opencas.cn/mariadb" -O mariadb-10.0.19.tar.gz [root@ZhongH100 ~]# tar xf mariadb-10.0.19.tar.gz && cd mariadb-10.0.19 [root@ZhongH100 mariadb-10.0.19]# groupadd -g 1500 mysql && useradd -g mysql -u 1500 -s /sbin/nologin -M mysql [root@ZhongH100 mariadb-10.0.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql ...(其餘編譯參數略) [root@ZhongH100 mariadb-10.0.19]# make -j $(grep -c processor /proc/cpuinfo) && make install [root@ZhongH100 mariadb-10.0.19]# mount -t nfs 172.16.6.102:/data/ /data [root@ZhongH100 mariadb-10.0.19]# cd /usr/local/mysql && cp support-files/mysql.server /etc/rc.d/init.d/mysqld && cp support-files/my-large.cnf /etc/my.cnf [root@ZhongH100 mysql]# sed -i '/query_cache_size/a datadir = /data/mysql' /etc/my.cnf [root@ZhongH100 mysql]# mkdir -p /data/mysql && /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ [root@ZhongH100 mysql]# service mysqld start # Node2 同步安裝包、配置文件,掛載 /data,啟動驗證後關閉,等待 Heartbeat 托管
[root@ZhongH100 ~]# cat > /etc/ha.d/haresources <<'EOF' ZhongH100.wxjr.com.cn IPaddr::172.16.7.200/16/eth0 Filesystem::172.16.6.102:/home/wwwroot/data::/usr/local/nginx/html::nfs Filesystem::172.16.6.102:/data::/data::nfs mysqld nginx EOF [root@ZhongH100 ~]# scp /etc/ha.d/haresources 172.16.6.101:/etc/ha.d/ [root@ZhongH100 ~]# /etc/init.d/heartbeat restart && ssh 172.16.6.101 "/etc/init.d/heartbeat restart"
Node1 持有 VIP、雙 NFS 掛載、MySQL 3306 端口、Nginx 80 端口。模擬 Node1 故障,Node2 秒級接管所有資源,Web 頁面與 MySQL 連接均正常。

本實踐完整展示了從基礎雙機熱備到引入共享存儲、再到數據庫層高可用的演進過程。關鍵點在於:
架構雖經典,但生產環境建議考慮 Pacemaker/Corosync 替代 Heartbeat,並對 NFS 單點進行高可用化(如 DRBD、GlusterFS 或商用存儲)。