您的位置:首页 > 运维架构 > Nginx

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

2014-01-14 23:37 555 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx keepalive