Mysql-cluster+Lvs+Keepalived实现cluster高可用

一、安装

我们在集群上做高可用是为了防止单点故障。

这里我在数据节点[192.168.1.117,192.168.1.118]上做lvs+keepalived将两台数据节点做成主备,这样就不用担心mysql单点故障了。这个我就简单的说下安装和配置以及realserver需要运行的脚本。

安装lvs只需要安装ipvsadm即可,然后再安装keepalived

[192.168.1.117,192.168.1.118]两台都需要安装

软件:

ipvsadm-1.24.tar.gz   

keepalived-1.1.20.tar.gz 

目录:/usr/src

[192.168.1.117,192.168.1.118]两台都需要安装

安装ipvsadm

[root@server117 src] tar -zxf ipvsadm-1.24.tar.gz  

[root@server117 src]#ln -s /usr/src/kernels/2.6.9-67.EL-i686/ #这步必须    要做要不然无法安装ipvsadm

[root@server117 ipvsadm-1.24]# make && make install

安装keepalived

[root@server117 src] tar -zxf keepalived-1.1.20.tar.gz 

[root@server117keepalived-1.1.20]#

./configur prefix=/usr/local/keepalived && make && make install

至此我们两个软件已经安装完毕

配置[192.168.1.117]为主

[root@sunye keepalived]# pwd

/usr/local/keepalived/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   router_id LVS_DEVEL

}

vrrp_instance VI_1 {

    state MASTER     #192.168.1.118上将这里改成BACKUP

    interface eth0

    virtual_router_id 51

    priority 100     #192.168.1.118上将权重降低为90,只要小于100即可

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.100/24

    }

}

virtual_server 192.168.1.100 3306 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

  real_server 192.168.1.120 3306 {

        weight 1

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

}

  real_server 192.168.1.120 3306 {

        weight 1

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

 }

}

配置[192.168.1.117]为备

这里与主的配置文件基本相同,只要注意下主配置文件的红色部门做下更改即可:

配置Mysql节点[192.168.1.119,192.168.1.120]脚本lvs-realserver.sh

#!/bin/bash

#description: start LVS of DirectorServer

VIP=192.168.1.100

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

case "$1" in

start)

echo "Start LVS of RealServer"

/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

;;

stop)

/sbin/ifconfig lo:0 down

echo "Stop LVS of RealServer"

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

~

至此mysql节点与keepalived节点全部配置完毕

二、启动

启动[192.168.1.117,192.168.1.118]数据节点的keepalived

[root@server117 keepalived]# /usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf

启动[192.168.1.119,192.168.1.120]SQL节点的lvs-realserver.sh

[root@server119~ ] chomd 777 lvs-realserver.sh

[root@server119~ ] ./lvs-realserver.sh start

全部启动完毕

三、测试

[root@server117 ~]# ip a

1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:0c:29:06:dc:2d brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.117/24 brd 192.168.1.255 scope global eth0

    inet 192.168.1.100/24 scope global secondary eth0

    inet6 fe80::20c:29ff:fe06:dc2d/64 scope link

       valid_lft forever preferred_lft forever

3: sit0: <NOARP> mtu 1480 qdisc noop

    link/sit 0.0.0.0 brd 0.0.0.0

[root@server117 ~]# ipvsadm -ln

IP Virtual Server version 1.2.0 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.1.100:3306 wrr persistent 50

  -> 192.168.1.119:3306           Route   1      0          0

  -> 192.168.1.120:3306           Route   1      0          0

至此,我们的mysql集群+高可用实验已经搭建完毕了,唯一不足的就是mysql建库建表没有写,其实这块主要注意建表建库的时候后面要写ndb引擎,其它的就没有什么了。如果有什么地方不足,请大家拍砖,多多指教!