zoukankan      html  css  js  c++  java
  • 用NGINX做负载均衡,keepalived高可用

    实验环境,四台虚拟机,两台做负载均衡,两台做RS
    IP地址:两台负载均衡分别为:10.0.0.7;10.0.0.8(高可用keepalived)
    两台 RS主机地址为: 10.0.0.9;10.0.0.10
    系统:centos6.6
    介绍说明
    实现Nginx负载均衡的组件主要有两个,
    ngx_http_proxy_module proxy代理模块,用于把请求抛给服务器节点或者upstream服务池
    ngx_http_unpstream_module 负载均衡模块,可以实现网站的负载均衡功能以及节点的健康检查

    1. 首先在四台机器上都安装上Nginx服务

    其中安装过程如下,

    #安装Nginx需要的依赖包 
    yum -y install openssl openssl-devel pcre pcre-devel
    #下载Nginx源码包
    wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
    #解压Nginx源码包
    tar xvf nginx-1.6.3.tar.gz
    #进入解压之后的Nginx目录
    cd nginx-1.6.3
    #创建Nginx的组
     groupadd nginx
    #创建Nginx的用户,并且不允许登录操作系统
    useradd -s /sbin/nologin -g nginx nginx
    #进行编译
    ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module
    #编译后安装
    make && make install
    #创建一个软连接
    ln -s /usr/local/nginx-1.6.3/sbin/nginx /etc/init.d/nginx
    

    启动Nginx服务
    /usr/local/nginx-1.6.3/sbin/nginx -c /usr/local/nginx-1.6.3/conf/nginx.conf
    添加80端口到防火墙,被允许访问
    sed -i ‘10i -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT’ /etc/sysconfig/iptables
    重启防火墙
    /etc/init.d/iptables restart

    其中,两台RS的nginx.conf配置如下:

    #Nginx的进程数
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    #主配置文件
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" ';
    #进行虚拟主机等配置的模块
        server {
            listen       80;
            server_name  bbs.etiantian.org;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
            access_log logs/access_bbs.log main;
        }
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
            access_log logs/access_bbs.log main;
        }
    }
    

    然后分别在两台上执行以下命令

    [root@web01 ~]# mkdir /usr/local/nginx-1.6.3/html/{www,bbs}
    [root@web01 ~]# for dir in www bbs;do echo "`ifconfig eth0|grep -o "10.0.0.[109]."` $dir " > /usr/local/nginx-1.6.3/html/$dir/index.html;done
    [root@web01 ~]# for dir in www bbs;do cat /usr/local/nginx-1.6.3/html/$dir/index.html ;done
    

    web02主机上执行以下

    [root@web01 ~]# mkdir /usr/local/nginx-1.6.3/html/{www,bbs}
    [root@web02 ~]# for dir in www bbs;do echo "`ifconfig eth0|grep -o "10.0.0.[109]."` $dir " > /usr/local/nginx-1.6.3/html/$dir/index.html;done
    [root@web02 ~]# for dir in www bbs;do cat /usr/local/nginx-1.6.3/html/$dir/index.html ;done
    

    然后在主备负载均衡器:10.0.0.7,8两台机器上配置nginx.conf文件

    [root@lb01 ~]# vim /usr/local/nginx-1.6.3/conf/nginx.conf
    worker_processes  1;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        keepalive_timeout  65;
    #定义web服务池,其中包含了两个节点
        upstream www_server_pools {
                server 10.0.0.9:80 weight=1;
                server 10.0.0.10:80 weight=1;
        }
    
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / {
    #访问web服务器池的节点
                 proxy_pass http://www_server_pools;
            }
        }
    
    }
    

    测试
    由于我本实验没有dns域名服务器解析IP地址,所以我们得要在hosts文件里面添加ip和对应的域名
    首先在两台RS/etc/hosts分别加入

    10.0.0.9     www.etiantian.org
    10.0.0.9     bbs.etiantian.org
    
    
    10.0.0.10     www.etiantian.org
    10.0.0.10     bbs.etiantian.org
    

    然后在Nginx主负载均衡服务器上/etc/hosts

    10.0.0.7     www.etiantian.org
    
    1. keepalive高可用之间是通过VRRP通信的,那么,什么是VRRP呢?

    VRRP是虚拟路由冗余协议,它是为了解决静态路由的单点故障的
    VRRP是通过一种竞选协议机制来将路由任务交给某台VRRP路由器的
    VRRP用IP多播的方式实现高可用之间的通信
    VRRP工作是主节点发包,备节点接包,档备节点收不到主节点发的数据包的时候,就启动接管程序接管主节点的资源。备节点可以有很多个,通过优先级竞选,但一般keepalive系统运维中都是一对存在的

    1. 因此,keepalive是通过VRRP进行通信的,VRRP是通过竞选机制进行确定主备的,主的优选级高于备的优级,工作时候,主首先获得所有资源,备节点处于等待状态,当主节宕机的时候,备节点就会接管主节点的所有资源,然后顶替主节点对外提供所有服
    

    开始安装keepalived软件
    yum -y install keepalived
    /etc/init.d/keepalived start
    修改配置文件
    主节点

    1 ! Configuration File for keepalived
    2
    3 global_defs {
    4 notification_email {
    5 919497370@qq.com
    6 #failover@firewall.loc
    7 #sysadmin@firewall.loc
    8 }
    9 notification_email_from Alexandre.Cassen@firewall.loc
    10 smtp_server smtp.qq.com
    11 smtp_connect_timeout 30
    12 router_id lb01
    13 }
    14
    15 vrrp_instance VI_1 {
    16 state MASTER
    17 interface eth0
    18 virtual_router_id 55
    19 priority 150
    20 advert_int 1
    21 authentication {
    22 auth_type PASS
    23 auth_pass 1111
    24 }
    25 virtual_ipaddress {
    26 #192.168.200.16
    27 #192.168.200.17
    28 #192.168.200.18
    29 10.0.0.12/24 dev eth0 label eth0:1
    30 }
    31 }

    备节点

      1 ! Configuration File for keepalived
      2
      3 global_defs {
      4    notification_email {
      5      919497370@qq.com
      6      #failover@firewall.loc
      7      #sysadmin@firewall.loc
      8    }
      9    notification_email_from Alexandre.Cassen@firewall.loc
     10    smtp_server smtp.qq.com
     11    smtp_connect_timeout 30
     12    router_id lb02
     13 }
     14
     15 vrrp_instance VI_1 {
     16     state BACKUP
     17     interface eth0
     18     virtual_router_id 55
     19     priority 100
     20     advert_int 1
     21     authentication {
     22         auth_type PASS
     23         auth_pass 1111
     24     }
     25     virtual_ipaddress {
     26         #192.168.200.16
     27         #192.168.200.17
     28         #192.168.200.18
     29         10.0.0.12/24 dev eth0 label eth0:1
     30     }
     31 }
     32
    
  • 相关阅读:
    01 mybatis框架整体概况(2018.7.10)-
    第一课(2018.7.10)
    JavaEE 企业级分布式高级架构师课程_汇总贴
    5-1条件运算符 & 5-2
    5-3运算符的优先级
    4-3逻辑非运算符及案例 & 4-4
    4-1逻辑与运算符介绍 & 4-2逻辑或运算符介绍
    3-3if-else条件结构 & 3-4 & 3-5
    3-2if条件结构
    3-1关系运算符
  • 原文地址:https://www.cnblogs.com/sujc-blogs/p/9722485.html
Copyright © 2011-2022 走看看