通过keepalived实现多主集群方案

2024-03-03 04:59

本文主要是介绍通过keepalived实现多主集群方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、 环境说明:
1、服务器列表:
proxy01:
eth0: 192.168.56.11 eth2: 192.168.156.11

proxy02:
eth0: 192.168.56.12 eth2: 192.168.156.12

proxy03:
eth0: 192.168.56.13 eth2: 192.168.156.13

 

2、拓扑图:

 

二、 实验步骤
1、 在所有节点上操作

# yum install –y keepalived haproxy

2、 proxy01节点配置

[root@proxy01 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalivedglobal_defs {router_id LVS_DEVELvrrp_skip_check_adv_addr
#   vrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_sync_group VG1 {group {VI_1VI_11}
}vrrp_sync_group VG2 {group {VI_2VI_12}
}vrrp_sync_group VG3 {group {VI_3VI_13}
}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51##nopreempt 启用抢占模式priority 10advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmac #启用vmac模式,会创建虚拟机接口,vip绑定到虚拟接口上vmac_xmit_base #发送和接收VRRP包的虚拟MAC地址unicast_src_ip 192.168.56.11unicast_peer {192.168.56.12192.168.56.13}virtual_ipaddress {192.168.56.101}
}vrrp_instance VI_2 {state BACKUPinterface eth0virtual_router_id 52priority 20##nopreemptadvert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.11unicast_peer {192.168.56.12192.168.56.13
}virtual_ipaddress {192.168.56.102}
}vrrp_instance VI_3 {state BACKUPinterface eth0virtual_router_id 53priority 30advert_int 1##nopreemptauthentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.11unicast_peer {192.168.56.12192.168.56.13}virtual_ipaddress {192.168.56.103}
}vrrp_instance VI_11 {state BACKUPinterface eth2virtual_router_id 51##nopreemptpriority 10advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.11unicast_peer {192.168.156.12192.168.156.13}virtual_ipaddress {192.168.156.101}
}vrrp_instance VI_12 {state BACKUPinterface eth2virtual_router_id 52priority 20#nopreemptadvert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.11unicast_peer {192.168.156.12192.168.156.13}virtual_ipaddress {192.168.156.102}
}vrrp_instance VI_13 {state BACKUPinterface eth2virtual_router_id 53priority 30advert_int 1#nopreemptauthentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.11unicast_peer {192.168.156.12192.168.156.13}virtual_ipaddress {192.168.156.103}
}

 

  

 

3、 proxy02节点配置

[root@proxy02 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalivedglobal_defs {router_id LVS_DEVELvrrp_skip_check_adv_addr
#   vrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_sync_group VG1 {group {VI_1VI_11}
}vrrp_sync_group VG2 {group {VI_2VI_12}
}vrrp_sync_group VG3 {group {VI_3VI_13}
}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51#nopreemptpriority 20advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.12unicast_peer {192.168.56.11192.168.56.13}virtual_ipaddress {192.168.56.101}
}vrrp_instance VI_2 {state BACKUPinterface eth0virtual_router_id 52#nopreemptpriority 30advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.12unicast_peer {192.168.56.11192.168.56.13}virtual_ipaddress {192.168.56.102}
}vrrp_instance VI_3 {state BACKUPinterface eth0virtual_router_id 53#nopreemptpriority 10advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.12unicast_peer {192.168.56.11192.168.56.13}virtual_ipaddress {192.168.56.103}
}vrrp_instance VI_11 {state BACKUPinterface eth2virtual_router_id 51#nopreemptpriority 20advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.12unicast_peer {192.168.156.11192.168.156.13}virtual_ipaddress {192.168.156.101}
}vrrp_instance VI_12 {state BACKUPinterface eth2virtual_router_id 52#nopreemptpriority 30advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.12unicast_peer {192.168.156.11192.168.156.13}virtual_ipaddress {192.168.156.102}
}vrrp_instance VI_13 {state BACKUPinterface eth2virtual_router_id 53#nopreemptpriority 10advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.12unicast_peer {192.168.156.11192.168.156.13}virtual_ipaddress {192.168.156.103}
}

 

  

 

4、 proxy03节点配置

[root@proxy03 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalivedglobal_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 30router_id LVS_DEVELvrrp_skip_check_adv_addr
#   vrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_sync_group VG1 {group {VI_1VI_11}
}vrrp_sync_group VG2 {group {VI_2VI_12}
}vrrp_sync_group VG3 {group {VI_3VI_13}
}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51#nopreemptpriority 30advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.13unicast_peer {192.168.56.11192.168.56.12}virtual_ipaddress {192.168.56.101}
}vrrp_instance VI_2 {state BACKUPinterface eth0virtual_router_id 52#nopreemptpriority 10advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.13unicast_peer {192.168.56.11192.168.56.12}virtual_ipaddress {192.168.56.102}
}vrrp_instance VI_3 {state BACKUPinterface eth0virtual_router_id 53#nopreemptpriority 20advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.56.13unicast_peer {192.168.56.11192.168.56.12}virtual_ipaddress {192.168.56.103}
}vrrp_instance VI_11 {state BACKUPinterface eth2virtual_router_id 51#nopreemptpriority 30advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.13unicast_peer {192.168.156.11192.168.156.12}virtual_ipaddress {192.168.156.101}
}vrrp_instance VI_12 {state BACKUPinterface eth2virtual_router_id 52#nopreemptpriority 10advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.13unicast_peer {192.168.156.11192.168.156.12}virtual_ipaddress {192.168.156.102}
}vrrp_instance VI_13 {state BACKUPinterface eth2virtual_router_id 53#nopreemptpriority 20advert_int 1authentication {auth_type PASSauth_pass 1111
}use_vmacvmac_xmit_baseunicast_src_ip 192.168.156.13unicast_peer {192.168.156.11192.168.156.12}virtual_ipaddress {192.168.156.103}
}

 

  

 

5、 启动服务

在所有节点操作
# systemctl enable keepalived.service && systemctl start keepalived.service

6、 结果验证

[root@proxy01 ~]# ip add 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:4b:3e:34 brd ff:ff:ff:ff:ff:ffinet 192.168.56.11/24 brd 192.168.56.255 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:fe4b:3e34/64 scope link valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:17:a2:bc brd ff:ff:ff:ff:ff:ffinet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic eth1valid_lft 85732sec preferred_lft 85732secinet6 fe80::a00:27ff:fe17:a2bc/64 scope link valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:ff:0e:a8 brd ff:ff:ff:ff:ff:ffinet 192.168.156.11/24 brd 192.168.156.255 scope global eth2valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:feff:ea8/64 scope link valid_lft forever preferred_lft forever
5: vrrp.51@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:33 brd ff:ff:ff:ff:ff:ff
6: vrrp.52@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:34 brd ff:ff:ff:ff:ff:ff
7: vrrp.53@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:35 brd ff:ff:ff:ff:ff:ffinet 192.168.56.103/32 scope global vrrp.53valid_lft forever preferred_lft forever
8: vrrp1.51@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:33 brd ff:ff:ff:ff:ff:ff
9: vrrp1.52@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:34 brd ff:ff:ff:ff:ff:ff
10: vrrp1.53@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:35 brd ff:ff:ff:ff:ff:ffinet 192.168.156.103/32 scope global vrrp1.53valid_lft forever preferred_lft forever[root@proxy02 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:ee:7d:7e brd ff:ff:ff:ff:ff:ffinet 192.168.56.12/24 brd 192.168.56.255 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:feee:7d7e/64 scope link valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:a4:ce:2f brd ff:ff:ff:ff:ff:ffinet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic eth1valid_lft 85717sec preferred_lft 85717secinet6 fe80::a00:27ff:fea4:ce2f/64 scope link valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:6c:16:4d brd ff:ff:ff:ff:ff:ffinet 192.168.156.12/24 brd 192.168.156.255 scope global eth2valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:fe6c:164d/64 scope link valid_lft forever preferred_lft forever
5: vrrp.51@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:33 brd ff:ff:ff:ff:ff:ff
6: vrrp.52@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:34 brd ff:ff:ff:ff:ff:ffinet 192.168.56.102/32 scope global vrrp.52valid_lft forever preferred_lft forever
7: vrrp.53@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:35 brd ff:ff:ff:ff:ff:ff
8: vrrp1.51@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:33 brd ff:ff:ff:ff:ff:ff
9: vrrp1.52@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:34 brd ff:ff:ff:ff:ff:ffinet 192.168.156.102/32 scope global vrrp1.52valid_lft forever preferred_lft forever
10: vrrp1.53@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:35 brd ff:ff:ff:ff:ff:ff[root@proxy03 ~]# ip add 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:87:1a:f0 brd ff:ff:ff:ff:ff:ffinet 192.168.56.13/24 brd 192.168.56.255 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:fe87:1af0/64 scope link valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:6e:be:14 brd ff:ff:ff:ff:ff:ffinet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic eth1valid_lft 85696sec preferred_lft 85696secinet6 fe80::a00:27ff:fe6e:be14/64 scope link valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:b0:64:83 brd ff:ff:ff:ff:ff:ffinet 192.168.156.13/24 brd 192.168.156.255 scope global eth2valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:feb0:6483/64 scope link valid_lft forever preferred_lft forever
5: vrrp.51@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:33 brd ff:ff:ff:ff:ff:ffinet 192.168.56.101/32 scope global vrrp.51valid_lft forever preferred_lft forever
6: vrrp.52@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:34 brd ff:ff:ff:ff:ff:ff
7: vrrp.53@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:35 brd ff:ff:ff:ff:ff:ff
8: vrrp1.51@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:33 brd ff:ff:ff:ff:ff:ffinet 192.168.156.101/32 scope global vrrp1.51valid_lft forever preferred_lft forever
9: vrrp1.52@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:34 brd ff:ff:ff:ff:ff:ff
10: vrrp1.53@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000link/ether 00:00:5e:00:01:35 brd ff:ff:ff:ff:ff:ff

 

7、 总结
  之前我们用keepalived做集群时一般使用它构建服务器主从,也就是只有一个vip,并且这个vip只是在主节点上,当主节点宕机时,vip漂移到从节点上,从而实现高可用。但随着业务的发展,单个节点随之成为业务的性能瓶颈,及时我们使用的负载均衡再强大,服务器配置再高,也不可能单节点抗住所有流量。而通过这种方案,在keepalived的主从基础上扩展一下,通过配置多个vip,每个keepalived节点互为主从,正常情况下保证所有服务器都能拥有一个vip,然后通过dns负载均衡技术,将业务流量转发到每个vip。从而在一定程度上避免了单服务器的性能瓶颈。

转载于:https://www.cnblogs.com/edenlong/p/10078975.html

这篇关于通过keepalived实现多主集群方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/768521

相关文章

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3