mmdbcn 发表于 2015-11-19 14:32:22

Nginx负载均衡+keepalived双机热备

环境:
web服务器:
web1:192.168.1.51
web2:192.168.1.52
负载均衡:
LB主:192.168.1.41
LB从;192.168.1.42
VIP:192.168.1.44



安装配置nginx负载均衡(主从都一样)
#tar zxf nginx-1.1.2.tar.gz
# ./configure --user=www --group=www --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_gzip_static_module--with-http_ssl_module
#make && make install
# vim /usr/local/nginx/conf/nginx.conf
#iptables -A INPUT -p vrrp -jACCEPT    //开防火墙需要添加规则,否则备用机检测不到主机会认为主机死掉而强制切换为主机
userwww;
worker_processes1;
#pid       /data/www/logs/nginx.pid;


events {
    useepoll;
   worker_connections65535;
}


http {
   include      mime.types;
   default_typeapplication/octet-stream;


   connection_pool_size   256;
   client_max_body_size   20m;
   client_header_buffer_size   32k;
   large_client_header_buffers   4 32k;


   sendfile       on;
   tcp_nopush   on;
   tcp_nodelay   on;



   keepalive_timeout   60;
   client_header_timeout   10;
   client_body_timeout   10;
   send_timeout       10;



   gzipon;
   gzip_min_length   1k;
   gzip_buffers   4 16k;
   gzip_http_version   1.1;
   gzip_comp_level   2;
   gzip_types   text/plain application/x-javascript text/css aplication/xml;
   gzip_vary   on;


   fastcgi_cache_path   /usr/local/nginx/fastcgi_cachelevels=1:2   keys_zone=TEST:10m   inactive=5m;
   fastcgi_connect_timeout   300;
   fastcgi_send_timeout   300;
   fastcgi_read_timeout   300;
   fastcgi_buffer_size   64k;
   fastcgi_buffers      4    64k;
   fastcgi_busy_buffers_size   128k;
   fastcgi_cache_valid   200   302    1h;
   fastcgi_cache_valid   301    1d;
   fastcgi_cache_valid   any    1m;





    upstreamlb.os.com {
#   ip_hash;
       server 192.168.1.51;
       server 192.168.1.52;
#       server 192.168.1.53   down;         //暂不参与负载均衡
#       server 192.168.1.54   max_fails=3   fail_timeout=20s;   //连接失败次数3,超时20s.
       }

    server{
       listen 80;
       server_name lb.os.com;
       location / {
                      proxy_next_upstream http_502 http_504 error timeoutinvalid_header;
                      proxy_pass       http://lb.os.com;
                      proxy_set_headerHost            $host;
                      proxy_set_headerX-Real-IP       $remote_addr;
                      proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
             }
   log_formatwww'$remote_addr -$remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for""$upstream_addr"';
   access_log/data/www/access.log www;
   error_log/data/www/error.log;


       }
}











安装keepalived
#tar -zxvf keepalived-1.2.7.tar.gz
#./configure --prefix=/usr/local/keepalived
#make && make install
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
#mkdir /etc/keepalived
vim /etc/keepalived/keepalived.conf



主服务器配置:



vim/etc/keepalived/keepalived.conf ,键入以下内容

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/sh/check_nginx.sh"

interval2                  (检测的间隔)

weight 2

}


vrrp_instance VI_1 {

stateMASTER          (显示定义为主服务器)

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

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

mcast_src_ip 192.168.1.41(主服务器的IP)

priority100               (优先级,任意定义,但是一定要比从服务器高)   

advert_int 1

authentication {

auth_typePASS   

auth_pass1111      (默认即可)

}


track_script {

chk_http_port         (调用检测脚本)

}


virtual_ipaddress {

192.168.1.44       (绑定的虚IP)

}

}






从服务器配置



vim/etc/keepalived/keepalived.conf ,键入以下内容

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/sh/check_nginx.sh"

interval2                        (检测脚本执行的间隔)

weight 2

}


vrrp_instance VI_1 {

stateBACKUP          (显示定义为从服务器)

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

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

mcast_src_ip 192.168.1.42(从服务器的IP)

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

advert_int 1

authentication {

auth_typePASS   

auth_pass1111      (默认即可)

}


track_script {

chk_http_port         (调用检测脚本)

}


virtual_ipaddress {

192.168.1.44       (绑定的虚IP)

}

}



#vim /opt/sh/check_nginx.sh

#!/bin/bash
       nginxpid=`ps -C nginx --no-header | wc -l`
               if [ $nginxpid -eq 0 ];then
                     /usr/local/nginx/sbin/nginx
               sleep 3
       nginxpid=`ps -C nginx --no-header | wc -l`
               echo $nginxpid
                     if [ $nginxpid -eq 0 ];then
                               /etc/init.d/keepalived stop
                     fi
       fi





开启keepalived服务
#/etc/init.d/keepalived start


查看网卡状态
#ip a
1: lo: mtu 16436 qdisc noqueue
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether00:0c:29:3c:7e:a7 brd ff:ff:ff:ff:ff:ff
    inet192.168.1.41/24 brd 192.168.1.255 scope global eth0
    inet192.168.1.44/32 scope global eth0






             版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Nginx负载均衡+keepalived双机热备