nginx+keepalived实现nginx双主的高可用

nginx+keepalived来实现nginx服务器的高可用,其中的一台nginx是处于闲置的,感觉有点浪费资源,,这次再介绍下nginx+keepalived的nginx双主模式。就是2台nginx同时提供服务,域名解析的时候解析到2个IP上去,有一台挂掉了,所以的请求都会转移到另一台上面去。

环境如下:
主nginx服务器:184.173.91.43
备nginx服务器:184.173.91.44
VIP1:184.173.91.47
VIP2:184.173.91.48
nginx和keepalived安装好以后编辑主nginx上面的keepalived.conf

[caoy@localhost ~]$ vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
linuxcy@126.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 “/bin/whole51/nginx_pid.sh”
interval 2
weight 2
}
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 {
184.173.91.47
}
}
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 {
184.173.91.48
}
}

备nginx上面的keepalived.conf文件如下:

[caoy@localhost ~]$ vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
linuxcy@126.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 “/bin/whole51/nginx_pid.sh”
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
184.173.91.47
}
}
vrrp_instance VI_2 {
state MASTER
interface eth1
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
184.173.91.48
}
}

然后分别在2台nginx上面添加检测脚本:
[caoy@localhost ~]$ vi /bin/whole51/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi

分别在2台nginx上启动nginx和keepalived服务,然后分别用ip a来查看IP
主nginx(184.173.91.43)的ip
[caoy@localhost ~]$ /sbin/ip a
3: eth1: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether ae:bb:d0:43:c0:fc brd ff:ff:ff:ff:ff:ff
inet 184.173.91.43/27 brd 184.173.91.63 scope global eth1
inet 184.173.91.47/32 scope global eth1
inet6 fe80::acbb:d0ff:fe43:c0fc/64 scope link
valid_lft forever preferred_lft forever

备nginx(184.173.91.44)的ip
[caoy@localhost ~]$ /sbin/ip a
3: eth1: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether f2:3b:4b:db:e5:d7 brd ff:ff:ff:ff:ff:ff
inet 184.173.91.44/27 brd 184.173.91.63 scope global eth1
inet 184.173.91.48/32 scope global eth1
inet6 fe80::f03b:4bff:fedb:e5d7/64 scope link
valid_lft forever preferred_lft forever


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值