一、简介
vrrp协议的软件实现,原生设计目的为了高可用ipvs服务
功能
- 基于vrrp协议完成地址流动
- 为vip地址所在的节点生成ipvs规则(在配置文件中预先定义)
- 为ipvs集群的各RS做健康状态检测
- 基于脚本调用接口通过执行脚本完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务
组件
- 用户空间核心组件
- vrrp stack-VIP消息通告
- checkers-监测real server
- system call-标记real server权重
- SMTP-邮件组件
- ipvs wrapper-生成IPVS规则
- Netlink Reflector-网络接口
- WatchDog-监控进程
- 控制组件:配置文件解析器
- IO复用器
- 内存管理组件
术语
- 虚拟路由器:Virtual Router
- 虚拟路由器标识:VRID(0-255),唯一标识虚拟路由器
- 物理路由器:
- master:主设备
- backup:备用设备
- priority:优先级
- VIP:Virtual IP
- VMAC:Virutal MAC (00-00-5e-00-01-VRID)
工作
- 通告:心跳,优先级等;周期性
- 工作方式:抢占式,非抢占式
- 安全工作:
-
认证:
无认证
简单字符认证:预共享密钥
-
- 工作模式:
- 主/备:单虚拟路由器
- 主/主:主/备(虚拟路由器1),备/主(虚拟路由器2)
二、安装使用
1、yum及apt安装
# yum install keepalived (CentOS)
# apt-get install keepalived (Ubuntu)
2、编译安装
2.1、下载并安装
[[email protected] ~]# cd /usr/local/src/
#下载源码包
[[email protected] src]# wget https://keepalived.org/software/keepalived-2.0.20.tar.gz
[[email protected] src]# tar -xf keepalived-2.0.20.tar.gz
#安装依赖包
[[email protected] keepalived-2.0.20]# yum install libnfnetlink-devel libnfnetlink ipvsadm libnl libnl-devel \
libnl3 libnl3-devel lm_sensors-libs net-snmp-agent-libs net-snmp-libs openssh-server openssh-clients openssl \
openssl-devel automake iproute
#安装
[[email protected] keepalived-2.0.20]# ./configure --prefix=/usr/local/keepalived --disable-fwmark
[[email protected] keepalived-2.0.20]# make && make install
2.2、复制配置文件
[[email protected] keepalived-2.0.20]# cp /usr/local/src/keepalived-2.0.20/bin/keepalived /usr/sbin/
[[email protected] keepalived-2.0.20]# mkdir /etc/keepalived
[[email protected] keepalived]# cp /usr/local/src/keepalived-2.0.20/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#启动
[[email protected] keepalived]# systemctl start keepalived
3、程序环境
- 主配置文件:/etc/keepalived/keepalived.conf
- 主程序文件:/usr/sbin/keepalived
- Unit File:
- /usr/lib/systemd/system/keepalived.service (CentOS)
- /lib/systemd/system/keepalived.service (Ubuntu)
- Unit File的环境配置文件:
- /etc/sysconfig/keepalived
4、配置语法
配置虚拟路由器
vrrp_instance <STRING> {
....
}
配置参数
- state MASTER|BACKUP:当前节点在此虚拟路由器上的初始状态,状态为MASTER或者BACKUP
- interface IFACE_NAME:绑定为当前虚拟路由器使用的物理接口ens32,eth0,bond0,br0
- virtual_router_id VRID:当前虚拟路由器惟一标识,范围是0-255
- priority 100:当前物理节点在此虚拟路由器中的优先级;范围1-254
- advert_int 1:vrrp通告的时间间隔,默认1s
#认证机制
authentication {
auth_type AH|PASS
auth_pass <PASSWORD> 仅前8位有效
}
#虚拟IP
virtual_ipaddress {
<IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL>
192.168.200.17/24 dev eth1
192.168.200.18/24 dev eth2 label eth2:1
}
#配置监控网络接口,一旦出现故障,则转为FAULT状态实现地址转移
track_interface {
eth0
eth1
…
}
4、示例
4.1 组播配置
MASTER配置
! Configuration File for keepalived
global_defs {
notification_email {
#keepalived 发生故障切换时邮件发送的对象,可以按行区分写多个
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ha1.example.com
#所有报文都检查比较消耗性能,此配置为如果收到的报文和上一个报文是同一个路由器则跳过检查报文中的源地址
vrrp_skip_check_adv_addr
#严格遵守VRRP协议,不允许状况:1,没有VIP地址,2.配置了单播邻居,3.在VRRP版本2中有IPv6地址.
vrrp_strict
#ARP报文发送延迟
vrrp_garp_interval 0
#消息发送延迟
vrrp_gna_interval 0
#默认组播IP地址,224.0.0.0到239.255.255.255
vrrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.100.110 dev eth0 label eth0:0
}
}
BACKUP配置
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ha1.example.com
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
vrrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.100.110 dev eth0 label eth0:0
}
}
启动查看
#启动
[[email protected] ~]# systemctl start keepalived
#查看vip
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:0a:19:ae brd ff:ff:ff:ff:ff:ff
inet 10.10.100.106/24 brd 10.10.100.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.10.100.110/32 scope global eth0:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe0a:19ae/64 scope link
valid_lft forever preferred_lft forever
[[email protected] ~]#
VIP测试
4.2 单播配置
#指定单播的源IP
unicast_src_ip
#指定单播的对方IP
unicast_peer {
目标主机IP
}
MASTER配置
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ha1.example.com
vrrp_skip_check_adv_addr
#单播需要关闭改选项
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
vrrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 80
priority 100
advert_int 1
#指定单播的源IP
unicast_src_ip 10.10.100.106
#指定单播的对方IP
unicast_peer {
10.10.100.107
}
BACKUP配置
...
unicast_src_ip 10.10.100.107
unicast_peer {
10.10.100.106
}
...
重启查看
[[email protected] ~]# systemctl restart keepalived
[[email protected] ~]# tcpdump -i eth0 host -nn 10.10.100.106 and host 10.10.100.107
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
22:29:10.177008 IP 10.10.100.106 > 10.10.100.107: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20
22:29:11.178314 IP 10.10.100.106 > 10.10.100.107: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20
22:29:12.179164 IP 10.10.100.106 > 10.10.100.107: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20
22:29:13.180201 IP 10.10.100.106 > 10.10.100.107: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20
22:29:14.181897 IP 10.10.100.106 > 10.10.100.107: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20
22:29:15.182902 IP 10.10.100.106 > 10.10.100.107: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20
4.3 非抢占
keepliaved默认为抢占模式,可以通过nopreempt参数关闭VIP抢占
- nopreempt #关闭VIP抢占,需要各keepalived服务器state为BACKUP
#节点1
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 100
advert_int 1
nopreempt
...
#节点2
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 90
advert_int 1
nopreempt
...
#重启测试可以发现,主节点故障恢复后并不会抢占VIP
4.4 抢占延迟模式
- preempt_delay 60s #抢占延迟模式,默认延迟300s,需要各keepalived服务器state为BACKUP
#节点1
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 100
advert_int 1
preempt_delay 60s
#节点2
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 90
advert_int 1
preempt_delay 60s
4.5 双主配置
- 两个或以上VIP分别运行在不同的keepalived服务器,以实现服务器并行提供web访问的目的,提高服务器资源利用率。
#节点1
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 100
advert_int 1
preempt_delay 60s
unicast_src_ip 10.10.100.106
unicast_peer {
10.10.100.107
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.100.110 dev eth0 label eth0:0
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 81
priority 80
advert_int 1
unicast_src_ip 10.10.100.106
unicast_peer {
10.10.100.107
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.100.111 dev eth0 label eth0:0
}
#节点2
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 90
advert_int 1
preempt_delay 60s
unicast_src_ip 10.10.100.107
unicast_peer {
10.10.100.106
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.100.110 dev eth0 label eth0:0
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 81
priority 100
advert_int 1
unicast_src_ip 10.10.100.107
unicast_peer {
10.10.100.106
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.100.111 dev eth0 label eth0:0
}
}
重启查看
节点1 为10.10.100.110主节点,10.10.100.111备节点
节点2为10.10.100.111主节点,10.10.100.110备节点
4.6 通知配置
#邮箱配置
[[email protected] ~]# yum install mailx -y
[[email protected] ~]# vim /etc/mail.rc
set from=***@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=***@qq.com
set smtp-auth-password=exffioleeuzxbbhb
set smtp-auth=login
set ssl-verify=ignore
#通知脚本
[[email protected] ~]# cat /etc/keepalived/notify.sh
#!/bin/bash
contact='[email protected]'
notify() {
mailsubject="$(hostname) to be $1, vip 转移"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
#脚本调用配置
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 80
priority 100
advert_int 1
unicast_src_ip 10.10.100.106
unicast_peer {
10.10.100.107
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.100.110 dev eth0 label eth0:0
}
##调用通知脚本
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
验证查看