这是一个坑...磨了不少时间。见证自己功力有待提升。。。

架构图

数据库

1.安装数据库

这块不难, 直接引用:mysql安装

2.数据库主备

这块不难, 直接引用: mysql主备

虚拟VIP

重点来了!!

上脚本, 把这个脚本命名后仍进/etc/init.d, 然后授予权限755或者+x

SNS_VIP=192.168.158.110
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig eth0:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev eth0:0
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 eth0:0 down
route del $SNS_VIP >/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

启动这个脚本后会出现虚拟ip, 启动方法service realserver start

查看有好几种方式

第一种: ifconfig

第二种: ip addr

第三种: ifconfig eth0:0

好了, 虚拟ip也有了,记得两台mysql是一样的操作。

LVS

重点!

1.依赖:

yum install -y libnl* popt*

rpm -ivh popt-static-1.13-7.el6.x86_64.rpm

第二个是后面发现缺少, 恰恰yum中也没有的安装而独自下载rpm

2.查看是否已加载lvs模块

modprobe -l |grep ipvs

3.正常的情况下是这样的:

[root@localhost init.d]# modprobe -l |grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
[root@localhost init.d]#

4.安装正包

tar -zxvf ipvsadm-1.26.tar.gz

make && make install

5.编译安装完成后测试:

ipvsadm -L -n
[root@localhost init.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn

lvs大功告成!

Keepalived

重点!!

1.安装依赖

yum install -y kernel-devel openssl openssl-devel

2.编译安装

新建安装目录

mkdir -p /usr/local/keepalived

编译

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/
Keepalived configuration
------------------------
Keepalived version : 1.2.13
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lssl -lcrypto -lcrypt
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : No
SHA1 support : No
Use Debug flags : No

注意: 这里的2.6.32-358.el6.x86_64是您的内核, 不要照抄哦, 可能咋们内核不一致

安装

make && make install

3.其他操作

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/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ chkconfig keepalived on

LVS+Keepalived配置

~~负载均衡主机~~

1.新建keepalived配置文件:

vim /etc/keepalived/keepalived.conf

2.keepalived.conf详细:

! Configuration File for keepalived

global_defs {                           #全局标识模块
notification_email { #定义邮件通知
test@163.com #目标邮箱
}
notification_email_from test@163.com #发送邮箱
smtp_server 127.0.0.1 #发送邮箱的smtp服务器
smtp_connect_timeout 30 #smtp服务器连接超时时间
router_id LVS_DEVEL
} vrrp_instance VI_1 {
state MASTER #本实例启动状态:MASTER/BACKUP
interface eth0 #监控的网络接口
virtual_router_id 51 #vrrp实例(同一个组主备服务器设置一样)
priority 100 #优先级高的为master,不能超过255。(BACKUP可设置为50)
advert_int 1 #均衡器检测间隔1秒(服务器设置都一样)
authentication { #验证类型及密码(服务器设置都一样)
auth_type PASS #认证方式,PASS或AH
auth_pass 123456 #认证密码
}
virtual_ipaddress { #虚拟ip地址virtual_ipaddress,可以定义多个
192.168.158.110
}
} virtual_server 192.168.158.110 3306 { #定义虚拟服务器,与上面的virtual_server一样
delay_loop 3 #健康检查时间间隔,6秒
lb_algo wrr #负载均衡调度算法:rr|wrr|lc|wlc|sh|dh|lblc
lb_kind DR #负载均衡转发规则:NAT|DR|TUN
nat_mask 255.255.255.0
#persistence_timeout 50 #回话保持时间50秒,动态服务建议开启
protocol TCP #转发协议protocol,一般有tcp和udp两种 #后端真实服务器,有几台就设置几个
real_server 192.168.158.134 3306 {
weight 1 #权重越大负载分越大,0表示失效
SSL_GET { #健康检查方式:HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.158.139 3306 {
weight 1
SSL_GET {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
} 

3.附上启动脚本

#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived # Source function library
. /etc/rc.d/init.d/functions # Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived RETVAL=0 prog="keepalived" start() {
echo -n $"Starting $prog: "
daemon keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
} stop() {
echo -n $"Stopping $prog: "
killproc keepalived
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
} reload() {
echo -n $"Reloading $prog: "
killproc keepalived -1
RETVAL=$?
echo
} # See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
RETVAL=1
esac exit $RETVAL

4.检测keepalived

service keepalived start

service keepalived stop

service keepalived status

5.检测lvs

ipvsadm

lvs数据参考:

[root@localhost init.d]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.158.110:mysql wrr
-> 192.168.158.134:mysql Route 1 0 0
-> 192.168.158.139:mysql Route 1 0 0

~~负载均衡备机~~

备机负载服务器与主负载服务器大致相同,只是在keepalived的配置文件中需要改以下两处:

(1)将state由MASTER改为BACKUP

(2)将priority由100改为99

lvs+keepalived大功告成!!

验证性测试

1.启动mysql(两台)

service mysqld start

2.启动vip

service realserver start

3.启动keepalived(两台)

service keepalived start

4.使用本地ping vip

5.使用本地连接mysql(连接的是vip)

mysql.exe -u root -p"lizhenghua" -P 3306 -h 192.168.158.110

6.在从库139上建立数据库cheng(先断开主从)

192.168.158.139

192.168.158.134

笔记

! Configuration File for keepalived

global_defs {
notification_email {
test@163.com
}
notification_email_from test@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_1
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.158.110
}
} virtual_server 192.168.158.110 3306 {
delay_loop 3
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP real_server 192.168.158.134 3306 {
weight 1
SSL_GET {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.158.139 3306 {
weight 1
SSL_GET {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}

监测脑裂 

#!/bin/bash
# 检查脑裂的脚本,在备节点上进行部署
LB01_VIP=192.168.158.110
LB01_IP=192.168.158.140
LB02_IP=192.168.158.141
while true
do
ping -c 2 -W 3 $LB01_VIP &>/dev/null
if [ $? -eq 0 -a `ip add|grep "$LB01_VIP"|wc -l` -eq 1 ];then
echo "ha is brain."
else
echo "ha is ok"
fi
sleep 5
done 

完成!!

LVS+Keepalived+Mysql+主备数据库架构[4台]的更多相关文章

  1. LVS+Keepalived+Mysql+主主数据库架构[2台]

    架构图 安装步骤省略. 158.140 keepalived.conf ! Configuration File for keepalived global_defs { #全局标识模块 notifi ...

  2. LVS+MYCAT+读写分离+MYSQL主备同步部署手册

    LVS+MYCAT+读写分离+MYSQL主备同步部署手册 1          配置MYSQL主备同步…. 2 1.1       测试环境… 2 1.2       配置主数据库… 2 1.2.1  ...

  3. 【转载】LVS+MYCAT+读写分离+MYSQL主备同步部署手册(邢锋)

    LVS+MYCAT+读写分离+MYSQL主备同步部署手册 1          配置MYSQL主备同步…. 2 1.1       测试环境… 2 1.2       配置主数据库… 2 1.2.1  ...

  4. Lvs+keepalived+mysql主从热备

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  5. MySQL主备复制原理、实现及异常处理

    复制概述 MySQL支持三种复制方式:基于行(Row)的复制.基于语句(Statement)的复制和混合类型(Mixed)的复制. 基于语句的复制早在3.23版本中就存在,而基于行的复制方式在5.1版 ...

  6. MySQL主备模式的数据一致性解决方案

     根据阿里交易型业务的特点,以及在双十一这样业内罕有的需求推动下,我们在官方的MySQL基础上增加了非常多实用的功能.性能补丁.而在使用MySQL的过程中,数据一致性是绕不开的话题之一.本文主要从阿里 ...

  7. mysql主备切换[高可用]

    到这一步的时候, 是主备部署已经处理好, 请关注:mysql主备部署[高可用] 这次使用的是keepalived-1.2.22.tar.gz版, 官网地址:keeplived官网 笼统知识请自行查询百 ...

  8. keepalived nginx 主备配置

    keepalived  nginx 主备配置(多主多备同理) 1.Nginx服务安装 nginx 不区分主备,在两台服务上安装两个即可. 安装参考:https://www.cnblogs.com/zw ...

  9. mysql主备搭建

    mysql主备搭建参考文档https://www.cnblogs.com/clsn/p/8150036.html前提条件:系统:Ubuntu 16.04.6 LTSMySQL版本:5.7.24主库IP ...

随机推荐

  1. 用int还是用Integer?

    昨天例行code review时大家有讨论到int和Integer的比较和使用. 这里做个整理,发表一下个人的看法.   [int和Integer的区别] int是java提供的8种原始类型之一,ja ...

  2. Web API 入门 二 媒体类型

    还是拿上面 那篇 Web API 入门 一  的那个来讲 在product类中加一个时间属性

  3. 函数式编程语言(functional language)

    内容根据百度词条整理! 转载请声明来源:https://baike.baidu.com/item/%E5%87%BD%E6%95%B0%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8 ...

  4. Xshell连接不上虚拟机提示ssh服务器拒绝了密码,请再试一次

    1. 设置ubuntu的管理员root的密码 hughes@hughes-virtual:~$ sudo passwd (供xshell连接时使用) 2. 确保源文件和系统已更新 hughes@hug ...

  5. beego 初体验 - 环境搭建

    首先,安装go运行时和beego beego,在git bash 运行命令: go get github.com/beego/bee go get github.com/astaxie/beego g ...

  6. Django后台管理系统讲解及使用

    大家在创建Django项目后,在根路由urls.py文件中,会看到一行代码 from django.contrib import admin urlpatterns = [ url(r'^admin/ ...

  7. factory源码分析——component_registry和object_registry

    registry类主要是为object和component提供一个轻量级的代理(lightweight proxy)来方便factory实现: registry class从uvm_object_wr ...

  8. html5 手机端 通讯录 touch 效果

    不说那么多直接上代码. <html> <head> <meta http-equiv="Content-Type" content="tex ...

  9. C# 队列(Queue)和 堆栈(Stack)

    C# 队列(Queue)和 堆栈(Stack) C# 队列(Queue) 队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队 ...

  10. 【Hbase学习之四】Hbase表设计案例

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...