主網關配置:
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
#指定keepalived在發生切換時需要發送email到的對象,一行一個
global_defs {
notification_email {
[email protected]
}
#指定發件人
notification_email_from [email protected]
#指定smtp服務器地址
smtp_server 127.0.0.1
#指定smtp連接超時時間
smtp_connect_timeout 30
router_id LVS_DEVEL #運行keepalived機器的一個標識
}
vrrp_sync_group VI_1 { #監控多個網段的實例
group { #實例名
VI_1
}
}
vrrp_instance VI_1 {
state MASTER #狀態
interface eth1 #監聽網卡
virtual_router_id 51 #VPID標記
priority 50 #優先級,高優先級競選爲master
advert_int 1 #檢查間隔,默認1秒
authentication { #設置認證
auth_type PASS #認證方式
auth_pass 1111 #認證密碼
}
virtual_ipaddress {
192.168.100.73 #虛擬ip
}
}
virtual_server 192.168.100.73 80 {
lb_algo wrr
lb_kind DR
persistence_timeout 600
net_mask 255.255.255.0
protocol TCP
real_server 192.168.100.74 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.100.75 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
備網關配置:
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
#指定keepalived在發生切換時需要發送email到的對象,一行一個
global_defs {
notification_email {
[email protected]
}
#指定發件人
notification_email_from [email protected]
#指定smtp服務器地址
smtp_server 127.0.0.1
#指定smtp連接超時時間
smtp_connect_timeout 30
router_id LVS_DEVEL 運行keepalived機器的一個標識
}
vrrp_sync_group VI_1 { #監控多個網段的實例
group { #實例名
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP #狀態
interface eth0 #監聽網卡
virtual_router_id 51 #VPID標記
priority 10 #優先級,高優先級競選爲master
advert_int 1 #檢查間隔,默認1秒
authentication { #設置認證
auth_type PASS #認證方式
auth_pass 1111 #認證密碼
}
virtual_ipaddress {
192.168.100.73 #虛擬ip
}
}
virtual_server 192.168.100.73 80 {
lb_algo wrr
lb_kind DR
persistence_timeout 600
net_mask 255.255.255.0
protocol TCP
real_server 192.168.100.74 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.100.75 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
在測試過程中,當將2臺設備的keepalived啓動後,client去訪問沒有問題;過一段時間後client就無法訪問應用;而且觀察lvs負載情況時發現client訪問正常是主備網關顯示的負載條目一致,當client訪問不正常是,主備網關顯示的負載條目不一致,以附件中圖片爲例,訪問正常時如果負載到75,那麼在74和75上觀察負載是一致的都顯示負載到75;訪問不正常時,74顯示負載到75上,而在75上顯示負載到74上。