Lvs+Keepalived实现MySQL高可用
LVS+Keepalived+MySQL高可用配置
本文所有配置前提是已实现MySQL双主备份(MySQL双主)
安装前的准备:
VIP:192.168.0.201
Keepalived:
Keepalived-Master:192.168.0.241
Keepalived-Backup:192.168.0.277
RealServer:
MySQL1:192.168.0.225
MySQL2:192.168.0.226
kernel-devel各版本下载地址:http://rpmfind.net/linux/rpm2html/search.php?query=kernel-devel
keepalived下载地址:http://www.keepalived.org/download.html
kernel-devel.2.6.32-431.el6.x86_64.rpm (根据系统实际内核版本)
keepalived-1.2.7.tar.gz
ipvsadm-1.26.tar.gz
依赖包 :
yum install openssl-devle popt-devel popt-static libnl*
一、软件安装
1、安装kernel-devel:
rpm -ivh kernel-devel-2.6.32-431.29.2.el6.x86_64.rpm
2、安装lvs管理工具ipsadm:
tar zxvf ipvsadm-1.26.tar.gz
cd ipvsadm
make
make install
3、安装keepalived:
tar zxvf keepalived-1.2.7.tar.gz
cd keepalived
./configure --prefix=/usr/local/keepalived
make
make install
4、将keepalived添加到系统服务:
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
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
二、配置
1、MySQL服务器中的配置 :
将realserver脚本放在/etc/init.d/目录下,执行即可:
#!/bin/sh
VIP=192.168.0.201 sh /etc/rc.d/init.d/functions case "$1" in
# 禁用本地的ARP请求、绑定本地回环地址
start)
/sbin/ifconfig lo down
/sbin/ifconfig lo up
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/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 "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/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
然后使用 ip addr 命令查看VIP是否绑定:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 192.168.0.201/32 brd 192.168.0.201 scope global lo:0
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:59:f1:3f brd ff:ff:ff:ff:ff:ff
inet 192.168.0.225/24 brd 192.168.0.255 scope global eth0
inet6 fe80::5054:ff:fe59:f13f/64 scope link
valid_lft forever preferred_lft forever
可以看到vip已经绑定在了lo上。两台msyql服务器执行同样的操作。
2、Keepalived的配置:
2.1、首先关闭系统防火墙iptables和selinux
2.2、开启路由转发及非本机ip绑定支持功能:
echo 1 >/proc/sys/net/ipv4/ip_forward echo 1 >/proc/sys/net/ipv4/ip_nonlocal_bind
2.3、修改Keepalived的配置文件/etc/keepalived/keepalived.conf:
! Configuration File for keepalived global_defs {
notification_email {
123@sina.com
}
notification_email_from 321@sina.com
smtp_server 192.168.0.241
smtp_connect_timeout 30
router_id LVS1
} vrrp_instance VI_1 {
state BACKUP #角色,若都为BACKUP,则表示当其中一台挂掉时,以另一台为MASTER,(即不会主动抢VIP)
interface eth0 #主备之间健康检查的网卡
virtual_router_id 51 #主备id相同
priority 100
advert_int 1
authentication {
auth_type PASS #主备之间通信认证
auth_pass 0711
}
virtual_ipaddress {
192.168.0.201
}
} virtual_server 192.168.0.201 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP real_server 192.168.0.225 3306 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_prot 3306
}
}
real_server 192.168.0.226 3306 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_prot 3306
}
}
}
keepalived的Master和Backup配置文件不同的是,
Master: state为MASTER,priority的值比BACKUP大。
BACKUP:state为BACKUP,priority的值比MASTER小。
2.4、启动Keepalived:
service keepalived start
然后使用ipvsadm工具查看集群列表:
主、备结果一样。
[root@localhost ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.201:3306 rr persistent 50
-> 192.168.0.225:3306 Route 1 0 0
-> 192.168.0.226:3306 Route 1 0 0
查看ip地址情况:
MASTER:
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: p4p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 64:00:6a:34:c3:e2 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.241/24 brd 192.168.0.255 scope global p4p1
inet 192.168.0.201/32 scope global p4p1
inet6 fe80::6600:6aff:fe34:c3e2/64 scope link
valid_lft forever preferred_lft forever
BACKUP:
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:b3:34:e6 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.227/24 brd 192.168.0.255 scope global eth0
inet6 fe80::5054:ff:feb3:34e6/64 scope link
valid_lft forever preferred_lft forever
可以看到,MASTER上绑定了VIP,而BACKUP没有。
3、测试:
3.1keepalived主从测试:
把MASTER的keepalived服务停掉,service keepalived stop ,然后再查看ip:
[root@localhost ~]# service keepalived stop
Stopping keepalived: [ OK ] [root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: p4p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 64:00:6a:34:c3:e2 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.241/24 brd 192.168.0.255 scope global p4p1
inet6 fe80::6600:6aff:fe34:c3e2/64 scope link
valid_lft forever preferred_lft forever
发现VIP没了。此时再查看BACKUP的ip:
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:b3:34:e6 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.227/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.201/32 scope global eth0
inet6 fe80::5054:ff:feb3:34e6/64 scope link
valid_lft forever preferred_lft forever
VIP成功漂移到了BACKUP上。
同时,通过keepalived的日志也可以看出,当master停掉后,backup主动成为master:
cat /var/log/messages
Jan 25 16:18:28 localhost Keepalived_healthcheckers[1199]: SMTP connection ERROR to [192.168.0.227]:25.
Jan 25 16:42:41 localhost Keepalived_vrrp[1200]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan 25 16:42:42 localhost Keepalived_vrrp[1200]: VRRP_Instance(VI_1) Entering MASTER STATE
Jan 25 16:42:42 localhost Keepalived_vrrp[1200]: VRRP_Instance(VI_1) setting protocol VIPs.
Jan 25 16:42:42 localhost Keepalived_vrrp[1200]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.0.201
Jan 25 16:42:42 localhost Keepalived_healthcheckers[1199]: Netlink reflector reports IP 192.168.0.201 added
Jan 25 16:42:47 localhost Keepalived_vrrp[1200]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.0.201
3.2MySQL高可用测试:
通过VIP连接mysql:
localhost:~ ahaii$ mysql -h192.168.0.201 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 643
Server version: 5.5.54-log Source distribution Copyright (c) 2000, 2015, 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>
连接成功。
同时在keepalived-master上查看lvs分配情况:
[root@localhost ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.201:3306 rr persistent 50
-> 192.168.0.225:3306 Route 1 0 0
-> 192.168.0.226:3306 Route 1 1 0
[root@localhost ~]#
显示现在连接的是226的那台mysql服务器。
此时,将226服务器上的mysql服务停掉,然后再通过vip连接mysql,并查看lvs分配情况:
依然能通过vip连接mysql:
localhost:~ ahaii$ mysql -h192.168.0.201 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37014
Server version: 5.5.54-log Source distribution Copyright (c) 2000, 2015, 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>
此时,226已被keepalived集群剔除掉,并且客户端连接的是225服务器 :
[root@localhost ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.201:3306 rr persistent 50
-> 192.168.0.225:3306 Route 1 1 0
当把226服务器的mysql服务重新启动时,keepalived又将其加入到集群中:
[root@localhost ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.201:3306 rr persistent 50
-> 192.168.0.225:3306 Route 1 1 0
-> 192.168.0.226:3306 Route 1 0 0
keepalived剔除、添加226服务器的日志记录如下:
Jan 25 17:00:27 localhost Keepalived_healthcheckers[1429]: TCP connection to [192.168.0.226]:3306 failed !!!
Jan 25 17:00:27 localhost Keepalived_healthcheckers[1429]: Removing service [192.168.0.226]:3306 from VS [192.168.0.201]:3306
Jan 25 17:03:27 localhost Keepalived_healthcheckers[1429]: TCP connection to [192.168.0.226]:3306 success.
Jan 25 17:03:27 localhost Keepalived_healthcheckers[1429]: Adding service [192.168.0.226]:3306 to VS [192.168.0.201]:3306
疑惑:
1、客户点请求VIP时,由于调度器和realserver都有VIP,如何保证是调度器接收请求,而不是realserver?
为了保证是调度器接收客户端的请求,在realserver上都做了如下配置:
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
用arp_ignore参数设定当请求的目标地址是本机IP时,对其ARP的查询作出不同级别的响应。
1:表示只回答目标IP地址是本地网卡的arp请求
2:表示只回答目标IP地址是本地网卡的arp请求,并且来访IP与本机IP在同一网段内。
用arp_announce参数设定本机向外宣告自身IP-MAC时的级别。
1:表示尽量避免响应ARP请求中MAC不是本网卡的。如一个主机有多块网卡,其中一块网卡接收到了ARP请求,发现所请求的MAC是本机另一块网卡的,这个时候接收到ARP请求的这块网卡就尽量避免响应。
2:表示总是使用最合适的网卡来响应。一个主机有多块网卡,其中一块网卡接收到了ARP请求,发现所请求的MAC是本机另一块网卡的,这个时候接收到ARP请求的这块网卡就一定不响应,只有发现请求的MAC是自己的才给与响应。
以上设置,就是为了接收客户点请求的是调度器的VIP,而不是realserver的VIP。
2、为什么负载均衡器和realserver上都有VIP呢?
当均衡器收到客户端请求后,根据配置的算法选取一台realserver,将客户端请求报文的目标MAC地址修改为选取的realserver的MAC地址后重新封装该数据帧并发送给选取的realserver。realserver收到该数据帧后检查IP报文发现该报文的目标IP(VIP)是自己,然后对该请求报文做出响应,将响应结果直接发送给客户端(整个过程中源地址不变)。
以上是整个安装配置和测试的过程。
Lvs+Keepalived实现MySQL高可用的更多相关文章
- LVS+Keepalived搭建MyCAT高可用负载均衡集群
LVS+Keepalived 介绍 LVS LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国 ...
- 基于keepalived搭建MySQL高可用集群
MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,MMM,Heartbeat+DRBD,PXC,Galera Cluster 比较常用的是keepalived+双主,MHA和 ...
- 基于Keepalived的MySQL高可用
keepalived负责的是故障转移,至于故障转以后的节点之间数据的一致性问题依赖于具体的复制模式.不管是主从.一主多从还是双主.集群节点个数.主从具体的模式无关(常规复制,半同步复制,GTID复制, ...
- 使用KeepAlived搭建MySQL高可用环境
使用KeepAlived搭建MySQL的高可用环境.首先搭建MySQL的主从复制在Master开启binlog,创建复制帐号,然后在Slave输入命令 2016年7月25日 配置安装技巧: ...
- lvs + keepalived + nginx + tomcat高可用负载反向代理服务器配置(一) 简介
一. 为什么这样构架 1. 系统高可用性 2. 系统可扩展性 3. 负载均衡能力 LVS+keepalived能很好的实现以上的要求,keepalived提供健康检查,故障转移,提高系统的可用性!采用 ...
- LVS+Keepalived+Nginx+Tomcat高可用负载均衡集群配置(DR模式,一个VIP,多个端口)
一.概述 LVS作用:实现负载均衡 Keepalived作用:监控集群系统中各个服务节点的状态,HA cluster. 配置LVS有两种方式: 1. 通过ipvsadm命令行方式配置 2. 通过Red ...
- lvs + keepalived + nginx + tomcat高可用负载反向代理服务器配置(二) LVS+Keepalived
一.安装ipvs sudo apt-get install ipvsadm 二.安装keepalived sudo apt-get install keepalived 三.创建keepalived. ...
- KEEPALIVED+LVS+MYCAT实现MYSQL高可用环境搭建
一.安装keepalived和ipvsadm 注意:ipvsadm并不是lvs,它只是lvs的配置工具. 为了方便起见,在这里我们使用yum的安装方式 分别在10.18.1.140和10.18.1.1 ...
- LVS+Keepalived搭建MyCAT高可用負載均衡集群
1.前面我们已经搭建好mysql主主,并且用mycat实现双写功能,主要配置文件: [root@mycat2 conf]# cat schema.xml <?xml version=" ...
随机推荐
- Thunder——爱阅app(测评人:方铭)
B.Thunder——爱阅app(测评人:方铭) 一.基于NABCD评论作品,及改进建议 每个小组评论其他小组Alpha发布的作品: 1.根据(不限于)NABCD评论作品的选题: 2.评论作品对选题的 ...
- BugPhobia开发篇章:Alaph阶段Scurm Meeting
[github] https://github.com/bugphobia/XuebaOnline 0x01 :目录与摘要 If you weeped for the missing sunset ...
- 20172321 20172333 2017-2018 暑假作业APP
20172321 20172333 2017-2018 暑假作业APP 项目介绍 项目成员 吴恒佚 20172321 严域俊 20172333 项目简介 从理论上来说,这是一个贪吃蛇游戏. <贪 ...
- 20172325『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结
20172325『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结 结对伙伴 学号:20172306 姓名:刘辰 结对伙伴博客链接 刘辰同学对编程的积极程度很高,并且在编程能力上很不错,有 ...
- Java基础第一节.Java简介
第一节 Java简介 Java是一个由Sun公司开发而成的新一代的编程语言. Java语言是对软件开发有深远影响.应用前景广泛.具有丰富的类库.继承了C++的传统(摈弃了某些不足)广泛使用的网络编程语 ...
- MongoDB中的数据导出为excel CSV 文件
1.打开命令行,进入我们所安装的mongodb路径下的bin文件夹 2.我们采用bin文件夹下的mongoexport方法进行导出, mongoexport -d myDB -c user -f _i ...
- 固态盘经常性蓝屏处理方法(WIN7/8)
型号:intel 520S 大小;120G 我的是笔记本,这段时间辞职,有了时间折腾自己的电脑系统了,想装WIN8来着,PE下直接把固态盘的分区都干掉了,分了C,D(40G,剩下的空间),安装阶段完美 ...
- Window环境下搭建Vue.js开发环境
原文链接:http://blog.csdn.net/solo95/article/details/72084907 笔者最近在进行前端的学习,在点完了HTML5.CSS3.JavaScript等技能树 ...
- vue & keep-alive & activated
vue & keep-alive & activated vue 生命周期 钩子函数 缓存 http://www.cnblogs.com/nekoooo/p/6442077.html ...
- java中的==操作符和equals函数
基本规则 “==”操作符的使用需要分成两种情况 判值类型相等 这一点很好理解,两个值类型代表的数值相等,则“==”表达式返回true “==”可以用与不同值类型的比较,语言会自动进行类型转换 判引用类 ...