【Keepalived】主备模式MASTER/BACKUP的vrrp实例配置详解

发布时间:2026/5/25 6:05:19

【Keepalived】主备模式MASTER/BACKUP的vrrp实例配置详解 前情提要本篇博客将介绍使用Keepalived搭建主从架构的vrrp实例的全部流程通过本篇博客你可以学会搭建vrrp实例环境系统RHEL9.3Keepalived版本Keepalived v2.2.8 (04/04,2023)1、配置MASTER主机编辑配置文件# 编辑主配置文件/etc/keepalived/keepalived.conf global_defs { notification_email { double_dragonY163.com } notification_email_from double_dragonY163.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id KA1 vrrp_skip_check_adv_addr #vrrp_strict # 添加此参数无法访问VIP可以用nft list ruleset查看 vrrp_garp_interval 1 vrrp_gna_interval 1 vrrp_mcast_group4 224.0.0.44 } include /etc/keepalived/conf.d/*.conf # 引用子配置文件 # 编辑子配置文件创建虚拟路由WEB_VIP [rootKA1 ~]# vim /etc/keepalived/conf.d/WEB_VIP.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 } } # 重启keepalived服务2、配置BACKUP主机编辑配置文件# 编辑主配置文件/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { double_dragonY163.com } notification_email_from double_dragonY163.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 } include /etc/keepalived/conf.d/*.conf # 编辑子配置文件 [rootKA2 ~]# vim /etc/keepalived/conf.d/WEB_VIP.conf vrrp_instance WEB_VIP { state BACKUP interface eth0 virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.254.100/24 dev eth0 label eth0:0 } } # 重启服务 [rootKA2 ~]# systemctl restart keepalived.service3、检测检测VIP# 在master主机查看 [rootKA1 ~]# 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 mq state UP group default qlen 1000 link/ether 00:0c:29:93:eb:8f brd ff:ff:ff:ff:ff:ff altname enp3s0 altname ens160 inet 172.25.254.50/24 brd 172.25.254.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 172.25.254.100/24 scope global secondary eth0:0 valid_lft forever preferred_lft forever inet6 fe80::e3b4:c2e9:dca7:2b9b/64 scope link noprefixroute valid_lft forever preferred_lft forever抓包观察[rootKA1 ~]# tcpdump -i eth0 -nn host 224.0.0.44 dropped privs to tcpdump tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes 14:02:20.431090 IP 172.25.254.50 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20 14:02:21.441604 IP 172.25.254.50 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20 14:02:22.445593 IP 172.25.254.50 224.0.0.44: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20检测可见VIP存在MASTER配置没有问题已经实现了虚拟路由实例的配置VIP为172.25.254.100/24至此实验结束

相关新闻