负载均衡集群介绍
主流开源软件 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 模式
这种模式借助 iptables 的 nat 表来实现 用户的请求到分发器后, 通过预设的 iptables 规则, 把请求的数据包转发到后端的 rs 上去 rs 需要设定网关为分发器的内网 ip 用户请求的数据包和返回给用户的数据包全部经过分发器, 所以分发器成为瓶颈 在 nat 模式中, 只需要分发器有公网 ip 即可, 所以比较节省公网 ip 资源.
LVS IP Tunnel 模式
这种模式, 需要有一个公共的 IP 配置在分发器和所有 rs 上, 我们把它叫做 vip 客户端请求的目标 IP 为 vip, 分发器接收到请求数据包后, 会对数据包做一个加工, 会把目标 IP 改为 rs 的 IP, 这样数据包就到了 rs 上 rs 接收数据包后, 会还原原始数据包, 这样目标 IP 为 vip, 因为所有 rs 上配置了这个 vip, 所以它会认为是它自己
LVS DR 模式
这种模式, 也需要有一个公共的 IP 配置在分发器和所有 rs 上, 也就是 vip 和 IP Tunnel 不同的是, 它会把数据包的 MAC 地址修改为 rs 的 MAC 地址 rs 接收数据包后, 会还原原始数据包, 这样目标 IP 为 vip, 因为所有 rs 上配置了这个 vip, 所以它会认为是它自己
LVS 的调度算法
轮询 Round-Robin rr
加权轮询 Weight Round-Robin wrr
最小连接 Least-Connection lc
加权最小连接 Weight Least-Connection wlc
基于局部性的最小连接 Locality-Based Least Connections lblc
带复制的基于局部性最小连接 Locality-Based Least Connections with Replication lblcr
目标地址散列调度 Destination Hashing dh
源地址散列调度 Source Hashing sh
LVS NAT 模式搭建
NAT 模式搭建 - 准备工作
三台机器 分发器, 也叫调度器(简写为 dir)
内网: 218.130, 外网: 194.128(vmware 仅主机模式) rs1
内网: 218.129, 设置网关为 192.168.218.2 rs2
内网: 218.131, 设置网关为 192.168.218.2
三台机器上都执行执行
systemctl stop firewalld;
systemctl start iptables-services; iptables -F; service iptables save
在 dir 上安装 ipvsadm
yum install -y ipvsdam
在 dir(分发器)上编写脚本
- [root@localhost ~]# 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/ens37/send_redirects
- // director 设置 nat 防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.64.0/24 -j MASQUERADE
- // director 设置 ipvsadm 规则
- IPVSADM='/usr/sbin/ipvsadm'
- $IPVSADM -C
- // -C=clear, 清除规则
- $IPVSADM -A -t 192.168.64.151:80 -s rr
- // -A:=add, 添加规则, 定义算法的规则;
- // -t: 分发器 ip(外网 ip);
- // -s 指定算法;
- // -p: 指定超时时间 (解决 session 问题: 保证同一请求被分发到同一 rs 上) 单位为秒.
- // 因为添加 - p 选项后会影响测试效果, 所以在此不加该参数(注: 时间不能设置为 0)
- $IPVSADM -a -t 192.168.64.151:80 -r 192.168.159.131:80 -m -w 1
- $IPVSADM -a -t 192.168.64.151:80 -r 192.168.159.132:80 -m -w 1
- // -a:=add, 增加 nat 架构中的 rs;
- // -r: 指定 rs 的 IP;
- // -m: 指定 LVS 模式为 NAT(masquerade)
- // -w:=weight, 指定权重
执行脚本:
- [root@localhost ~]# sh /usr/local/sbin/lvs_nat.sh
- // 执行该脚本时无错误输出说明没问题.
查看 ipvsadm 规则:
- [root@localhost ~]# 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.64.151:80 rr
- -> 192.168.159.131:80 Masq 1 0 0
- -> 192.168.159.132:80 Masq 1 0 0
配置 rs
在两台 rs 上安装 Nginx, 并分别设置主页来区分两台机器.
- [root@localhost ~]# yum install -y nginx
- [root@localhost ~]# systemctl start nginx
- // 如果 nginx 是 yum 安装, 其主页目录为 /usr/share/nginx/html/index.html
测试
访问外网 ip:192.168.194.128
- [root@localhost ~]# curl 192.168.194.128
- This is real server 2
- [root@localhost ~]# curl 192.168.194.128
- this is real server 1
- [root@localhost ~]# curl 192.168.194.128
- This is real server 2
- [root@localhost ~]# curl 192.168.194.128
- this is real server 1
来源: http://www.bubuko.com/infodetail-2613420.html