雅冰石 / nginx+keepalive实现高可用负载均衡

来自CSDN博客:nginx+keepalive实现高可用负载均衡 http://blog.csdn.net/YABIGNSHI/article/details/52038332#

  系统运维 最后更新时间 2016-07-27 08:54:58
blog_20160726_1_8795678 7行 Text
Raw
 1
 2
 3
 4
 5
 6
 7
yum install -y popt-devel
cd /usr/local/src
tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/
make
make install
blog_20160726_2_7399455 7行 Text
Raw
 1
 2
 3
 4
 5
 6
 7
vi /root/check_nginx.sh
if [ "$(ps -ef | grep "nginx:master process"| grep -v grep )" == "" ]
then
/etc/init.d/keepalived stop
else
echo "nginx is running"
fi
blog_20160726_3_7606415 31行 Text
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
#ConfigurationFile for keepalived
global_defs {
notification_email { ######定义接受邮件的邮箱
wangjj@hrloo.com
}
notification_email_from jiankong@staff.tuge.com ######定义发送邮件的邮箱
smtp_server mail.tuge.com
smtp_connect_timeout 10
}
vrrp_script check_nginx { ######定义监控nginx的脚本
script "/root/check_nginx.sh"
interval 2 ######监控时间间隔
weight 2 ######负载参数
}
vrrp_instance vrrptest { ######定义vrrptest实例
state BACKUP ######服务器状态
interface eth0 ######使用的接口
virtual_router_id 51 ######虚拟路由的标志,一组lvs的虚拟路由标识必须相同,这样才能切换
priority 150 ######服务启动优先级,值越大,优先级越高,BACKUP 不能大于MASTER
advert_int 1 ######服务器之间的存活检查时间
authentication {
auth_type PASS ######认证类型
auth_pass ufsoft ######认证密码,一组lvs 服务器的认证密码必须一致
}
track_script { ######执行监控mysql进程的脚本
check_nginx
}
virtual_ipaddress { ######虚拟IP地址
10.192.203.106
}
}
blog_20160726_4_3876662 18行 Text
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
[root@PCkeepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 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: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000
link/ether 08:00:27:04:05:16 brd ff:ff:ff:ff:ff:ff
inet 10.192.203.201/24 brd 10.192.203.255 scope global eth0
inet 10.192.203.106/32 scope global eth0
inet6 fe80::a00:27ff:fe04:516/64 scope link
valid_lft forever preferred_lft forever
3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000
link/ether 08:00:27:3a:ec:3c brd ff:ff:ff:ff:ff:ff
inet 10.0.0.1/24 brd 10.0.0.255 scope global eth1
inet6 fe80::a00:27ff:fe3a:ec3c/64 scope link
valid_lft forever preferred_lft forever
blog_20160726_5_2306498 12行 Text
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
[root@PC keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 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: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000
link/ether 08:00:27:04:05:16 brd ff:ff:ff:ff:ff:ff
inet 10.192.203.102/24 brd 10.192.203.255 scope global eth0
inet 10.192.203.106/32 scope global eth0
inet6 fe80::a00:27ff:fe04:516/64 scope link
valid_lft forever preferred_lft forever