lvs配置-MySQL负载均衡
| 实验环境 | 主机名 | 主机 | IP地址 |
|---|---|---|---|
| centos7 | client | 客户端 | 192.168.88.131 |
| centos7 | lvs | lvs服务器 | DIP:192.168.88.128 VIP:192.168.88.250/32 |
| centos7 | rs1 | RS1 | VIP:192.168.88.250/32 RIP:192.168.88.129 |
| centos7 | rs2 | RS2 | VIP:192.168.88.250/32 RIP:192.168.88.130 |
首先配置好DR模式
配置IP地址
//配置client的IP地址
[root@client ~]# hostname -I
192.168.88.131
//配置lvs服务器
[root@lvs ~]# hostname -I
192.168.88.128
//配置lvs的VIP地址
[root@lvs ~]# ip addr add 192.168.88.250/32 dev lo
//配置rs的RIP
[root@rs1 ~]# hostname -I
192.168.88.129
[root@rs2 ~]# hostname -I
192.168.88.130
//修改RS网卡内核参数
[root@rs1 ~]# vim /etc/sysctl.conf
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.conf.all.arp_ignore = 1 //添加以下两行
net.ipv4.conf.all.arp_announce = 2
[root@rs1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
//配置RS的vip
[root@rs1 ~]# ip addr add 192.168.88.250/32 dev lo
//rs2同上
//配置路由信息:lvs和rs都要配
[root@lvs ~]# route add -host 192.168.88.250 dev lo
[root@rs1 ~]# route add -host 192.168.88.250 dev lo
[root@rs2 ~]# route add -host 192.168.88.250 dev lo
//lvs添加规则
[root@lvs ~]# yum -y install ipvsadm
[root@lvs ~]# ipvsadm -A -t 192.168.88.250:3306 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.88.250:3306 -r 192.168.88.129:3306 -g
[root@lvs ~]# ipvsadm -a -t 192.168.88.250:3306 -r 192.168.88.130:3306 -g
[root@lvs ~]# ipvsadm -S > /etc/sysconfig/ipvsadm
配置数据库
//安装数据库
[root@rs1 ~]# yum -y install mariadb*
//设置开机自动启动
[root@rs1 ~]# systemctl enable --now mariadb.service
//在RS1上创建数据库
[root@rs1 ~]# mysql -e 'create database RS1;'
//数据库授权,rs1和rs2都要做
[root@rs1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to root@'192.168.88.%' identified by '12321';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
在client上验证
[root@client ~]# yum -y install mariadb
[root@client ~]# for i in $(seq 5);do mysql -uroot -p12321 -h192.168.88.250 -e 'show databases;';done
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
+--------------------+
| Database |
+--------------------+
| information_schema |
| RS1 |
| mysql |
| performance_schema |
| test |
+--------------------+
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
+--------------------+
| Database |
+--------------------+
| information_schema |
| RS1 |
| mysql |
| performance_schema |
| test |
+--------------------+
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
本文详细介绍了如何通过lvs在Linux系统中配置DR模式,进行MySQL数据库的负载均衡。首先,讲述了如何设置DR模式的lvs配置,接着是IP地址的配置,然后详细说明了数据库的配置步骤。最后,在客户端进行了验证,确保负载均衡的正确实施。

1864

被折叠的 条评论
为什么被折叠?



