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
<h1>RS1.
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
<h1>RS2.
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: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
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
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
}
}
sorry_server 127.0.0.1 80
#增加一行sorry_server
}
|
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 ~]
# 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 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
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
}
}
sorry_server 127.0.0.1 80
#增加一行sorry_server
}
|
1
2
3
4
5
6
7
8
9
10
|
[root@node1 ~]
# service httpd stop
停止 httpd: [确定]
[root@node2 ~]
# service httpd stop
停止 httpd: [确定]
[root@master ~]
# service keepalived restart
停止 keepalived: [确定]
正在启动 keepalived: [确定]
[root@slave ~]
# service keepalived restart
停止 keepalived: [确定]
正在启动 keepalived: [确定]
|
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
-> 127.0.0.1:80 Local 1 0 0
|
1
2
3
4
5
6
7
|
vrrp_srcipt chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
#监控间隔
weight -5
#减小优先级
fall 2
#监控失败次数
rise 1
#监控成功次数
}
|
1
2
3
|
track_script {
chk_schedown
#执行chk_schedown脚本
}
|
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
58
59
60
61
62
63
64
65
66
67
|
[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_script chk_schedown {
#定义vrrp执行脚本
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
#查看是否有down文件,有就进入维护模式
interval 1
#监控间隔时间
weight -5
#降低优先级
fall 2
#失败次数
rise 1
#成功数次
}
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
}
track_script {
#执行脚本
chk_schedown
}
}
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
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
}
}
sorry_server 127.0.0.1 80
}
|
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
58
59
60
61
62
63
64
65
66
|
[root@slave ~]
# 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_script chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state 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
}
track_script {
chk_schedown
}
}
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
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
}
}
sorry_server 127.0.0.1 80
|
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
|
[root@master keepalived]
# touch down #新建一下down文件
[root@master keepalived]
# ll
总用量 4
-rw-r--r-- 1 root root 0 8月 22 13:39 down
-rw-r--r-- 1 root root 1317 8月 22 13:35 keepalived.conf
[root@master keepalived]
# tail -f /var/log/messages #查看一下日志
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering MASTER STATE
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) setting protocol VIPs.
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0
for
192.168.18.200
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Received higher prio advert
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering BACKUP STATE
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) removing protocol VIPs.
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 added
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 removed
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent.
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent.
^C
[root@master keepalived]
# ip add show #查看VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:4b:a1:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.208
/24
brd 192.168.18.255 scope global eth0
inet6 fe80::20c:29ff:fe4b:a185
/64
scope link
valid_lft forever preferred_lft forever
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@slave ~]
# ip addr show #查看一下VIP已转移到slave上
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
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
|
-
-s, --service SERVICE,...:指定服务脚本名称,当状态切换时可自动启动、重启或关闭此服务;
-
-a, --address VIP: 指定相关虚拟路由器的VIP地址;
-
-m, --mode {mm|mb}:指定虚拟路由的模型,mm表示主主,mb表示主备;它们表示相对于同一种服务而方,其VIP的工作类型;
-
-n, --notify {master|backup|fault}:指定通知的类型,即vrrp角色切换的目标角色;
-
-h, --help:获取脚本的使用帮助;
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#!/bin/bash
# Author: freeloda
# description: An example of notify script
# Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP -n|--notify {master|backup|falut} -h|--help
contact=
'[email protected]'
helpflag=0
serviceflag=0
modeflag=0
addressflag=0
notifyflag=0
Usage() {
echo
"Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>"
echo
"Usage: notify.sh -h|--help"
}
ParseOptions() {
local
I=1;
if
[ $
# -gt 0 ]; then
while
[ $I -
le
$
# ]; do
case
$1
in
-s|--service)
[ $
# -lt 2 ] && return 3
serviceflag=1
services=(`
echo
$2|
awk
-F
","
'{for(i=1;i<=NF;i++) print $i}'
`)
shift
2 ;;
-h|--help)
helpflag=1
return
0
shift
;;
-a|--address)
[ $
# -lt 2 ] && return 3
addressflag=1
vip=$2
shift
2
;;
-m|--mode)
[ $
# -lt 2 ] && return 3
mode=$2
shift
2
;;
-n|--notify)
[ $
# -lt 2 ] && return 3
notifyflag=1
notify=$2
shift
2
;;
*)
echo
"Wrong options..."
Usage
return
7
;;
esac
done
return
0
fi
}
#workspace=$(dirname $0)
RestartService() {
if
[ ${
#@} -gt 0 ]; then
for
I
in
$@;
do
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I restart
else
echo
"$I is not a valid service..."
fi
done
fi
}
StopService() {
if
[ ${
#@} -gt 0 ]; then
for
I
in
$@;
do
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I stop
else
echo
"$I is not a valid service..."
fi
done
fi
}
Notify() {
mailsubject=
"`hostname` to be $1: $vip floating"
mailbody=
"`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1."
echo
$mailbody | mail -s
"$mailsubject"
$contact
}
# Main Function
ParseOptions $@
[ $? -
ne
0 ] && Usage &&
exit
5
[ $helpflag -
eq
1 ] && Usage &&
exit
0
if
[ $addressflag -
ne
1 -o $notifyflag -
ne
1 ];
then
Usage
exit
2
fi
mode=${mode:-mb}
case
$notify
in
'master'
)
if
[ $serviceflag -
eq
1 ];
then
RestartService ${services[*]}
fi
Notify master
;;
'backup'
)
if
[ $serviceflag -
eq
1 ];
then
if
[
"$mode"
==
'mb'
];
then
StopService ${services[*]}
else
RestartService ${services[*]}
fi
fi
Notify backup
;;
'fault'
)
Notify fault
;;
*)
Usage
exit
4
;;
esac
|
-
notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200"
-
notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
-
notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200"
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[root@master 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_script chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
weight -5
fall 2
rise 1
}
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
}
track_script {
chk_schedown
}
#增加以下三行
notify_master
"/etc/keepalived/notify.sh -n master -a 192.168.18.200"
notify_backup
"/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
notify_fault
"/etc/keepalived/notify.sh -n fault -a 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
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
}
}
sorry_server 127.0.0.1 80
}
|
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[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_script chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state 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
}
track_script {
chk_schedown
}
#增加以下三行
notify_master
"/etc/keepalived/notify.sh -n master -a 192.168.18.200"
notify_backup
"/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
notify_fault
"/etc/keepalived/notify.sh -n fault -a 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
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
}
}
sorry_server 127.0.0.1 80
}
|
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
[root@slave keepalived]
# pwd
/etc/keepalived
[root@slave keepalived]
# vim notify.sh
[root@slave keepalived]
# cat notify.sh
#!/bin/bash
# Author: freeloda
# description: An example of notify script
# Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP -n|--notify {master|backup|falut} -h|--help
contact=
'[email protected]'
helpflag=0
serviceflag=0
modeflag=0
addressflag=0
notifyflag=0
Usage() {
echo
"Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>"
echo
"Usage: notify.sh -h|--help"
}
ParseOptions() {
local
I=1;
if
[ $
# -gt 0 ]; then
while
[ $I -
le
$
# ]; do
case
$1
in
-s|--service)
[ $
# -lt 2 ] && return 3
serviceflag=1
services=(`
echo
$2|
awk
-F
","
'{for(i=1;i<=NF;i++) print $i}'
`)
shift
2 ;;
-h|--help)
helpflag=1
return
0
shift
;;
-a|--address)
[ $
# -lt 2 ] && return 3
addressflag=1
vip=$2
shift
2
;;
-m|--mode)
[ $
# -lt 2 ] && return 3
mode=$2
shift
2
;;
-n|--notify)
[ $
# -lt 2 ] && return 3
notifyflag=1
notify=$2
shift
2
;;
*)
echo
"Wrong options..."
Usage
return
7
;;
esac
done
return
0
fi
}
#workspace=$(dirname $0)
RestartService() {
if
[ ${
#@} -gt 0 ]; then
for
I
in
$@;
do
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I restart
else
echo
"$I is not a valid service..."
fi
done
fi
}
StopService() {
if
[ ${
#@} -gt 0 ]; then
for
I
in
$@;
do
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I stop
else
echo
"$I is not a valid service..."
fi
done
fi
}
Notify() {
mailsubject=
"`hostname` to be $1: $vip floating"
mailbody=
"`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1."
echo
$mailbody | mail -s
"$mailsubject"
$contact
}
# Main Function
ParseOptions $@
[ $? -
ne
0 ] && Usage &&
exit
5
[ $helpflag -
eq
1 ] && Usage &&
exit
0
if
[ $addressflag -
ne
1 -o $notifyflag -
ne
1 ];
then
Usage
exit
2
fi
mode=${mode:-mb}
case
$notify
in
'master'
)
if
[ $serviceflag -
eq
1 ];
then
RestartService ${services[*]}
fi
Notify master
;;
'backup'
)
if
[ $serviceflag -
eq
1 ];
then
if
[
"$mode"
==
'mb'
];
then
StopService ${services[*]}
else
RestartService ${services[*]}
fi
fi
Notify backup
;;
'fault'
)
Notify fault
;;
*)
Usage
exit
4
;;
esac
|
1
|
[root@slave keepalived]
# chmod +x notify.sh
|
1
|
[root@slave keepalived]
# scp -p notify.sh [email protected]:/etc/keepalived/
|
1
2
3
4
5
6
7
|
[root@slave keepalived]
# ./notify.sh -h
Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>
Usage: notify.sh -h|--help
[root@slave keepalived]
# ./notify.sh --help
Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>
Usage: notify.sh -h|--help
[root@slave keepalived]
# ./notify.sh -m mb -a 1.1.1.1 -n master
|
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
|
[root@master keepalived]
# ip addr show #查看一下VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:4b:a1:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.208
/24
brd 192.168.18.255 scope global eth0
inet 192.168.18.200
/32
scope global eth0
inet6 fe80::20c:29ff:fe4b:a185
/64
scope link
valid_lft forever preferred_lft forever
[root@master keepalived]
# touch down #进入维护模式
[root@master keepalived]
# ll
总用量 8
-rw-r--r-- 1 root root 0 8月 22 14:39 down
-rw-r--r-- 1 root root 1543 8月 22 14:04 keepalived.conf
-rwxr-xr-x 1 root root 2516 8月 22 14:15 notify.sh
[root@master keepalived]
# ip addr show #再次查看VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:4b:a1:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.208
/24
brd 192.168.18.255 scope global eth0
inet6 fe80::20c:29ff:fe4b:a185
/64
scope link
valid_lft forever preferred_lft forever
[root@slave keepalived]
# ip addr show #大家可以看到VIP成功移动到slave上
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
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
|