实现Keepalived+Lvs , 主要做 RealServer 健康检查及LoadBalance 主机与Master,Backup 主机正常运行,其中一部分主机做高可用,另一部分做负载均衡。通过在HA 主机上安装 keepalived 服务形成主备形式并进行 rr 轮询调度,使用VIP 来 Director向客户端提供服务 IP,Keepalived 能在服务器出现故障时将其从系统中剔除并将服务改换至其他正常节点,当坏掉的节点恢复正常时能够将其加入到服务器集群中,并在实时发现故障时能够发送警告到指定邮箱。
主机环境:RHEL6 X86 (selinux&iptables disabled)
实验主机:
LVS ‐ ACTIVE:
192.168.122.103(server3)
LVS ‐ BACKUP:
192.168.122.104(server4)
LVS ‐ VIP:
192.168.122.111(vip)
Realsever:
192.168.122.101(server1)
192.168.122.102(server2)
SOFT :keepalived-1.2.20
LVS(Server3,server4)配置:
[[email protected] ~]#cd /mnt/
[[email protected]]# ls
keepalived-1.2.20.tar.gz
[[email protected]]# tar zxf keepalived-1.2.20.tar.gz
[[email protected]]# ls
keepalived-1.2.20 keepalived-1.2.20.tar.gz
[[email protected]]# cd keepalived-1.2.20
[[email protected]]# ./configure --prefix=/usr/local/keepalived
(看来是缺少C编译器了,我们安装上GCC)
[[email protected]]# yum install gcc
[[email protected]]#./configure --prefix=/usr/local/keepalived
(还缺少 openssl)
太麻烦了查了查执行 yum install ipvsadmkernel-devel openssl-devel popt-devel libnl-devel gcc make -y 一次安装完
居然还少东西,libnfnetlink-devel这个东西没有
我在我的rhel6.5镜像里面居然没有搜索到这个东西,在网上搜索下载后安装即可重新编译。
成功无报错.
[[email protected]]# cd /usr/local/keepalived/
执行4个软链接
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 /usr/sbin
(环境变量添加)
scp -r/usr/local/keepalived/ [email protected]:/usr/local/(发到SERVER4后也需要四个链接)
vim /etc/keepalived/keepalived.conf
Scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
将设置文件拷入BACKUP机中,然后作以下修改
启动server3&server4上的keepalived服务
/etc/init.d/keepalived start
在两个real_server上添加
ip addr add192.168.122.111/32 dev eth0
浏览器 192.168.122.101上显示
浏览器 192.168.122.102上显示
在浏览器输入 http://192.168.122.111,会出现
刷新一下
会发现在权重都为1时,虚拟IP所显示的为192.168.122.101和
192.168.122.102的轮流替换,实现了负载均衡的功能。
高可用测试:停止master 上的 keepalived 服务,看backup 是否接管。
如上图,在BACKUP机测试,关掉MASTER后BACKUP进入了MASTER状态。当恢复MASTER机的服务后,如下
可以看到备机重新恢复了BACKUP标签状态。
这样,简单的keepalived和lvs的功能就得以实现了。