keepalived+LVS 详解(2) -- keepalived.conf解析

本文详细解析了Keepalived的配置文件,包括全局定义、VRRP实例设置、优先级及虚拟IP地址管理等关键参数。深入探讨了如何通过调整配置实现高可用性和故障转移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

! Configuration File for keepalived

global_defs {

   notification_email {             #通知的email,需要系统支持发送email

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

  

   smtp_server 192.168.200.1                #用于发送通知电子邮件的远程SMTP服务器

   smtp_connect_timeout 30                   #SMTP服务器连接超时(以秒为单位)。

  

   router_id LVS_DEVEL           #标识信息,一个名字而已        

   vrrp_mcast_group4 224.0.0.18     #用于IPv4 VRRP广告的组播组,默认值:224.0.0.18

   vrrp_mcast_group6 ff02 :: 12        #用于IPv6 VRRP广告的组播组,默认值:ff02 :: 12

   vrrp_garp_master_refresh 60       #在MASTER期间刷新免费ARP的最小时间间隔,默认值:0,不刷新

   vrrp_garp_master_refresh_repeat 2      #在MASTER时一次发送的免费ARP消息数,默认值:1

   vrrp_garp_interval 0.001              #接口发送免费ARP报文的时间间隔(毫秒)十进制,秒(分辨率usecs)       默认值:0)    

   vrrp_gna_interval 0.000001       #接口上主动发送的NA消息之间的延迟时间(毫秒)十进制,秒(分辨率usecs)(默认值:0)。

  

   # Checking all the addresses in a received VRRP advert can be time

   # consuming. Setting this flag means the check won't be carried out

   # if the advert is from the same master router as the previous advert

   # received.

   # (default: don't skip)

   vrrp_skip_check_adv_addr

  

   # Enforce strict VRRP protocol compliance. This will prohibit:

   #   0 VIPs

   #   unicast peers

   #   IPv6 addresses in VRRP version 2  

   # 通过ip addr可以看到vip已经顺利挂载,但是无法ping通,并且防火墙都已关闭:

   #原因是keepalived.conf配置中默认vrrp_strict  打开了,需要把它注释掉。
   # 重启keepalived即可ping通。  

   vrrp_strict

  

   # The following options can be used if vrrp, checker or bfd processes

   # are timing out. This can be seen by a backup vrrp instance becoming

   # master even when the master is still running because the master or

   # backup system is too busy to process vrrp packets.

   # --

   # Set the vrrp child process priority (Negative values increase priority)

   vrrp_priority <-20 to 19>

  

   # Set the checker child process priority

   checker_priority <-20 to 19>

  

   # Set the BFD child process priority

   bfd_priority <-20 to 19>

  

   # Set the default VRRP version to use

   # (default: 2 , but IPv6 instances will use version 3)

   vrrp_version <2 or 3>  

}

 

vrrp_instance VI_1 {

    state MASTER                     #角色是master

    interface ens6                     #vip 绑定端口

    virtual_router_id 51              #让master 和backup在同一个虚拟路由里,id 号必须相同;

    priority 100                         #优先级,谁的优先级高谁就是master ;

    advert_int 1                         #心跳间隔时间

    authentication {

        auth_type PASS                   #认证方式

        auth_pass 1111                   #密码

    }

    virtual_ipaddress {               #虚拟ip,可以多个

        192.168.200.16

    }

}

LVS的配置:

LVS配置后面再来补充

 

更详细的参考

https://www.keepalived.org/manpage.html

### LNMT 架构集成 KeepalivedLVS 的配置教程 #### 1. 方案概述 为了实现高可用性和负载均衡,在 LNMT (Linux, Nginx, MySQL, Tomcat) 架构中引入 KeepalivedLVS 是一种常见做法。该方案通过在用户空间(user space)[^1]运行的 Keepalived 实现 VRRP 协议来管理虚拟 IP 地址,而 LVS 则工作于内核空间(kernel space),负责流量转发。 #### 2. 环境准备 确保所有服务器已安装必要的软件包并完成基础设置。对于 Linux 发行版而言,通常需要安装 `keepalived` 软件包以及根据需求可能涉及的其他组件如 `ipvsadm` 工具用于管理和监控 LVS 设置。 #### 3. 安装与基本配置 ##### 3.1 安装 Keepalived 和 ipvsadm ```bash sudo apt-y ``` ##### 3.2 修改 `/etc/keepalived/keepalived.conf` 定义主备节点的角色分配,并指定 VIP(Virtual IP Address),这部分位于用户空间的操作由 Keepalived 处理: ```plaintext vrrp_instance VI_1 { state MASTER|BACKUP interface eth0 virtual_router_id 51 priority 100 | 90 # 主机优先级更高 advert_int 1 authentication { auth_type PASS auth_pass password } virtual_ipaddress { 192.168.x.y # 这里填写实际要使用的VIP地址 } } ``` #### 4. 配置 LVS LVS 的配置主要集中在内核层面,即 kernel space 中执行命令或编写脚本自动化部署。下面是一个简单的例子展示如何创建一个基于轮询算法的服务集群: ```bash # 清除现有规则 sudo ipvsadm -C # 添加服务调度器 sudo ipvsadm -A -t 192.168.x.y:80 -s rr # 注册真实服务器 sudo ipvsadm -a -t 192.168.x.y:80 -r 192.168.a.b:80 -g sudo ipvsadm -a -t 192.168.x.y:80 -r 192.168.c.d:80 -g ``` 此处 `-g` 参数表示采用直接路由模式(DR Mode),意味着客户端请求的数据包会直接发送给 RealServer,响应则绕过 Director 返回给 Client。 #### 5. 测试和服务验证 启动 Keepalived 后应检查状态以确认 Master 和 Backup 正常切换;同时利用 curl 或浏览器访问 VIP 来测试 Web 应用程序是否可以正常加载页面。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值