注意:lvs+keepalived 高可用方案中主备可以不需要绑定脚本,只需要keepalived的单个文件就可以整个集群.
本实验为了方便理解采用了rpm 包安装的方式,对tar包情有独钟的可以绕过.本次讨论的重点是lvs+keepalived
1.1 IP地址的配置:
lvs1master 192.168.80.145 主keepalived
lvs2 slave 192.168.80.144 备keepalived
realserver 192.168.80.146 web1
realserver 192.168.80.147 web2
realserver 192.168.80.149 web3 实验里面未添加(机器太烂)
GW 192.168.80.254 网关
1.2 架构图:
安装支持包:
yum install openssl-devel kernel-devel
安装ipvsadm :
yum install ipvsadm
安装keepalived :
yum install keepalived
配置开机启动项:
chkconfig ipvsadm on chkconfig keepalived on
配置keepalived :
vim /etc/keepalived/keepalived.confg
内容如下:
! Configuration File for keepalived global_defs { notification_email { # [email protected] # [email protected] # [email protected] [email protected] # 联系人 } notification_email_from [email protected] smtp_server smtp.qq.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER #从上直接改为BACKUP 即可。 interface eth0 virtual_router_id 51 priority 100 # 主上是100 从一定要设置比主小,90 advert_int 1 authentication { auth_type PASS #密码验证类型 auth_pass 1111 #主从之间的验证密码,主从2端一定要一致 } virtual_ipaddress { # 192.168.200.16 # 192.168.200.17 # 192.168.200.18 #10.0.0.100 192.168.80.148 # 虚拟ip (vip)地址后面,下面可以继续叠加。 } } #-------------------------------------- virtual_server 192.168.80.148 80 { # 虚拟IP delay_loop 6 #(每隔10秒查询realserver状态) lb_algo rr #(lvs 算法) 更改算法在此处更改 lb_kind DR #(Direct Route) persistence_timeout 5 #(同一IP的连接60秒内被分配到同一台realserver) protocol TCP #(用TCP协议检查realserver状态) real_server 192.168.80.146 80 { # web端地址 weight 1 #(权重) TCP_CHECK { connect_timeout 10 #(10秒无响应超时) nb_get_retry 3 delay_before_retry 3 connect_port 80 #端口 } } real_server 192.168.80.147 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
#后面还有很多示例可以负载其他服务,再此不做演示
------------------------主ha配置完成-------------------------------------
backup 配置:
安装ipvsadm 和keepalived 是一样的
scp [email protected]:/etc/keepalived/keepalived.conf
修改keepalived 配置文件使之成为备ha,(仅需要修改2个地方)
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 { state BACKUP #直接改为BACKUP 即可。 interface eth0 virtual_router_id 51 priority 90 #主上是100 从一定要设置比主小,90 advert_int 1
设置完成重启keepalived 即可,启动ipvsadm start
service keepalived start
------------------------备ha配置完成-------------------------------------
web 端:
vim /root/realserver.sh
#!/bin/bash # description: Config realserver #Written by : http://kerry.blog.51cto.com #此脚本转自以上作者,尊重他人劳动成果。特此保留版权信息 SNS_VIP=192.168.80.148 # VIP 地址 /etc/rc.d/init.d/functions #如果提示权限不够 执行:chmod 777 /etc/rc.d/init.d/functions case "$1" in start) /sbin/ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_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 sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) /sbin/ifconfig lo:0 down /sbin/route del $SNS_VIP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
赋予可执行权限:
chmod a+x realserver.sh
加入开启自启动:
echo /root/realserver.sh &>>/etc/rc.local
启动脚本:
/root/realserver.sh &
查看本机的ip信息:
#
ifconfig
把realserver.sh 复制到其他的web端上同样操作,验证即可。
------------------------至此realserver配置完成-----------------------
测试部分:
任意客户端访问http://192.168.80.148应该都在146,147,149 三台主机之间切换
主lvs + keepalived 访问效果图:
ipvsadm �Cln
ipvsadm �Cln�C-stats
backup 上的内容
ipvsadm �Cln
ipvsadm �Cln �Cstats:
主从切换日志内容:
停掉主上的keepalived
tail �CF/var/log/message
backup日志
从接替主之后访问VIP 产生的效果:
主从切换正常,试验完成。