三、负载均衡与web服务集群搭建

一、负载均衡LVS基本介绍

    LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director Server上,然后它把用户的请求根据设置好的调度算法,智能均衡地分发到后端真正服务器(real server)上。为了避免不同机器上用户请求得到的数据不一样,需要用到了共享存储,这样保证所有用户请求的数据是一样的。

    LVS是 Linux Virtual Server 的简称,也就是Linux虚拟服务器。这是一个由章文嵩博士发起的一个开源项目,它的官方网站是http://www.linuxvirtualserver.org 现在 LVS 已经是 Linux 内核标准的一部分。使用 LVS 可以达到的技术目标是:通过 LVS 达到的负载均衡技术和 Linux 操作系统实现一个高性能高可用的 Linux 服务器集群,它具有良好的可靠性、可扩展性和可操作性。从而以低廉的成本实现最优的性能。LVS 是一个实现负载均衡集群的开源软件项目,LVS架构从逻辑上可分为调度层、Server集群层和共享存储。

 

二、LVS的基本工作原理

1. 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间
2. PREROUTING链首先会接收到用户请求,判断目标IP确定是本机IP,将数据包发往INPUT链
3. IPVS是工作在INPUT链上的,当用户请求到达INPUT时,IPVS会将用户请求和自己已定义好的集群服务进行比对,如果用户请求的就是定义的集群服务,那么此时IPVS会强行修改数据包里的目标IP地址及端口,并将新的数据包发往POSTROUTING链
4. POSTROUTING链接收数据包后发现目标IP地址刚好是自己的后端服务器,那么此时通过选路,将数据包最终发送给后端的服务器

 

三、LVS的组成

LVS 由2部分程序组成,包括 ipvs 和 ipvsadm。

1. ipvs(ip virtual server):一段代码工作在内核空间,叫ipvs,是真正生效实现调度的代码。
2. ipvsadm:另外一段是工作在用户空间,叫ipvsadm,负责为ipvs内核框架编写规则,定义谁是集群服务,而谁是后端真实的服务器(Real Server)

 

四、LVS相关术语

1. DS:Director Server。指的是前端负载均衡器节点。
2. RS:Real Server。后端真实的工作服务器。
3. VIP:向外部直接面向用户请求,作为用户请求的目标的IP地址。
4. DIP:Director Server IP,主要用于和内部主机通讯的IP地址。
5. RIP:Real Server IP,后端服务器的IP地址。
6. CIP:Client IP,访问客户端的IP地址。

下边是三种工作模式的原理和特点总结。

 

五、LVS/NAT原理和特点

1. 重点理解NAT方式的实现原理和数据包的改变。

(a). 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP 
(b). PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
(c). IPVS比对数据包请求的服务是否为集群服务,若是,修改数据包的目标IP地址为后端服务器IP,然后将数据包发至POSTROUTING链。 此时报文的源IP为CIP,目标IP为RIP 
(d). POSTROUTING链通过选路,将数据包发送给Real Server
(e). Real Server比对发现目标为自己的IP,开始构建响应报文发回给Director Server。 此时报文的源IP为RIP,目标IP为CIP 
(f). Director Server在响应客户端前,此时会将源IP地址修改为自己的VIP地址,然后响应给客户端。 此时报文的源IP为VIP,目标IP为CIP

2. LVS-NAT模型的特性

  • RS应该使用私有地址,RS的网关必须指向DIP

  • DIP和RIP必须在同一个网段内

  • 请求和响应报文都需要经过Director Server,高负载场景中,Director Server易成为性能瓶颈

  • 支持端口映射

  • RS可以使用任意操作系统

  • 缺陷:对Director Server压力会比较大,请求和响应都需经过director server

 

六、LVS/DR原理和特点

1. 重将请求报文的目标MAC地址设定为挑选出的RS的MAC地址

 

(a) 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP
(b) PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
(c) IPVS比对数据包请求的服务是否为集群服务,若是,将请求报文中的源MAC地址修改为DIP的MAC地址,将目标MAC地址修改RIP的MAC地址,然后将数据包发至POSTROUTING链。 此时的源IP和目的IP均未修改,仅修改了源MAC地址为DIP的MAC地址,目标MAC地址为RIP的MAC地址 
(d) 由于DS和RS在同一个网络中,所以是通过二层来传输。POSTROUTING链检查目标MAC地址为RIP的MAC地址,那么此时数据包将会发至Real Server。
(e) RS发现请求报文的MAC地址是自己的MAC地址,就接收此报文。处理完成之后,将响应报文通过lo接口传送给eth0网卡然后向外发出。 此时的源IP地址为VIP,目标IP为CIP 
(f) 响应报文最终送达至客户端


2. LVS-DR模型的特性

  • 特点1:保证前端路由将目标地址为VIP报文统统发给Director Server,而不是RS

  • RS可以使用私有地址;也可以是公网地址,如果使用公网地址,此时可以通过互联网对RIP进行直接访问

  • RS跟Director Server必须在同一个物理网络中

  • 所有的请求报文经由Director Server,但响应报文必须不能进过Director Server

  • 不支持地址转换,也不支持端口映射

  • RS可以是大多数常见的操作系统

  • RS的网关绝不允许指向DIP(因为我们不允许他经过director)

  • RS上的lo接口配置VIP的IP地址

  • 缺陷:RS和DS必须在同一机房中

3. 特点1的解决方案:

  • 在前端路由器做静态地址路由绑定,将对于VIP的地址仅路由到Director Server

  • 存在问题:用户未必有路由操作权限,因为有可能是运营商提供的,所以这个方法未必实用

  • arptables:在arp的层次上实现在ARP解析时做防火墙规则,过滤RS响应ARP请求。这是由iptables提供的

  • 修改RS上内核参数(arp_ignore和arp_announce)将RS上的VIP配置在lo接口的别名上,并限制其不能响应对VIP地址解析请求。

 

七、LVS/Tun原理和特点

在原有的IP报文外再次封装多一层IP首部,内部IP首部(源地址为CIP,目标IIP为VIP),外层IP首部(源地址为DIP,目标IP为RIP)

(a) 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP 。
(b) PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
(c) IPVS比对数据包请求的服务是否为集群服务,若是,在请求报文的首部再次封装一层IP报文,封装源IP为为DIP,目标IP为RIP。然后发至POSTROUTING链。 此时源IP为DIP,目标IP为RIP 
(d) POSTROUTING链根据最新封装的IP报文,将数据包发至RS(因为在外层封装多了一层IP首部,所以可以理解为此时通过隧道传输)。 此时源IP为DIP,目标IP为RIP
(e) RS接收到报文后发现是自己的IP地址,就将报文接收下来,拆除掉最外层的IP后,会发现里面还有一层IP首部,而且目标是自己的lo接口VIP,那么此时RS开始处理此请求,处理完成之后,通过lo接口送给eth0网卡,然后向外传递。 此时的源IP地址为VIP,目标IP为CIP
(f) 响应报文最终送达至客户端

LVS-Tun模型特性

  • RIP、VIP、DIP全是公网地址

  • RS的网关不会也不可能指向DIP

  • 所有的请求报文经由Director Server,但响应报文必须不能进过Director Server

  • 不支持端口映射

  • RS的系统必须支持隧道

其实企业中最常用的是 DR 实现方式,而 NAT 配置上比较简单和方便,后边实践中会总结 DR 和 NAT 具体使用配置过程。

 

八、LVS的八种调度算法

1. 轮叫调度 rr
这种算法是最简单的,就是按依次循环的方式将请求调度到不同的服务器上,该算法最大的特点就是简单。轮询算法假设所有的服务器处理请求的能力都是一样的,调度器会将所有的请求平均分配给每个真实服务器,不管后端 RS 配置和处理能力,非常均衡地分发下去。

2. 加权轮叫 wrr
这种算法比 rr 的算法多了一个权重的概念,可以给 RS 设置权重,权重越高,那么分发的请求数越多,权重的取值范围 0 – 100。主要是对rr算法的一种优化和补充, LVS 会考虑每台服务器的性能,并给每台服务器添加要给权值,如果服务器A的权值为1,服务器B的权值为2,则调度到服务器B的请求会是服务器A的2倍。权值越高的服务器,处理的请求越多。

3. 最少链接 lc
这个算法会根据后端 RS 的连接数来决定把请求分发给谁,比如 RS1 连接数比 RS2 连接数少,那么请求就优先发给 RS1 

4. 加权最少链接 wlc
这个算法比 lc 多了一个权重的概念。

5. 基于局部性的最少连接调度算法 lblc
这个算法是请求数据包的目标 IP 地址的一种调度算法,该算法先根据请求的目标 IP 地址寻找最近的该目标 IP 地址所有使用的服务器,如果这台服务器依然可用,并且有能力处理该请求,调度器会尽量选择相同的服务器,否则会继续选择其它可行的服务器

6. 复杂的基于局部性最少的连接算法 lblcr
记录的不是要给目标 IP 与一台服务器之间的连接记录,它会维护一个目标 IP 到一组服务器之间的映射关系,防止单点服务器负载过高。

7. 目标地址散列调度算法 dh
该算法是根据目标 IP 地址通过散列函数将目标 IP 与服务器建立映射关系,出现服务器不可用或负载过高的情况下,发往该目标 IP 的请求会固定发给该服务器。

8. 源地址散列调度算法 sh
与目标地址散列调度算法类似,但它是根据源地址散列算法进行静态分配固定的服务器资源。



下面实践lvs nat模式+nginx反向代理

QQ截图20180524174723.png


主机名 ip地址 角色 vip
lvs01 192.168.1.90 4层负载 192.168.1.206
lvs02 192.168.1.91 4层负载 192.168.1.206
lb01 192.168.1.5 7层负载
lb02  192.168.1.6 7层负载
web-nginx01  172.16.1.33 静态服务器
web-nginx02 172.16.1.34 静态服务器
web-tomcat01 172.16.1.31 动态服务器
web-tomcat02 172.16.1.32 动态服务器


lvs部署

1.编辑realserver脚本文件(在lb01 lb02上)

cd /etc/init.d/
vim realserver
# chkconfig: 35 23 94
SNS_VIP=192.168.1.206
. /etc/init.d/functions
case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_VIP dev lo:0
       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       ifconfig lo:0 down
       route del $SNS_VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
exit 0

chmod 755 realserver
service realserver start
chkconfig realserver on


2.配置主负载服务器

安装Keepalived ipvsadm相关包

yum install -y keepalived ipvsadm
vim /etc/keepalived/keepalived.conf
global_defs {  
   notification_email {  
         [email protected]  
   }  
   notification_email_from [email protected]  
   smtp_server 192.168.80.1  
   smtp_connection_timeout 30
   router_id LVS01              #设置lvs的id,在一个网络内应该是唯一的
}  
vrrp_instance VI_1 {  
    state MASTER               #指定Keepalived的角色,MASTER为主,BACKUP为备          
    interface eth1              #指定Keepalived检测网卡
    virtual_router_id 51           #虚拟路由编号,主备要一致
    priority 100               #定义优先级,数字越大,优先级越高,主DR必须大于备用DR    
    advert_int 1               #检查间隔,默认为1s
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    virtual_ipaddress {  
        192.168.1.206           #定义虚拟IP(VIP)为192.168.1.206,可多设,每行一个
    }  
}  
                                                  
virtual_server 192.168.80.200 80 {           #定义对外提供服务的LVS的VIP以及port
    delay_loop 6               #设置健康检查时间,单位是秒                    
    lb_algo wrr               #设置负载调度的算法为wrr                   
    lb_kind DR                #设置LVS实现负载的机制,有NAT、TUN、DR三个模式   
    nat_mask 255.255.255.0                
    persistence_timeout 0          
    protocol TCP                  
    real_server 192.168.1.5 80 {      #指定real server1的IP地址
        weight 3                    #配置节点权值,数字越大权重越高              
        TCP_CHECK {  
        connect_timeout 10         
        nb_get_retry 3  
        delay_before_retry 3  
        connect_port 80  
        }  
    }  
    real_server 192.168.1.6 80 {      #指定real server2的IP地址
        weight 3   
        TCP_CHECK {  
        connect_timeout 10  
        nb_get_retry 3  
        delay_before_retry 3  
        connect_port 80  
        }  
     }  
}


3.配置备负载服务器

只需要修改

router_id LVS02
priority 99


4.启动keepalived (lvs01 lvs02)

/etc/init.d/keepalived start
chkconfig keepalived on



nginx反向代理 7层负载

1.安装nginx(lb01 lb02)

yum install  -y pcre-devel openssl-devel pcre openssl
mkdir -p /application/tools/
cd /application/tools/
wget  http://nginx.org/download/nginx-1.12.2.tar.gz
useradd www -M -s /sbin/nologin -u 504
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
ln -s /application/nginx-1.12.2/ /application/nginx
echo "#nginx" >>/etc/rc.local
echo "/application/nginx/sbin/nginx" >>/etc/rc.local
cd /application/nginx/conf
egrep -v "#|^$" /application/nginx/conf/nginx.conf.default > /application/nginx/conf/nginx.conf


添加后端服务器nginx健康检测模块

1.下载模块

wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
unzip master


2.打补丁

cd /application/tools/nginx-1.12.2
patch -p1 < /application/tools/nginx_upstream_check_module-master/check_1.12.1+.patc


3.重新编译

./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --add-module=/application/tools/nginx_upstream_check_module-master
make
rm -rf /usr/local/bin/nginx 
mv /application/nginx/sbin/nginx /application/nginx/sbin/nginx.bak
cp ./objs/nginx /application/nginx/sbin/
ln -s /application/nginx/sbin/nginx /usr/local/bin/nginx


4.修改配置文件 动静分离 静态内容由nginx服务器处理(web-nginx01 web-nginx02) 动态内容由tomcat处理(web-tomcat01 web-tomcat02) 

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream tomcat {  
         server 172.16.1.31:8080 weight=1;
         server 172.16.1.32:8080 weight=1;
              check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    }  
    
    upstream nginx {  
         server 172.16.1.33 weight=1;
         server 172.16.1.34 weight=1;
         check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    }
    server {
        listen       80;
        server_name  www.e3mall.com;
        location / {
            proxy_pass http://tomcat;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
            proxy_pass http://nginx;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
        location /status {
                check_status;
            access_log   off;
            allow 172.16.1.0/24;
            deny all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


nginx服务器安装(web-nginx01 web-nginx02)

1.部署nginx

yum install  -y pcre-devel openssl-devel pcre openssl
rpm -qa  pcre-devel openssl-devel


2.下载nginx-1.12.2

cd /application/tools/
wget  http://nginx.org/download/nginx-1.12.2.tar.gz
useradd www -M -s /sbin/nologin -u 504


3.编译安装

tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
ln -s /application/nginx-1.12.2/ /application/nginx
chown -R www:www /application/nginx-1.12.2
echo "#nginx" >>/etc/rc.local
echo "/application/nginx/sbin/nginx" >>/etc/rc.local
cd /application/nginx/conf
egrep -v "#|^$" nginx.conf.default > nginx.conf



tomcat服务器安装(web-tomcat01 web-tomcat02)

1.安装jdk1.7

http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-i586.tar.gz
mkdir /java
cd /java
wget http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-i586.tar.gz
tar xf jdk-7u80-linux-x64.tar.gz
chown -R root:root /java/jdk1.7.0_80
cat << EOF >>/etc/profile
export PATH=/java/jdk1.7.0_80/bin:$PATH
export CLASSPATH=.:/java/jdk1.7.0_80/lib/dt.jar:$JAVA_HOME/lib/tools.jar
EOF
source /etc/profile


2.安装tomcat

wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.88/bin/apache-tomcat-7.0.88.tar.gz
tar xf apache-tomcat-7.0.88.tar.gz
ln -s /application/tools/apache-tomcat-7.0.88 /application/apache-tomcat
cd /application/apache-tomcat/bin/
tar xf commons-daemon-native.tar.gz 
cd commons-daemon-1.1.0-native-src/unix/
./configure
make
cp jsvc /application/apache-tomcat/bin/
建立一个用来运行Tomcat的用户
useradd tomcat -M  -s /usr/sbin/nologin -u 504
vim /application/apache-tomcat/bin/daemon.sh
靠前的地方加入注释
#!/bin/sh
# chkconfig: - 80 20
若创建用户不是tomcat 这里修改为创建用户
test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat
cd /application/apache-tomcat/bin/
chown -R tomcat:tomcat /application/tools/apache-tomcat-7.0.88
chmod a+x /application/apache-tomcat/bin/daemon.sh 
ln -s /application/apache-tomcat/bin/daemon.sh /etc/init.d/tomcat7
chkconfig tomcat7 on
/etc/init.d/tomcat7 start



部署动静分离测试网站

1.tomcat1

cd /application/apache-tomcat/webapps/
mkdir nginx/image -p
chown -R tomcat.tomcat /application/apache-tomcat/webapps/nginx
cd nginx
vim index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"  
    pageEncoding="utf-8"%>  
<!DOCTYPE html>  
<body>
<h2>Nginx动静分离</h2>  
<%    
        out.println("["+request.getLocalAddr()+":" +request.getLocalPort()+"]" + "<br/>session id:" + session.getId());     
%>   
%<br/>  
%<img alt="图片" src="/nginx/img/1.jpg" style="width:300px;height:300px">
%</body>
%</html>

vim /application/apache-tomcat/conf/server.xml
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context path="" docBase="/application/apache-tomcat/webapps/nginx" reloadable="true" />
tomcat2上同上修改网站路径

  

2.复制测试网站到其他三台服务器

scp -r nginx/ [email protected]:/application/apache-tomcat/webapps/
scp -r nginx/ [email protected]:/application/nginx/html
scp -r nginx/ [email protected]:/application/nginx/html



3.nginx01 nginx02 配置

chown -R www.www /application/nginx/html/nginx
vim /application/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.e3mall.com;
        location / {
            root   html/nginx;
            index  index.jsp;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


动静分离测试

1.删除tomcat服务器下的image目录下的图片

[root@web-tomcat01 image]# rm -rf 1.png
[root@web-tomcat02 image]# rm -rf 1.png


2.网页访问测试

QQ截图20180524164158.png


猜你喜欢

转载自blog.51cto.com/13712476/2130519