喜欢搞Nginx,这两天研究了一下Nginx的负载配置
Nginx平台搭建:Nginx+mysql+php-fpm搭建高性能Nginx平台
单Nginx负载均衡:Nginx 负载均衡 配置全过程
熟悉上面的配置之后,今天我再来搞Nginx + Keepalived 的配置,具体拓扑如下:
两台前端Nginx,任意一台挂了另一台会接管岩机Nginx的虚拟Ip,前面还可以做一个DNS轮询,没在线上环境跑过,不知性能如何勒!
一、安装Keepalived
ln -s /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux |
二、配置Keepalived
/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { vrrp_script chk_nginx { vrrp_instance VI_1 { vrrp_instance VI_2 { |
global_defs 是全局配置,定义邮件报警,我无视它了。
vrrp_script 定义一个监控脚本,作用:发现Nginx挂了,马上关掉keepalived
vrrp_instance 定义一个虚拟IP,对外服务的,其中还有个track_script ,这个就是调用刚才定义的监控脚本
另外一台前端Nginx的Keepalived配置如下:
! Configuration File for keepalived global_defs { vrrp_script chk_nginx { vrrp_instance VI_1 { vrrp_instance VI_2 { |
另外监控脚本check_http.sh可以自己发挥,原则就是发现Nginx挂了就杀掉keepalived就行,写法多种
pid=`ps -C nginx --no-header | wc -l` if [ $pid -eq 0 ] then /usr/local/nginx/sbin/nginx sleep 5 newpid=`ps -C nginx --no-header | wc -l` if [ $newpid -eq 0 ] then killall keepalived fi fi |
Nginx的配置就不多说
启动服务
/usr/local/nginx/sbin/nginx service keepalived start ps -aef | grep keepalived root 28742 3288 0 03:21 pts/1 00:00:00 grep keepalived root 31674 1 0 Oct31 ? 00:00:00 keepalived -D root 31676 31674 0 Oct31 ? 00:00:00 keepalived -D root 31677 31674 0 Oct31 ? 00:00:02 keepalived -D |
三、测试
主Nginx
ip add 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:e0:a3:bf brd ff:ff:ff:ff:ff:ff inet 192.168.93.137/24 brd 192.168.93.255 scope global eth0 inet 192.168.93.144/32 scope global eth0 inet6 fe80::20c:29ff:fee0:a3bf/64 scope link valid_lft forever preferred_lft forever |
备Nginx
ip add 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:ee:a6:22 brd ff:ff:ff:ff:ff:ff inet 192.168.93.138/24 brd 192.168.93.255 scope global eth0 inet 192.168.93.145/32 scope global eth0 inet6 fe80::20c:29ff:feee:a622/64 scope link valid_lft forever preferred_lft foreve |
我把主Nginx服务停掉之后:
主Nginx
ip add 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:e0:a3:bf brd ff:ff:ff:ff:ff:ff inet 192.168.93.137/24 brd 192.168.93.255 scope global eth0 inet6 fe80::20c:29ff:fee0:a3bf/64 scope link valid_lft forever preferred_lft forever |
备Nginx
ip add 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:ee:a6:22 brd ff:ff:ff:ff:ff:ff inet 192.168.93.138/24 brd 192.168.93.255 scope global eth0 inet 192.168.93.145/32 scope global eth0 inet 192.168.93.144/32 scope global eth0 inet6 fe80::20c:29ff:feee:a622/64 scope link valid_lft forever preferred_lft forever |
备机已经接管了主的ip,然后再把主机的服务恢复:
主Nginx
ip add 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:e0:a3:bf brd ff:ff:ff:ff:ff:ff inet 192.168.93.137/24 brd 192.168.93.255 scope global eth0 inet 192.168.93.144/32 scope global eth0 inet6 fe80::20c:29ff:fee0:a3bf/64 scope link valid_lft forever preferred_lft forever |
备Nginx
ip add 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:ee:a6:22 brd ff:ff:ff:ff:ff:ff inet 192.168.93.138/24 brd 192.168.93.255 scope global eth0 inet 192.168.93.145/32 scope global eth0 inet6 fe80::20c:29ff:feee:a622/64 scope link valid_lft forever preferred_lft forever |
配置完成!
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/703123如需转载请自行联系原作者
lihuipeng