通过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

相关文章

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

PyCharm接入DeepSeek实现AI编程的操作流程

《PyCharm接入DeepSeek实现AI编程的操作流程》DeepSeek是一家专注于人工智能技术研发的公司,致力于开发高性能、低成本的AI模型,接下来,我们把DeepSeek接入到PyCharm中... 目录引言效果演示创建API key在PyCharm中下载Continue插件配置Continue引言

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

基于Go语言实现一个压测工具

《基于Go语言实现一个压测工具》这篇文章主要为大家详细介绍了基于Go语言实现一个简单的压测工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录整体架构通用数据处理模块Http请求响应数据处理Curl参数解析处理客户端模块Http客户端处理Grpc客户端处理Websocket客户端

Java CompletableFuture如何实现超时功能

《JavaCompletableFuture如何实现超时功能》:本文主要介绍实现超时功能的基本思路以及CompletableFuture(之后简称CF)是如何通过代码实现超时功能的,需要的... 目录基本思路CompletableFuture 的实现1. 基本实现流程2. 静态条件分析3. 内存泄露 bug