18.6 负载均衡集群介绍
主流开源软件 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 层的更加灵活, 能实现更多的个性化需求
18.7 LVS 介绍
LVS 是由国人章文嵩开发
流行度不亚于 Apache 的 httpd, 基于 TCP/IP 做的路由和转发, 稳定性和效率很高
LVS 最新版本基于 Linux 内核 2.6, 有好多年不更新了
LVS 有三种常见的模式: NAT,DR,IP,Tunnel
LVS 架构中有一个核心角色叫做分发器 (Load Runner), 它用来分发用户的请求, 还有诸多处理用户请求的服务器 (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, 所以它会认为是它自己
18.8 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
18.9-18.10 LVS NAT 模式搭建
NAT 模式搭建 - 准备工作
三台机器
分发器, 也叫调度器 (简写为 dir)
内网: 192.168.0.130/24 , 外网: 192.168.147.144 (VMware 仅主机模式)
[root@taoyuan ~]# hostnamectl set-hostname qingyun-01
# 进入个子 shell
[root@taoyuan ~]# bash
[root@qingyun-01 ~]#
rs1
内网: 192.168.0.132/24, 设置网关为 192.168.0.130
[root@qingyun-02 html]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.130 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
rs2
内网: 192.168.0.133/24, 设置网关为 192.168.0.130
[root@qingyun-03 html]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.130 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
三台机器上都执行, 关闭防火墙
systemctl stop firewalld;systemctl disable firewalld
# 由于 3 台是重新, 都关闭防火墙
[root@qingyun-01 ~]# systemctl stop firewalld
[root@qingyun-01 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@qingyun-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@qingyun-03 ~]# yum install -y iptables-services
# 如果安装很慢, 可以取消 epel
# 把 / etc/yum.repos.d / 目录下 epel.repo 改一下名字
# 查看包安装的文件
[root@qingyun-01 yum.repos.d]# rpm -ql iptables-services
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service
# 启动 iptables.service
[root@qingyun-01 yum.repos.d]# systemctl start iptables
[root@qingyun-01 yum.repos.d]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
systemctl start iptables-services;iptables -F; service iptables save
# 开启的目的是为了调用一个空的规则
[root@qingyun-03 ~]# iptables -F
[root@qingyun-03 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
# 关闭 selinux
[root@qingyun-01 yum.repos.d]# setenforce 0
[root@qingyun-01 yum.repos.d]# vi /etc/selinux/config
#SELINUX=disabled
# 查看网关
[root@qingyun-03 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
NAT 模式搭建
在 dir 上安装 ipvsadm
[root@qingyun-01 yum.repos.d]# yum install -y ipvsadm
在 dir 上编写脚本, vim /usr/local/sbin/lvs_nat.sh // 内容如下
[root@qingyun-01 ~]# 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
# dirrector 设置 nat 防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
# director 设置 ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 192.168.147.144:80 -s wlc -p 3
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.132:80 -m -w 1
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.133:80 -m -w 1
执行脚本
[root@qingyun - 01~]#sh / usr / local / sbin / lvs_nat.sh#没有输出,
说明没有错误
NAT 模式效果测试
两台 rs 上都安装 nginx
设置两台 rs 的主页, 做一个区分, 也就是说直接 curl 两台 rs 的 ip 时, 得到不同的结果
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-132
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-133
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-132
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-133
[root@qingyun-01 ~]# cat /usr/local/sbin/lvs_nat.sh
...........
$IPVSADM -C
$IPVSADM -A -t 192.168.147.144:80 -s wlc
.............
浏览器里访问 192.168.147.144, 多访问几次查看结果差异
[root@qingyun-01 ~]# 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.147.144:80 wlc
-> 192.168.0.132:80 Masq 1 0 0
-> 192.168.0.133:80 Masq 1 0 4
负载均衡集群介绍, LVS 介绍, LVS 调度算法, LVS NAT 模式搭建
来源: http://www.bubuko.com/infodetail-2480137.html