负载均衡,LVS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了负载均衡,LVS相关的知识,希望对你有一定的参考价值。

负载均衡介绍

技术分享图片

LVS介绍

技术分享图片

有三种模式
1.NAT模式,访问量很大的时候分发器就成为瓶颈
技术分享图片
2.IP Tunnel模式:

技术分享图片

3.DR模式

技术分享图片

LVS的调度算法

技术分享图片

一共八种,前面四种比较常用,后面用的比较少

LVS NAT模式搭建

技术分享图片

1.设置IP地址
分发器:
内网:188.130 外网:252.147(仅主机模式)

rs1

内网:188.129 设置网关 188.130

rs2

内网:188.133 设置网关 188.130

2.关闭防火墙:三台都完成

[[email protected] ~]# 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      

3.然后三台机器开启iptables规则,并且调用空规则:

[[email protected] ~]# systemctl start iptables.service 
[[email protected] ~]# iptables -F
[[email protected] ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

技术分享图片

4.安装工具:只在dir上

[[email protected] ~]# yum install -y ipvsadm
已加载插件:fastestmirror
base                                                                          | 3.6 kB  00:00:00   

5.编辑脚本:在dir上

[[email protected] ~]# 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.188.0/24  -j MASQUERADE
# director设置ipvsadm
IPVSADM=‘/usr/sbin/ipvsadm‘
$IPVSADM -C
$IPVSADM -A -t 192.168.252.147:80 -s lc -p 3
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.129:80 -m -w 1
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.133:80 -m -w 1

6.编辑两个rs上的默认页,做区分:

[[email protected] ~]# vi /usr/share/nginx/html/index.html 
[[email protected] ~]# curl localhost
weixing02
[[email protected] ~]# curl localhost
weixing03

7.编辑配置文件:

$IPVSADM -A -t 192.168.252.147:80 -s rr              更改规则为rr
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.129:80 -m -w 1
$IPVSADM -a -t 192.168.252.147:80 -r 192.168.188.133:80 -m -w 1

8.进行测试:

[[email protected] ~]# curl 192.168.252.147
weixing03
[[email protected] ~]# curl 192.168.252.147
weixing02
[[email protected] ~]# curl 192.168.252.147
weixing03
[[email protected] ~]# curl 192.168.252.147
weixing02
[[email protected] ~]# curl 192.168.252.147
weixing03
[[email protected] ~]# curl 192.168.252.147
weixing02

以上是关于负载均衡,LVS的主要内容,如果未能解决你的问题,请参考以下文章

企业运维之 Lvs 负载均衡和高可用

超详细!一文带你了解 LVS 负载均衡集群!

基于lvs实现4层负载均衡

IPVS(LVS)负载均衡简介及实验测试

负载均衡介绍LVS介绍算法介绍LVS_NAT负载均衡模式配置

LVS负载均衡策略的部署与应用