LB之Nginx




Nginx实现七层的负载均衡

调度到不同组后端服务器
1. 动静分离
2. 网站进行分区
=================================================================================

拓扑结构

         [vip: 20.20.20.20]

       [LB1 Nginx]   [LB2 Nginx]
       192.168.1.2   192.168.1.3

  [index]   [milis]  [videos]  [images]       [news]
  1.11    1.21   1.31    1.41    1.51
  1.12    1.22   1.32    1.42    1.52
  1.13    1.23   1.33    1.43    1.53
  ...     ...    ...     ...     ...
  /web             /web/milis    /web/videos    /web/images   /web/news
  index.html      index.html   index.html                           index.html


一、实施过程
方案一 根据站点分区进行调度
http {
    upstream index {
        server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
    upstream milis {
        server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
     upstream videos {
        server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
     upstream images {
        server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
      upstream news {
        server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
        server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
       }
      
     server {
           location / {
        proxy_pass
http://index;
        }
        
        location  /news {
        proxy_pass
http://news;
        }
        
        location /milis {
        proxy_pass
http://milis;
        }
        
        location ~* \.(wmv|mp4|rmvb)$ {
        proxy_pass
http://videos;
        }
        
        location ~* \.(png|gif|jpg)$ {
        proxy_pass
http://images;
        }
}
 

方案二 根据动静分离进行调度
http {
     upstream htmlservers {
        server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.2:80 weight=2 max_fails=2 fail_timeout=2;
         }
        
  upstream phpservers {
        server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
         }
        
      server {
        location ~* \.html$ {
        proxy_pass
http://htmlservers;
        }
        
        location ~* \.php$ {
        proxy_pass
http://phpservers;
        }
      }
 }


二、Keepalived实现调度器HA
注:主/备调度器均能够实现正常调度
1. 主/备调度器安装软件
[root@master ~]# yum -y install keepalived
[root@backup ~]# yum -y install keepalived

2. Keepalived
BACKUP1
[root@uplook ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id director1   //辅助改为director2
}

vrrp_instance VI_1 {
    state BACKUP
    nopreempt       
    interface eth0    //心跳接口,尽量单独连接心跳
    virtual_router_id 80  //整个集群的调度器一致
    priority 100     //辅助改为50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        20.20.20.20
    }
}

BACKUP2


3. 启动KeepAlived(主备均启动)
[root@uplook ~]# chkconfig keepalived on
[root@uplook ~]# service keepalived start
[root@uplook ~]# ip addr

到此:
可以解决心跳故障keepalived
不能解决Nginx服务故障

4. 扩展对调度器Nginx健康检查(可选)
思路:
让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Nginx失败,则关闭本机的Keepalived
a. script
[root@master ~]# cat /etc/keepalived/check_nginx_status.sh
#!/bin/bash            
/usr/bin/curl -I
http://localhost &>/dev/null 
if [ $? -ne 0 ];then          
 /etc/init.d/keepalived stop      
fi                
[root@master ~]# chmod a+x /etc/keepalived/check_nginx_status.sh

b. keepalived使用script
! Configuration File for keepalived

global_defs {
   router_id director1
}

vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx_status.sh"
   interval 5
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    nopreempt
    virtual_router_id 90
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass uplook
    }
   
    virtual_ipaddress {
        192.168.1.80
    }

    track_script {
        check_nginx
    }
}

注:必须先启动nginx,再启动keepalived





  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值