全部博文(56)
分类: LINUX
2013-12-12 14:19:16
系统需要的 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
两台服务器,一主一备。提供两个虚拟 IP。
Server A: 10.10.0.41 (主服务器)
Server B: 10.10.0.118 (备服务器)
Virtual IP: 10.10.0.44/45
要求正常情况,主服务器提供服务,主服务器失效时,备服务器接管。
[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
增加上面一行。
# 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
} }
# 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
}
}
# service keepalived start
# ip a
1: lo:
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:
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:
link/sit 0.0.0.0 brd 0.0.0.0
可以看到,10.10.0.44/45两个虚拟 IP已经挂接在网卡 eth0上。
# service keepalived start
# ip a
1: lo:
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:
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:
link/sit 0.0.0.0 brd 0.0.0.0
可以看到,10.10.0.44/45两个虚拟 IP没有挂接在网卡 eth0上。
测试服务: 在两台服务器上分别启动 apache 服务,并修改默认的 index.html 文件,
显示当前服务器 IP 以便识别。
# curl
it works! 10.10.0.118
# curl
it works! 10.10.0.41
可以看到两个服务器的 httpd服务正常。
# curl
it works! 10.10.0.41
# curl
it works! 10.10.0.41
# service keepalived stop
# ip a
[root@test01 ~]# ip a
1: lo:
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:
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:
link/sit 0.0.0.0 brd 0.0.0.0
可以看到,10.10.0.44/45两个虚拟 IP没有挂接在网卡 eth0上。
[root@localhost ~]# curl
it works! 10.10.0.118
[root@localhost ~]# curl
it works! 10.10.0.118
# service keepalived start
# ip a
1: lo:
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:
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:
link/sit 0.0.0.0 brd 0.0.0.0
[root@localhost ~]# curl
it works! 10.10.0.41
[root@localhost ~]# curl
it works! 10.10.0.41
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 使用)。