nginx+keepalived实现nginx双主的高可用[转]

 上次介绍了一篇nginx+keepalived来实现nginx服务器的高可用,其中的一台nginx是处于闲置的,感觉有点浪费资源,,这次再介绍下nginx+keepalived的nginx双主模式。就是2台nginx同时提供服务,域名解析的时候解析到2个IP上去,有一台挂掉了,所以的请求都会转移到另一台上面去。
之前nginx以及keepalived的安装在这里有介绍:http://www.linuxyan.com/web-server/32.html

环境如下:
主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

原文链接: http://www.linuxyan.com/web-server/88.html
300*300
 文章首页关于迷茫时代关于我写意人生
版权所有:迷茫时代 All rights reserved   
执行时间:0.00470 秒