首页 > 其他分享 >k1.Keepalived 架构和安装

k1.Keepalived 架构和安装

时间:2024-07-23 17:59:29浏览次数:15  
标签:架构 Keepalived keepalived vrrp etc k1 conf Ubuntu2204 root

Keepalived 架构和安装

vrrp 协议的软件实现,原生设计目的为了高可用 ipvs服务

keepalived 是高可用集群的通用无状态应用解决方案

官网:http://keepalived.org/

功能:

  • 基于vrrp协议完成地址流动
  • 为vip地址所在的节点生成ipvs规则(在配置文件中预先定义)
  • 为ipvs集群的各RS做健康状态检测
  • 基于脚本调用接口完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务

Keepalived 相关文件

  • 软件包名:keepalived
  • 主程序文件:/usr/sbin/keepalived
  • 主配置文件:/etc/keepalived/keepalived.conf
  • 配置文件示例:/usr/share/doc/keepalived/
  • Unit File:/lib/systemd/system/keepalived.service
  • Unit File的环境配置文件:
    • /etc/sysconfig/keepalived CentOS
    • /etc/default/keepalived Ubuntu

1.Keepalived 安装

1.1 包安装

[root@Ubuntu2204 ~]#apt update;apt -y install keepalived

# 默认缺少配置,服务无法启动,提示/etc/keepalived/keepalived.conf 不存在
[root@Ubuntu2204 ~]#systemctl status keepalived.service 
○ keepalived.service - Keepalive Daemon (LVS and VRRP)
     Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
  Condition: start condition failed at Mon 2024-07-22 20:56:40 CST; 1min 12s ago
             └─ ConditionFileNotEmpty=/etc/keepalived/keepalived.conf was not met

Jul 22 20:56:40 Ubuntu2204 systemd[1]: Condition check resulted in Keepalive Daemon (LVS and VRRP) being skipped.

# 利用范例生成配置文件
[root@Ubuntu2204 ~]#cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf
[root@Ubuntu2204 ~]#systemctl start keepalived.service 
[root@Ubuntu2204 ~]#systemctl status keepalived.service 
● keepalived.service - Keepalive Daemon (LVS and VRRP)
     Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-07-22 20:59:54 CST; 3s ago
   Main PID: 4013 (keepalived)
      Tasks: 3 (limit: 2176)
     Memory: 4.4M
        CPU: 81ms
     CGroup: /system.slice/keepalived.service
             ├─4013 /usr/sbin/keepalived --dont-fork
             ├─4014 /usr/sbin/keepalived --dont-fork
             └─4015 /usr/sbin/keepalived --dont-fork
             
[root@Ubuntu2204 ~]#pstree -p | grep keepalived
           |-keepalived(4013)-+-keepalived(4014)
           |                  `-keepalived(4015)

1.2 编译安装

# Ubuntu20.04安装相关包
[root@Ubuntu2204 ~]#apt update;apt -y install make gcc ipvsadm build-essential pkg-config automake autoconf libipset-dev libnl-3-dev libnl-genl-3-dev libssl-dev libxtables-dev libip4tc-dev libip6tc-dev libmagic-dev libsnmp-dev libglib2.0-dev libpcre2-dev libnftnl-dev libmnl-dev libsystemd-dev

[root@Ubuntu2204 ~]#wget https://keepalived.org/software/keepalived-2.2.7.tar.gz
[root@Ubuntu2204 ~]#tar xf keepalived-2.3.1.tar.gz -C /usr/local/src/
[root@Ubuntu2204 ~]#cd /usr/local/src/keepalived-2.2.7/

# 选项--disable-fwmark 可用于禁用iptables规则,可访止VIP无法访问,无此选项默认会启用iptables规则
[root@Ubuntu2204 keepalived-2.2.7]#./configure --prefix=/usr/local/keepalived --disable-fwmark
[root@Ubuntu2204 keepalived-2.2.7]#make -j 2 && make install
[root@Ubuntu2204 keepalived-2.2.7]#mkdir /etc/keepalived
[root@Ubuntu2204 keepalived-2.2.7]#cp /usr/local/keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
[root@Ubuntu2204 keepalived-2.2.7]#cp ./keepalived/keepalived.service /lib/systemd/system/
[root@Ubuntu2204 keepalived-2.2.7]#systemctl daemon-reload 
[root@Ubuntu2204 keepalived-2.2.7]#systemctl enable --now keepalived.service

2. KeepAlived 配置说明

配置文件:/etc/keepalived/keepalived.conf

配置文件组成

  • GLOBAL CONFIGURATION Global definitions:
    • 定义邮件配置,route_id,vrrp配置,多播地址等
  • VRRP CONFIGURATION VRRP instance(s):
    • 定义每个vrrp虚拟路由器
  • LVS CONFIGURATION
    • Virtual server group(s)
    • Virtual server(s):LVS集群的VS和RS

2.1 全局配置

#/etc/keepalived/keepalived.conf 
global_defs {
 notification_email {
 root@localhost #keepalived 发生故障切换时邮件发送的目标邮箱,可以按行区分写多个
 [email protected] 
  [email protected] 
 }
 notification_email_from keepalived@localhost  #发邮件的地址
 smtp_server 127.0.0.1     #邮件服务器地址
 smtp_connect_timeout 30   #邮件服务器连接timeout
 router_id ka1.example.com #每个keepalived主机唯一标识,建议使用当前主机名,如果多节点重名可能会影响切换脚本执行
vrrp_skip_check_adv_addr  #对所有通告报文都检查,会比较消耗性能,启用此配置后,如果收到的通告报文和上一个报文是同一个路由器,则跳过检查,默认值为全检查
 vrrp_strict #严格遵守VRRP协议,启用此项后以下状况将无法启动服务:1.无VIP地址 2.配置了单播邻居 3.在VRRP版本2中有IPv6地址,开启动此项并且没有配置vrrp_iptables时会自动开启iptables防火墙规则,默认导致VIP无法访问,建议不加此项配置
 vrrp_garp_interval 0 #gratuitous ARP messages 报文发送延迟,0表示不延迟
 vrrp_gna_interval 0  #unsolicited NA messages (不请自来)消息发送延迟
 vrrp_mcast_group4 224.0.0.18 #指定组播IP地址范围:224.0.0.0到239.255.255.255,默认值:224.0.0.18 
 vrrp_iptables        #此项和vrrp_strict同时开启时,则不会添加防火墙规则,如果无配置vrrp_strict项,则无需启用此项配置
}

2.2 配置虚拟路由器

vrrp_instance <STRING> { #<String>为vrrp的实例名,一般为业务名称
 配置参数
 ......
 }
#配置参数:
state MASTER|BACKUP#当前节点在此虚拟路由器上的初始状态,状态为MASTER或者BACKUP
interface IFACE_NAME #绑定为当前虚拟路由器使用的物理接口,如:eth0,bond0,br0,可以和VIP不在一个网卡
virtual_router_id VRID #每个虚拟路由器唯一标识,范围:0-255,每个虚拟路由器此值必须唯一,否则服务无法启动,同属一个虚拟路由器的多个keepalived节点必须相同,务必要确认在同一网络中此值必须唯一
priority 100 #当前物理节点在此虚拟路由器的优先级,范围:1-254,每个keepalived主机节点此值不同
advert_int 1 #vrrp通告的时间间隔,默认1s
authentication { #认证机制
 auth_type AH|PASS   #AH为IPSEC认证(不推荐),PASS为简单密码(建议使用)
 auth_pass <PASSWORD> #预共享密钥,仅前8位有效,同一个虚拟路由器的多个keepalived节点必须一样
}
virtual_ipaddress { #虚拟IP,生产环境可能指定上百个IP地址
 <IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL>
 192.168.200.100 #指定VIP,不指定网卡,默认为eth0,注意:不指定/prefix,默认
为/32
 192.168.200.101/24 dev eth1   #指定VIP的网卡,建议和interface指令指定的网卡不在一个
网卡
 192.168.200.102/24 dev eth2 label eth2:1 #指定VIP的网卡label 
}
track_interface { #配置监控网络接口,一旦出现故障,则转为FAULT状态实现地址转移
 eth0
 eth1
 …
} 

2.3启用 Keepalived 日志功能

# 包安装
[root@Ubuntu2204 ~]#vim /etc/default/keepalived

DAEMON_ARGS="-D -S 6 "

# 编译安装
[root@Ubuntu2204 ~]#vim /apps/keepalived/etc/sysconfig/keepalived

KEEPALIVED_OPTIONS="-D -S 6"

[root@Ubuntu2204 ~]#vim /etc/rsyslog.conf

local6.*    /var/log/keepalived.log

[root@Ubuntu2204 ~]#systemctl restart keepalived.service rsyslog.service
[root@Ubuntu2204 ~]#tail -f /var/log/keepalived.log
Jul 23 10:31:12 ym Keepalived_vrrp[2585]: Sending gratuitous ARP on eth0 for 192.168.200.13
Jul 23 10:31:12 ym Keepalived_vrrp[2585]: Sending gratuitous ARP on eth0 for 192.168.200.11
Jul 23 10:31:12 ym Keepalived_vrrp[2585]: Sending gratuitous ARP on eth0 for 192.168.200.12
Jul 23 10:31:12 ym Keepalived_vrrp[2585]: Sending gratuitous ARP on eth0 for 192.168.200.13
Jul 23 10:31:12 ym Keepalived_vrrp[2585]: Sending gratuitous ARP on eth0 for 192.168.200.11
Jul 23 10:31:12 ym Keepalived_vrrp[2585]: Sending gratuitous ARP on eth0 for 192.168.200.12
Jul 23 10:31:12 ym Keepalived_vrrp[2585]: Sending gratuitous ARP on eth0 for 192.168.200.13

2.4 实现 Keepalived 独立子配置文件

当生产环境复杂时, /etc/keepalived/keepalived.conf 文件中内容过多,不易管理,可以将不同集 群的配置,比如:不同集群的VIP配置放在独立的子配置文件中

利用include 指令可以实现包含子配置文件

[root@Ubuntu2204 ~]#mkdir /etc/keepalived/conf.d/
[root@Ubuntu2204 ~]#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id ka1.anan.com
   vrrp_mcast_group4 224.1.1.1
}

include /etc/keepalived/conf.d/*.conf

[root@Ubuntu2204 ~]#vim /etc/keepalived/conf.d/cluster1.conf

3. Keepalived 实现 VRRP

3.1 实现master/slave的 Keepalived 单主架构

# MASTER配置
[root@Ubuntu2204 ~]#vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id ka1.anan.com
   vrrp_mcast_group4 224.1.1.1
}

include /etc/keepalived/conf.d/*.conf


[root@Ubuntu2204 ~]#vim /etc/keepalived/conf.d/cluster1.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 66
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 123456
    }

    virtual_ipaddress {
        10.0.0.200/24 dev eth0 label eth0:0
    }
}
[root@Ubuntu2204 ~]#systemctl restart keepalived.service

# BACKUP配置
[root@Ubuntu2204 ~]#vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id ka2.anan.com
   vrrp_mcast_group4 224.1.1.1
}

include /etc/keepalived/conf.d/*.conf

[root@Ubuntu2204 ~]#vim /etc/keepalived/conf.d/cluster1.conf
vrrp_instance VI_1 {
    state BACKUP		# 修改
    interface eth0
    virtual_router_id 66
    priority 80			# 修改
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 123456
    }

    virtual_ipaddress {
        10.0.0.200/24 dev eth0 label eth0:0
    }
}
[root@Ubuntu2204 ~]#systemctl restart keepalived.service

# 抓包观察
[root@Ubuntu2204 ~]#tcpdump -i eth0 -nn host 224.1.1.1
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
11:38:00.626377 IP 10.0.0.100 > 224.1.1.1: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
11:38:01.627249 IP 10.0.0.100 > 224.1.1.1: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20

4. 抢占模式和非抢占模式

默认为抢占模式 preempt,即当高优先级的主机恢复在线后,会抢占低先级的主机的master角色,造成 网络抖动,建议设置为非抢占模式 nopreempt ,即高优先级主机恢复后,并不会抢占低优先级主机的 master 角色

注意: 非抢占模式下,如果原主机down机, VIP迁移至的新主机, 后续新主机也发生down时,仍会将VIP迁移 回原主机

非抢占模式 nopreempt

注意:要关闭 VIP抢占,必须将各 Keepalived 服务器 state 配置为 BACKUP

# ka1
[root@Ubuntu2204 ~]#cat /etc/keepalived/conf.d/cluster1.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    priority 100
    advert_int 1
    nopreempt			# 修改为非抢占式

# ka2
[root@Ubuntu2204 ~]#cat /etc/keepalived/conf.d/cluster1.conf 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    priority 80
    advert_int 1

抢占延迟模式 preempt_delay

抢占延迟模式,即优先级高的主机恢复后,不会立即抢回VIP,而是延迟一段时间(默认300s)再抢回 VIP

需要各keepalived服务器state为BACKUP,并且不要启用 vrrp_strict

# ka1
[root@Ubuntu2204 ~]#cat /etc/keepalived/conf.d/cluster1.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    priority 100
    advert_int 1
    preempt_delay 60    #抢占延迟模式,默认延迟300s

# ka2
[root@Ubuntu2204 ~]#cat /etc/keepalived/conf.d/cluster1.conf 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    priority 80
    advert_int 1

5. VIP 单播配置

默认keepalived主机之间利用多播相互通告消息,会造成网络拥塞,可以替换成单播,减少网络流量

注意:启用 vrrp_strict 时,不能启用单播

#在所有节点vrrp_instance语句块中设置对方主机的IP,建议设置为专用于对应心跳线网络的地址,而非使用业务网络
unicast_src_ip <IPADDR>  #指定发送单播的源IP
unicast_peer {
   <IPADDR>     #指定接收单播的对方目标主机IP
   ......
}

# 每台主机上添加一块仅主机网卡  绑定为当前虚拟路由器使用的物理接口
eth1: ka1 :192.168.10.100
eth1: ka2 :192.168.10.110
# ka1
[root@Ubuntu2204 ~]#vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id ka1.anan.com
   #vrrp_mcast_group4 224.1.1.1
}

include /etc/keepalived/conf.d/*.conf

[root@Ubuntu2204 ~]#cat /etc/keepalived/conf.d/cluster1.conf 
vrrp_instance VI_1 {
    state MASTER
    interface eth1		# 修改为eth1
    virtual_router_id 66
    priority 100
    advert_int 1
    preempt_delay 60    
    
    authentication {
        auth_type PASS
        auth_pass 123456
    }

    virtual_ipaddress {
        10.0.0.200/24 dev eth0 label eth0:0
    }
    
    unicast_src_ip 192.168.10.100
    unicast_peer{
        192.168.10.110
    }
}

# ka2
[root@Ubuntu2204 ~]#vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id ka2.anan.com
   #vrrp_mcast_group4 224.1.1.1
}

include /etc/keepalived/conf.d/*.conf

[root@Ubuntu2204 ~]#cat /etc/keepalived/conf.d/cluster1.conf 
vrrp_instance VI_1 {
    state BACKUP
    interface eth1		# 修改为eth1
    virtual_router_id 66
    priority 80
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 123456
    }

    virtual_ipaddress {
        10.0.0.200/24 dev eth0 label eth0:0
    }
    
    unicast_src_ip 192.168.10.110
    unicast_peer{
        192.168.10.100
    }
}

# 抓包观察
[root@Ubuntu2204 ~]#tcpdump -i eth1 -nn src host 192.168.10.100 and dst host 192.168.10.110
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), snapshot length 262144 bytes
12:58:00.850213 IP 192.168.10.100 > 192.168.10.110: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
12:58:01.851947 IP 192.168.10.100 > 192.168.10.110: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
12:58:02.853426 IP 192.168.10.100 > 192.168.10.110: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20

标签:架构,Keepalived,keepalived,vrrp,etc,k1,conf,Ubuntu2204,root
From: https://blog.csdn.net/weixin_56408536/article/details/140633166

相关文章

  • STM32 芯片架构
    STM32芯片架构把STM32想像类比成一台电脑。CPU通过总线(Bus)控制各类外设。各部分功能如下:CPU:包含运算器,控制器及若干寄存器,是单片机的控制和指挥中心。ROM(STM32中称为FLASH):用于存放程序和数据,为只读储存器。更改(擦除)和写入程序都较为麻烦,需要遵守FLASH相关的......
  • 简单架构:采集库dll、检测算法dll、项目程序exe,框架库dll
    一般项目exe通过调用各种封装的dll来完成工作。视觉项目exe调用采集库dll、检测算法dll就可以了,有一定积累后凝练出框架库dll(日志、队列、线程池等必不可少的部分封装)它们之间通过“接口函数+数据”来配合。针对采集dll:IGrabber.h里放接口函数,如开始采集、停止采集、set参数......
  • 龙芯3A6000架构安装x86架构安装包
    1.环境准备:一台龙芯3A6000架构的银河麒麟桌面版系统2.步骤:以安装realvncserver为例打开必应(https://cn.bing.com/)搜索realvnc,打开下载安装包,下载x64架构的deb格式的安装包下载完成后,进行安装提示与当前架构不符,对这个deb安装包使用归档管理器解压到当前目录,然后打开control......
  • 架构师的新武器:Codigger之软件项目体检Software Project HealthCheck
    在软件架构这一充满挑战与创新的领域中,架构师宛如精心雕琢宏伟建筑的艺术大师,他们的每一个决策都决定着软件系统的稳固性、可扩展性和性能表现。而Codigger软件项目体检,无疑是架构师手中一件崭新且威力强大的神兵利器,为打造卓越的软件架构提供了有力支撑。项目体检在架构合理性......
  • 多模态大模型主流架构模式的演化历程
    多模态大模型主流架构模式的演化历程一、引言近年来,随着深度学习技术的飞速发展,多模态学习逐渐成为人工智能领域的研究热点。与单一模态不同,多模态学习旨在利用不同模态数据(如文本、图像、音频等)之间的互补信息,构建更加全面、准确的智能模型。多模态融合是实现多模......
  • AI大模型技术的四大核心架构演进之路
    随着人工智能技术的飞速发展,大模型技术已经成为AI领域的重要分支。本文将深入探讨四种关键的大模型技术架构:纯粹Prompt提示词法、Agent+FunctionCalling机制、RAG(检索增强生成)以及Fine-tuning微调技术,揭示它们的特性和应用场景。一、纯粹Prompt提示词法:构建直观交互模......
  • 多模态大模型:基础架构
    多模态大型语言模型(MLLM)是人工智能领域的前沿创新,它结合了语言和视觉模型的功能,可以处理复杂的任务,如视觉问答和图像字幕。这些模型利用大规模预训练,集成了多种数据模态,以显著提高其在各种应用程序中的性能。架构概览较为常见的MLLM框架可以分为三个主要模块:接收且有效......
  • 一文看懂AI的 Transformer 架构!
    1AI的转换器是啥?转换器,一种将输入序列转换或更改为输出序列的神经网络架构。它们通过学习上下文和跟踪序列组件之间的关系来做到这一点。例如,请考虑以下输入序列:“天空是什么颜色的?”转换器模型会使用内部数学表示法来识别颜色、天空和蓝色这三个词之间的相关性和关系。......
  • Dali信号装换器架构
    Dali是一个生态完善的智能照明协议需求是:在自己的产品品类接入到Dali协议,通过Dali总线传输信号到Dali控制器,从而实现控制Dali的总线是有个Master主机,任务是识别和寻址、存储等等的功能在Dali总线上,接入自己的产品主要有三种模式简单的模式(单播):Master主机寻址了简单地址的......
  • Revit 二次开发多版本架构
    一、选对项目模板二、选择".NetStandard2.0"三、请直接Copy至项目配置中(各版本API需要提前准备好,路径引用即可)<PropertyGroup><TargetFramework>net48</TargetFramework><Configurations>Debug;Release;RVT_18;RVT_19;RVT_20;RVT_21;RVT_22;RVT_23......