18.6 负载均衡集群介绍 18.7 LVS介绍18.8 LVS调度算法 18.9/18.10 LVS NAT模式搭建

18.6 负载均衡集群介绍

  1. 主流开源软件LVS、keepalived、haproxy、nginx等
  2. 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做7层使用
  3. keepalived的负载均衡功能其实就是lvs,lvs是keepalived内置的
  4. lvs这种4层的负载均衡是可以分发除80外的其他端口通信的,比如MySQL的,而nginx仅仅支持http,https,mail;haproxy也支持MySQL这种
    7层有限制,不过有些更高级的功能,nginx可以通过站点目录,去区分网站服务器之前,4层的就不支持
  5. 相比较来说,LVS这种4层的更稳定,能承受更多的请求,而nginx这种7层的更加灵活,能实现更多的个性化需求

18.7 LVS介绍

LVS介绍

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

LVS NAT模式

  1. 这种模式借助iptables的nat表来实现
  2. 用户的请求到分发器后,通过预设的iptables规则,把请求的数据包转发到后端的rs上去
  3. rs需要设定网关为分发器的内网ip
  4. 用户请求的数据包和返回给用户的数据包全部经过分发器,所以分发器成为瓶颈
  5. 在nat模式中,只需要分发器有公网ip即可,所以比较节省公网ip资源

image

原理图解释: Load Balancer,就是一个分发器;把用户的请求,分发给后端的Real Server ,Real Server这些服务器接收到请求以后,处理好请求以后,就重新丢回给Load Balancer;最后Load Balancer再返回给用户;这个模式的弊端,就是请求量、反馈量大的时候,Load Balancer的压力很大,一般最多支持10来台服务器,超过10台的话就会有力不从心;这个结构,只需要有一个公网IP,其他real server服务器全部在内网就可以实现。优点,节省很多的资源

LVS IP Tunnel模式

  1. 这种模式,需要有一个公共的IP配置在分发器和所有rs上,我们把它叫做vip
  2. 客户端请求的目标IP为vip,分发器接收到请求数据包后,会对数据包做一个加工,会把目标IP改为rs的IP,这样数据包就到了rs上
  3. rs接收数据包后,会还原原始数据包,这样目标IP为vip,因为所有rs上配置了这个vip,所以它会认为是它自己

image

原理图解释: 在load balancer与real server之间建立了虚拟通道 ip tunnel ;实际上是更改了数据包的IP;请求过来通过load balancer,通过在real server上配置的VIP;用户请求的时候,数据包里面包好的目的IP,当数据包到达load balancer的时候,load balancer会进行一个数据包目的IP的更改,然后发送到具体的real server上,通过lvs的自己的算法,进行实现到底传输到那个real server上;然后real server再解包处理,再通过一个VIP直接返回到用户,这就省略数据回到load balancer分发器的过程,这样就load balancer就没有瓶颈

LVS DR模式

  1. 这种模式,也需要有一个公共的IP配置在分发器和所有rs上,也就是vip
  2. 和IP Tunnel不同的是,它会把数据包的MAC地址修改为rs的MAC地址
  3. rs接收数据包后,会还原原始数据包,这样目标IP为vip,因为所有rs上配置了这个vip,所以它会认为是它自己

image

18.8 LVS调度算法

  1. 轮询 Round-Robin 简称:rr 最简单的也是最容易理解

用户请求过来,均衡的分发到rs上

  1. 加权轮询 Weight Round-Robin 简称:wrr

带权重的轮询,可以对机器单独设置权重,对高权重的机器发送的请求会多一些

  1. 最小连接 Least-Connection简称: lc

把请求发送到请求数量小的rs上

  1. 加权最小连接 Weight Least-Connection简称: wlc

对请求数量小的rs,加上一个权重,使他优先

  1. 基于局部性的最小连接 Locality-Based Least Connections简称: lblc
  2. 带复制的基于局部性最小连接 Locality-Based Least Connections with Replication 简称: lblcr
  3. 目标地址散列调度 Destination Hashing 简称:dh
  4. 源地址散列调度 Source Hashing 简称: sh

==5、6、7、8 几条算法不常用==

18.9/18.10 LVS NAT模式搭建

准备工作

三台机器

  1. 分发器,也叫调度器(简写为dir)

• 内网:133.131,外网:142.144(vmware仅主机模式);
dir机器上需要安装有两台网卡;一块为内网使用,一块为外网使用,如果没有,需要单独添加,ens配置文件可以直接拷贝原有的网卡配置,只设置IP即可

  1. rs1 • 内网:133.130 网关 133.131 //设置网卡的目的,就是要实现通过dir机器进行外网通信;

  2. rs2 • 内网:133.132 网关 133.131

机器准备好以后,还需要调节iptables工具使其拥有centos 6的netfilter服务,方便后续做实验

关闭firewalld服务

systemctl stop firewalld

使服务不再开机启动

systemctl disable firewalld

安装iptables-services 包

yum install -y iptables-services 

启动netfilter服务

systemctl start iptables

设置开机netfilter服务启动

systemctl enable iptables
 [root@aminglinux-01 ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

查看表,是否使用了netfilter服务了

iptables -nvL
[root@aminglinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
   66  4808 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 46 packets, 4336 bytes)
pkts bytes target     prot opt in     out     source               destination

清空表的规则,以便后续实验

[root@aminglinux-01 ~]# iptables -F
[root@aminglinux-01 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

以上步骤需要检查另外两台 rs 机器是否开启firewalld 服务,如果开启切换为netfilter服务

设置两台rs机器的网关

设置为分发器的网关,如果不做这个操作就没有办法实现通信

配置

在分发器dir上,安装ipvsadm ,这个是实现 lvs 的一个重要的工具,缺少这个工具,将没有办法实现 lvs 的功能

yum install -y ipvsadm

编写一个脚本,用脚本进行维护会比较方便,不用一条命令一条命令的进行操作
在dir机器上编写脚本

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  //伪装操作,不然不能转发rs的数据
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects //伪装操作,不然不能转发rs的数据
# 注意区分网卡名字,dir机器的两块网卡分别为ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens32/send_redirects  
echo 0 > /proc/sys/net/ipv4/conf/ens34/send_redirects
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.133.0/24  -j MASQUERADE  //MASQUERADE实现同网段的机器去上网,路由器使用的就是这个功能
# director设置ipvsadm
IPVSADM='/usr/sbin/ipvsadm' //设置一个变量,方便下面命令引用
$IPVSADM -C //清空规则
$IPVSADM -A -t 192.168.142.144:80 -s lc -p 3   //用来定义lvs 的模式;wlc,为算法,可以按需求选择 lvs 里面适合的算法
$IPVSADM -a -t 192.168.142.144:80 -r 192.168.133.132:80 -m -w 1 //小规则,-r 指定dir机器IP,-m 指定nat模式,-w指定rs权重 
$IPVSADM -a -t 192.168.142.144:80 -r 192.168.133.130:80 -m -w 1//小规则,-r 指定dir机器IP,-m 指定nat模式,-w指定rs权重 
  1. IPVSADM -A -t 192.168.142.144:80 -s lc -p 3 : -A增加一个规则,-t 制定lvs 模式,之后IP 就是dir的IP,-s 指定算法;-p 指定超时时间,(数据包转发超时时间)

超时时间解释:
用户1访问的是a机器,-p 的意思就是在同一个时间,一直在同一台机器上进行请求

查看ipvsadm 规则

[root@aminglinux-02 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.142.144:80 wlc
  -> 192.168.133.130:80           Masq    1      0          2
  -> 192.168.133.132:80           Masq    1      0          0

配置好脚本以后,执行以下脚本

sh /usr/local/sbin/lvs_nat.sh

没有输出,证明脚本没有错误

测试

  • 两台rs上都安装nginx

检查两台rs机器上nginx是否启动,因为配置规则的时候,指定的是80端口,所以80端口需要启动才能使用

aminglinux-01机器

[root@aminglinux-01 ~]# !ps
ps aux |grep nginx
root      2951  0.0  0.1 122280  2072 ?        Ss   23:58   0:00 nginx: master process /usr/sbin/nginx
nginx     2952  0.0  0.1 122712  3104 ?        S    23:58   0:00 nginx: worker process
root      2954  0.0  0.0 112664   976 pts/1    S+   23:58   0:00 grep --color=auto nginx
[root@aminglinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2951/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1091/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1958/master
tcp6       0      0 :::3306                 :::*                    LISTEN      1739/mysqld
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::80                   :::*                    LISTEN      2951/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      1091/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1958/master

aminglinux-03机器

[root@aminglinux-03 ~]# !ps
ps aux |grep nginx
root      2924  0.0  0.1 122280  2068 ?        Ss   23:58   0:00 nginx: master process /usr/sbin/nginx
nginx     2925  0.0  0.1 122712  3100 ?        S    23:58   0:00 nginx: worker process
root      2927  0.0  0.0 112664   976 pts/1    S+   23:58   0:00 grep --color=auto nginx
[root@aminglinux-03 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2924/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1311/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2044/master
tcp6       0      0 :::3306                 :::*                    LISTEN      1853/mysqld
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::80                   :::*                    LISTEN      2924/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      1311/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2044/master
  • 设置两台rs的主页,做一个区分,也就是说直接curl两台rs的ip时,得到不同的结果

编辑aminglinux机器的索引页

[root@aminglinux-01 ~]# vim /usr/share/nginx/html/index.html
[root@aminglinux-01 ~]# curl localhost
aminglinux-01

[root@aminglinux-03 ~]# vim /usr/share/nginx/html/index.html
[root@aminglinux-03 ~]# curl localhost
aminglinux-03
  • 浏览器里访问192.168.142.144,多访问几次看结果差异

经过测试发现,一直在重发出现,同一台主机上的索引页,可能是因为浏览器有缓存,所有没有办法详细的查看到区别

回到dir机器上,使用curl来进行测试,查看结果

[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-01
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-03
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-01
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-03
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-01
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-03
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-01
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-03
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-01
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-03
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-01
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-03
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-01
[root@aminglinux-02 ~]# curl 192.168.142.144
aminglinux-03

通过上面的测试,发现,通过配置iptables NAT表,实现了负载均衡

转载于:https://my.oschina.net/nova12315/blog/1785485

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值