Keepalived实现双主的LVS-DR模式

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived实现双主的LVS-DR模式相关的知识,希望对你有一定的参考价值。

ka1的全局配置

[root@ka1 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     123456@163.com
   }
   notification_email_from 123456@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1.example.com
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

include /etc/keepalived/conf.d/*.conf
[root@ka1 ~]# 

ka1的子配置文件

[root@ka1 ~]# cat /etc/keepalived/conf.d/m1.conf 
vrrp_instance m1 {
    state MASTER
    interface ens33
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
      10.0.0.100/24 dev ens33 label ens33:1
    }
}
virtual_server 10.0.0.100 80 {
      delay_loop 3
      lb_algo rr
      lb_kind DR
      protocol TCP
      sorry_server 127.0.0.1 80
      real_server 10.0.0.40 80 {
      weight 1
      HTTP_GET {
         url {
            path /
            status_code 200
}
           connect_timeout 3
           nb_get_retry 3
           delay_before_retry 3
}
}
     real_server 10.0.0.50 80 {
         weight 1
     HTTP_GET {
           url { 
             path /
             status_code 200
}
         connect_timeout 3
         nb_get_retry 3
         delay_before_retry 3
}
}
}
[root@ka1 ~]# 
[root@ka1 ~]# cat /etc/keepalived/conf.d/m2.conf 
vrrp_instance m2 {
    state BACKUP
    interface ens33
    virtual_router_id 88
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
      10.0.0.200/24 dev ens33 label ens33:2
    }
}
virtual_server 10.0.0.200 80 {
      delay_loop 3
      lb_algo rr
      lb_kind DR
      protocol TCP
      sorry_server 127.0.0.1 80
      real_server 10.0.0.40 80 {
      weight 1
      HTTP_GET {
         url {
            path /
            status_code 200
}
           connect_timeout 3
           nb_get_retry 3
           delay_before_retry 3
}
}
     real_server 10.0.0.50 80 {
         weight 1
     HTTP_GET {
           url { 
             path /
             status_code 200
}
         connect_timeout 3
         nb_get_retry 3
         delay_before_retry 3
}
}
}
[root@ka1 ~]# 

ka2的全局配置

[root@ka2 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     123456@163.com
   }
   notification_email_from 123456@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1.example.com
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

include /etc/keepalived/conf.d/*.conf
[root@ka2 ~]# 

ka2的子配置文件

[root@ka2 ~]# cat /etc/keepalived/conf.d/m1.conf 
vrrp_instance m1 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
      10.0.0.100/24 dev ens33 label ens33:1
    }
}
virtual_server 10.0.0.100 80 {
      delay_loop 3
      lb_algo rr
      lb_kind DR
      protocol TCP
      sorry_server 127.0.0.1 80
      real_server 10.0.0.40 80 {
      weight 1
      HTTP_GET {
         url {
            path /
            status_code 200
}
           connect_timeout 3
           nb_get_retry 3
           delay_before_retry 3
}
}
     real_server 10.0.0.50 80 {
         weight 1
     HTTP_GET {
          url {
              path /
              status_code 200
}
         connect_timeout 3
         nb_get_retry 3
         delay_before_retry 3
}
}
}
[root@ka2 ~]# 
[root@ka2 ~]# cat /etc/keepalived/conf.d/m2.conf 
vrrp_instance m2 {
    state MASTER
    interface ens33
    virtual_router_id 88
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
      10.0.0.200/24 dev ens33 label ens33:2
    }
}
virtual_server 10.0.0.200 80 {
      delay_loop 3
      lb_algo rr
      lb_kind DR
      protocol TCP
      sorry_server 127.0.0.1 80
      real_server 10.0.0.40 80 {
      weight 1
      HTTP_GET {
         url {
            path /
            status_code 200
}
           connect_timeout 3
           nb_get_retry 3
           delay_before_retry 3
}
}
     real_server 10.0.0.50 80 {
         weight 1
     HTTP_GET {
          url {
              path /
              status_code 200
}
         connect_timeout 3
         nb_get_retry 3
         delay_before_retry 3
}
}
}
[root@ka2 ~]# 

查看IP

[root@ka1 ~]# hostname -I
10.0.0.10 10.0.0.100 
[root@ka1 ~]# 

[root@ka2 ~]# hostname -I
10.0.0.20 10.0.0.200 
[root@ka2 ~]# 

ka1主机故障,测试VIP漂移至ka2主机

[root@ka1 ~]# killall keepalived
[root@ka1 ~]# hostname -I
10.0.0.10 
[root@ka1 ~]# 

[root@ka2 ~]# hostname -I
10.0.0.20 10.0.0.200 10.0.0.100 
[root@ka2 ~]#

恢复ka1主机

[root@ka1 ~]# systemctl start keepalived 
[root@ka1 ~]# hostname -I
10.0.0.10 10.0.0.100 
[root@ka1 ~]# 

[root@ka2 ~]# hostname -I
10.0.0.20 10.0.0.200 
[root@ka2 ~]# 

 

以上是关于Keepalived实现双主的LVS-DR模式的主要内容,如果未能解决你的问题,请参考以下文章

双主模型高可用负载均衡集群的实现(keepalived+lvs-dr)

LVS-DR+Keepalived+httpd(双主)

Keepalived+MySQL 8.0.17 实现MySQL双主的高可用

Keepalived+MySQL 8.0.17 实现MySQL双主的高可用

项目实战03:Keepalived 实现高可用

Mysq+Haproxy+Keepalived高可用