实验环境及需要的软件:
两台 Redhat7 系统 (一台做负载均衡Load Balancer,一台做服务器Server)
ipvsadm-1.24-8.1.i386.rpm
实验过程:
Load Balancer服务器
配置ip地址
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno33554960
IPADDR=172.25.0.10
NETMASK=255.255.255.0
GATEWAY=0.0.0.0
安装 ipvsadm 管理程序
[root@localhost ~]# rpm -ivh ipvsadm-1.27-4.el7.x86_64.rpm
配置 VIP 设定脚本
[root@localhost ~]# vi /opt/vip.sh
#!/bin/bash
VIP=172.25.0.100
/sbin/ifconfig eno33554960:0 VIPbroadcast VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eno33554960:0
sysctl -w net.ipv4.conf.all.send_redirects=0
sysctl -w net.ipv4.conf.default.send_redirects=0
sysctl -w net.ipv4.conf.eno33554960.send_redirects=0
设置脚本可执行权限
[root@localhost ~]# chmod u+x /opt/vip.sh
开机自启动
[root@localhost ~]# echo “/opt/vip.sh” >> /etc/rc.local
运行脚本
[root@localhost ~]# /opt/vip.sh
查看是否成功
[root@localhost ~]# ifconfig eno33554960:0
增加 virtual service
[root@localhost ~]# ipvsadm -A -t 172.25.0.100:80 -s rr
增加virtual service并指定 lvs 使用 DR 直接路由模式
[root@localhost ~]# ipvsadm -a -t 172.25.0.100:80 -r 172.25.0.12:80 -g
保存设置
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
查看 ipvsadm 列表
[root@localhost ~]# ipvsadm -l
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
Server服务器
配置 VIP 设定脚本
[root@localhost ~]# vi /opt/lvs-dr
#!/bin/sh
VIP=172.25.0.100
/sbin/ifconfig eno33554992 172.25.0.12/24
/sbin/ifconfig lo:0 VIPbroadcast VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev 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
设置脚本可执行权限
[root@classroom ~]# chmod +x /opt/lvs-dr
开机自启动
[root@classroom ~]# echo “/opt/lvs-dr” >>/etc/rc.local
运行脚本
[root@classroom ~]# /opt/lvs-dr
安转Apache
[root@classroom ~]# yum -y install httpd
编辑Apache网站主页面
[root@classroom ~]# echo “this is a server” > /var/www/html/index.html
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
测试
在Load Balancer服务器的浏览器输入Server的ip地址,看是否会跳转到Server的网站主页面