部署nginx+keepalived实现双机双主高可用性环境

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了部署nginx+keepalived实现双机双主高可用性环境相关的知识,希望对你有一定的参考价值。

 

双机热备,意思是说两台机器一台处于运行中,另一台永久处于备份。这样的话资源浪费严重,适用于国企,政府类系统。今天我们所讲的是双机双主热备,意味着两台机器同时处于工作状态,两台主机又同时为备份状态。

准备:

两台主机192.168.100.128/24 192.168.100.129/24

两个单独的ip 192.168.100.133/24 192.168.100.134/24

可以将你域名解析到这两个单独的ip,keepalived会将这两个ip通过虚拟ip技术漂移到两台主机上。如果一台机器down,keepalived会将原本漂移在它身上的ip,漂移到另一台机器上,这样就实现了双机双主互为主机和备份。

安装

此处省略......

配置

 

128机器 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
global_defs {
notification_email {
1491318829@qq.com
}
notification_email_from keepalived@localhost.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh"
interval 2
weight -20 #每检查一次,权重减去20
}
vrrp_instance VI_1 {
state MASTER
interface eth1 #注意看到你本机的网络接口
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.100.133
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth1 #两处都为本机的网络接口
virtual_router_id 52
priority 99 #权重
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.134/24
}
}

129机器 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
global_defs {
notification_email {
1491318829@qq.com               #接收警报的email地址,可以添加多个
}
notification_email_from admin@caicongyang.com   ###发件人地址
smtp_server 127.0.0.1          ###发送邮件的服务器
smtp_connect_timeout 30      ###超时时间
router_id LVS_DEVEL      ####load balancer 的标识 ID,用于email警报
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh" ####检测nginx状态的脚本路径
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth2
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.133/24
}
}
vrrp_instance VI_2 {
state MASTER
interface eth2
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.100.134/24
}
}

 

启动和测试

启动nginx

1
#/usr/local/nginx/sbin/nginx

启动keepalived

1
#service keepalived start


测试

1
#ip a


128机器结果

1
2
3
4
5
6
7
8
9
10
11
12
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: eth1: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ff:ca:24 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.128/24 brd 192.168.100.255 scope global eth1
    inet 192.168.100.133/32 scope global eth1
    inet6 fe80::20c:29ff:feff:ca24/64 scope link
       valid_lft forever preferred_lft forever
</broadcast,multicast,up,lower_up></loopback,up,lower_up>


129机器结果

1
2
3
4
5
6
7
8
9
10
11
12
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: eth2: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c0:02:92 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.129/24 brd 192.168.100.255 scope global eth2
    inet 192.168.100.134/24 scope global secondary eth2
    inet6 fe80::20c:29ff:fec0:292/64 scope link
       valid_lft forever preferred_lft forever
</broadcast,multicast,up,lower_up></loopback,up,lower_up>


我们可以看到ip 133已经绑定到128的机器上了,134已经绑定到129的机器上了 接下来我们可以利用在128机器killall nginx查看上篇文章中的脚本能否起作用,结果是我们可以看到nginx会不断的重启。 那我们直接关掉128的机器,或者关闭的它的网络,然后看到ip133是否能自动绑定到129的机器上呢?结果当然是可以的

1
2
3
4
5
6
7
8
9
10
11
12
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: eth2: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c0:02:92 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.129/24 brd 192.168.100.255 scope global eth2
    inet 192.168.100.134/24 scope global secondary eth2
    inet 192.168.100.133/24 scope global secondary eth2
    inet6 fe80::20c:29ff:fec0:292/64 scope link
       valid_lft forever preferred_lft forever</broadcast,multicast,up,lower_up></loopback,up,lower_up>

如同上篇文章所说的,你可以用一下命令查看整个过程

1
#tail -f /var/log/messages

 

以上是关于部署nginx+keepalived实现双机双主高可用性环境的主要内容,如果未能解决你的问题,请参考以下文章

基于Keepalived实现LVS双主高可用集群

单主模型的IPVS高可用和nginx+keepalived的双主高可用模型

LVS基础及LVS+Keepalived实现双主高可用负载均衡

配置Keepalived实现mysql双主高可用

Nginx+keepalived双主配置(双机双主热备)

Haproxy+Keepalived实现网站双主高可用-理论篇