lvs+keepalived 02

时间:2022-01-14 09:53:03

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 {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

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 {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

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