服务器层的双机热备
1、环境:
- S-A 192.168.8.183 centos5 keepalived nginx
- S-B 192.168.8.150 centos5 keepalived nginx
- VirtualIP (虚拟IP):192.168.8.191
2、测试要求:
主机宕机、keepalived宕了、网络不通时,备机能自动接管,不影响VIP:192.168.8.191的访问
3、配置:
1)Master(S-A 192.168.8.181)配置文件/etc/keepalived/keepalived.conf: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! Configuration File for keepalived
global_defs {
notification_email {
#admin@michael-demo.com
}
#notification_email_from admin@michael-demo.com
#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 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.8.191
}
}
2)Backup(S-B 192.168.8.150)配置信息/etc/keepalived/keepalived.conf: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! Configuration File for keepalived
global_defs {
notification_email {
#admin@michael-demo.com
}
#notification_email_from admin@michael-demo.com
#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 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.8.191
}
}
注:
• state 参数值:主的是MASTER、备用的是BACKUP
• priority 参数值: MASTER > BACKUP
• virtual_router_id: 参数值要一样
3)在两台机子上启动keepalived和nginx服务。
4、测试:
- 分别通过http://192.168.8.183:8088/ http://192.168.8.150:8088/ 和虚拟ip http://192.168.8.191:8088/ 去访问nginx;
- 停掉其中一台机子的keepalived,然后再使用虚拟ip访问;(会发现备机的keepalived会接管这个虚拟ip)
- 查看keepalived的日志;
应用层的双机热备:
1、修改两台机子上的keepalived配置文件:
1)Master(S-A 192.168.8.181)配置文件/etc/keepalived/keepalived.conf:
1 | ! Configuration File for keepalived |
2)Backup(S-B 192.168.8.150)配置文件/etc/keepalived/keepalived.conf:
1 | ! Configuration File for keepalived |
2、说明:
- 为什么主备的参数state都是MASTER?否则,不能实现我们想要的VIP漂浮的效果,我测试很久才发现的。state都设置成MASTER后,会根据priority的值大小竞争来决定谁是真正的MASTER,脚本检测也是在失败的时候会把权重减去相应的值,比如原来master(181)的priority=100,如果脚本检测到端口8088无法连接,就会priority-2=98,< S-B(150)的priority(99),此时 S-B(150) 将竞争成为master,这样就实现了web应用的热备。
- 配置文件格式一定要注意,比如:track_script {,在“track_script”和“{”之间一定要加空格 ,本来测试时少了个空格,一直无法运行检测脚本,调试了很久,非常郁闷。
- vrrp_script中的script”</dev/tcp/127.0.0.1/8088” 是使用了shell中的tcp编程,访问某个主机和端口。
3、总结:
- 监测服务器层时,主机、备机的keepalived配置文件state分别配置层MASTER和SLAVE。
- 如果要监测应用层时,主备机的keepalived配置文件中state都应该是MASTER。(让其根据priority来自动选举)