一、简介
1. Keepalived
- Keepalived 是一个基于 VRRP (虚拟路由冗余协议)可用来实现服务高可用性的软件方案,避免出现单点故障。Keepalived一般用来实现轻量级高可用性,且不需要共享存储,一般用于两个节点之间,常见有LVS+Keepalived、Nginx+Keepalived组合。
2. LVS
-
LVS (Linux Virtual Server)是一个高可用性虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。
LVS主要用于多服务器的负载均衡,作用于网络层。LVS构建的服务器集群系统中,前端的负载均衡层被称为Director Server;后端提供服务的服务器组层被称为Real Server。通过下图可以大致了解LVS的基础架构。 - 本文中将利用LVS实现MySQL的读写负载均衡,Keepalived避免节点出现单点故障。
3. MySQL双主复制
二、安装配置
- 192.168.245.66:Keepalived + LVS Master
192.168.245.88:Keepalived + LVS Slave
192.168.245.22:MySQL Replication Master
192.168.245.33:MySQL Replication Master
192.168.245.100:VIP(虚拟IP)
192.168.245.11:Web服务器(测试用)
三、服务器192.168.245.66配置Keepalived + LVS Master
1. 安装LVS
yum -y install ipvsadm
2. 下载安装Keepalived
yum install -y gcc
yum install -y kernel-devel openssl openssl-devel
wget -q http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
tar -zxvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure && make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
chkconfig --add keepalived
chkconfig --level 345 keepalived on
3. Keepalived配置文件修改
vim /etc/keepalived/keepalived.conf
- 修改内容如下
! Configuration File for keepalived
global_defs {
router_id LVS1
}
vrrp_instance VI_1 {
state MASTER #指定instance初始状态,实际根据优先级决定.backup节点不一样
interface ens33 #虚拟IP所在网卡通过 ifconfig可以查看
virtual_router_id 51 #VRID,相同VRID为一个组,决定多播MAC地址
priority 100 #优先级,另一台改为90.backup节点不一样
advert_int 1 #检查间隔
authentication {
auth_type PASS #认证方式,可以是pass或ha
auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.245.100 #VIP
}
}
virtual_server 192.168.245.100 3306 {
delay_loop 6 #服务轮询的时间间隔
lb_algo wrr #加权轮询调度,LVS调度算法 rr|wrr|lc|wlc|lblc|sh|sh
lb_kind DR #LVS集群模式 NAT|DR|TUN,其中DR模式要求负载均衡器网卡必须有一块与物理网卡在同一个网段
#nat_mask 255.255.255.0
persistence_timeout 50 #会话保持时间
protocol TCP #健康检查协议
## Real Server设置,3306就是MySQL连接端口
real_server 192.168.245.22 3306 {
weight 3 ##权重
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.245.33 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
4. 启动Keepalived
systemctl start keeplived
5. 查看虚拟网卡添加是否成功
ip a
- 如图会出现虚拟网卡
四、服务器192.168.245.88配置Keepalived + LVS Slave
1. 安装LVS
yum -y install ipvsadm
2. 下载安装Keepalived
yum install -y gcc
yum install -y kernel-devel openssl openssl-devel
wget -q http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
tar -zxvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure && make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
chkconfig --add keepalived
chkconfig --level 345 keepalived on
3. Keepalived配置文件修改
vim /etc/keepalived/keepalived.conf
- 修改内容如下
! Configuration File for keepalived
global_defs {
router_id LVS1
}
vrrp_instance VI_1 {
state BACKUP #指定instance初始状态,实际根据优先级决定.backup节点不一样
interface ens33 #虚拟IP所在网卡通过 ifconfig可以查看
virtual_router_id 51 #VRID,相同VRID为一个组,决定多播MAC地址
priority 90 #优先级,另一台改为90.backup节点不一样
advert_int 1 #检查间隔
authentication {
auth_type PASS #认证方式,可以是pass或ha
auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.245.100 #VIP
}
}
virtual_server 192.168.245.100 3306 {
delay_loop 6 #服务轮询的时间间隔
lb_algo wrr #加权轮询调度,LVS调度算法 rr|wrr|lc|wlc|lblc|sh|sh
lb_kind DR #LVS集群模式 NAT|DR|TUN,其中DR模式要求负载均衡器网卡必须有一块与物理网卡在同一个网段
#nat_mask 255.255.255.0
persistence_timeout 50 #会话保持时间
protocol TCP #健康检查协议
## Real Server设置,3306就是MySQL连接端口
real_server 192.168.245.22 3306 {
weight 3 ##权重
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.245.33 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
4. 启动Keepalived
systemctl start keeplived
5. 查看虚拟网卡添加是否成功
ip a
- 由于Master上已经有虚拟网卡了,Slave上是不显示的,只有当Master出现故障时Slave才会有
五、在数据服务器编写RealServer的网络配置脚本
-
两个数据服务器192.168.245.22和192.168.245.33进行同样操作。
1.添加RealServer脚本
vim /etc/init.d/realserver
#!/bin/sh
VIP=192.168.245.100
. /etc/rc.d/init.d/functions
case "$1" in
# 禁用本地的ARP请求、绑定本地回环地址
start)
/sbin/ifconfig lo down
/sbin/ifconfig lo up
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
/sbin/sysctl -p >/dev/null 2>&1
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up # 在回环地址上绑定VIP,设定掩码,与Direct Server上自身的IP保持通信
/sbin/route add -host $VIP dev lo:0
echo "LVS-DR real server starts successfully.\n"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP >/dev/null 2>&1
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
echo "LVS-DR real server stopped.\n"
;;
status)
isLoOn=`/sbin/ifconfig lo:0 | grep "$VIP"`
isRoOn=`/bin/netstat -rn | grep "$VIP"`
if [ "$isLoON" == "" -a "$isRoOn" == "" ]; then
echo "LVS-DR real server has run yet."
else
echo "LVS-DR real server is running."
fi
exit 3
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
2. 修改脚本权限并启动realserver
chmod +x /etc/init.d/realserver
echo "/etc/init.d/realserver" >> /etc/rc.d/rc.local
service realserver start
3. 查看虚拟网卡
ip a
- 出现虚拟网卡
六、查看LVS集群状态
ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.245.100:3306 wrr persistent 50
-> 192.168.245.22:3306 Route 1 0 0
-> 192.168.245.33:3306 Route 1 0 0
七、测试
在WEB服务器(192.168.245.11)上,添加测试文件mysql.php
<?php
/**
* Created by PhpStorm.
* User: wen
* Date: 7/22/2020
* Time: 5:00 PM
*/
$servername = "192.168.245.100";
$username = "root";
$password = "123123";
$dbname = "test";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}{
echo "连接成功";
}
$sql = "INSERT INTO activity (admin_id, user_id, name)
VALUES ('1', '122', 'keepalived1')";
if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
- 在浏览器访问 http://192.168.245.11/mysql.php 即可进行测试。
网友评论