码迷,mamicode.com
首页 > 其他好文 > 详细

nginx+keepalived实现双机双主高可用性

时间:2015-06-12 11:48:05      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:nginx+keepalived   双机双主热备   ha可用性   

1.前言

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

2.准备:

两台主机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,漂移到另一台机器上,这样就实现了双机双主互为主机和备份。

3.安装

安装可以查看我的上篇文章:nginx+keepalived实现双机热备高可用性 http://blog.csdn.net/caicongyang/article/details/46444007

4.配置

128机器 keepalived.conf配置文件

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配置文件

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
}
}

5.启动和测试

启动nginx
#/usr/local/nginx/sbin/nginx
启动keepalived
#service keepalived start

测试
#ip a

128机器结果
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

129机器结果
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

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

如同上篇文章所说的,你可以用一下命令查看整个过程
#tail -f /var/log/messages  


后记:这两边nginx+keepalied高可用性的文章中,我们配置了notification_email但是都没用到,楼主将在下篇文章配置smtp服务,让其生效,这样就能在服务发生异常的情况下,发送邮件提醒运维人员。当然博主我也会在后期发布免费网络监视工具Nagios的相关文章,将其整合进来,具体利弊后期分析。


不懂运维的程序员,不是好的工程师!



我的个人网站:http://www.caicongyang.com

我的CSDN博客地址: http://blog.csdn.net/caicongyang 








nginx+keepalived实现双机双主高可用性

标签:nginx+keepalived   双机双主热备   ha可用性   

原文地址:http://blog.csdn.net/caicongyang/article/details/46468397

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!