一、简介
上一篇博文介绍了keepalived的高可用集群,其实使用nginx做前端代理,负载均衡照样可以实现双主或主备模式的高可用集群比起keepalived基于lvs的dr配置更为简单易学,下面是本人所理解的nginx高可用集群。此实验拓扑图类似keepalived拓扑,把图中keepalived字样换成nginx即可,我就不画了,有意者可重画。
实验拓扑图:
二、nginx主备模式高可用
配置nginx的upstream模块,让其反代到后端主机,后端主机配置和keepalived中雷同即可。
1
2
3
4
5
|
upstream websrvs {
server 10.1.10.5:80;
#后端主机real server01
server 10.1.10.6:80;
#后端主机real server02
server 10.1.10.2:80 backup;
#当后端主机均不能响应时此主机响应应急页面
}
|
配置keepalived配置文件,实现VIP地址漂移
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
|
! Configuration File
for
keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.100.18
}
vrrp_script chk_down {
script
"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -5
}
vrrp_script chk_nginx {
script
"killall -0 nginx && exit 0 || exit 1"
interval 1
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 57
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 98181111
}
virtual_ipaddress {
10.1.10.7
/16
dev eno16777736
}
track_script {
chk_down
chk_httpd
}
notify_master
"/etc/keepalived/notify.sh master"
notify_backup
"/etc/keepalived/notify.sh backup"
notify_fault
"/etc/keepalived/notify.sh fault"
}
|
实现图:
模拟单台后端主机无法提供服务及其全部宕机实验结果如下:
上诉结果可看出,此实验成功的展示了主备模式下的nginx反代,实现高可用。此实验课检测nginx的健康状态及其主备模式的切换时发邮件给管理员。
三、nginx主主模式高可用
配置keepalived配置文件,实现VIP地址漂移,和上诉主备相似
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
|
! Configuration File
for
keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.120.18
}
vrrp_script chk_down {
#检测此文件下是否存在down这个文件,灰度模式下上线时可使用此
script
"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -5
}
vrrp_script chk_nginx {
#检测nginx的健康状态信息
script
"killall -0 nginx && exit 0 || exit 1"
interval 1
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 53
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 9818sss1
}
virtual_ipaddress {
10.1.10.7
/16
dev eno16777736
}
track_script {
chk_down
chk_nginx
}
notify_master
"/etc/keepalived/notify.sh master"
notify_backup
"/etc/keepalived/notify.sh backup"
notify_fault
"/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 54
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 9818rss1
}
virtual_ipaddress {
10.1.10.77
/16
dev eno16777736
}
track_script {
chk_down
chk_nginx
}
notify_master
"/etc/keepalived/notify.sh master"
notify_backup
"/etc/keepalived/notify.sh backup"
notify_fault
"/etc/keepalived/notify.sh fault"
}
|
此时,nginx上keepalived均启动结果如图:
模拟后端主机宕机及其恢复正常结果类似keepalived如图所示:
到此,nginx的高可用集群均已实现,做实验的方法和keepalived有很多相似之处。
本文转自chengong1013 51CTO博客,原文链接:http://blog.51cto.com/purify/1867871,如需转载请自行联系原作者