lvs+keepalived 02

Posted 答案

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lvs+keepalived 02相关的知识,希望对你有一定的参考价值。

LVS keepalived 高可用负载均衡

  1. 环境

IP

HOSTNAME

Describe

192.168.100.30

lvs01

主负载

192.168.100.31

lvs02

备负载

192.168.100.40

 

VIP

192.168.100.29

node01

节点

192.168.100.28

node02

节点

192.168.100.27

node03

节点

 

  1. 安装

yum -y install ipvsadm keepalived

 

  1. 主负载配置

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

   vrrp_skip_check_adv_addr

   vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.100.40/24 dev eth0 label eth0:0

    }

}

 

virtual_server 192.168.100.40 3306 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.100.27 3306 {

        weight 1

         MISC_CHECK {

         misc_path "/etc/keepalived/check.sh 192.168.100.27"

         misc_dynamic

         }

    }

    real_server 192.168.100.28 3306 {

        weight 1

         MISC_CHECK {

         misc_path "/etc/keepalived/check.sh 192.168.100.28"

         misc_dynamic

         }

    }

    real_server 192.168.100.29 3306 {

        weight 1

         MISC_CHECK {

         misc_path "/etc/keepalived/check.sh 192.168.100.29"

         misc_dynamic

         }

    }

}

  1. 备负载配置

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL_BACKUP

   vrrp_skip_check_adv_addr

   vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 50

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.100.40/24 dev eth0 label eth0:0

    }

}

 

virtual_server 192.168.100.40 3306 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

protocol TCP

 

    real_server 192.168.100.27 3306 {

        weight 1

         MISC_CHECK {

         misc_path "/etc/keepalived/check.sh 192.168.100.27"

         misc_dynamic

         }

    }

 

    real_server 192.168.100.28 3306 {

        weight 1

         MISC_CHECK {

         misc_path "/etc/keepalived/check.sh 192.168.100.28"

         misc_dynamic

         }

    }

    real_server 192.168.100.29 3306 {

        weight 1

         MISC_CHECK {

         misc_path "/etc/keepalived/check.sh 192.168.100.29"

         misc_dynamic

         }

    }

}

 

 

/etc/keepalived/check.sh

#!/bin/bash

 

mysql -uwsrep -p123456 -h$1 -e "select 1;" &>/dev/null

ret=$?

if [ $ret -eq 0 ];then

    exit 0

else

    exit 1

fi

 

  1. 启动服务

/etc/init.d/keepalived start

/etc/init.d/ipvsadm start

  1. 测试

关闭主负载keepalived,VIP漂移到备负载

/etc/init.d/keepalived stop

启动主负载keepalived,VIP漂移回来

/etc/init.d/keepalived start

以上是关于lvs+keepalived 02的主要内容,如果未能解决你的问题,请参考以下文章

Keepalive工作原理

LVS-keepalived高可用

keepalived入门与掌握之keepalive+lvs实例部署

高可用服务之Keepalived高可用LVS集群

lvs+keepalived+nginx环境部署

HTTP keepalive详解