這裏咱們僅僅只利用Keepalived作雙機熱備,也就是保證服務器的高可用性,其餘的不用管。html
特性:
1,至少須要兩臺服務器,其中一臺爲master始終提供服務,另一臺做爲backup始終處於空閒狀態,只有在主服務器掛掉的時候他就來幫忙了,這是典型的雙機熱備
2,能根據需求判斷服務是否可用,在不可用的時候要即便切換 優缺點:
優勢:數據同步很是簡單,不像負載均衡對數據一致性要求很是高,實現起來相對複雜維護也頗爲不便,雙機熱備用rsync就能夠實現了操做和維護很是簡單
缺點:服務器有點浪費,始終有一臺處於空閒狀態linux
首先畫個雙機熱備拓撲圖吧:
vim
操做系統:Linux6.3 2.6.32centos
下載軟件
keepalived官方地址:http://www.keepalived.org/download.html服務器
你們能夠到這裏下載最新版本的keepalived(若是已經提供用提供的安裝包keepalived-1.2.15.tar.gz)
建立目錄、壓縮文件上傳、解壓(主備機進行相同操做)負載均衡
mkdir -p /usr/local/HAsoft cd /usr/local/HAsoft tar -zxvf keepalived-1.2.15.tar.gz cd keepalived-1.2.15
預編譯測試
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
(kernels後面的版本「2.6.32-279.el6.x86_64」以實際安裝linux版本爲準,不要複製直接按Tab鍵自動補全便可) 結果以下:(出現以下相似結果就行)操作系統
Keepalived configuration ------------------------ Keepalived version : 1.2.15 Compiler : gcc Compiler flags : -g -O2 -DETHERTYPE_IPV6=0x86dd Extra Lib : -lssl -lcrypto -lcryptUse IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : No Use VRRP Framework : Yes Use Debug flags : No
編譯3d
make && make install
整理管理文件:調試
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
創建配置文件目錄
mkdir -p /etc/keepalived/
注意:keepalived的配置文件默認在/etc/keepalived/目錄 兩臺服務器(兩個節點)都這樣安裝便可
配置 打開配置文件:(該文件不存在,需手動建立)
vim /etc/keepalived/keepalived.conf
按i鍵將下面主機配置的內容複製粘貼到keepalived.conf中,並按時間環境修改標紅的配置項。
主機配置
global_defs { } vrrp_instance N1 { //定義一個vrrp組 state MASTER //指定哪一個爲master(主機),哪一個爲backup(備機)。 mcast_src_ip 192.166.66.80 //此處ip填寫本機ip便可(非必要設置) interface eth2 //設置實例綁定的網卡 virtual_router_id 51 // VPID標記 (主機備機要一致) priority 100 //定義優先級,數字越大,優先級越高 authentication { //驗證類型和密碼 auth_type PASS auth_pass 123456 } virtual_ipaddress { //虛擬IP 192.166.66.85 } }
備機配置
vim /etc/keepalived/keepalived.conf
將下面內容複製到keepalived.cong中,並按實際環境修改標紅配置項。
global_defs { } vrrp_instance N1 { state BACKUP interface eth2 mcast_src_ip 192.166.66.81 virtual_router_id 51 priority 88 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.166.66.85 } }
在節點A即主機上啓動keepalive
/etc/init.d/keepalived start
啓動日誌:(/var/log/message)
tail -f /var/log/messages Sep 8 18:26:02 centosaKeepalived_vrrp: Registering Kernel netlink reflector Sep 8 18:26:02 centosaKeepalived_vrrp: Registering Kernel netlink command channel Sep 8 18:26:02 centosaKeepalived_vrrp: Registering gratutious ARP shared channel Sep 8 18:26:02 centosaKeepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. Sep 8 18:26:02 centosaKeepalived_vrrp: Configuration is using : 36076 Bytes Sep 8 18:26:02 centosaKeepalived_vrrp: Using LinkWatch kernel netlink reflector... Sep 8 18:26:02 centosa Keepalived: Starting VRRP child process, pid=5606 Sep 8 18:26:07 centosaKeepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTER STATE Sep 8 18:26:12 centosaKeepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATE Sep 8 18:26:12 centosaavahi-daemon[2528]: Registering new address record for 192.168.17.200 on eth0.
在節點B即備機上上啓動
/etc/init.d/keepalived start
啓動日誌:
tail -f /var/log/messages Sep 8 18:30:02 centosb Keepalived: Starting Keepalived v1.2.2 (09/08,2011) Sep 8 18:30:02 centosb Keepalived: Starting Healthcheck child process, pid=5837 Sep 8 18:30:02 centosbKeepalived_vrrp: Registering Kernel netlink reflector Sep 8 18:30:02 centosbKeepalived_vrrp: Registering Kernel netlink command channel Sep 8 18:30:02 centosbKeepalived_vrrp: Registering gratutious ARP shared channel Sep 8 18:30:02 centosb Keepalived: Starting VRRP child process, pid=5839 Sep 8 18:30:02 centosb kernel: IPVS: Registered protocols (TCP, UDP, AH, ESP) Sep 8 18:30:02 centosb kernel: IPVS: Connection hash table configured (size=4096, memory=32Kbytes) Sep 8 18:30:02 centosb kernel: IPVS: ipvs loaded. Sep 8 18:30:02 centosbKeepalived_healthcheckers: Registering Kernel netlink reflector Sep 8 18:30:02 centosbKeepalived_healthcheckers: Registering Kernel netlink command channel Sep 8 18:30:02 centosbKeepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'. Sep 8 18:30:02 centosbKeepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. Sep 8 18:30:02 centosbKeepalived_vrrp: Configuration is using : 36252 Bytes Sep 8 18:30:02 centosbKeepalived_vrrp: Using LinkWatch kernel netlink reflector... Sep 8 18:30:02 centosbKeepalived_healthcheckers: Configuration is using : 6271 Bytes Sep 8 18:30:02 centosbKeepalived_healthcheckers: Using LinkWatch kernel netlink reflector... Sep 8 18:30:02 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Entering BACKUP STATE
從日誌能夠看出,啓動都沒有問題,而且安裝我給的優先級完成了競選,各自成就了各自的狀態
在本機運行cmd命令 Ping 192.168.17.200 看可否正常ping通。
關閉節點A主機的服務測試切換是否正常
/etc/init.d/keepalived stop
觀察節點B的日誌:
Sep 8 18:32:55 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTER STATE Sep 8 18:33:00 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATE Sep 8 18:33:00 centosbavahi-daemon[2531]: Registering new address record for 192.168.17.200 on eth0.
在本機運行cmd命令 Ping 192.168.17.200 看可否正常ping通。
啓動節點A的服務測試切換是否正常
/etc/init.d/keepalived start
觀察節點B的日誌:
Sep 8 18:33:31 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Received higher prio advert Sep 8 18:33:31 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Entering BACKUP STATE Sep 8 18:33:31 centosbavahi-daemon[2531]: Withdrawing address record for 192.168.17.200 on eth0.
在本機運行cmd命令 Ping 192.168.17.200 看可否正常ping通。
注意把keepalived寫在開機啓動裏面 準備機同時設置
vim /etc/rc.local
輸入下面內容保存
service keepalived start