部署基于TUN的LVS负载均衡
一、系统资源规划
节点名称 | 系统名称 | CPU/内存 | 网卡 | 磁盘 | IP地址 | OS |
---|---|---|---|---|---|---|
LVS | lvs | 2C/4G | ens33 | 64G | 192.168.0.10 | CentOS7 |
tunl0 | 192.168.0.100 | |||||
Server1 | server1 | 2C/4G | ens33 | 64G | 192.168.0.11 | CentOS7 |
tunl0 | 192.168.0.100 | |||||
Server2 | server2 | 2C/4G | ens33 | 64G | 192.168.0.12 | CentOS7 |
tunl0 | 192.168.0.100 | |||||
Client | client | 2C/4G | ens33 | 64G | 192.168.0.20 | CentOS7 |
二、系统软件安装与设置
如未指定,下述命令在所有节点执行!
1、安装基本软件
yum -y install vim net-tools tcpdump bash-completion
2、设置名称解析
echo 192.168.0.10 lvs >> /etc/hosts
echo 192.168.0.11 server1 >> /etc/hosts
echo 192.168.0.12 server2 >> /etc/hosts
echo 192.168.0.20 client >> /etc/hosts
echo 192.168.0.100 vip >> /etc/hosts
3、设置NTP
yum -y install chrony
systemctl start chronyd
systemctl enable chronyd
systemctl status chronyd
chronyc sources
4、设置防火墙、SELinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
三、部署基于TUN的LVS负载均衡
1、设置LVS后端服务
在Server节点上加载ipip模块,并设置开机自动加载:
modprobe ipip
echo /usr/sbin/modprobe ipip >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
在Server节点上设置内核参数:
cat > /etc/sysctl.conf << EOF
net.ipv4.conf.tunl0.arp_ignore = 1
net.ipv4.conf.tunl0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.tunl0.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0
EOF
sysctl -p
在Server节点上添加tunl0接口地址,并设置开机自动添加:
ip address add 192.168.0.100 dev tunl0
ip link set up tunl0
echo /usr/sbin/ip address add 192.168.0.100 dev tunl0 >> /etc/rc.local
echo /usr/sbin/ip link set up tunl0 >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
在Server节点上安装httpd:
yum -y install httpd
在Server1节点上配置index文件:
echo Server1 > /var/www/html/index.html
在Server2节点上配置index文件:
echo Server2 > /var/www/html/index.html
在Server节点上启动Apache,并设置自启动:
systemctl start httpd
systemctl enable httpd
systemctl status httpd
2、设置LVS负载均衡
在LVS节点上安装ipvsadm:
yum -y install ipvsadm
在LVS节点上添加tunl0接口地址,并设置开机自动添加:
ip address add 192.168.0.100 dev tunl0
ip link set up tunl0
echo /usr/sbin/ip address add 192.168.0.100 dev tunl0 >> /etc/rc.local
echo /usr/sbin/ip link set up tunl0 >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
3、设置负载均衡策略
在LVS节点上设置调度策略:
ipvsadm -C
ipvsadm -A -t 192.168.0.100:80 -s rr
ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.11:80 -i
ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.12:80 -i
ipvsadm -ln
ipvsadm-save > /etc/sysconfig/ipvsadm
cat /etc/sysconfig/ipvsadm
四、验证基于TUN的LVS负载均衡
在Client节点上验证调度策略:
while true; do curl 192.168.0.100; sleep 2; done
在Server1节点上抓包分析:
tcpdump -i ens33 tcp port 80
客户端访问VIP地址,LVS仅处理数据请求,而让真实服务器响应数据包直接返回给客户端