Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > 负载均衡 > » 正文

Nginx + Keepalived 负载均衡

来源: lihuipeng 分享至:

喜欢搞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
tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/   #主配置文件
cp -v /usr/local/keepalived/sbin/keepalived /usr/sbin/

service keepalived start|stop           #启动、停止服务

二、配置Keepalived

/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
    
acassen@firewall.loc
    
failover@firewall.loc
    
sysadmin@firewall.loc
   }
   notification_email_from
Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/usr/local/keepalived/check_http.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51   #同一个vrrp_instance,这个ID要相同
    priority 100               #同一个vrrp_instance,这个值master要大于backup
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
        track_script {
                chk_nginx
        }
    virtual_ipaddress {
        192.168.93.144    #定义vip,也就是对外IP,可以写多个(换行)
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.93.145
    }
}

global_defs 是全局配置,定义邮件报警,我无视它了。

vrrp_script 定义一个监控脚本,作用:发现Nginx挂了,马上关掉keepalived

vrrp_instance 定义一个虚拟IP,对外服务的,其中还有个track_script ,这个就是调用刚才定义的监控脚本

另外一台前端Nginx的Keepalived配置如下:

 ! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/usr/local/keepalived/check_http.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.93.144
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
        track_script {
                chk_nginx
        }
    virtual_ipaddress {
        192.168.93.145
    }
}

另外监控脚本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

配置完成!


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史