负载均衡集群介绍、LVS介绍、LVS的调度算法、LVS NAT模式搭建

负载均衡集群介绍

  • 主流开源软件LVS、keepalived、haproxy、nginx等;
  • 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既能够认为是4层,也能够当作7层使用;
  • keepalived的负载均衡功能其实就是lvs;
  • lvs这种4层的负载均衡是能够分发除80外的其余端口通讯的,好比MySQL的,而nginx仅仅支持http,https,mail,haproxy也支持MySQL这种;
  • 相比较来讲,LVS这种4层的更稳定,能承受更多的请求,而nginx这种7层的更加灵活,能实现更多的个性化需求;

LVS介绍

  • LVS是由国人章文嵩开发;
  • 流行度不亚于apache的httpd,基于TCP/IP作的路由和转发,稳定性和效率很高;
  • LVS最新版本基于Linux内核2.6,有好多年不更新了;
  • LVS有三种常见的模式:NAT、DR、IP Tunnel;
  • LVS架构中有一个核心角色叫作分发器(Load balance),它用来分发用户的请求,还有诸多处理用户请求的服务器(Real Server,简称rs);

LVS NAT模式

负载均衡集群介绍、LVS介绍、LVS的调度算法、LVS NAT模式搭建

  • 这种模式借助iptables的nat表来实现;
  • 用户的请求到分发器后,经过预设的iptables规则,把请求的数据包转发到后端的rs上去;
  • rs须要设定网关为分发器的内网ip;
  • 用户请求的数据包和返回给用户的数据包所有通过分发器,因此分发器成为瓶颈;
  • 在nat模式中,只须要分发器有公网ip便可,因此比较节省公网ip资源;

LVS IP Tunnel模式

负载均衡集群介绍、LVS介绍、LVS的调度算法、LVS NAT模式搭建

  • 这种模式,须要有一个公共的IP配置在分发器和全部rs上,咱们把它叫作vip;
  • 客户端请求的目标IP为vip,分发器接收到请求数据包后,会对数据包作一个加工,会把目标IP改成rs的IP,这样数据包就到了rs上;
  • rs接收数据包后,会还原原始数据包,这样目标IP为vip,由于全部rs上配置了这个vip,因此它会认为是它本身;

LVS DR模式

负载均衡集群介绍、LVS介绍、LVS的调度算法、LVS NAT模式搭建

  • 这种模式,也须要有一个公共的IP配置在分发器和全部rs上,也就是vip
  • 和IP Tunnel不一样的是,它会把数据包的MAC地址修改成rs的MAC地址
  • rs接收数据包后,会还原原始数据包,这样目标IP为vip,由于全部rs上配置了这个vip,因此它会认为是它本身

LVS的调度算法

1. 轮询 Round-Robin rr (重点)

很是简单的一种高度算法,就是按顺序把请求依次发送给后端的服务器,它无论后端服务器的处理速度和响应时间怎样。当后端服务器性能不一致时,用这种调度算法就不合适过了。html

2. 加权轮询 Weight Round-Robin wrr (重点)

比第一种算法多了一个权重的设置,权重越高的服务器被分配到的请求就越多,这样后端服务器被分配到的请求就越多,这样后端服务器性能不一致时,就能够给配置低的服务器较小的权重。linux

3. 最小链接 Least-Connection lc (重点)

这种算法会根据各真实服务器上的链接数来决定把新的请求分配给谁,链接数少说明服务器是空闲的,这样把新的请求分配到空闲服务器上才更加合理。nginx

4. 加权最小链接 Weight Least-Connection wlc (重点)

在最小链接高度的基础上再增长一个权重设置,这样就能够人为地去控制哪些服务上多分配请求,哪些少分配请求。web

5. 基于局部性的最小链接 Locality-Based Least Connections lblc

这种算法简称LBLC,是针对请求报文的目标IP地址的负载均衡调度,目前主要用于Cache集群系统,由于在Cache集群客户请求报文的目标IP地址是变化的。算法的设计目标是在服务器的负载基本平衡的状况下,将相同目标IP地址的请求调度到同一台服务器,来提升各台服务器的访问局部性和主存Cache命中率。 算法

6. 带复制的基于局部性最小链接 Locality-Based Least Connections with Replication lblcr

该算法简称LBLCR,也是针对目标IP地址的负载均衡,它与LBLC算法的不一样之处是:它要维护从一个目标IP地址到一组服务器的映射,而LBLC算法是维护从一个目标IP地址到一台服务器的映射。LBLCR算法先根据请求的目标IP地址找出该目标IP地址对应的服务器组,按“最小链接”原则从该服务器组中选出一台服务器,若服务器没有超载,则将请求发送到该服务器;若服务器超载,则按“最小链接”原则从整个集群中选出一台服务器,将该服务器加入到服务器组中,将请求发送到该服务器。同时,当该服务器组有一段时间没有被修改,将最忙的服务器从服务器组中删除,以下降复制的程度。 shell

7. 目标地址散列调度 Destination Hashing dh

该算法也是针对目标IP地址的负载均衡的,但它是一种静态映射算法,经过一个散列(hash)函数将一个目标IP地址映射到一台服务器。目标地址散列调度算法先根据请求的目标IP地址,做为散列键(hash key)从静态分配的散列表找出对应的服务器,若该服务器是可用的且未超载,将请求发送到该服务器,不然返回空。apache

8. 源地址散列调度 Source Hashing sh

该算法正好与目标地址散列调度算法相反,它根据请求的源IP地址,做为散列键从静态分配的散列表找出对应的服务器,若该服务器是可用的且未超载,将请求发送到该服务器,不然返回空。它的算法流程与目标地址散列调度算法的基本类似,只不过将请求的目标IP地址换成请求的源IP地址。 vim

LVS NAT模式搭建

1.准备工做

1. 准备三台机器,按需求配置好IP。
  • 分发器,也叫调度器(简写为dir)两张网卡,一个内网IP:172.16.111.100/24,一个外网IP:172.16.13.129/24(vmware仅主机模式)
  • 真实服务器rs1,内网IP:172.16.111.110/24,设置网关为172.16.111.100(网关设置成分发器的内网ip,作最后一步准备工做在改)
  • 真实服务器rs2,内网IP:172.16.111.120/24,设置网关为172.16.111.100(网关设置成分发器的内网ip,作最后一步准备工做在改)
  • 为了好区分实验效果更改计算机名
[root@localhost ~]# hostnamectl set-hostname gary-tao-03
[root@localhost ~]# bash
##进入子shell,更改为功
[root@gary-tao-03 ~]#
2. 三台机器上都要关闭防火墙
[root@localhost ~]# iptables -nvL   //查看
[root@localhost ~]# systemctl stop firewalld  //关闭
[root@gary-tao ~]# systemctl disable firewalld  //关闭开机启动
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
3. 操做在真实服务器rs1与rs2上,调用一个空的iptables规则,在配置一个新的iptables规则开启,以避免旧的规则影响到实验效果(使用centos6里面的iptables形式)
[root@gary ~]# yum install -y iptables-services
#因为要调用epel国外资源安装会很慢,能够临时取消epel源的调用方式安装,操做以下
#把/etc/yum.repos.d/目录下epel.repo 改一下名字
[root@gary ~]# cd /etc/yum.repos.d/
[root@gary yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  nginx.repo
[root@gary yum.repos.d]# mv epel.repo epel.repo.1

#查看安装包
[root@gary yum.repos.d]# yum list |grep iptables-service
iptables-services.x86_64                   1.4.21-18.2.el7_4           @updates 
[root@gary ~]# yum install -y iptables-services

#查看一个包都安装了哪些文件
[root@gary ~]# rpm -ql iptables-services  
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service
/usr/libexec/initscripts/legacy-actions/ip6tables
/usr/libexec/initscripts/legacy-actions/ip6tables/panic
/usr/libexec/initscripts/legacy-actions/ip6tables/save
/usr/libexec/initscripts/legacy-actions/iptables
/usr/libexec/initscripts/legacy-actions/iptables/panic
/usr/libexec/initscripts/legacy-actions/iptables/save
/usr/libexec/iptables
/usr/libexec/iptables/ip6tables.init
/usr/libexec/iptables/iptables.init

#启动iptables.service
[root@gary ~]# systemctl start iptables
[root@gary ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

#开启的目的是为了调用一个空的规则
[root@localhost ~]# iptables -F
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  肯定  ]

#关闭SELinux
[root@localhost ~]# getenforce  #查看selinux
Enforcing
[root@localhost ~]# setenforce 0  #临时关闭
[root@gary-tao ~]# getenforce   #已关闭状态
Disabled
[root@gary ~]# getenforce   #临时关闭状态
Permissive
[root@localhost ~]# vi /etc/selinux/config 
#永久关闭,修改配置文件,把Enforcing改为Disabled,重启生效。

#rs1与rs2的网关都要是dir机器的内网ip
[root@garytao-03 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.111.100  0.0.0.0         UG    100    0        0 ens33
172.16.111.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33

2.配置分发器dir

安装LVS核心工具ipvsdam后端

[root@garytao-01 ~]# yum install -y ipvsadm
//安装LVS的核心工具,只须要在dir机器安装

#假设若是报错没有可用软件包 ipvsadm,按以下方式安装:

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz 
tar zxf ipvsadm-1.26.tar.gz 
cd ipvsadm-1.26 
rpm -qa | grep kernel-devel(默认已经安装) 
(安装所需依赖包 
yum install popt-static kernel-devel make gcc openssl-devel lftplibnl* popt* openssl-devel lftplibnl* popt* libnl* libpopt* gcc*)
make && make install 

检测是否安装成功:

[root@gary-tao ipvsadm-1.26]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@gary-tao ipvsadm-1.26]# lsmod |grep ip_vs
ip_vs                 141092  0 
nf_conntrack          111302  1 ip_vs
libcrc32c              12644  2 xfs,ip_vs

在dir上编写一个脚本:centos

[root@gary-tao ~]# vim /usr/local/sbin/lvs_nat.sh

增长以下配置内容:

#! /bin/bash
# director 服务器上开启路由转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 关闭icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 注意区分网卡名字,这里两个网卡分别为ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens38/send_redirects
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 172.16.111.0/24  -j MASQUERADE
# director设置ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 172.16.13.129:80 -s wlc -p 3
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.110:80 -m -w 1
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.120:80 -m -w 1

[root@gary-tao ~]# sh /usr/local/sbin/lvs_nat.sh  //执行脚本

3.NAT模式效果测试

  • 两台rs上都安装nginx;
  • 设置两台rs的主页,作一个区分,也就是说直接curl两台rs的ip时,获得不一样的结果

这里改了下第二台rs的nginx配置,第三台没有改。

[root@gary ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@gary ~]# vi /usr/share/nginx/html/index.html 
[root@gary ~]# curl localhost
garytao02.
  • 使用curl访问172.16.13.129,多访问几回看测试结果
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.

#链接访问屡次,一直请求到rs1上。这是由于在前面的脚本中有设置有-p参数,理论上有300秒内会一直请求到rs1上。

#更改脚本的时间值,把wlc -p 3参数删除了,改为rr
[root@garytao-01 ~]# cat /usr/local/sbin/lvs_nat.sh
#! /bin/bash
# director 服务器上开启路由转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 关闭icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 注意区分网卡名字,这里两个网卡分别为ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens38/send_redirects
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 172.16.111.0/24  -j MASQUERADE
# director设置ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 172.16.13.129:80 -s rr 
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.110:80 -m -w 1
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.120:80 -m -w 1

#再次测试,作到了均衡访问
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
相关文章
相关标签/搜索