LVS && HA
1.DR模式实现lvs
Server11端作为调度器
Sever11操作
首先配置yum源如图
Yum repolist##查看已拥有的yum仓库
安装ipvsadm
Yum install -y ipvsadm
ip addr add 172.25.15.100/32 dev eth0 ##添加一个虚拟网络172.25.15.100
ipvsadm -C #清空转发表
ipvsadm -A -t 172.25.15.100:80 -s rr##-A添加一个虚拟服务-ttcp服务-s使用的调度算法
ipvsadm -a -t 172.25.15.100:80 -r 172.25.15.13:80 -g##-a在内核虚拟服务器表的一条记录里添加一条新的服务器-r真实服务器-g指定工作模式为直接路由模式
ipvsadm -a -t 172.25.15.100:80 -r 172.25.15.14:80 -g
/etc/init.d/ipvsadm save##保存ipvsadm表
/etc/init.d/iptables stop##关闭防火墙
Server13 && server14
添加虚拟网络ip
yum install arptables_jf.x86_64 -y
arptables -A IN -d 172.25.15.100 -j DROP##-A追加规则-d目的地址-j跳到目标-s源地址
arptables -A OUT -s 172.25.15.100 -j mangle --mangle-ip-s 172.25.15.13
##这两条指令意思是将目的地址为172.25.15.100的包丢掉将源地址为172.25.15.100的请求包发给172.25.15.13
##server14同样操作
/etc/init.d/arptables_jf save##保存
安装httpd并开启
Yum install -y httpd
/etc/init.d/httpd start
并在默认发布目录上编辑index.html作为轮询时区别不同的虚拟机
Cd /var/www/html
Server13:
Echo <h1>server13.example.com</h1> >index.html
Server14:
Echo <h1>server14.example.com</h1> >index.html
测试1
真机curl虚拟ipcurl 172.25.15.100会发现server13 && server14轮询
测试2
当关闭serever13端的lvs时轮询访问的server13会中断
但下图会发现server11 ipvsadm -l 查询时并不会发现错误所以说明lvs本身没有健康检查功能
2.配置ldirectord 服务
想要有监测功能需要加入ldirectord服务
先配置ldirectord 服务:
Server11:
yum install ldirectord-3.9.5-3.1.x86_64.rpm
cd /etc/ha.d/
cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf .
vim ldirectord.cf
测试
/etc/init.d/ldirectord start
轮询正常且虚拟服务存在
当server13关闭httpd服务时server11端再ipvsadm测试会发现server13虚拟服务已经关闭且curl 172.25.15.100时只会访问server14
所以安装ldirectord 服务会具有健康检查功能
3.将ldirectord服务加入heartbeat 服务中
先将server11 && server12中的httpldirectord服务关掉并删掉虚拟网络
/etc/init.d/ldirectord stop
/etc/init.d/httpd stop
ip addr del 172.25.15.100/24 dev eth0
两边虚拟机都修改配置文件加入ldirectord服务
vim haresources
两边虚拟机都启动heartbeat服务
/etc/init.d/heartbeat start
测试1
都启动服务后客户端正常访问且服务正常轮询虚拟网络服务正常运行
测试2
当集群中server11关掉hearbeat服务时server12会自动接管服务而且客户端会正常工作
4.keepalived + lvs
1用httpd服务测试
Server11端
下载keepalived-1.2.24.tar.gz压缩包并解压
tar zxf keepalived-1.2.24.tar.gz
安装软件包解决依赖性
yum install -y ipvsadm kernel-devel openssl-devel popt-devel libnl-devel gcc make -y
yum install libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm
./configure --prefix=/usr/local/keepalived
make && make install
做四个软连接
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到server12上来
scp -r keepalived/ root@172.25.15.12:/usr/local/
Server12同样做软连接“
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/
Server11端
cd /etc/keepalived/
vim keepalived.conf
scp keepalived.conf root@172.25.15.12:/etc/keepalived/##发送给server12
Server 11 && server12 都安装mailx用来接受邮件
yum install -y mailx
Server12端
修改server11端传过来的keepalived的配置文件
Cd /etc/keepalived
Vim keepalived.conf
##修改备份机状态为BACKUP以及备份机优先级为50
主机与备份机两边都给执行权限并开启
chmod +x /etc/init.d/keepalived
/etc/init.d/keepalived start
测试
正常启动后用 ip addr show 查看会发现真实运行在server11上但是ipvsadm会发现在备份机上也存在说明数据同时备份。当server11挂掉后数据就可继续运行不用从0开始
而且对客户端的访问并没有影响
2用ftp服务测试
Server13 && server14端都安装vsftp服务
分别创建server13以及server14文件用来区分连接的虚拟机
Server11&& server12端
Vim keepalived.conf
Server13 && server14端
添加172.25.15.200虚拟ip:
Server11 &&server12端启动服务
/etc/init.d/keepalived start
测试
当server13断开服务时会自动切到server13上来