LVS+keepalived做Mysql集群的负载均衡

Posted

tags:

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

LVS+keepalived做mysql集群的负载均衡:

注:
1、LVS本身不对node做健康检查的,所以要借助keepalived
2、根据配置当某node down掉会有几秒判断时间,这点请注意。

负载均衡端:

yum install ipvsadm keepalived ipset-devel

注:
keepalived的log在/var/log/message里

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {br/>aa@ming.com
}
notification_email_from aa@ming.com
smtp_server 10.0.0.10
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr

vrrp_strict (这行一定要注掉,不注掉vip可能会ping不通)

vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER
interface eno1 (这里网卡名要写对)
virtual_router_id 51
priority 100
advert_int 3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.1.10
}
}

virtual_server 10.0.1.10 3306 {
delay_loop 6
lb_algo rr
lb_kind DR (此处需改为DR)
persistence_atimeout 10
protocol TCP

real_server 10.0.1.2 3306 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
    }

}

real_server 10.0.1.3 3306 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
        retry 3
        delay_before_retry 1
        connect_port 3306
    }

}

real_server 10.0.1.4 3306 {
    weight 1
    TCP_CHECK {
        connect_timeout 3
        retry 3
        delay_before_retry 1
        connect_port 3306
    }

}

}

:wq

systemctl restart keepalived

systemctl enable keepalived

systemctl status keepalived

ip a (检查eno1是不是多了个10.0.1.10的vip,这个ip在ifconfig是看不到的,要确保可以ping通)

ipvsadm -L --stats (查看各node分配情况)

Mysql node端(所有node都做):

ifconfig lo:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10

route add -host 10.0.1.10 dev lo:0

vi /etc/sysctl.conf

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

:wq

sysctl -p

ifconfig lo:0

route -n

验证(可以看到访问的为不同node):

mysql -u aa -p‘xxxx‘ -h 10.0.1.10 -e "select @@hostname;"


以上是关于LVS+keepalived做Mysql集群的负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

lvs keepalived做DNS集群负载

LVS&Keepalived—集群负载均衡企业高可用详解

LVS+keepalived高可用负载均衡集群部署---LAMP网站服务器与LVS服务器

LVS+keepalived高可用负载均衡集群部署 ----数据库的读写分离

负载均衡集群-Lvs

MySQL集群搭建-MMM+LVS+Keepalived