负载均衡介绍
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
原文地址:http://blog.51cto.com/13517254/2096727
时间: 2024-10-29 22:21:03