云原生(Keepalived 核心功能配置与实验)

实验简介

1. Keepalived 抢占模式配置实验

该实验分为普通抢占、非抢占、延迟抢占三种场景:

  • 默认抢占模式:优先级高的主机自动抢占 VIP;
  • 非抢占模式 :将两台主机的 VRRP 实例状态均设为 BACKUP,添加nopreempt参数,即使高优先级主机恢复,也不会抢占已在低优先级主机上的 VIP;
  • 延迟抢占模式 :通过preempt_delay参数设置抢占延迟时间(如 10 秒),验证高优先级主机恢复后,VIP 延迟迁移的效果,避免频繁切换。

2. Keepalived 子配置文件设定实验

为解决主配置文件内容过多、可读性差的问题,该实验演示了子配置文件的拆分与引用方法。在主配置文件keepalived.conf中通过include /etc/keepalived/conf.d/*.conf指定子配置文件路径,将 VRRP 实例的配置独立拆分到conf.d目录下的webvip.conf文件中,配置完成后验证配置文件语法正确性并重启服务,确认 VIP 正常生效。

3. Keepalived 单播模式配置实验

该实验解决了 Keepalived 组播模式无法跨网络通信的问题,核心是配置单播模式实现跨网络的 VRRP 通告。实验中分别在两台主机(KA1、KA2)的 keepalived.conf 配置文件中,关闭组播配置,指定unicast_src_ip(本机单播源 IP)和unicast_peer(对端单播接收 IP),重启服务后通过 tcpdump 工具监控两台主机间的单播通信,验证 VIP 在主备机间的迁移效果。

4. Keepalived 业务 VIP 迁移告警实验

该实验实现 VIP 迁移时的邮件告警功能,分为三个步骤:

  • 邮件环境构建:安装 s-nail、postfix 工具,配置公网邮箱(163 邮箱)的 SMTP 参数,测试邮件发送功能;
  • 告警脚本编写 :创建waring.sh脚本,根据 VIP 迁移状态(master/backup/fault)发送不同主题的告警邮件;
  • 集成到 Keepalived :在配置文件中添加notify_master/notify_backup/notify_fault参数,关联告警脚本,重启服务后验证 VIP 迁移时邮件告警的触发效果。

抢占模式

抢占模式( 默认的,谁优先级高就把vip放到哪里)

非抢占模式(持有vip只要vrrp通告正常就不做vip迁移)

复制代码
#kA1中
[root@KA1 yxs]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
    state BACKUP			#非抢占模式互为backup
    interface eth0
    virtual_router_id 51
    nopreempt				#启动非抢占模式
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
}
[root@KA1 yxs]# systemctl stop keepalived.service

#KA2中
[root@KA2 yxs]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
    state BACKUP
    interface eth0
    virtual_router_id 51
    nopreempt				#开启非抢占模式
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
}
[root@KA2 yxs]# systemctl stop keepalived.service

#测试
[root@KA1 yxs]# systemctl start keepalived.service
[root@KA2 yxs]# systemctl start keepalived.service
[root@KA1 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.50  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::2548:cded:e9d2:8517  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)
        RX packets 507  bytes 48237 (47.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1926  bytes 120445 (117.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 132  bytes 12493 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 132  bytes 12493 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@KA2 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.60  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::7a00:745:7105:ccf3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:94:28:97  txqueuelen 1000  (Ethernet)
        RX packets 1439  bytes 104210 (101.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 637  bytes 45034 (43.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 128  bytes 12679 (12.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 128  bytes 12679 (12.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@KA1 yxs]# systemctl stop keepalived.service
[root@KA1 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.50  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::2548:cded:e9d2:8517  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)
        RX packets 519  bytes 49567 (48.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1963  bytes 122514 (119.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 132  bytes 12493 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 132  bytes 12493 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@KA2 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.60  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::7a00:745:7105:ccf3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:94:28:97  txqueuelen 1000  (Ethernet)
        RX packets 1472  bytes 106387 (103.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 672  bytes 47486 (46.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:94:28:97  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 128  bytes 12679 (12.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 128  bytes 12679 (12.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#开启KA1的服务ip不会被抢占到1中
[root@KA1 yxs]# systemctl start keepalived.service
[root@KA1 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.50  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::2548:cded:e9d2:8517  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)
        RX packets 521  bytes 49695 (48.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1965  bytes 122622 (119.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 132  bytes 12493 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 132  bytes 12493 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

延迟抢占

复制代码
#kA1中
[root@KA1 yxs]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
    state BACKUP			#非抢占模式互为backup
    interface eth0
    virtual_router_id 51
    preempt_delay 10				#启动延迟抢占,延迟10s抢占
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
}
[root@KA1 yxs]# systemctl stop keepalived.service

#KA2中
[root@KA2 yxs]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
    state BACKUP
    interface eth0
    virtual_router_id 51
    preempt_delay 10				#启动延迟抢占,延迟10s抢占
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
}
[root@KA2 yxs]# systemctl stop keepalived.service

#测试
[root@KA1 yxs]# systemctl start keepalived.service
[root@KA2 yxs]# systemctl start keepalived.service
[root@KA1 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.50  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::2548:cded:e9d2:8517  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)
        RX packets 625  bytes 57043 (55.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2013  bytes 125776 (122.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 132  bytes 12493 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 132  bytes 12493 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@KA1 yxs]# systemctl stop keepalived.service
[root@KA1 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.50  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::2548:cded:e9d2:8517  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)
        RX packets 519  bytes 49567 (48.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1963  bytes 122514 (119.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 132  bytes 12493 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 132  bytes 12493 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@KA2 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.60  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::7a00:745:7105:ccf3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:94:28:97  txqueuelen 1000  (Ethernet)
        RX packets 1540  bytes 111366 (108.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 872  bytes 59489 (58.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:94:28:97  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 128  bytes 12679 (12.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 128  bytes 12679 (12.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@KA1 yxs]# systemctl start keepalived.service
[root@KA1 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.50  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::2548:cded:e9d2:8517  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)
        RX packets 625  bytes 57043 (55.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2013  bytes 125776 (122.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 132  bytes 12493 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 132  bytes 12493 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Keepalived的子配置文件设定

复制代码
[root@KA1 yxs]# vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     yunlei713@qq.com
   }
   notification_email_from yunlei713@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id KA1
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
   vrrp_mcast_group4 224.0.0.44
}

include /etc/keepalived/conf.d/*.conf                   #指定独立子配置文件

[root@KA1 yxs]# vim /etc/keepalived/conf.d/webvip.conf
vrrp_instance WEB_VIP {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
}
[root@KA1 yxs]# keepalived -t -f /etc/keepalived/keepalived.conf
(/etc/keepalived/keepalived.conf: Line 18) vrrp instance WEB_VIP already defined
[root@KA1 yxs]# systemctl restart keepalived.service
[root@KA1 yxs]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.50  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::2548:cded:e9d2:8517  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)
        RX packets 644  bytes 58916 (57.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2047  bytes 127852 (124.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:8b:6e:34  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 132  bytes 12493 (12.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 132  bytes 12493 (12.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

keepalived的单播模式

复制代码
#在KA1中
[root@KA1 yxs]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     yunlei713@qq.com
   }
   notification_email_from yunlei713@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id KA1
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
   #vrrp_mcast_group4 224.0.0.44			#关闭组播
}

vrrp_instance WEB_VIP {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    unicast_src_ip 172.25.254.50		#指定单播源地址,通常是本机IP
    unicast_peer {
      172.25.254.60						#指定单播接收地址
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
}
[root@KA1 yxs]# systemctl restart keepalived.service

#在KA2中
[root@KA2 yxs]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     timinglee_zln@163.com
   }
   notification_email_from timinglee_zln@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id KA2
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
   #vrrp_mcast_group4 224.0.0.44			#关闭组播
}

vrrp_instance WEB_VIP {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    unicast_src_ip 172.25.254.60		#指定单播源地址,通常是本机IP
    unicast_peer {
      172.25.254.50						#指定单播接收地址
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
}
[root@KA2 yxs]# systemctl restart keepalived.service

#测试
#在KA1中开启独立shell监控播报信息
[root@KA1 yxs]# tcpdump -i eth0 -nn src host 172.25.254.50 and dst 172.25.254.60
dropped privs to tcpdump
#在KA2中开启独立shell监控播报信息
[root@KA2 yxs]# tcpdump -i eth0 -nn src host 172.25.254.60 and dst 172.25.254.50
dropped privs to tcpdump
#在KA1正常时
#kA1播报信息不显示通告内容
[root@KA1 yxs]# systemctl stop keepalived.service
#vip会被迁移到KA2,KA1上开始显示播报内容
[root@KA1 yxs]# systemctl restart keepalived.service
#vip被KA1抢占,KA2上开始播报内容

Keepalived业务vip迁移告警

邮件告警环境构建

复制代码
#安装邮件软件
[root@KA2 yxs]# dnf install s-nail postfix   -y
[root@KA2 yxs]# dnf install s-nail postfix   -y

#启动邮件代理
[root@KA1 yxs]# systemctl start postfix.service
[root@KA2 yxs]# systemctl start postfix.service

#在Linux主机中配置mailrc(KA1+KA2)
[root@KA1 yxs]# vim /etc/mail.rc
set smtp=smtp.qq.com
set smtp-auth=login
set smtp-auth-user=yunlei713@qq.com
set smtp-auth-password=TGfdKaJT7EB
set from=yunlei713@qq.com
set ssl-verify=ignore                                       
[root@KA2 yxs]# vim /etc/mail.rc
set smtp=smtp.qq.com
set smtp-auth=login
set smtp-auth-user=yunlei713@qq.com
set smtp-auth-password=TGfdKaJT7EB
set from=yunlei713@qq.com
set ssl-verify=ignore         
   
#测试邮件
[root@KA2 yxs]# echo hello | mailx -s test yunlei713@qq.com               
[root@KA1 yxs]# mailq		#查看邮件队列
Mail queue is empty         
#查看对应邮箱是否有邮件收到

设定keepalived告警脚本

复制代码
[root@KA1 yxs]# mkdir -p /etc/keepalived/scripts
[root@KA2 yxs]# mkdir -p /etc/keepalived/scripts

#编写告警脚本
[root@KA1 yxs]# vim /etc/keepalived/scripts/waring.sh
#!/bin/bash
mail_dest='yunlei713@qq.com'

mail_send()
{
    mail_subj="$HOSTNAME to be $1 vip 转移"
    mail_mess="`date +%F\ %T`: vrrp 转移,$HOSTNAME 变为 $1"
    echo "$mail_mess" | mail -s "$mail_subj" $mail_dest
}
case $1 in
    master)
    mail_send master
    ;;
    backup)
    mail_send backup
    ;;
    fault)
    mail_send fault
    ;;
    *)
    exit 1
    ;;
esac
[root@KA2 yxs]# vim /etc/keepalived/scripts/waring.sh
#!/bin/bash
mail_dest='yunlei713@qq.com'

mail_send()
{
    mail_subj="$HOSTNAME to be $1 vip 转移"
    mail_mess="`date +%F\ %T`: vrrp 转移,$HOSTNAME 变为 $1"
    echo "$mail_mess" | mail -s "$mail_subj" $mail_dest
}
case $1 in
    master)
    mail_send master
    ;;
    backup)
    mail_send backup
    ;;
    fault)
    mail_send fault
    ;;
    *)
    exit 1
    ;;
esac
[root@KA1 yxs]# chmod  +x /etc/keepalived/scripts/waring.sh
[root@KA1 yxs]# /etc/keepalived/scripts/waring.sh master
[root@KA2 yxs]# chmod  +x /etc/keepalived/scripts/waring.sh
[root@KA2 yxs]# /etc/keepalived/scripts/waring.sh master
#对应邮箱中会出现邮件

配置keepalived告警

复制代码
#在KA1和KA2中设定配置文件
[root@KA1 yxs]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     yunlei713@qq.com
   }
   notification_email_from yunlei713@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id KA1
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
   vrrp_mcast_group4 224.0.0.44
   enable_script_security
   script_user root
}
vrrp_instance WEB_VIP {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
  # unicast_src_ip 172.25.254.50
  # unicast_peer {
  #   172.25.254.60
#   }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
    notify_master "/etc/keepalived/scripts/waring.sh master"
    notify_backup "/etc/keepalived/scripts/waring.sh backup"
    notify_fault "/etc/keepalived/scripts/waring.sh fault"
}

[root@KA1 yxs]# systemctl restart keepalived.service
[root@KA2 yxs]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     yunlei713@qq.com
   }
   notification_email_from yunlei713@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id KA1
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 1
   vrrp_gna_interval 1
   vrrp_mcast_group4 224.0.0.44
   enable_script_security
   script_user root
}
vrrp_instance WEB_VIP {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
  # unicast_src_ip 172.25.254.50
  # unicast_peer {
  #   172.25.254.60
#   }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:0
    }
    notify_master "/etc/keepalived/scripts/waring.sh master"
    notify_backup "/etc/keepalived/scripts/waring.sh backup"
    notify_fault "/etc/keepalived/scripts/waring.sh fault"
}

[root@KA2 yxs]# systemctl restart keepalived.service

#测试
#停止服务后查看邮件
[root@KA1 yxs]# systemctl stop keepalived.service
[root@KA2 yxs]# systemctl stop keepalived.service
相关推荐
AI攻城狮4 小时前
OpenClaw 里 TAVILY_API_KEY 明明写在 ~/.bashrc,为什么还是失效?一次完整排查与修复
人工智能·云原生·aigc
阿里云云原生1 天前
零配置部署顶级模型!函数计算一键解锁 Qwen3.5
云原生
AI攻城狮1 天前
Kimi Bot + OpenClaw 完整配置指南:5 步实现本地 AI Agent 集成
人工智能·云原生·aigc
AI攻城狮2 天前
RAG Chunking 为什么这么难?5 大挑战 + 最佳实践指南
人工智能·云原生·aigc
茶杯梦轩2 天前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
服务器·后端·消息队列
哈里谢顿4 天前
Kubernetes Operator核心概念、实现原理和实战开发
云原生
阿里云云原生4 天前
你的 OpenClaw 真的在受控运行吗?
云原生
阿里云云原生4 天前
5 分钟零代码改造,让 Go 应用自动获得全链路可观测能力
云原生·go
Shanyoufusu124 天前
RKE2 单节点集群安装 Rancher+ 私有镜像仓库搭建 完整教程
云原生
阿里云云原生4 天前
Dify 官方上架 Higress 插件,轻松接入 AI 网关访问模型服务
云原生