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

Jaystrong 2020-06-10

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 {

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;"

相关推荐