通过lvs+keepalived实现对mysql的负载均衡高可用

时间:2021-09-03 23:00:53

1、摘要

本文主要通过lvs+keepalived实现对mysql的负载均衡高可用。

keepalived是基于VRRP协议(虚拟路由冗余协议),目的是模拟路由器双机。


2、安装

1)、软件包准备

ipvsadm-1.26.tar.gz

mysql-5.5.28.tar.gz

cmake-2.8.11.2.tar.gz

keepalived-1.2.13.tar.gz


2)、配置Director Server

a、检查kernel是否已经支持LVS的ipvs模块

# modprobe -l |grep ipvs

kernel/net/netfilter/ipvs/ip_vs.ko

kernel/net/netfilter/ipvs/ip_vs_rr.ko

kernel/net/netfilter/ipvs/ip_vs_wrr.ko

kernel/net/netfilter/ipvs/ip_vs_lc.ko

kernel/net/netfilter/ipvs/ip_vs_wlc.ko

kernel/net/netfilter/ipvs/ip_vs_lblc.ko

kernel/net/netfilter/ipvs/ip_vs_lblcr.ko

kernel/net/netfilter/ipvs/ip_vs_dh.ko

kernel/net/netfilter/ipvs/ip_vs_sh.ko

kernel/net/netfilter/ipvs/ip_vs_sed.ko

kernel/net/netfilter/ipvs/ip_vs_nq.ko

kernel/net/netfilter/ipvs/ip_vs_ftp.ko

kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko


b、在Director Server中安装LVS

# ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux#解决ipvsadm找不到内核

# yum install -y libnl*

# yum install -y popt*

# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm#前三步是解决安装ipvsadm-1.26报错的软件包

# tar zxvf ipvsadm-1.26.tar.gz

# cd ipvsadm-1.26

# make

# make install

# ipvsadm --help#出现ipvsadm帮助提示表示ipvsadm已经安装成功


c、在Director Server中配置VIP,route、路由转发等

vim /etc/init.d/lvsDR

#!/bin/sh

# description: Start LVS of Director server

VIP=10.0.2.200

chmod 755 /etc/rc.d/init.d/functions

/etc/rc.d/init.d/functions

case "$1" in

    start)

# set the Virtual IP Address and sysctl parameter

/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up

/sbin/route add -host $VIP dev eth0:0

echo "1" >/proc/sys/net/ipv4/ip_forward 

;;

    stop)

        echo "0" >/proc/sys/net/ipv4/ip_forward

/sbin/route del -host $VIP dev eth0:0

        /sbin/ifconfig eth0:0 down

        ;;

    *)

        echo "Usage: $0 {start|stop}"

        exit 1

esac

# chmod 755 /etc/init.d/lvsDR

# /etc/init.d/lvsDR start


d、在Director Server中安装配置keepalived

# yum install openssl* -y

# tar zxvf keepalived-1.2.13.tar.gz

# cd keepalived-1.2.13

# ./configure --prefix=/usr/local/keepalived 

# make && make install

# mv /usr/local/keepalived/etc/keepalived/keepalived.conf /usr/local/keepalived/etc/keepalived/keepalived.conf.bak

# vim /usr/local/keepalived/etc/keepalived/keepalived.conf


global_defs {  

router_id LVS_MySQL  

}  

 

vrrp_instance VI_MYSQL {  

state MASTER  

interface eth0  

virtual_router_id 100  

priority 100  

advert_int 1  

authentication {  

auth_type PASS  

auth_pass 1111  

}  

virtual_ipaddress {  

10.0.2.200

}  

}  

 

virtual_server 10.0.2.200 3306 {  

delay_loop 6  

lb_algo rr  

lb_kind DR  

# nat_mask 255.255.0.0  

# persistence_timeout 50  

protocol TCP  

 

real_server 10.0.2.203 3306 {  

weight 3  

TCP_CHECK {  

connect_timeout 3  

nb_get_retry 3  

delay_before_retry 3  

connect_port 3306  

}  

}  

 

real_server 10.0.2.204 3306 {  

weight 3  

TCP_CHECK {  

connect_timeout 3  

nb_get_retry 3  

delay_before_retry 3  

connect_port 3306  

}  

}  

}  

# 启动keepalived

/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf


# 备注:在BACKUP Director Server上安装配置keepalived同MASTER Director Server配置基本一样,需要修改的地方在keepalived配置文件中,将state MASTER => state BACKUP,priority 100 => priority 99


3)、配置Real Server

a、mysql的安装以及主从配置参考“MySQL同步复制”一文


b、配置vip和arp

# vim /etc/init.d/lvsrs

#!/bin/bash

#description : Start Real Server 

VIP=10.0.2.200

/etc/rc.d/init.d/functions

case "$1" in

start)

echo " Start LVS  of  Real Server"

/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

/sbin/route add -host $VIP dev lo:0

echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

sysctl -p

;;

stop)

/sbin/ifconfig lo:0 down

/sbin/route del -host $VIP dev lo:0

echo "close LVS Director server"

echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

;;

*)

        echo "Usage: $0 {start|stop}"

        exit 1

esac

# chmod 755 /etc/init.d/lvsrs

# /etc/init.d/lvsrs start


4)、测试


本文出自 “kimileonis” 博客,请务必保留此出处http://kimileonis.blog.51cto.com/5531747/1545377