LB集群即load balance负载均衡集群,常用的负载均衡软件有nginx、LVS、keepalived,商业硬件负载设备F5、Netsale
LVS的三种工作模式:1.VS/NAT模式(Network address translation)2.VS/TUN模式(tunneling) 3.DR模式(Direct routing)
命令提要:
ipvsadm -ln 在DR1上运行,用于查看当前集群规则
测试环境:
Hostname |
Eth0 (内网IP) |
Eth1 (公网IP) |
DR1 |
192.168.137.100 |
192.168.137.33 |
RS1 |
192.168.137.111 |
|
RS2 |
192.168.137.122 |
一、 NAT模式设置:
在DR1上配置:
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
echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects
# director 设置nat防火墙
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.137.0/24 -j MASQUERADE
# director设置ipvsadm
IPVSADM=‘/sbin/ipvsadm‘
$IPVSADM -C
$IPVSADM -A -t 192.168.137.33:80 -s lc -p 300
##lc表示最小连接数
$IPVSADM -a -t 192.168.137.33:80 -r 192.168.137.111:80 -m -w 1
$IPVSADM -a -t 192.168.137.33:80 -r 192.168.137.122:80 -m -w 1
sh !$
RS1以及RS2上运行nginx服务
二、DR模式
vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.137.88
RS1=192.168.137.111
RS2=192.168.137.122
ifconfig eth1:0 down
ifconfig eth1:0 $vip broadcast $vip netmask 255.255.255.255 up
##需要绑定公网网卡
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s wrr
##w参数用于加权
$ipv -a -t $vip:80 -r $RS1:80 -g -w 2
$ipv -a -t $vip:80 -r $RS2:80 -g -w 1
在RS1以及RS2上做同样的配置:
vi /usr/local/sbin/lvs_rs.sh
#! /bin/bash
vip=192.168.137.88
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
先执行DR1上的脚本,后执行RS上的脚本即可