负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群。
LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director Server上,然后它把用户的请求根据设置好的调度算法,智能均衡地分发到后端真正服务器(real server)上。为了避免不同机器上用户请求得到的数据不一样,需要用到了共享存储,这样保证所有用户请求的数据是一样的。
通过 LVS 达到的负载均衡技术和 Linux 操作系统实现一个高性能高可用的 Linux 服务器集群,它具有良好的可靠性、可扩展性和可操作性。从而以低廉的成本实现最优的性能。LVS 是一个实现负载均衡集群的开源软件项目,LVS架构从逻辑上可分为调度层、Server集群层和共享存储。
VS/DR 利用大多数 Internet 服务的非对称特点,负载调度器中只负责调度请求,而服务器直接将响应返回给客户,可以极大地提高整个集群系统的吞吐量调度器和服务器组都必须在物理上有一个网卡通过不分断的局域网相连,如通过交换机或者高速的HUB 相连。VIP 地址为调度器和服务器组共享,调度器配置的 VIP 地址是对外可见的,用于接收虚拟服务的请求报文;所有的服务器把 VIP 地址配置在各自的 NonARP 网络设备上,它对外面是不可见的,只是用于处 理目标地址为 VIP 的网络请求。
server1主机(VS:VirtualServer)
调度器配置
[root@server1 ~]# vim /etc/yum.repos.d/rhel-source.repo
[root@server1 ~]# yum repolist
[root@server1 ~]# yum install -y ipvsadm 类似于防火墙
[root@server1 ~]# ip addr add 172.25.68.100/24 dev eth0 #添加IP
[root@server1 ~]# ipvsadm -A -t 172.25.68.100:80 -s rr #rr调度算法
[root@server1 ~]# ipvsadm -a -t 172.25.68.100:80 -r 172.25.68.2:80 -g #添加在同一个局域网(g:DR工作模式)
[root@server1 ~]# ipvsadm -a -t 172.25.68.100:80 -r 172.25.68.3:80 -g
[root@server1 ~]# ipvsadm -l
server2主机(RS:RealServer)
[root@server2 ~]# yum install -y arptables_jf
[root@server2 ~]# ip addr add 172.25.68.100/24 dev eth0
[root@server2 ~]# arptables -A IN -d 172.25.68.100 -j DROP #添加策略
[root@server2 ~]# arptables -A OUT -s 172.25.68.100 -j mangle --mangle-ip-s 172.25.68.2 #转换
[root@server2 ~]# /etc/init.d/arptables_jf save 保存策略
Saving current rules to /etc/sysconfig/arptables: [ OK ]
server3主机(RS:RealServer)
配置虚拟IP(vip)
[root@server3 ~]# yum install -y arptables_jf
[root@server3 ~]# ip addr add 172.25.68.100/24 dev eth0
[root@server3 ~]# arptables -A IN -d 172.25.68.100 -j DROP
[root@server3 ~]# arptables -A OUT -s 172.25.68.100 -j mangle --mangle-ip-s 172.25.68.3
[root@server3 ~]# /etc/init.d/arptables_jf save
Saving current rules to /etc/sysconfig/arptables: [ OK ]
物理主机
1、访问 172.25.68.100 实现负载均衡
2、查看172.25.68.100 的mac地址 (与server1相同)
server1主机(VS:VirtualServer)
[root@server1 ~]# yum install -y ldirectord-3.9.5-3.1.x86_64.rpm
[root@server1 ~]# rpm -ql ldirectord #查找配置文件
[root@server1 ~]# cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf /etc/ha.d
[root@server1 ~]# cd /etc/ha.d
[root@server1 ha.d]# ls
ldirectord.cf resource.d shellfuncs
[root@server1 ha.d]# vim ldirectord.cf
[root@server1 ha.d]# ipvsadm -C 删除策略
[root@server1 ha.d]# ipvsadm -l 查看策略
[root@server1 ha.d]# cd /var/www/html
[root@server1 html]# ls
[root@server1 html]# rm -fr *
[root@server1 html]# ls
[root@server1 html]# vim index.html
Server1 - 此站点正在维修
[root@server1 html]# /etc/init.d/varnish stop #关闭varnish
[root@server1 html]# vim /etc/httpd/conf/httpd.conf
更改端口 Linsten:80
[root@server1 html]# /etc/init.d/httpd restart #重启httpd
此时将server2和server3的httpd服务关闭
物理主机访问 172.25.68.100
注意:只要有一个站点存活服务器就访问不到server1
keepalived同样可以对lvs后端主机实现健康检查,同样也可以实现调度器高可用,架设多个keepalived可以实现高可用。
应该注意的是ldirectord和keepalived只能开启一个,开启两个会冲突。
[root@server1 html]# /etc/init.d/ldirectord stop
Stopping ldirectord... success
[root@server1 html]# chkconfig ldirectord off
[root@server1 html]# cd
[root@server1 ~]# tar zxf keepalived-2.0.6.tar.gz
[root@server1 ~]# cd keepalived-2.0.6
[root@server1 keepalived-2.0.6]# yum install -y openssl-devel
[root@server1 keepalived-2.0.6]# ./configure --prefix=/usr/local/keepalived --with-init=SYSV
[root@server1 keepalived-2.0.6]# make #编译
[root@server1 keepalived-2.0.6]# make install
[root@server1 keepalived-2.0.6]# cd
[root@server1 ~]# ls
[root@server1 ~]# cd /usr/local
[root@server1 local]# ls
[root@server1 local]# cd keepalived/
[root@server1 keepalived]# ls
[root@server1 keepalived]# pwd
[root@server1 keepalived]# cd etc
[root@server1 etc]# ls
[root@server1 etc]# cd rc.d/
[root@server1 rc.d]# ls
[root@server1 rc.d]# cd init.d/
[root@server1 init.d]# ls
[root@server1 init.d]# chmod +x keepalived
[root@server1 init.d]# ls
[root@server1 init.d]# pwd
/usr/local/keepalived/etc/rc.d/init.d
[root@server1 init.d]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d
[root@server1 init.d]# cd ..
[root@server1 rc.d]# ls
[root@server1 rc.d]# cd ..
[root@server1 etc]# ls
[root@server1 etc]# cd keepalived/
[root@server1 keepalived]# ls
[root@server1 keepalived]# ln -s /usr/local/keepalived/etc/keepalived/ /etc
[root@server1 keepalived]# ls
[root@server1 keepalived]# cd ..
[root@server1 etc]# ls
keepalived rc.d sysconfig
[root@server1 etc]# cd sysconfig/
[root@server1 sysconfig]# ls
[root@server1 sysconfig]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@server1 sysconfig]# cd ..
[root@server1 etc]# cd ..
[root@server1 keepalived]# ls
[root@server1 keepalived]# cd sbin/
[root@server1 sbin]# ls
[root@server1 sbin]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@server1 sbin]# which keepalived
[root@server1 sbin]# /etc/init.d/keepalived start
#可以开启keepalived说明软链接没有错误
[root@server1 sbin]# /etc/init.d/keepalived stop
[root@server1 sbin]# cd
[root@server1 ~]# cd /usr/local
[root@server1 local]# scp -r keepalived/ [email protected]:/usr/local
建立一个server4主机:
[root@server4 ~]# cd /usr/local
[root@server4 local]# ls
[root@server4 local]# cd keepalived/
[root@server4 keepalived]# ls
[root@server4 keepalived]# cd etc/
[root@server4 etc]# ls
[root@server4 etc]# cd rc.d/
[root@server4 rc.d]# cd init.d/
[root@server4 init.d]# ls
[root@server4 init.d]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d
[root@server4 init.d]# ln -s /usr/local/keepalived/etc/keepalived/ /etc
[root@server4 init.d]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@server4 init.d]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@server4 init.d]# /etc/init.d/keepalived start
[root@server4 init.d]# /etc/init.d/keepalived stop
server1主机:
[root@server1 local]# cd keepalived/
[root@server1 keepalived]# ls
[root@server1 keepalived]# cd etc
[root@server1 etc]# ls
[root@server1 etc]# cd keepalived/
[root@server1 keepalived]# ls
[root@server1 keepalived]# vim keepalived.conf
[root@server1 keepalived]# /etc/init.d/keepalived start
[root@server1 keepalived]# scp keepalived.conf [email protected]:/etc/keepalived/
在server4主机:
[root@server4 keepalived]# vim keepalived.conf
[root@server4 keepalived]# /etc/init.d/keepalived start
在server1和server4主机上下载mailx方便查看实验
在server1关闭服务破坏内核使其不能工作:
[root@server1 keepalived]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
[root@server1 keepalived]# echo c >/proc/sysrq-trigger
开启server2和server3的httpd服务,物理机上测试:
查看server4的IP:
**总结:**server1主机上的keepalived坏掉了,可以由server4的备份keepalived接管负载均衡调度。