1、keepalived的说明
Keepalived是Linux下一个轻量级别的高可用解决方案。高可用(High Avalilability,HA),其实两种不同的含义:广义来讲,是指整个系统的高可用行,狭义的来讲就是主机的冗余和接管。
Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考模型的第三、第四层、第五层交换机制检测每个服务节点的状态,如果某个服务器节点出现异常,或者工作出现故障,Keepalived将检测到,并将出现的故障的服务器节点从集群系统中剔除,这些工作全部是自动完成的,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。
后来Keepalived又加入了VRRP的功能,VRRP(Vritrual Router Redundancy Protocol,虚拟路由冗余协议)出现的目的是解决静态路由出现的单点故障问题,通过VRRP可以实现网络不间断稳定运行,因此Keepalvied 一方面具有服务器状态检测和故障隔离功能,另外一方面也有HA cluster功能。
1.1集群中的常见概念 ---- 心跳和脑裂
脑裂:如果节点之间的心跳通信中断或不正常,会导致彼此之间抢夺资源,这种现象称为脑裂
心跳:在高可用集群中,主机之间会相互监控彼此对方的状态,以决定哪台主机提供服务,这一机制称为心跳
1.2 Keepalived的两大功能:
对LVS进行健康检查
通过VRRPV2来处理负载均衡集群的故障切换
VRRP选举机制
2、Keepalived虚拟路由管理:
2.1 示例:实现一个主备切换的实验
Ka1:
[root@ka1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
}
Ka2:
[root@ka2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 100
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
}
测试:
[root@ka2 ~]# tcpdump -i ens33 -nn host 224.0.0.18
2.2 启用keepalived日志功能:
[root@ka1 ~]# vim /etc/sysconfig/keepalived
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp -P Only run with VRRP subsystem.
# --check -C Only run with Health-checker subsystem.
# --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs -I Dont remove IPVS topology on daemon stop.
# --dump-conf -d Dump the configuration data.
# --log-detail -D Detailed log messages.
# --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS="-D -S 6"
[root@ka1 ~]# vim /etc/rsyslog.conf
添加以下内容
[root@ka1 ~]# systemctl restart keepalived.service
[root@ka1 ~]# systemctl restart rsyslog.service
[root@ka1 ~]# ll /var/log/keepalived.log
-rw------- 1 root root 3051 Aug 12 01:11 /var/log/keepalived.log
2.3 实现独立子配置文件:
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
注释下面几行并添加最下面一行
#vrrp_instance VI_1 {
# state MASTER
# interface ens33
# virtual_router_id 100
# priority 100
# advert_int 1
# authentication {
# auth_type PASS
# auth_pass 1111
# }
# virtual_ipaddress {
# 192.168.182.100/24 dev ens33 label ens33:1
# }
#}
include "/etc/keepalived/conf.d/*.conf"
[root@ka1 ~]# vim /etc/keepalived/conf.d/virtul.conf
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
}
[root@ka1 ~]# systemctl restart keepalived.service
2.4 抢占模式和非抢占模式:
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 100
priority 100
nopreempt 非抢占模式 注意:两台主机必须都是备机或主机
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
}
[root@ka1 ~]# systemctl restart keepalived.service
配置了非抢占模式后vip不会飘逸,只有当本机挂掉后,vip会托管到另一台主机
2.5 延迟抢占模式:
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 100
priority 100
advert_int 1
nopreempt
preempt_delay 5s 指定延时时间为5s 注意:两台主机必须都是备机或主机
authentication {
auth_type PASS
auth_pass 1111
}
}
[root@ka1 ~]# systemctl restart keepalived.service
2.6 VIP单播配置:
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 100
priority 100
advert_int 1
nopreempt
preempt_delay 5s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
unicast_src_ip 192.168.182.145
unicast_peer {
192.168.182.146
}
}
测试:
[root@ka1 ~]# tcpdump -i ens33 -nn src host 192.168.182.145 and dst 192.168.182.146
2.7 邮件配置:
[root@ka1 ~]# vim /etc/mail.rc
set from=17791528889@163.com
set smtp=smtp.163.com
set smtp-auth-user=17791528889@163.com
set smtp-auth-password=JSLWHXNJMROXZKIG
set smtp-auth=login
set ssl-verify=ignore
末尾添加这几行
写一个shell脚本:
[root@ka1 ~]# vim /etc/keepalived/mail.sh
#!/bin/bash
mail_dst="17791528889@163.com"
send_message()
{
mail_sub="$HOSTNAME to be $1 vip move"
mail_msg="`date +%F\ %T`:vrrp move $HOSTNAME change $1"
echo $mail_msg | mail -s "$mail_sub" $mail_dst
}
case $1 in
master)
send_message master
;;
backup)
send_message backup
;;
fault)
send_message fault
;;
*)
;;
esac
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 100
priority 100
advert_int 1
#nopreempt
#preempt_delay 5s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
notify_master "/etc/keepalived/mail.sh master" 触发主备,发给邮件,当vip飘逸就会发给邮件
notify_backup "/etc/keepalived/mail.sh backup"
notify_fault "/etc/keepalived/mail.sh fault"
}
[root@ka1 ~]# systemctl restart keepalived.service
Ka2主机做同样操作
测试:
3、 实现master/master的keepalived 双主架构
作用 :将两个或以上VIP分别运行在不同的keepalived服务器,以实现服务器并行提供web访问的目的,提高
服务器资源利用率
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 200
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.200/24 dev ens33 label ens33:2
}
}
[root@ka1 ~]# systemctl restart keepalived.service
[root@ka2 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 100
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.100/24 dev ens33 label ens33:1
}
track_script {
check_haproxy
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 200
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.200/24 dev ens33 label ens33:2
}
}
[root@ka2 ~]# systemctl restart keepalived.service