keepalived基本應用解析


概念簡單認知:
node

Keepalived:它的誕生最初是爲ipvs(一些服務,內核中的一些規則)提供高可用性的最初最主要目的是可以自主調用ipvsadm來生成規則而且可以自動實現將用戶訪問的地址轉移到其餘節點進行實現web

Keepalived:核心包含兩個ckeckers和VRRP協議。vim

ckeckers:檢查服務檢查reserved的健康情況的,基於腳本也可檢查服務自己的健康情況。這裏實現ipvs後端健康情況的檢測的後端

VRRP:是一種容錯協議,它保證當主機的下一跳路由器出現故障時,由另外一臺路由器來代替出現故障的路由器進行工做,從而保持網絡通訊的連續性和可靠性。VRRP中每一個節點之間都有優先級的通常爲0-255(0,255有特殊用法數字越大優先級越高。centos

相關術語解析:bash

虛擬路由器:由一個Master路由器和多個Backup路由器組成。主機將虛擬路由器看成默認網關。服務器

VRID:虛擬路由器的標識。有相同VRID的一組路由器構成一個虛擬路由器。網絡

Master路由器:虛擬路由器中承擔報文轉發任務的路由器。負載均衡

Backup路由器:Master路由器出現故障時,可以代替Master路由器工做的路由器。ssh

虛擬IP 地址:虛擬路由器的IP 地址。一個虛擬路由器能夠擁有一個或多個IP地址。

IP地址擁有者:接口IP地址與虛擬IP地址相同的路由器被稱爲IP地址擁有者。

虛擬MAC地址:一個虛擬路由器擁有一個虛擬MAC地址。虛擬MAC地址的格式爲00-00-5E-00-01-{VRID}。一般狀況下,虛擬路由器迴應ARP請求使用的是虛擬MAC地址,只有虛擬路由器作特殊配置的時候,纔回應接口的真實MAC地址。

優先級:VRRP根據優先級來肯定虛擬路由器中每臺路由器的地位。

非搶佔方式:若是Backup路由器工做在非搶佔方式下,則只要Master路由器沒有出現故障Backup路由器即便隨後被配置了更高的優先級也不會成爲Master路由器。

搶佔方式:若是Backup路由器工做在搶佔方式下,當它收到VRRP報文後,會將本身的優先級與通告報文中的優先級進行比較。若是本身的優先級比當前的Master路由器的優先級高,就會主動搶佔成爲Master路由器;不然,將保持Backup狀態。



平臺信息介紹

 Master:172.16.18.7

 backup:172.16.18.9

 系統版本:centosx86_64

 keepalived版本:1.2.7

 主配置文件:/etc/keepalived/keepalived.conf

 服務腳本/etc/rc.d/init,d/keepalived



應用實踐:

將兩個節點的時間同步

##############實現雙機互信#######
#########node1#######
ssh-keygen -t rsa -P ''
ssh-copy-id -i .ssh/id_rsa.pub root@172.16.18.9
#########node2#######
ssh-keygen -t rsa -P ''
ssh-copy-id -i .ssh/id_rsa.pub root@172.16.18.7
##############查看時間###########
[root@node1 ~]# date;ssh node2 'date'
#####爲實現同步可以使用下面同步#####
crontab -e
*/5 * * * * /usr/sbin/ntpdate 172.16.0.1 &> /dev/null

安裝

[root@node1 ~]# yum -y install keepalived

查看編輯配置文件:

vim /etc/keepalived/keepalived.conf

解析配置文件:

配置文件有三部分組成:

(1):GLOBAL CONFIGURATION        全局配置段
    有兩個字段:
    Global definitions              #全局定義
    Static routes                   #靜態路由
(2):VRRPD CONFIGURATION         配置VRRP子進程協議段又稱定義虛擬路由的
    有兩個字段:
    VRRP synchronization group(s)   #VRRP的同步組(通常不用)
    什麼是同步組?就是一臺機器上有配置兩個VIP,爲了實現兩個VIP要同步工做同時轉移出去,因此必需要定義成同步組從而當成一個資源來轉移。
    VRRP instance(s)                #VRRP的實例
    任何一個虛擬路由定義好以後在任何一個節點上都應該定義一個keepalived運行實例,這兩個節點上的實例要匹配。keepalived最使人頭疼的是兩個節點上的初始實例是不同的,由於每個節點都有初始狀態並且它有默認的優先級,高的爲Master低的爲Backup因此致使了兩個節點上的虛擬路由的實例配置是不同的。
(3):LVS CONFIGURATION            LVS配置段
    有兩個字段:
    Virtual server group(s)         #虛擬服務器組
    Virtual server(s)               #虛擬服務器(ipvs規則)

詳細解析:

keepalived.conf

global_defs {    #全局配置,這裏額外的靜態路由並未添加由於它是非必要的,除非咱們在當前或特定的主機上生成特殊的靜態路由等
   notification_email {                #收件人信息
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc   #發件人信息(能夠隨意假裝)
   smtp_server 192.168.200.1      #發郵件的服務器(必定不可爲外部地址)
   smtp_connect_timeout 30      #鏈接超時時間
   router_id LVS_DEVEL          #路由器的標識(能夠隨便改動)
}
vrrp_instance VI_1 {            #配置虛擬路由器的(VI_1是實例名稱)
    state MASTER               #初始狀態,master|backup,當state指定的instance的初始化狀態,在兩臺服務器都啓動之後,立刻發生競選,優先級高的成爲MASTER,因此這裏的MASTER並非表示此臺服務器一直是MASTER
    interface eth0              #通告選舉所用端口
    virtual_router_id 51        #虛擬路由的ID號(通常不可大於255)
    priority 100                #優先級信息
    advert_int 1                #初始化通告幾個
    authentication {            #認證
        auth_type PASS          #認證機制
        auth_pass 1111          #密碼(儘可能使用隨機)
    }
    virtual_ipaddress {         #虛擬地址(VIP地址)
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}

編輯設置配置信息:

[root@node1 ~]# vim /etc/keepalived/keepalived.conf     #主節點
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from keadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 55
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.18.100
    }
}
[root@node1 ~]# scp /etc/keepalived/keepalived.conf 172.16.18.9:/etc/keepalived/             #複製至備節點
[root@node2 keepalived]# vim keepalived.conf     #備節點
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from keadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
vrrp_instance VI_1 {
    state BACKUP                         #狀態
    interface eth0
    virtual_router_id 55                 #必定要和主節點一致
    priority 90                          #優先級別必定低於主節點
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.18.100
    }
}

啓動主節點:

[root@node1 ~]# service keepalived start

啓動備節點:

[root@node2 ~]# service keepalived start

查看狀態

###############節點一############
[root@node1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:06:a6:49 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.7/16 brd 172.16.255.255 scope global eth0
    inet 172.16.18.100/32 scope global eth0    #此時VIP在node1節點上
    inet6 fe80::20c:29ff:fe06:a649/64 scope link
       valid_lft forever preferred_lft forever
###############節點二############
[root@node2 keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:12:c8:b5 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.9/16 brd 172.16.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe12:c8b5/64 scope link
       valid_lft forever preferred_lft forever

查看主節點啓動日誌信息

##############主節點#############
[root@node1 ~]# tail -20 /var/log/messages
Sep 25 17:32:17 node1 Keepalived_healthcheckers[16628]: Netlink reflector reports IP 172.16.18.7 added
Sep 25 17:32:17 node1 Keepalived_healthcheckers[16628]: Netlink reflector reports IP fe80::20c:29ff:fe06:a649 added
Sep 25 17:32:17 node1 Keepalived_healthcheckers[16628]: Registering Kernel netlink reflector
Sep 25 17:32:17 node1 Keepalived_healthcheckers[16628]: Registering Kernel netlink command channel
Sep 25 17:32:17 node1 Keepalived_healthcheckers[16628]: Opening file '/etc/keepalived/keepalived.conf'.
Sep 25 17:32:17 node1 Keepalived_healthcheckers[16628]: Configuration is using : 6832 Bytes
Sep 25 17:32:18 node1 kernel: IPVS: Registered protocols (TCP, UDP, SCTP, AH, ESP)
Sep 25 17:32:18 node1 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes)
Sep 25 17:32:18 node1 kernel: IPVS: ipvs loaded.
Sep 25 17:32:18 node1 Keepalived_healthcheckers[16628]: Using LinkWatch kernel netlink reflector...
Sep 25 17:32:18 node1 Keepalived_vrrp[16629]: Opening file '/etc/keepalived/keepalived.conf'.
Sep 25 17:32:18 node1 Keepalived_vrrp[16629]: Configuration is using : 62657 Bytes
Sep 25 17:32:18 node1 Keepalived_vrrp[16629]: Using LinkWatch kernel netlink reflector...
Sep 25 17:32:18 node1 Keepalived_vrrp[16629]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Sep 25 17:32:19 node1 Keepalived_vrrp[16629]: VRRP_Instance(VI_1) Transition to MASTER STATE       #事務開始轉換爲Master狀態
Sep 25 17:32:20 node1 Keepalived_vrrp[16629]: VRRP_Instance(VI_1) Entering MASTER STATE          #進入master狀態
Sep 25 17:32:20 node1 Keepalived_vrrp[16629]: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 25 17:32:20 node1 Keepalived_vrrp[16629]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.18.100
Sep 25 17:32:20 node1 Keepalived_healthcheckers[16628]: Netlink reflector reports IP 172.16.18.100 added             #添加IP172.16.18.100
Sep 25 17:32:25 node1 Keepalived_vrrp[16629]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.18.100
###############備節點##########
[root@node2 keepalived]# tail -20 /var/log/messages
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Interface queue is empty
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Interface queue is empty
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Netlink reflector reports IP 172.16.18.9 added
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Netlink reflector reports IP 172.16.18.9 added
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Netlink reflector reports IP fe80::20c:29ff:fe12:c8b5 added
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Netlink reflector reports IP fe80::20c:29ff:fe12:c8b5 added
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Registering Kernel netlink reflector
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Registering Kernel netlink reflector
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Registering Kernel netlink command channel
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Registering Kernel netlink command channel
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Registering gratuitous ARP shared channel
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Opening file '/etc/keepalived/keepalived.conf'.
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Configuration is using : 6985 Bytes
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Opening file '/etc/keepalived/keepalived.conf'.
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Configuration is using : 62678 Bytes
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: Using LinkWatch kernel netlink reflector...
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: VRRP_Instance(VI_1) Entering BACKUP STATE              #進入BACKUP狀態
Sep 25 17:32:23 node2 Keepalived_vrrp[20358]: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Sep 25 17:32:23 node2 Keepalived_healthcheckers[20357]: Using LinkWatch kernel netlink reflector...

測試將node1關閉node2會不會將地址取走??

##############主節點############
[root@node1 ~]# service keepalived stop
Stopping keepalived:                                       [  OK  ]
[root@node1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:06:a6:49 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.7/16 brd 172.16.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe06:a649/64 scope link
       valid_lft forever preferred_lft forever
###############備節點##########
[root@node2 keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:12:c8:b5 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.9/16 brd 172.16.255.255 scope global eth0
    inet 172.16.18.100/32 scope global eth0
    inet6 fe80::20c:29ff:fe12:c8b5/64 scope link
       valid_lft forever preferred_lft forever

測試結果:這樣是成立的,若node1從新上線會當即將VIP獲取走。

如何使用keepalived調用外部腳本或手動執行命令實現VIP轉移??

思路:經過addr_script(腳本定義檢測機制;而後經過track_script在實例中追蹤這個腳本

下面這個檢測機制

vrrp_script chk_mantaince_down {             # chk_mantaince_down定義腳本的名稱,可隨意取
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"   #命令(其實這裏能夠是本身定義好的腳本路徑也能夠是判斷命令)#這裏的意思是若是在這個文件下有down這個文件就表示指望這 個節點爲備用狀態。
   interval 1           #每隔1秒鐘執行一次
   weight -2            #一旦命令執行失敗,權重下降2個
}

(1)將此檢查機制應用到咱們的示例中測試實現過程:

#############主節點###########
[root@node1 keepalived]# vim keepalived.conf
 router_id LVS_DEVEL
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
vrrp_script chk_main {      腳本
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 1
   weight -2
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 157       #若是環境中操做者比較多,儘可能在每次更改配置文件以後改變一下這個值,從而實現ARPs快速轉接。
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.18.100
    }
    track_script {        #追蹤腳本
        chk_main
    }
}
##############備節點############
[root@node2 keepalived]# vim keepalived.conf
smtp_connect_timeout 30
   router_id LVS_DEVEL
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
vrrp_script chk_main {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 1
   weight -2
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 157
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.18.100
    }
    track_script {
        chk_main
    }
}

(2)測試

#######未添加文件以前:節點一########
[root@node1 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:06:a6:49 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.7/16 brd 172.16.255.255 scope global eth0
    inet 172.16.18.100/32 scope global eth0
    inet6 fe80::20c:29ff:fe06:a649/64 scope link
       valid_lft forever preferred_lft forever
#######未添加文件以前:節點二########
[root@node2 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:12:c8:b5 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.9/16 brd 172.16.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe12:c8b5/64 scope link
       valid_lft forever preferred_lft forever
############添加文件########
[root@node1 keepalived]# touch down
###########查看狀態#########
[root@node1 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:06:a6:49 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.7/16 brd 172.16.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe06:a649/64 scope link
       valid_lft forever preferred_lft forever
[root@node2 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:12:c8:b5 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.9/16 brd 172.16.255.255 scope global eth0
    inet 172.16.18.100/32 scope global eth0
    inet6 fe80::20c:29ff:fe12:c8b5/64 scope link
       valid_lft forever preferred_lft forever

如何在狀態轉換時進行通知??

(1)keepalive內部提供了兩個配置指令詳細參考man keepalived.conf,通常在vrrp_instance或者vrrp_sync_group使用中使用

第一類指令:

# to MASTER transition
notify_master /path/to_master.sh     #轉換爲master狀態時使用此腳本通知
# to BACKUP transition
notify_backup /path/to_backup.sh     #轉換爲backup狀態時使用此腳本通知
# FAULT transition
notify_fault "/path/fault.sh VG_1"   #若是變成了fault就是用此腳本通知,若是腳本帶有參數也就是有空格必須使用引號

第二類指令:使用notify直接引用

# $1 = "GROUP"|"INSTANCE"            #參數1:必須可以指定接受組或實例
# $2 = name of group or instance     #這個組或實例的名稱
# $3 = target state of transition    #指定轉換成哪一個狀態進行通知的
#     ("MASTER"|"BACKUP"|"FAULT")
notify /path/notify.sh     notify    #腳本的路徑(自行寫)

(2)通知腳本定義

[root@node1 keepalived]# vim notify.sh
#!/bin/bash
#
vip=172.16.18.100           #指定VIP
contact='root@localhost'    #通知給誰
thisip=`ifconfig eth0 | awk '/inet addr:/{print $2}' | awk -F: '{print $2}'`                       #獲取當前節點IP地址
notify() {
    mailsubject="$thisip to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, $thisip changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
    master)
        notify master
        exit 0
    ;;
    backup)
        notify backup
        exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac
##########賦予此腳本執行權限###########
[root@node1 keepalived]# chmod +x notify.sh
###########測試腳本####################
[root@node1 keepalived]# ./notify.sh master
###########查看郵件信息################
[root@node1 keepalived]# mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Wed Sep 25 22:24  18/693   "172.16.18.7 to be master: 172.16.18.100 flo"
& 1        #第一封郵件
Message  1:
From root@node1.magedu.com  Wed Sep 25 22:24:40 2013
Return-Path: <root@node1.magedu.com>
X-Original-To: root@localhost
Delivered-To: root@localhost.magedu.com
Date: Wed, 25 Sep 2013 22:24:39 +0800
To: root@localhost.magedu.com
Subject: 172.16.18.7 to be master: 172.16.18.100 floating
User-Agent: Heirloom mailx 12.4 7/29/08
Content-Type: text/plain; charset=us-ascii
From: root@node1.magedu.com (root)
Status: R
2013-09-25 22:24:39: vrrp transition, 172.16.18.7 changed to be master #內容
###########使用quit退出郵件##############
#############配置測試狀態轉換##############
[root@node1 keepalived]# vim keepalived.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 157
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.18.100
    }
    track_script {
        chk_main
    }
    notify_master "/etc/keepalived/notify.sh master" #指定切換到Master狀態時執行的腳本
    notify_backup "/etc/keepalived/notify.sh backup" #指定切換到Backup狀態時執行的腳本
    notify_fault "/etc/keepalived/notify.sh fault" #指定切換到Mfault狀態時執行的腳本
}
##########注意將上面此代碼寫入備節點中############
notify_master "/etc/keepalived/notify.sh master" #指定切換到Master狀態時執行的腳本
notify_backup "/etc/keepalived/notify.sh backup" #指定切換到Backup狀態時執行的腳本
notify_fault "/etc/keepalived/notify.sh fault"   #指定切換到Mfault狀態時執行的腳本
##########腳本一樣在備節點中存在##################
[root@node1 keepalived]# scp notify.sh 172.16.18.9:/etc/keepalived/

(3)測試

[root@node1 keepalived]# touch down
[root@node1 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:06:a6:49 brd ff:ff:ff:ff:ff:ff
    inet 172.16.18.7/16 brd 172.16.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe06:a649/64 scope link
       valid_lft forever preferred_lft forever
[root@node1 keepalived]# mail       #節點1上
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 3 messages 2 unread
    1 root                  Wed Sep 25 22:24  19/704   "172.16.18.7 to be master: 172.16.18.100 flo"
>U  2 root                  Wed Sep 25 22:47  19/703   "172.16.18.7 to be master: 172.16.18.100 flo"
 U  3 root                  Wed Sep 25 22:47  19/703   "172.16.18.7 to be backup: 172.16.18.100 flo"
&
[root@node2 keepalived]# mail      #節點2上
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/root": 3 messages 3 new
>N  1 root                  Wed Sep 25 22:46  18/693   "172.16.18.9 to be backup: 172.16.18.100 flo"
 N  2 root                  Wed Sep 25 22:47  18/693   "172.16.18.9 to be backup: 172.16.18.100 flo"
 N  3 root                  Wed Sep 25 22:47  18/693   "172.16.18.9 to be master: 172.16.18.100 flo"
&


關於ipvs配置生成規則實現負載均衡和web服務器實現高可用等更多關於keepalived高級應用將在後續博客中持續更新,請繼續關注!!謝謝!!

相關文章
相關標籤/搜索