1环境准备
系统需要的 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
it works!
10.10.0.118
# curl
it works!
10.10.0.41
可以看到两个服务器的httpd服务正常。
7.2检查虚拟IP的工作状态
# curl
it works!
10.10.0.41
# curl
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
it works!
10.10.0.118
[root@localhost
~]# curl
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
it works!
10.10.0.41
[root@localhost
~]# curl
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使用)。