Keepalived配置文件
全局配置
#以下是官方给的默认配置文件(示例配置)
#以#或!为注释符
global_defs {
notification_email { # 用于填写邮件报警地址
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
#notification_email_from 用于填写邮件的发送地址
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1 #设置邮件的SMTP服务器地址
smtp_connect_timeout 30 #设置超时时间
router_id LVS_DEVEL #LVS负载均衡器标识(router_id)在一个局域网内,应该是唯一的。
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
VRRP实例
vrrp_instance VI_1 {
state MASTER #状态标识,只有MASTER/BACKUP两种,且在配置时需大写
interface eth0 #用于指定HA监测网络的接口
virtual_router_id 51 #虚拟路由标识,若使用了同一个标识则表示在同一个VRRP实例下
priority 100 #用于定义节点优先级 数字越大越优
advert_int 1 #用于设置主从间同步检查的时间间隔,单位为秒
authentication { #以下花括号内的内容为设置主机间通信的验证方式(PASS/HA)及密码等
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #用于设置virtual IP 即漂移IP,可以设置多个。
192.168.200.16
192.168.200.17
192.168.200.18
}
}
#nopreempt 非抢占模式,防止网络抖动。
主备配置对比
#MASTER
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_MASTER #1
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER #2
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
#BACKUP
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_BACKUP #1'
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP #2'
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
以上配置中主要区别在于 router_id 、state 两者。
LVS配置
virtual_server 192.168.200.100 443 { #标志虚拟服务器配置文件开始
delay_loop 6 #设置健康检测时间间隔 单位为秒
lb_algo rr #LVS负载均衡调度算法
lb_kind NAT #LVS负载均衡模式
persistence_timeout 50 #会话保持时间,单位是秒
protocol TCP #设置转发协议,TCP/UDP
ha_suspend #节点间状态切换时,暂时不启用对RS的健康检查
virtualhost #在通过HTTP_GET/SSL_GET做健康检查时,指定web服务器的虚拟IP
soory_server # 备用Real Server 在所有RS失效后启用该服务器
real_server 192.168.201.100 443 { # 标志着RS的配置开始
weight 1 #权值,越大被分配的负载越多
inhibit_on_failure #在监测到RS失效后,将其权值改为0,而不是从IPVS中删除
notify_up #在监测到RS生效后执行后面的脚本
notify_up #与notify_up相反
TCP_CHECK{ #标志着配置检查方式
connect_port 80 #指定健康检测端口,不设置则为RS指定的端口
connect_timeout 3 #指定连接超时时间
nb_get_retry 3 #指定重新尝试连接数
delay_before_retry 3 #重新连接时间间隔
}
SSL_GET { #标志着检测实例配置
url { #指定HTTP/SSL检查的url信息,可以指定多个
path / #后跟详细的url路径
digest ff20ad2481f97b1754ef3e12ecd3a9cc #SSL检查后的信息摘要,通过工具genhash获取。如:genhash -s 192.168.1.12 -p 80 -u /index.html
status_code #http健康检测返回码 一般为200
bindto #表示用此地址来发送请求对服务器进行检查
}
}
}
}
Keepalived一主一从IP接管实战
IP | 角色 | OS |
---|---|---|
192.168.110.134 | BACKUP | Rocky Linux 8 |
192.168.110.137 | MASTER | CentOS 7 |
注:在开始前请准备两台虚拟机,并确保已安装Keepalived。
MASTER配置文件
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_MASTER
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 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.125
}
}
BACKUP配置文件
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_BACKUP
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.125
}
}
配置中主要区别为:
- router_id LVS_MASTER
- state MASTER
- interface ens33 :根据自己的网卡而定
- priority 100 :从的优先级应低于主
- 172.16.1.125 :Virtual IP
检查效果
#MASTER上的虚拟IP
[root@keepalived ~]# ip addr | grep "^.*inet 172"
inet 172.16.1.125/32 scope global ens33
#BACKUP上无
[root@localhost ~]# ip addr | grep "^.*inet 172"
[root@localhost ~]#
测试
#停止MASTER上的keepalived服务
[root@keepalived ~]# systemctl stop keepalived.service
[root@keepalived ~]#
#在BACKUP上查看IP是否飘逸
[root@localhost ~]# ip addr | grep "^.*inet 172"
inet 172.16.1.125/32 scope global ens160