LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。
LVS-DR:director(分配器)分配请求到real server。real server 处理请求后直接回应给用户,director负载均衡器仅处理客户机与服务器一半连接。从而避免性能瓶颈,同样增加系统可伸缩性。Direct Routing由于采用物理层(修改MAC地址)技术,因此所有服务器都必须处理同一物理网段。
环境:
server1 LB(调度器) 172.25.152.1
server2 RS(真实后端服务器) 172.25.152.2
server3 RS(真实后端服务器) 172.25.152.3
server4 RS(???) 172.25.152.4
在所有虚拟机中:
1.配置与物理机处于同一vlan,防火墙关闭,selinux设置为disabled
2.配置yum源:vim /etc/yum.repos.d/rhel-source.repo
3.添加虚拟IP:ip addr add 172.25.152.100/24 dev eth0
检查IP添加情况:ip addr
其中Server2.3后端服务器中:
1.修改默认发布目录:vim /var/www/html/index.html
重启阿帕奇:/etc/init.d/httpd restart
Server1调度器:
1.安装ipvsadm:yum install -y ipvsadm
#安装前必须先将下载的压缩包解压,并扩展yum 源配置,增加LoadBalancer部分才能下载
2.开启服务:
ipvsadm -A -t 172.25.152.100:80 -s rr
#rr表示轮询算法
ipvsadm -a -t 172.25.152.100:80 -r 172.25.152.2:80 -g
ipvsadm -a -t 172.25.152.100:80 -r 172.25.152.3:80 -g
3.查看:ipvsadm -ln
测试:
物理机:curl 172.25.152.100
arp -an | grep 100
100的IP地址指向的并不是指向调度器(即server1),而是server3(后端真实服务器)
server1调度器:ipvsadm -ln
2.清除缓存:arp -d 172.25.152.100
3.查看:curl 172.25.152.100
此效果不是理想效果,结果具有随机性
在实验1的基础上:
Server2,server3调度器:
以server2为例:
1.安装服务:yum install -y arptables_jf
2.检查:arptables -L
3.抑制ARP:arptables -A IN -d 172.25.152.100 -j DROP
arptables -A OUT -S 172.25.0.100 -j mangle --mangle-ip-s 172.25.152.2
4.保存:/etc/init.d/arptables_jf save
5.检查:arptables -L
测试:
物理机:curl 172.25.152.100
1.安装ldirectord:yum install -y ldirectord-3.9.5-3.1.x86_64.rpm
#安装前必须先将下载的压缩包解压,并扩展yum 源配置,增加HighAvailability部分才能下载
2.根据提示进行初始化:cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf /etc/ha.d
vim /etc/ha.d/ldirectord.cf
4.清空策略:ipvsadm -C
查看当前策略:ipvsadm -l
5.修改默认发布目录:vim /var/www/html/index.html
重启阿帕奇:/etc/init.d/httpd restart
测试:
Server1调度器:curl localhost
物理机:curl 172.25.152.100
#server1 是主master,server4是备用master。当主master工作时,server4处于休眠状态,一旦主master挂掉,立马成为新的主master
Server1调度器:
1.关闭ldirectord服务:/etc/init.d/ldirectord stop
chkconfig ldirectord off
2.安装keepalived服务(源码编译):
#下载安装包:keepalived-2.0.6.tar.gz
tar zxf keepalived-2.0.6.tar.gz
./configure --with-init=SYSV --prefix=/usr/local/keepalived
#此命令无法输入时,需先下载openssl-devel服务
cd keepalived-2.0.6
make
#此命令必须进入解压后的文件夹使用
make install
cd /usr/local/keepalived/etc/rc.d/init.d
chmod +x keepalived
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived /etc/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
3.发送文件:scp -r /usr/local/keepalived/ server4:/usr/local
/etc/init.d/keepalived restart
Server4:执行相同链接
4.安装mail服务:yum install -y mailx
5.删除附加IP:ip addr del 172.25.152.100/32 dev eth0
6.编辑默认文件:cd /etc/keepalived
vim keepalived.conf
7.发送文件:scp keepalived.conf server4:/etc/keepalived/
Server4:vim /etc/keepalived/keepalived.conf
/etc/init.d/keepalived restart
测试:
物理机:curl 172.25.152.100
Sever4:cat /var/log/messages
关闭server1的keepalived再次实验相同步骤
#结果为server4会自动替补,通过查看邮件 cat /var/log/message ,可以看到server4的状态由BACKUP升级为master。