http://freeloda.blog.51cto.com/2033581/1280962
-
在主机上使用动态路由协议(RIP、OSPF等)
-
在主机上配置静态路由
-
设置定时通告定时器;
-
用VRRP虚拟MAC地址响应路由器IP地址的ARP请求;
-
转发目的MAC是VRRP虚拟MAC的数据包;
-
如果是虚拟路由器IP的拥有者,将接受目的地址是虚拟路由器IP的数据包,否则丢弃;
-
当收到shutdown的事件时删除定时通告定时器,发送优先权级为0的通告包,转初始化状态;
-
如果定时通告定时器超时时,发送VRRP通告信息;
-
收到VRRP通告信息时,如果优先权为0,发送VRRP通告信息;否则判断数据的优先级是否高于本机,或相等而且实际IP地址大于本地实际IP,设置定时通告定时器,复位主机超时定时器,转BACKUP状态;否则的话,丢弃该通告包;
-
设置主机超时定时器;
-
不能响应针对虚拟路由器IP的ARP请求信息;
-
丢弃所有目的MAC地址是虚拟路由器MAC地址的数据包;
-
不接受目的是虚拟路由器IP的所有数据包;
-
当收到shutdown的事件时删除主机超时定时器,转初始化状态;
-
主机超时定时器超时的时候,发送VRRP通告信息,广播ARP地址信息,转MASTER状态;
-
收到VRRP通告信息时,如果优先权为0,表示进入MASTER选举;否则判断数据的优先级是否高于本机,如果高的话承认MASTER有效,复位主机超时定时器;否则的话,丢弃该通告包;
-
CentOS 6.4 X86_64
-
ipvsadm.x86_64 0:1.25-10.el6
-
keepalived.x86_64 0:1.2.7-3.el6
-
httpd-2.2.15-29.el6.centos.x86_64
1
|
[root@node1 ~]
# ntpdate 202.120.2.101
|
1
|
[root@node2 ~]
# ntpdate 202.120.2.101
|
1
|
[root@master ~]
# ntpdate 202.120.2.101
|
1
|
[root@slave ~]
# ntpdate 202.120.2.101
|
1
2
3
4
5
|
[root@node1 ~]
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.18.201 node1.
test
.com node1
192.168.18.202 node2.
test
.com node2
|
1
2
3
4
5
|
[root@node2 ~]
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.18.201 node1.
test
.com node1
192.168.18.202 node2.
test
.com node2
|
1
2
|
[root@node1 ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@node1 ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
1
2
|
[root@node2 ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@node2 ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
1
2
|
[root@master ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@master ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
1
2
|
[root@slave ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@slave ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
1
|
[root@node1 ~]
# yum install -y httpd
|
1
2
|
[root@node1 ~]
# vim /var/www/html/index.html
test
.com<
/h1
>
|
1
|
[root@node1 ~]
# service httpd start
|
1
2
3
|
[root@node1 ~]
# chkconfig httpd on
[root@node1 ~]
# chkconfig httpd --list
httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
[root@node1 ~]
# mkdir src
[root@node1 ~]
# cd src/
[root@node1 src]
# vim realserver.sh
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
.
/etc/rc
.d
/init
.d
/functions
VIP=192.168.18.200
#修改你的VIP
host=`
/bin/hostname
`
case
"$1"
in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig
lo down
/sbin/ifconfig
lo up
echo
1 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
1 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig
lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route
add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig
lo:0 down
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`
/sbin/ifconfig
lo:0 |
grep
$VIP`
isrothere=`
netstat
-rn |
grep
"lo:0"
|
grep
$VIP`
if
[ !
"$islothere"
-o !
"isrothere"
];
then
# Either the route or the lo:0 device
# not found.
echo
"LVS-DR real server Stopped."
else
echo
"LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo
"$0: Usage: $0 {start|status|stop}"
exit
1
;;
esac
[root@node1 src]
# chmod +x realserver.sh
[root@node1 src]
# ll
总用量 4
-rwxr-xr-x 1 root root 1485 8月 22 10:18 realserver.sh
[root@node1 src]
# ./realserver.sh start
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[root@node1 src]
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:11:92:E4
inet addr:192.168.18.201 Bcast:192.168.18.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe11:92e4
/64
Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:115061 errors:0 dropped:0 overruns:0 frame:0
TX packets:14979 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:43448483 (41.4 MiB) TX bytes:1224926 (1.1 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1
/128
Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:168 (168.0 b) TX bytes:168 (168.0 b)
lo:0 Link encap:Local Loopback
inet addr:192.168.18.200 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
[root@node1 src]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.18.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
192.168.18.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.18.254 0.0.0.0 UG 0 0 0 eth0
[root@node1 src]
# cat /proc/sys/net/ipv4/conf/lo/arp_ignore
1
[root@node1 src]
# cat /proc/sys/net/ipv4/conf/lo/arp_announce
2
[root@node1 src]
# cat /proc/sys/net/ipv4/conf/all/arp_ignore
1
[root@node1 src]
# cat /proc/sys/net/ipv4/conf/all/arp_announce
2
|
1
|
[root@node2 ~]
# yum install -y httpd
|
1
2
|
[root@node2 ~]
# vim /var/www/html/index.html
test
.com<
/h1
>
|
1
|
[root@node2 ~]
# service httpd start
|
1
2
3
|
[root@node2 ~]
# chkconfig httpd on
[root@node2 ~]
# chkconfig httpd --list
httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
[root@node2 ~]
# mkdir src
[root@node2 ~]
# cd src/
[root@node2 src]
# vim realserver.sh
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
.
/etc/rc
.d
/init
.d
/functions
VIP=192.168.18.200
host=`
/bin/hostname
`
case
"$1"
in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig
lo down
/sbin/ifconfig
lo up
echo
1 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
1 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig
lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route
add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig
lo:0 down
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`
/sbin/ifconfig
lo:0 |
grep
$VIP`
isrothere=`
netstat
-rn |
grep
"lo:0"
|
grep
$VIP`
if
[ !
"$islothere"
-o !
"isrothere"
];
then
# Either the route or the lo:0 device
# not found.
echo
"LVS-DR real server Stopped."
else
echo
"LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo
"$0: Usage: $0 {start|status|stop}"
exit
1
;;
esac
[root@node2 src]
# chmod +x realserver.sh
[root@node2 src]
# ./realserver.sh start
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[root@node2 src]
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:B8:DF:6A
inet addr:192.168.18.202 Bcast:192.168.18.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb8:df6a
/64
Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:110545 errors:0 dropped:0 overruns:0 frame:0
TX packets:10461 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:32853789 (31.3 MiB) TX bytes:889109 (868.2 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1
/128
Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:616 (616.0 b) TX bytes:616 (616.0 b)
lo:0 Link encap:Local Loopback
inet addr:192.168.18.200 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
[root@node2 src]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.18.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
192.168.18.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.18.254 0.0.0.0 UG 0 0 0 eth0
[root@node2 src]
# cat /proc/sys/net/ipv4/conf/lo/arp_ignore
1
[root@node2 src]
# cat /proc/sys/net/ipv4/conf/lo/arp_announce
2
[root@node2 src]
# cat /proc/sys/net/ipv4/conf/all/arp_ignore
1
[root@node2 src]
# cat /proc/sys/net/ipv4/conf/all/arp_announce
2
|
1
2
|
[root@master ~]
# yum install -y keepalived ipvsadm
[root@slave ~]
# yum install -y keepalived ipvsadm
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[root@master ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
[email protected]
#配置管理员邮箱
}
notification_email_from root
#配置发件人
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 51
priority 101
#配置优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
#配置虚拟IP地址
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
real_server 192.168.18.201 80 {
#配置realaserver
weight 1
HTTP_GET {
#监控配置
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}
|
1
|
[root@master ~]
# scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[root@slave keepalived]
# cat keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
#修改为BACKUP
interface eth0
virtual_router_id 51
priority 100
#修改优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}
|
1
2
3
4
|
[root@master ~]
# service keepalived start
正在启动 keepalived: [确定]
[root@slave ~]
# service keepalived start
正在启动 keepalived: [确定]
|
1
2
3
4
5
6
7
|
[root@master ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.201:80 Route 1 0 0
-> 192.168.18.202:80 Route 1 0 0
|
1
2
|
[root@node1 src]
# service httpd stop
停止 httpd: [确定]
|
1
2
3
4
5
6
|
[root@master ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.202:80 Route 1 0 0
|
1
2
|
[root@node1 src]
# service httpd start
正在启动 httpd: [确定]
|
1
2
3
4
5
6
7
|
[root@master ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.201:80 Route 1 0 0
-> 192.168.18.202:80 Route 1 0 0
|
1
2
3
4
5
6
|
[root@master ~]
# service keepalived stop
停止 keepalived: [确定]
[root@master ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@slave ~]
# ip addr show
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:
link
/ether
00:0c:29:f9:e6:26 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.207
/24
brd 192.168.18.255 scope global eth0
inet 192.168.18.200
/32
scope global eth0
inet6 fe80::20c:29ff:fef9:e626
/64
scope link
valid_lft forever preferred_lft forever
[root@slave ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.201:80 Route 1 0 0
-> 192.168.18.202:80 Route 1 0 0
|
-
所有realserver都down机,怎么处理?是不是用户就没法打开,还是提供一下维护页面。
-
怎么完成维护模式keepalived切换?
-
如何在keepalived故障时,发送警告邮件给指定的管理员?
1
2
|
[root@master ~]
# yum install -y httpd
[root@slave ~]
# yum install -y httpd
|
1
2
3
4
|
[root@master ~]
# vim /var/www/html/index.html
Website is currently under maintenance, please come back later!
[root@slave ~]
# vim /var/www/html/index.html
Website is currently under maintenance, please come back later!
|
1
2
3
4
|
[root@master ~]
# service httpd start
正在启动 httpd: [确定]
[root@slave ~]
# service httpd start
正在启动 httpd: [确定]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[root@master ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
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 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
|