LVS同步连接状态信息(IPVS Connection Synchronization)

在使用LVS负载均衡器的时候,通常使用主备(master-backup)模式,防止负载均衡器单点故障。

大多数情况使用keepalived+lvs完成架构

请参考

CentOS7 Keepalived+LVS 负载均衡 后台节点健康检查

LVS-NAT(Virtual Server via NAT)原理说明与配置用例

LVS-DR(Virtual Server via Direct Routing)原理说明与配置用例

LVS-TUN(Virtual Server via IP Tunneling)原理说明与配置用例

CentOS7 下配置Keepalived为系统服务,开机自动启动。

 

由于LVS负载均衡器需要保存大量的连接信息,记录每个TCP连接由哪台真实服务器处理。

[root@localhost keepshell]# ipvsadm -L -n -c
IPVS connection entries
pro expire state       source             virtual            destination
TCP 14:58  ESTABLISHED 192.168.80.1:57622 192.168.80.138:8080 192.168.80.135:8080
TCP 14:58  ESTABLISHED 192.168.80.1:57624 192.168.80.138:8080 192.168.80.135:8080
TCP 14:58  ESTABLISHED 192.168.80.1:57621 192.168.80.138:8080 192.168.80.136:8080
TCP 14:58  ESTABLISHED 192.168.80.1:57625 192.168.80.138:8080 192.168.80.136:8080
TCP 14:58  ESTABLISHED 192.168.80.1:57623 192.168.80.138:8080 192.168.80.136:8080
TCP 14:58  ESTABLISHED 192.168.80.1:57626 192.168.80.138:8080 192.168.80.135:8080

 

当主负载均衡器宕机以后,备机提升为主,但备机缺省没有这些连接信息,会导致客户端的连接失效,为了解决这一问题,LVS在Linux内核实现了同步连接信息的功能.

ipvsadm -L --daemon  查看同步进程信息

ipvsadm --start-daemon master|backup --mcast-interface=网卡名称 --syncid 编号,主备需要一致

ipvsadm --stop-daemon master|backup 停止同步
ipvsadm -L -n -c 查看连接状态信息

 

 

下面给出配置

keepalived.conf

 

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from root@localhost  
   smtp_server localhost  
   smtp_connect_timeout 30  
   router_id  NodeA
}


vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 100
    nopreempt 
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.138
    }
    notify_master /home/keepshell/notify_master.sh
    notify_backup /home/keepshell/notify_backup.sh
    notify_fault  /home/keepshell/notify_fault.sh
    notify_stop   /home/keepshell/notify_stop.sh
}

virtual_server 192.168.80.138 8080 { 
   delay_loop 6                  
   lb_algo rr                     
   lb_kind DR                          
   persistence_timeout 0           
   protocol TCP                    
   real_server 192.168.80.135 8080 {   
       weight 1                      
        HTTP_GET {
            url { 
              path /index.jsp
              digest 5cce221db9752be2116860efb866144e
            }
            connect_timeout 5
            nb_get_retry 3
            delay_before_retry 3
        }       
   }

   real_server 192.168.80.136 8080 {    
       weight 1
        HTTP_GET {
            url { 
              path /index.jsp
              digest 345e829e9a900a87a8fce740ef243198
            }
            connect_timeout 5
            nb_get_retry 3
            delay_before_retry 3
        }        
   }
}

 

 

 

 

 

 

notify_master.sh

 

#!/bin/bash
echo $(date "+%Y-%m-%d %H:%M:%S") "The keepalived service is master." >> /home/keepshell/gexin.txt
ipvsadm --stop-daemon backup
ipvsadm --start-daemon master --mcast-interface=eno16777736 --syncid 1


notify_backup.sh

 

 

 

 

#!/bin/bash
echo $(date "+%Y-%m-%d %H:%M:%S") "The keepalived service is backup." >> /home/keepshell/gexin.txt
ipvsadm --stop-daemon master
ipvsadm --start-daemon backup --mcast-interface=eno16777736 --syncid 1


notify_stop.sh

 

 

 

 

#!/bin/bash
echo $(date "+%Y-%m-%d %H:%M:%S") "The keepalived service is stop." >> /home/keepshell/gexin.txt
ipvsadm --stop-daemon master
ipvsadm --stop-daemon backup

 

 

技术交流群:212320390

LVS同步连接状态信息(IPVS Connection Synchronization)_第1张图片

 

 

 

你可能感兴趣的:(Linux)