欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

Ubuntu下LVS + Keepalived 实现MySQL高可用负载均衡实现

程序员文章站 2024-02-08 23:55:16
...

在192.85.1.4上,在终端输入:sudo /usr/local/mysql/bin/mysql -h 192.85.1.10 -u zichen -penter password:62672000正常情况下

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节点上查看数据是否同步。

Ubuntu下LVS + Keepalived 实现MySQL高可用负载均衡实现