使用 keepalived 实现无失败请求的架构

使用 keepalived 实现无失败请求的架构

我有一个由 3 个 CentOs 服务器组成的本地集群,我在每台服务器上安装了 Keepalived,然后我使用 ab 运行一些基准测试,如下所示:
ab -c 1000 -n 100000 -r host

然后在基准测试中,我关闭了主服务器,Keepalived 将浮动 IP 的所有者更改为备份服务器之一,但此过程需要一点时间,因此我有一些失败的请求。我的问题是如何最大限度地减少停机时间?有没有办法设计一些在关闭一个节点时完全没有停机时间的集群?

这是我的 keepalived 配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     user@localhost
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script health_check {
  script       "curl host"
  interval 2   # check every 2 seconds
  fall 2       # require 2 failures for KO
  rise 2       # require 2 successes for OK
}

vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass password
    }
    virtual_ipaddress {
        <host ip>
    }

    track_script {
        health_check
    }
}    

这是我的基准测试结果:

Concurrency Level:      1000
Time taken for tests:   25.502 seconds
Complete requests:      100000
Failed requests:        7618
   (Connect: 0, Receive: 2539, Length: 2539, Exceptions: 2540)
Write errors:           0
Total transferred:      13644540 bytes
HTML transferred:       2241603 bytes
Requests per second:    3921.28 [#/sec] (mean)
Time per request:       255.019 [ms] (mean)
Time per request:       0.255 [ms] (mean, across all concurrent requests)
Transfer rate:          522.50 [Kbytes/sec] received

这表明更改虚拟 IP 的所有者和处理请求几乎需要 2 秒钟。我该怎么做才能最大限度地减少这段时间,并且如果可能的话,最好不要有停机时间。

答案1

基本上,即使在硬件负载均衡器中也很难避免停机,它需要时间来检测主机是否已关闭并迁移 VIP 地址。

您可以通过调整 keepalived 心跳频率(advert_int 以秒为单位)来最大限度地减少停机时间

当备份服务器在“advert_int”选项中定义的 3 倍时间内没有收到来自 MASTER 的 VRRP 通告时,会触发从 MASTER 到 BACKUP 的故障转移。

尝试设置较低的advert_int(<1),注意不要因网络超时触发故障转移。

您可以在应用程序层设置会话持久性/复制,这样用户就不会受到故障转移的影响。

相关内容