天天看點

heartbeat+nginx雙機熱備

1 、軟體包下載下傳

下載下傳位址:http://www.linux-ha.org/wiki/Downloads

wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2

wget http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2

wget https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

2、安裝Cluster Glue : 0a7add1d9996.tar.bz2

安裝依賴:

yum -y install gcc gcc-c++ autoconf automake libtool glib2-devel libxml2-devel bzip2 bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel asciidoc

添加使用者群組:

groupadd haclient

useradd -g haclient hacluster -s /sbin/nologin

解壓軟體包:我的軟體包全放在/opt 目錄下,需要把這三個包全部安裝到同一目錄下:/usr/local/heartbeat/

tar -jxvf  0a7add1d9996.tar.bz2

cd Reusable-Cluster-Components-glue--0a7add1d9996/

./autogen.sh

./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'

heartbeat+nginx雙機熱備

make && make install

        沒有error就完成這個軟體的編譯安裝了

3、安裝resource-agents: v3.9.6.tar.gz

        解壓:tar -jxvf v3.9.6.tar.gz

        cd resource-agents-3.9.6

        ./autogen.sh

        export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"

        ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'

heartbeat+nginx雙機熱備

        ln -s  /usr/local/heartbeat/lib/* /lib/

        ln -s  /usr/local/heartbeat/lib/* /lib64/

        make

        make install

4、安裝heartbeat:958e11be8686.tar.bz2

        tar -jxvf 958e11be8686.tar.bz2 

        cd Heartbeat-3-0-958e11be8686/

        ./bootstrap 

heartbeat+nginx雙機熱備

        把heartbeat的三個配置檔案ha.cf、haresources、authkeys拷貝到安裝目錄

        cp doc/{ha.cf,haresources,authkeys} /usr/local/heartbeat/etc/ha.d/

5、安裝nginx服務

        yum install epel*

        yum install -y nginx

        systemctl start nginx

        關閉防火牆和selinux:

        systemctl stop firewalld

        setenforce 0

        通過浏覽器通路IP位址,能正常顯示nginx測試頁面

5、兩台伺服器都編譯安裝完成,開始配置

        authkeys檔案用于heartbeat的鑒權設定,共有三種可用的方式crc、md5和sha1。兩台伺服器設定一樣即可。

        vim /usr/local/heartbeat/etc/ha.d/authkeys

heartbeat+nginx雙機熱備

        修改權限:chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys

        *這裡注意一點 一般啟動時會報錯因為 ping和ucast這些配置都需要插件支援 需要将lib64下面的插件軟連接配接到lib目錄 才不會抛出異常

        mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat

        cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/

        ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/RAExec/* /usr/local/heartbeat/lib/heartbeat/plugins/RAExec/

        ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/

         haresources主要是建立VIP和配置接管的資源

        vi /usr/local/heartbeat/etc/ha.d/haresources

        在結尾增加:xuexi1 IPaddr::192.168.15.77/24/ens33 nginx

        格式:本機主機名  虛拟VIP位址  網卡名  資源腳本

            資源腳本:在目錄:/usr/local/heartbeat/etc/ha.d/resource.d/

            vim nginx    增加如下一條語句:

            systemctl start nginx

            儲存并修改權限:chmod 755 nginx

        ha.cf 檔案是heartbeat的主要配置檔案, 可以對heartbeat的多數性能與狀态進行配置, 多數選項可以采用預設值

        [root@xuexi1 ha.d]# grep -v '^#' ha.cf 

        debugfile /var/log/ha-debug    ##設定日志

        logfile /var/log/ha-log

        keepalive 2        ##設定心跳時間,機關秒

        deadtime 30    ##超過該時間間隔沒有收到心跳,認為對方已經死了

        warntime 10    ##超過該時間間隔沒有收到心跳,則發出警告并記錄到日志中

        initdead 120        ##在部分系統上,系統啟動或重新開機時要經過一段時間,網絡才能正常工作,該選項用于解決此情況的時間間隔

        udpport 694        ##廣播通信的端口,預設

        ucast ens33 192.168.15.176    ##ha所用的網絡接口卡,從點的地位址和網卡

        auto_failback off        ##主節點恢複故障後是否接管從節點的資源

        node xuexi1 xuexi2 ##指定兩個節點

        ping 192.168.15.1    ##指定ping節點,主要用于網絡測試

        respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail        ##表示ping不通時,自動調用ipfail這個腳本

6、從節點配置三個配置檔案基本一樣,隻需要修改ha.cf 的心跳:ucast ens33 192.168.15.126 為主節點的ip

7、測試:把nginx關閉

     啟動主節點的heartbeat:service heartbeat start        ,等一會兒,看一下網卡資訊,VIP出來了啟動完成

heartbeat+nginx雙機熱備

    在浏覽器中通路VIP:能通路到nginx的預設頁面,啟動完成 

    啟動從節點的heartbeat:service ehartbeat start

    模拟:主節點當機:關閉主節點伺服器或關閉heartbeat服務和關閉nginx服務

    檢視從節點的網卡資訊:是否出現VIP

    繼續通過浏覽器通路VIP,還能繼續通路,說明VIP自動轉移到從節點了。