Keepalive工具介绍

专为LVS和HA设计的一款健康检查工具

Keepalive是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障。
1、支持故障自动切换
2、支持节点健康状态检查

实现原理

在一个LVS服务集群中通常有主服务器(master)和备份服务器(backup)两种角色的服务器,但是对外表现为一个虚拟IP,主服务器会发送VRRP通告信息给备份服务器,当备份服务器接收不到VRRP消息的时候,即主服务器异常的时候,备份服务器就会接管虚拟IP,继提供服务,从而保证了高可用性。

服务部署

主DR 服务器:192.168.226.40
备DR 服务器:192.168.226.50
Web 服务器1:192.168.226.60
Web 服务器2:192.168.226.70
vip:192.168.226.200
客户端:192.168.226.120

1.配置负载调度器(主、备相同)

systemctl stop firewalld.service
setenforce 0

yum -y install ipvsadm keepalived
modprobe ip_vs
cat /proc/net/ip_vs

vim /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

vim /etc/sysconfig/network-scripts/ifcfg-ens33:0

DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.226.200
NETMASK=255.255.255.255

systemctl restart network
ifup ens33:0
ifconfig ens33:0

route add -host 192.168.226.200 dev ens33:0

ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm

ipvsadm -C
ipvsadm -A -t 192.168.226.200:80 -s rr
ipvsadm -a -t 192.168.226.200:80 -r 192.168.226.60:80 -g
ipvsadm -a -t 192.168.226.200:80 -r 192.168.226.70:80 -g
ipvsadm
ipvsadm -ln

在两个负载调度器都要配置

2.配置节点服务器

systemctl stop firewalld
setenforce 0

yum -y install httpd
systemctl start httpd

–192.168.226.60—

echo 'this is kgc web!' > /var/www/html/index.html

–192.168.226.70—

echo 'this is accp web!' > /var/www/html/index.html

vim /etc/sysconfig/network-scripts/ifcfg-lo:0

DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.226.200
NETMASK=255.255.255.255

service network restart 或 systemctl restart network
ifup lo:0
ifconfig lo:0

route add -host 192.168.226.200 dev lo:0

vim /etc/sysctl.conf

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

sysctl -p
两个节点服务器做相同的配置

3.配置keeplived(主、备DR 服务器上都要设置)

cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf

global_defs {					
	smtp_server 127.0.0.1
	router_id LVS_01  #主为01,备为02
}


vrrp_instance VI_1 {
    state MASTER  #主为MASTER,备为BACKUP
    interface ens33
    virtual_router_id 10
    priority 100 优先级,备为90
    advert_int 1					
    authentication {			
		auth_type PASS			
        auth_pass abc123
    }
    virtual_ipaddress {	
        192.168.226.200
    }
}
virtual_server 192.168.226.200 80 {
    delay_loop 6					
    lb_algo rr						
    lb_kind DR
    persistence_timeout 50			
    protocol TCP					
    real_server 192.168.226.60 80 {
        weight 1					
        TCP_CHECK {
			connect_port 80			
			connect_timeout 3		
			nb_get_retry 3		
			delay_before_retry 4	
		}
	}
	real_server 192.168.226.70 80 { 第二个节点
        weight 1
        TCP_CHECK {
			connect_port 80
			connect_timeout 3
			nb_get_retry 3
			delay_before_retry 4
		}
	}
##删除后面多余的配置##
}

systemctl start keepalived
ip addr show dev ens33

5.测试验证
在客户端访问 http://192.168.226.200/ ,默认网关指向 192.168.226.200
再在主服务器禁用网卡后再测试, ifdown ens33:0
Keepalive工具介绍_第1张图片
Keepalive工具介绍_第2张图片
断掉主服务器ens33前
Keepalive工具介绍_第3张图片
Keepalive工具介绍_第4张图片
断掉后
Keepalive工具介绍_第5张图片
Keepalive工具介绍_第6张图片

你可能感兴趣的:(Keepalived)