标签:instance vrrp this ifconf Oz heartbeat abd failover web
Keepalived起初是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换(Failover)和健康检查(Health Checking)功能——判断LVS负载调度器、节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机恢复后将其重新加入群集。lvs是一个在四层上实现后端realserver的负载均衡的集群,lvs遗留下两个问题,一个是lvs的单点故障;第二个是lvs不能检测后端realserver的健康状态检查。
解决lvs的单点故障就用到了高可用集群:
解决lvs不能检测后端realserver的健康状态也后很多种方法:
名称 | 操作系统 | IP地址 |
---|---|---|
Master | CentOS7 | 192.168.100.201(VIP:192.168.100.10) |
Backup | CentOS7 | 192.168.100.202(VIP:192.168.100.10) |
Apache1 | CentOS7 | 192.168.100.221(VIP:192.168.100.10) |
Apache2 | CentOS7 | 192.168.100.222(VIP:192.168.100.10) |
Realserver | Win7 | 192.168.100.50(网关:192.168.100.10) |
#yum install keepalived ipvsadm –y //安装keepalived和ipvsadm服务
#vi /etc/sysctl.conf
net.ipv4.ip_forward=1 //开启路由转发功能
net.ipv4.conf.all.send_redirects = 0 //关闭重定向功能
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
#sysctl –p //使服务生效
#cd /etc/sysconfig/network-scripts/
#cp ifcfg-ens33 ifcfg-ens33:0 //创建虚拟网卡
#vim ifcfg-ens33:0
DEVICE=ens33:0 //网卡名称
ONBOOT=yes //网卡可用
IPADDR=192.168.100.10 //IP地址
NETMASK=255.255.255.0 //子网掩码
#service network restart
#ifup ens33:0 //开启网卡
#vim /etc/init.d/dr.sh //配置DR模式脚本文件
#!/bin/bash
GW=192.168.100.1 //网关地址
VIP=192.168.100.10 //虚拟网卡地址
RIP1=192.168.100.201 //节点服务器1IP地址
RIP2=192.168.100.202 //节点服务器2IP地址
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm //启用LVS工具
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up //开启虚拟网卡
/sbin/route add -host $VIP dev ens33:0 //配置路由段
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting --------------------[ok]" //启动完提示信息
;;
stop)
/sbin/ipvsadm –C //清空LVS
systemctl stop ipvsadm
ifconfig ens33:0 down //关闭端口
route del $VIP
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit 1
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
#chmod +x dr.sh //给予执行权限
#service dr.sh start //开启DR模式
#vim /etc/keepalived/keepalived.conf
global_defs {
...
smtp_server 127.0.0.1 //指向本地
router_id LVS_01 //指定名称,备份服务器不同名称
...
}
vrrp_instance VI_1 {
state MASTER //备份服务器是BACKUP
interface ens33 //对应端口号
virtual_router_id 10 //组号
...
auth_pass abc123 #验证密码
priority 100 #优先级backup小于master
...
virtual_ipaddress {
192.168.100.10
}
...
virtual_server 192.168.100.10 80 { //虚拟服务器地址(VIP)、端口
...
real_server 192.168.100.221 80 {
weight 1
TCP_CHECK { //健康检查方式
connect_port 80 //
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.222 80 {
weight 1
TCP_CHECK {
connect_port 80 #添加端口
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
Backup整体配置与Master相同,但在keepalived配置时需要注意:
router_id与Master的不同
state为BACKUP
priority优先级Backup小于Master
#systemctl start httpd.service //开启httpd服务
#systemctl stop firewalld.service //关闭防火墙
#setenforce 0
#echo “this is accp web” > /var/www/html/index.html //添加首页内容
#cd /etc/sysconfig/network-scripts
#cp ifcfg-lo ifcfg-lo:0
#vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes
#vim /etc/init.d/web.sh //编写网络配置脚本web.sh
#!/bin/bash
VIP=192.168.100.10
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $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)
ifconfig lo:0 down
route del $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 Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
#chmod +x web.sh
#service web.sh start
#ifup lo:0 //启动后Xshell远程连接会断开
#ifconfig //到虚拟机中检查虚拟网卡是否启动
#firefox http://127.0.0.1/ & //在虚拟机中自测
#service web.sh stop
#service web.sh start //重启网络配置脚本
整体配置过程同节点服务器1
标签:instance vrrp this ifconf Oz heartbeat abd failover web
原文地址:http://blog.51cto.com/13625810/2132290