当前位置: 首页 > MySQL数据库

怎么搭建LVS+Keepalived+MySQL

时间:2026-01-28 14:10:38

LVS负责负载均衡转发请求,keepalived负责检查LVS的realserver状态,及时remove失效节点、add复活节点。
本实验主要功能测试,所以只准备了两台MySQL服务器,仅为简单说明问题。
vm1 10.0.0.11        master   server_id 11
vm2 10.0.0.12        slave         server_id 12

Director:
vm3  10.0.0.14  

VIP: 10.0.0.20

1 在Directory server vm3 上安装ipvsadm和keepalived
yum install ipvsadm
安装keepalived
yum install libnfnetlink*
tar -zxvf keepalived-1.2.7.tar.gz
./configure --prefix=/usr/local/keepalived
make && make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived

2 vm1 vm2上,执行以下操作.
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  

ifconfig lo:0 10.0.0.20 netmask 255.255.255.255

3 在Directory server vm3上编辑/etc/keepalived/keepalived.conf文件,内容如下:

点击(此处)折叠或打开

    global_defs {

    router_id HaMySQL_1

    }

    vrrp_sync_group VGM {

    group {

    VI_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.0.20

    }

    }

    virtual_server 10.0.0.20 3306 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    protocol TCP

    nat_mask 255.255.255.0

    persistence_timeout 10

    real_server 10.0.0.11 3306 {

    weight 3

    TCP_CHECK {

    connect_timeout 3

    nb_get_retry 3

    delay_before_retry 3

    connect_port 3306

    }

    }

    real_server 10.0.0.12 3306 {

    weight 3

    TCP_CHECK {

    connect_timeout 3

    nb_get_retry 3

    delay_before_retry 3

    connect_port 3306

    }

    }

    }


4 启动MySQL和keepalived。
vm1:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3306/my.cnf &

vm2:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3306/my.cnf

vm3:
/etc/init.d/keepalived start

5 验证
vm3:
[root@vm3 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.20:3306 rr persistent 2
  -> 10.0.0.11:3306               Route   3      1          0
  -> 10.0.0.12:3306               Route   3      1          0

可以看到负载均衡已启动,vm2 vm3都已加进来了。
从客户端连接VIP 10.0.0.20:3306
C:\mysql-5.7.11-winx64\mysql-5.7.11-winx64\bin>mysql.exe -uroot -pmysql -h20.0.0.20 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 90
Server version: 5.6.27-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@server_id;exit;
+-------------+
| @@server_id |
+-------------+
|          11 |
+-------------+
1 row in set (0.00 sec)

从server_id可以看出客户端连接的是vm1。

关掉vm1上的MySQL服务。
[root@vm3 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.20:3306 rr persistent 2
  -> 10.0.0.12:3306               Route   3      1          0


LVS规则已经更新,remove了vm1,再次从客户端连接。
C:\mysql-5.7.11-winx64\mysql-5.7.11-winx64\bin>mysql.exe -uroot -pmysql -h20.0.0.20 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.6.27-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@server_id;exit;
+-------------+
| @@server_id |
+-------------+
|          12 |
+-------------+
1 row in set (0.00 sec)

重新启动vm1上的MySQL服务。
[root@vm3 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.20:3306 rr persistent 2
  -> 10.0.0.11:3306               Route   3      1          0
  -> 10.0.0.12:3306               Route   3      1          1

LVS规则已自动刷新,vm1重新被加入。


上一篇:MySQL集群NDB 7.5有哪些知识点
下一篇:MySQL高可用架构在业务层面举例分析
lvs mysql keepalived
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素