关闭

ubuntu下LVS + Keepalived 实现mysql高可用负载均衡实现

标签: mysql负载均衡ubuntuserverdelayauthentication
699人阅读 评论(0) 收藏 举报
分类:

 

LVS Server   : 192.85.1.5
Virtual IP   : 192.85.1.10
Real Server1 : 192.85.1.4
Real Server2 : 192.85.1.9
在 LVS Server 上安装 ipvsadm 和 keepalived。
$ sudo apt-get install ipvsadm keepalived
使用这两个做配合时,无需配置 ipvsadm,直接修改 keepalived.conf 即可。
$ sudo vim /etc/keepalived/keepalived.conf 

global_defs {
    router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100   #100为从服务器,主服务器为:101     
    advert_int 1        

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        192.85.1.10
    }
}

virtual_server 192.85.1.10 3306{
    delay_loop 1        # 每隔 1 秒查询 RealServer 状态
    lb_algo wrr         # LVS 算法
    lb_kind DR          # Direct Route
    #persistence_timeout 60 # 同一 IP 在 60 秒内分配到同一台 RealServer
    protocol TCP        # 使用 TCP 协议检查 RealServer 状态

    real_server 192.85.1.4 3306{
        weight 3    # 权重
        TCP_CHECK {
            connect_timeout 10 # 10 秒无响应超时
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }

    real_server 192.85.1.9 3306 {
        weight 3    
        TCP_CHECK {
            connect_timeout 10 
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}}
启动 keepalived 服务。
$ sudo service keepalived start #应该先启动mysql集群
 
检查 ipvsadm 设置。
$ sudo ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.85.1.10:3306 wrr
  -> 192.85.1.4:3306           Route   3      0          0         
  -> 192.85.1.9:3306           Route   3      0          0 
 
在所有 RealServer /etc/rc.local 中添加配置信息后重启
$ sudo nano /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

ifconfig lo:0 192.85.1.10 netmask 255.255.255.255 broadcast 192.85.1.10 up
route add -host 192.85.1.10 dev lo:0

echo "0" > /proc/sys/net/ipv4/ip_forward
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

exit 0
 
这时我们就可以进行测试了。在Real Server1 上,创建数据库:clustertest,
sudo /usr/local/mysql/bin/mysql -u root -p
密码默认为空
mysql> GRANT ALL ON clustertest.* TO 'zichen'@'$' IDENTIFIED BY '62672000';
mysql> FLUSH PRIVILEGES;
mysql> CREATE DATABASE clustertest;
mysql> USE clustertest;
mysql> CREATE TABLE test(id INT) ENGINE=NDBCLUSTER;
mysql> INSERT INTO test VALUES(1);
mysql> quit;
在 Real Server2上
sudo /usr/local/mysql/bin/mysql -u root -p
密码默认为空
mysql> GRANT ALL ON clustertest.* TO 'zichen'@'$' IDENTIFIED BY '62672000';
mysql> FLUSH PRIVILEGES;
mysql> quit;
 
在192.85.1.4上,在终端输入:sudo /usr/local/mysql/bin/mysql -h 192.85.1.10 -u zichen -p
enter password:62672000
正常情况下,应该能进入如下界面:
mysql >
mysql > use clustertest;
mysql > show tables;
mysql > select * from test;
mysql > insert into test values (2);
在两个SQL节点上查看数据是否同步。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7162次
    • 积分:175
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章存档