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

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的相关文章,将其整合进来,详细利弊后期分析。


不懂运维的程序猿,不是好的project师!



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

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








原文地址:https://www.cnblogs.com/wgwyanfs/p/7106884.html