keepalived 配置,則 可以參考: http://www.keepalived.org/documentation.html
環境準備
系統需要的 LINUX 版本為:2.6.9‐5.ELsmp 需要的keepalived 版本為:1.1.20
? 檢查 LINUX版本
[root@test01 etc]# uname -r
2.6.9-5.ELsmp
? 檢查 keepalived 版本
[root@test01 keepalived]# pwd
/root/disk/keepalived
[root@test01 keepalived]# cat VERSION
1.1.20 2目標
兩台伺服器,一主一備。提供兩個虛擬 IP。
Server A: 10.10.0.41 (主伺服器)
Server B: 10.10.0.118 (備伺服器)
Virtual IP: 10.10.0.44/45
要求正常情況,主伺服器提供服務,主伺服器失效時,備伺服器接管。 3 安裝 keepalive
[root@test01 keepalived]# ./configure
……
[root@test01 keepalived]# make
……
[root@test01 keepalived]# make install
……
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/sbin/keepalived /usr/sbin/
做成系統啟動服務方便管理.
# vi /etc/rc.local
/etc/init.d/keepalived start
增加上面一行。 4 配置 keepalived(Master / Slave 模式) 4.1 主伺服器
# vi /etc/keepalived/keepalived.conf
配置為如下內容:
! Configuration File for keepalived
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 {
state MASTER
interface eth0
virtual_router_id 51 # 保持主備伺服器一致
priority 100 # 優先順序 (主伺服器應比備份伺服器高)
advert_int 1 # 心跳廣播時間間隔(秒)
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.0.44 # 虛擬IP地址,可以多個。
10.10.0.45
} } 4.2 備伺服器
# vi /etc/keepalived/keepalived.conf
配置為如下內容:
! Configuration File for keepalived
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 {
state BACKUP
interface eth0
virtual_router_id 51 # 保持主備伺服器一致
priority 90 # 優先順序 (主伺服器應比備份伺服器高) advert_int 1 # 心跳廣播時間間隔(秒)
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.0.44 # 虛擬IP地址,可以多個。
10.10.0.45
}
} 5 啟動主伺服器 keepalived
# service keepalived start
# ip a
1: lo: mtu 16436 qdisc noqueue
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: mtu 1500 qdisc pfifo_fast qlen
1000
link/ether 00:10:5c:c8:1c:f2 brd ff:ff:ff:ff:ff:ff
inet 10.10.0.41/16 brd 10.10.255.255 scope global eth0
inet 10.10.0.44/32 scope global eth0 inet 10.10.0.45/32 scope global eth0
inet6 fe80::210:5cff:fec8:1cf2/64 scope link
valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
可以看到,10.10.0.44/45兩個虛擬 IP已經掛接在網卡 eth0上。 6 啟動備伺服器 keepalived
# service keepalived start
# ip a
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast qlen
1000
link/ether 00:14:2a:03:33:ca brd ff:ff:ff:ff:ff:ff
inet 10.10.0.118/16 brd 10.10.255.255 scope global eth0
inet6 fe80::214:2aff:fe03:33ca/64 scope link
valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
可以看到,10.10.0.44/45兩個虛擬 IP沒有掛接在網卡 eth0上。 7 驗證測試 7.1 驗證準備
測試服務: 在兩台伺服器上分別啟動 apache 服務,並修改預設的 index.html 檔案,
顯示當前伺服器 IP 以便識別。
# curl http://10.10.0.118
it works! 10.10.0.118
# curl http://10.10.0.41
it works! 10.10.0.41
可以看到兩個伺服器的 httpd服務正常。 7.2 檢查虛擬 IP 的工作狀態
# curl http://10.10.0.44
it works! 10.10.0.41
# curl http://10.10.0.45
it works! 10.10.0.41 7.3 停止主伺服器 keepalived
# service keepalived stop
# ip a
[root@test01 ~]# ip a
1: lo: mtu 16436 qdisc noqueue
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: mtu 1500 qdisc pfifo_fast
qlen 1000
link/ether 00:10:5c:c8:1c:f2 brd ff:ff:ff:ff:ff:ff
inet 10.10.0.41/16 brd 10.10.255.255 scope global eth0
inet6 fe80::210:5cff:fec8:1cf2/64 scope link
valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
可以看到,10.10.0.44/45兩個虛擬 IP沒有掛接在網卡 eth0上。 7.4 驗證備伺服器 keepalived狀態
[root@localhost ~]# curl http://10.10.0.44
it works! 10.10.0.118
[root@localhost ~]# curl http://10.10.0.45
it works! 10.10.0.118 7.5 重啟主伺服器 keepalived狀態
# service keepalived start
# ip a
1: lo: mtu 16436 qdisc noqueue
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: mtu 1500 qdisc pfifo_fast qlen
1000
link/ether 00:10:5c:c8:1c:f2 brd ff:ff:ff:ff:ff:ff
inet 10.10.0.41/16 brd 10.10.255.255 scope global eth0
inet 10.10.0.44/32 scope global eth0
inet 10.10.0.45/32 scope global eth0
inet6 fe80::210:5cff:fec8:1cf2/64 scope link
valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0 7.6 驗證主伺服器 keepalived狀態
[root@localhost ~]# curl http://10.10.0.44
it works! 10.10.0.41
[root@localhost ~]# curl http://10.10.0.45
it works! 10.10.0.41 8 附:Master / Master配置模式
Master / Slave 方案中備份伺服器(Server B)平時就是個擺設,有點浪費。我們完全可以用來跑其他服務,讓兩台主機形成相互熱備。
Server A: 192.168.1.10, Virtual IP: 192.168.1.100
Server B: 192.168.1.20, Virtual IP: 192.168.1.200
修改設定檔: Server A 主機:
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
vrrp_instance VI_2 {
state BACKUP interface eth0
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
Server B 主機:
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51 priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
其實很簡單,我們增加了一個新的配置 VI_2 (注意 virtual_router_id 不同)。不過這回用 Server B 做主伺服器,如此 Server A、Server B 各自擁有主虛擬 IP,同時備份
對方的虛擬 IP。 這個方案可以是不同的服務,或者是同一服務的訪問分流(配合 DNS 使用)。