MySQL主从复制与lvs+keepalived单点写入读负载均衡高可用实验【转】
一、环境
Master(主机A):192.168.1.1
Slave(主机B) :192.168.1.2
W-VIP(写入) :192.168.1.3
R-VIP(读取) :192.168.1.4
Client(测试) :192.168.1.100
操作系统版本:CentOS release 6.4
MySQL数据库版本:5.6.14
keepalived版本:1.2.7
LVS版本:1.26
所有环境均为虚拟机
二、设计思路
1. 服务器A和B,通过mysql的slave进程同步数据。
2. 通过keepalived启用两个虚IP:W-VIP/R-VIP,一个负责写入,一个负责读取,实现读写分离。
3. A和B都存在时,W-VIP下将请求转发至主机A,R-VIP将请求转发给A和B,实现负载均衡。
4. 当主机A异常时,B接管服务,W-VIP/R-VIP此时漂到了主机B上,此时这两个虚IP下都是主机B,实现高可用
5. 当主机B异常时,R-VIP会将B踢出,其他不变
三、架构图
四、软件安装
主从两个主机都要装以下软件:
1. MySQL的安装(略)
2. keepalived安装
yum install keepalived
2. LVS安装
yum install ipvsadm
五、配置
1. 配置MySQL的主从复制(略)
2. 配置keepalived
Master上的配置
vi /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- router_id MySQL-ha
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth1
- virtual_router_id 90
- priority 100
- advert_int 1
- notify_master "/usr/local/mysql/bin/remove_slave.sh"
- nopreempt
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.3 label eth1:1
- 192.168.1.4 label eth1:2
- }
- }
- virtual_server 192.168.1.3 6603 {
- delay_loop 2
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.168.1.1 6603 {
- weight 3
- notify_down /usr/local/mysql/bin/mysql.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 6603
- }
- }
- }
- virtual_server 192.168.1.4 6603 {
- delay_loop 2
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.168.1.1 6603 {
- weight 1
- notify_down /usr/local/mysql/bin/mysql.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 6603
- }
- }
- real_server 192.168.1.2 6603 {
- weight 3
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 6603
- }
- }
- }
keepalived配置成服务并开机启动
- cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
- cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
- chkconfig --add keepalived
- chkconfig --level 345 keepalived on
vi /usr/local/mysql/bin/remove_slave.sh
- #!/bin/bash
- user=u1
- password=12345
- log=/usr/local/mysql/log/remove_slave.log
- echo "`date`" >> $log
- /usr/local/mysql/bin/mysql -u$user -p$password -e "set global read_only=OFF;reset master;stop slave;change master to master_host='localhost';" >> $log
- /bin/sed -i 's#read-only#\#read-only#' /etc/my.cnf
vi /usr/local/mysql/bin/mysql.sh
- #!/bin/bash
- /etc/init.d/keepalived stop
Slave上的配置
vi /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- router_id MySQL-ha
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth1
- virtual_router_id 90
- priority 99
- advert_int 1
- notify_master "/usr/local/mysql/bin/remove_slave.sh"
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.3 label eth1:1
- 192.168.1.4 label eth1:2
- }
- }
- virtual_server 192.168.1.3 6603 {
- delay_loop 2
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.168.1.2 6603 {
- weight 3
- notify_down /usr/local/mysql/bin/mysql.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 6603
- }
- }
- }
- virtual_server 192.168.1.4 6603 {
- delay_loop 2
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.168.1.2 6603 {
- weight 3
- notify_down /usr/local/mysql/bin/mysql.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 6603
- }
- }
- }
keepalived配置成服务并开机启动
- cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
- cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
- chkconfig --add keepalived
- chkconfig --level 345 keepalived on
vi /usr/local/mysql/bin/remove_slave.sh
- #!/bin/bash
- user=u1
- password=12345
- log=/usr/local/mysql/log/remove_slave.log
- echo "`date`" >> $log
- /usr/local/mysql/bin/mysql -u$user -p$password -e "set global read_only=OFF;reset master;stop slave;change master to master_host='localhost';" >> $log
- /bin/sed -i 's#read-only#\#read-only#' /etc/my.cnf
vi /usr/local/mysql/bin/mysql.sh
- #!/bin/bash
- /etc/init.d/keepalived stop
3. 配置LVS
Master与Slave上的配置相同:
vi /usr/local/bin/lvs_real.sh
- #!/bin/bash
- # description: Config realserver lo and apply noarp
- SNS_VIP=192.168.1.3
- SNS_VIP2=192.168.1.4
- source /etc/rc.d/init.d/functions
- case "$1" in
- start)
- ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
- ifconfig lo:1 $SNS_VIP2 netmask 255.255.255.255 broadcast $SNS_VIP2
- /sbin/route add -host $SNS_VIP dev lo:0
- /sbin/route add -host $SNS_VIP2 dev lo: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
- sysctl -p >/dev/null 2>&1
- echo "RealServer Start OK"
- ;;
- stop)
- ifconfig lo:0 down
- ifconfig lo:1 down
- route del $SNS_VIP >/dev/null 2>&1
- route del $SNS_VIP2 >/dev/null 2>&1
- echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
- echo "RealServer Stoped"
- ;;
- *)
- echo "Usage: $0 {start|stop}"
- exit 1
- esac
- exit 0
- chmod 755 /usr/local/bin/lvs_real.sh
- echo "/usr/local/bin/lvs_real.sh start" >> /etc/rc.local
五、Master和Slave的启动
1. 启动Master上的MySQL
service mysql start
2. 启动Slave上的MySQL
service mysql start
3. 启动Master上的realserver脚本
/usr/local/bin/lvs_real.sh start
4. 启动Slave上的realserver脚本
/usr/local/bin/lvs_real.sh start
5. 启动Master上的keepalived
service keepalived start
6. 启动Slave上的keepalived
service keepalived start
六、测试
1. 查看lvs能否进行负载均衡转发
在Master和Slave上分别执行:
ipvsadm -ln
2. 在Client上验证连通性:
ping 192.168.1.3
ping 192.168.1.4
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
3. 停掉Master上的MySQL,看写IP否自动切换到Slave,看读IP是否去掉了Master的MySQL
在Master和Slave上分别执行:
ipvsadm -ln
在Client上执行:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
4. 停掉Master上的keepalived,看读写VIP是否会迁移到Slave上。
在Master和Slave上分别执行:
ipvsadm -ln
在Client上执行:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
5. 停掉Slave上的MySQL,看读IP是否去掉了Slave的MySQL
在Master和Slave上分别执行:
ipvsadm -ln
在Client上执行:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
6. 重启Master的系统,看看切换过程是否正常
七、参考
http://wenku.baidu.com/link?url=17K0UDApzfK18A8F5Of9wl0lmGx6AD_0PQ8GyVqmCsBMSz16xjt2JI88lPAeID2qECs7piUxkzQLyatTtDuEJxwZEy6joIBfBmT3vgTfdUS
http://blog.chinaunix.net/uid-23500957-id-3781918.html
http://blog.chinaunix.net/uid-23500957-id-3781919.html
http://blog.chinaunix.net/uid-20639775-id-3337471.html
MySQL主从复制与lvs+keepalived单点写入读负载均衡高可用实验【转】的更多相关文章
- Lvs+keepAlived实现负载均衡高可用集群(DR实现)
第1章 LVS 简介 1.1 LVS介绍 LVS是Linux Virtual Server的简写,意为Linux虚拟服务器,是虚拟的服务器集群系统,可在UNIX/LINUX平台下实现负载均衡集群功能. ...
- lvs+keepalived+nginx实现高性能负载均衡集群【转】
转自 lvs+keepalived+nginx实现高性能负载均衡集群 - 青衫lys - 博客园http://www.cnblogs.com/liuyisai/p/5990645.html 一.为什么 ...
- LVS+Keepalived-DR模式负载均衡高可用集群
LVS+Keepalived DR模式负载均衡+高可用集群架构图 工作原理: Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能. VRRP,虚拟路由冗余协议,是针对路由器的 ...
- 实现基于Haproxy+Keepalived负载均衡高可用架构
1.项目介绍: 上上期我们实现了keepalived主从高可用集群网站架构,随着公司业务的发展,公司负载均衡服务已经实现四层负载均衡,但业务的复杂程度提升,公司要求把mobile手机站点作为单独的服务 ...
- Nginx+Keepalived负载均衡高可用
Nginx+Keepalived负载均衡高可用方案: Nginx 使用平台:unix.linux.windows. 功能: A.www web服务 http 80 b.负载均衡(方向代理proxy) ...
- JAVAEE——宜立方商城03:Nginx负载均衡高可用、Keepalived+Nginx实现主备
1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...
- Keepalived+Nginx实现负载均衡高可用
一.负载均衡高可用 Nginx作为负载均衡器,所有请求都到了Nginx,可见Nginx处于非常重点的位置,如果Nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了避免负载均衡服务器的宕机 ...
- Nginx+keepalived实现负载均衡高可用配置
1. 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了屏蔽负载均衡服务 ...
- 23.Nginx+keepalived负载均衡高可用
Nginx+keepalived负载均衡高可用 结构图 环境: 主 服务器:192.168.239.10 备 服务器:192.168.239.20 Web 服务器1:192.168.239.40 We ...
随机推荐
- Nginx反向代理配置配置实例
为了节省支出,公司需要将分布在不同机器的站点都迁移到一台机器,而目前不同机器运行的是不同的web服务,部分是nginx,部分是apache,由于牵涉较多rewrite规则,为了节省修改功夫,打算迁移后 ...
- iOS加载启动图的时候隐藏statusbar + 指定启动图显示多少秒
只需需要在info.plist中加入Status bar is initially hidden 设置为YES 补充一下,现在手机越来越快,在6+下面启动图一闪而过,而美工童鞋辛辛苦苦做的图就看不到鸟 ...
- Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view...
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tabl ...
- 关于#include后面<>和" "的区别
1.以尖括号制定头文件,如下所示: #include <stdio.h> 用尖括号来制定文件时,预处理器是以特定的方式来寻找文件,一般是环境中或编译器命令行指定的某种寻找路径.这种设置寻找 ...
- C++ Primer 5th 第13章 拷贝控制
当一个对象的引用或者指针离开作用域时,析构函数不会执行. 构造函数有初始化部分(初始化列表)和函数体. 析构函数有析构部分和函数,但析构函数的析构部分是隐式的.
- underscorejs-groupBy学习
2.18 groupBy 2.18.1 语法 _.groupBy(list, iteratee, [context]) 2.18.2 说明 把list分为多个集合,iterator为分组的依据,返回值 ...
- RedHat9上安装jdk
1.先在windows下载jdk:jdk-6-dlj-linux-i586.bin 2.用ftp上传给linux下 3.chmod 777 jdk-6-dlj-linux-i586.bin 4.将jd ...
- yum 搭建lnmp环境详细步骤
1.关闭防火墙[root@CentOS ~]# chkconfig iptables off 2.关闭selinuxvi /etc/sysconfig/selinux//将SELINUX=enforc ...
- CodeForces 474B(标记用法)
CodeForces 474B Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- mybatis框架搭建学习初步
mybatis框架搭建步骤:1. 拷贝jar到lib目录下,而且添加到工程中2. 创建mybatis-config.xml文件,配置数据库连接信息 <environments default=& ...