码迷,mamicode.com
首页 > 其他好文 > 详细

nginx负载均衡+keepalived高可用完全配置小结

时间:2014-08-04 11:14:47      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:nginx   负载均衡   keepalived   高可用   

nginx做负载均衡(无高可用)

大致步骤。

1. 前端

nginx安装,pcre安装,具体步骤不解释。

2. 负载配置

A. 默认轮循

在nginx.conf  里加入一行

include upstream.conf,然后所有的负载均衡的配置直接在upstream.conf里配置。

[root@DS1 conf]# cat upstream.conf 

upstream httpservers {

     server 192.168.137.10:80 weight=5;

     server 192.168.137.20:80 weight=5;

}



server { 

    listen 80;

    server_name 192.168.137.100;

    location / {

       proxy_pass http://httpservers;

      }


}



A. ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题



[root@DS1 conf]# cat upstream.conf 

upstream httpservers {

     ip_hash; 

     server 192.168.137.10:80 weight=5;

     server 192.168.137.20:80 weight=5;

}

server { 

    listen 80;

    server_name 192.168.137.100;

    location / {

       proxy_pass http://httpservers;

      }

}

nginx做负载均衡(keepalived高可用)

拓扑图

bubuko.com,布布扣

bubuko.com,布布扣


1. nginx配置(在主备服务器上配置)


upstream httpservers {

     ip_hash; 

     server 192.168.137.10:80 weight=5;

     server 192.168.137.20:80 weight=5;

}

server { 

    listen 80;

    server_name 192.168.137.201;

    location / {

       proxy_pass http://httpservers;

      }

}

2. keepalived安装及配置(在主备服务器上配置)

下载:wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz

解压:tar zxvf keepalived-1.2.8.tar.gz

编译安装: ./configure --prefix=/usr/local/keepalived

                   make

                   make install




cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d

cp /usr/local/keepalived/sbin/keepalived /sbin/


mkdir -p /etc/keepalived/

 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/   (配置文件一定要在这里,让我费了好大的劲)


修改/etc/keepalived/keepalived.conf ,  以下分别列出master和backup的配置


+++++++++++++++++++++++++++++++++++++master+++++++++++++++++++++++++++++++++++++++++++

! Configuration File for keepalived

     global_defs {

     router_id nginx-proxy-ha

     }

vrrp_script chk_http_port {

     script "/usr/bin/check_nginx.sh"

     interval 2

     weight 2

     }

vrrp_instance VI_1 {

     state MASTER

     interface eth0

     virtual_router_id 51

     priority 200

     advert_int 1

     authentication {

     auth_type PASS

     auth_pass 1234

     }

track_interface { 

     eth0  

     } 

track_script {

     chk_http_port

     } 

virtual_ipaddress {

     192.168.137.201

     }

}

++++++++++++++++++++++++backup++++++++++++++++++++++++++++++++++++++++++++++++++

! Configuration File for keepalived

     global_defs {

     router_id nginx-proxy-ha

     }

vrrp_script chk_http_port {

     script "/usr/bin/check_nginx.sh"

     interval 2

     weight 2

     }

vrrp_instance VI_1 {

     state BACKUP

     interface eth0

     virtual_router_id 51

     priority 180

     advert_int 1

     authentication {

     auth_type PASS

     auth_pass 1234

     } 

track_interface { 

     eth0  

     } 

track_script {

     chk_http_port

     } 

virtual_ipaddress {

     192.168.137.201

     }

}

以上配置完成之后 启动nginx和keepalived,一定要先启动nginx,然后在启动keepalived。

测试效果:master和backup都启动keepalived和nginx后,默认的虚拟ip是在master上的,在master上kill nginx后,浮动ip会绑定在backup上。

各配置解释:

global_defs {

 

notification_email {

admin@company.com     (这里可以定义多个报警邮箱)

 

}

notification_email_from alarm@company.com (报警人)

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

 

vrrp_script chk_http_port {

script "/opt/tools/bin/check_ng.sh"

interval 2                           (检测脚本执行的间隔)

weight 2

}

 

vrrp_instance VI_1 {

state BACKUP           (显示定义为从服务器)

interface eth1            (绑定的网口,该网口即上面提到的两个IP的接口)

virtual_router_id 51    (定义的ID,官方的是 51,主从服务器必须一直)

mcast_src_ip 211.151.138.3  (从服务器的IP)

priority 50                (优先级,任意定义,但是一定要比主服务器低)     

advert_int 1

authentication {

auth_type PASS       

auth_pass 1111         (默认即可)  

}

 

track_script {

chk_http_port            (调用检测脚本)

}

 

virtual_ipaddress {

211.151.137.5         (绑定的虚IP)

}

}

++++++++++++++++++++++check_nginx.sh+++++++++++++++++++++++++++++++++++++++++++++++++

该脚本用来检测nginx状态,一旦nginx退出,则关闭master的 keepalived,此时的backup收到消息会自动切换为master并绑定虚拟ip。该脚本放在/usr/bin下

#!/bin/bash

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]

 then

  killall -9  keepalived

fi 


本文出自 “放飞梦想” 博客,请务必保留此出处http://flyingdreams.blog.51cto.com/802109/1535155

nginx负载均衡+keepalived高可用完全配置小结,布布扣,bubuko.com

nginx负载均衡+keepalived高可用完全配置小结

标签:nginx   负载均衡   keepalived   高可用   

原文地址:http://flyingdreams.blog.51cto.com/802109/1535155

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!