Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MySQL+KeepAlived+LVS single Point Writing Master synchronous High availability Architecture instance tutorial

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要讲解了"MySQL+KeepAlived+LVS单点写入主主同步高可用架构实例教程",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"MySQL+KeepAlived+LVS单点写入主主同步高可用架构实例教程"吧!

㈠ 实战环境

服务器名·IPOSMySQLodd.example.com192.168.1.116RHEL-5.85.5.16even.example.com192.168.1.115RHEL-5.85.5.16

㈡ 方案优缺点

优点

● 安装配置简单, 实现方便,高可用效率好,可以根据服务与系统的可用性多方面进行切换

● 可以将写 VIP 和读 VIP 分别进行设置,为读写分离做准备

● 可以在后面添加多个从服务器,并做到负载均衡

缺点

● 在启动或者恢复后会立即替换掉定义的 sorry_server

因此如果要实现指定条件替换或者不替换需要通过其他方式实现,比如:临时更改MySQL的端口等

● 扩展不是很方便

● 切换需要 1s 左右的时间

㈢ 方案适用场景

这个方案适用于只有两台数据库服务器并且还没有实现数据库的读写分离的情况、读和写都配置 VIP

这个方案能够便于单台数据库的管理维护以及切换工作

比如进行大表的表结构更改、数据库的升级等都是非常方便的

㈣ 方案架构图

㈤ 部署步骤

⑴ MySQL 安装

请查阅我之前的一篇文章:MySQL 5.5 三大安装方式:【二进制、源代码编译 和 RPM 包】 安装步骤及优缺点比较

⑵ MySQL主主同步配置

这也请查阅我之前的文章哈:MySQL 主主同步配置

⑶ LVS 安装

在ODD EVEN:

# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

# ln -s /usr/src/kernels/2.6.18-308.el5xen-i686/ /usr/src/linux

# tar -zxv -f ipvsadm-1.24.tar.gz

# cd ipvsadm-1.24

# make

# make install

⑷ KeepAlived安装

在ODD EVEN:

# wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz

# tar -zxv -f keepalived-1.1.19.tar.gz

# cd keepalived-1.1.19

# ./configure --prefix=/usr/local/keepalived

# make

# make install

# 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

⑸ KeepAlived配置

在 ODD

global_defs { notification_email { linwaterbin@gmail.com } notification_email_from bychjzh@gmail.com smtp_server smtp.gmail.com smtp_connect_timeout 30 router_id LVS1} vrrp_sync_group test {group { loadbalance}} vrrp_instance loadbalance { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111} virtual_ipaddress { 192.168.1.128 dev eth0 label eth0:1}} virtual_server 192.168.1.128 3306 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 20 protocol TCP sorry_server 192.168.1.115 3306 real_server 192.168.1.116 3306 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 3306}}}

在EVEN:

大部分都和 ODD 一样、只有 2 处:

● state BACKUP

● priority 150

⑹ ODD EVEN 的 realsever 配置

在 ODD EVEN 上

#!/bin/bash# description: Config realserver lo and apply noarp SNS_VIP=192.168.1.128/etc/rc.d/init.d/functionscase "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo: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 lo: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 1esacexit 0

⑺ 启动 KeepAlived和 realsever

在 ODD EVEN

# /etc/rc.d/init.d/realserver.sh start

# /etc/rc.d/init.d/keepalived start

# echo "/etc/rc.d/init.d/realserver.sh start" >> /etc/rc.local

# echo "/etc/rc.d/init.d/keepalived start" >> /etc/rc.local

配置到这边大概都已经差不多了、接下来看看测试

㈥ 简单测试

停掉ODD MySQL Server 、看看能否自动却换到 sorry_server

停止前:[root@odd keepalived]# ipvsadm -lnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConnTCP 192.168.1.128:3306 rr persistent 20 -> 192.168.1.116:3306 Local 3 0 0 停止:[mysql@odd ~]$ mysqladmin shutdown130508 00:53:50 mysqld_safe mysqld from pid file /home/mysql/mysql/data/odd.example.com.pid ended[1]+ Done mysqld_safe停止后:[root@odd keepalived]# ipvsadm -lnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConnTCP 192.168.1.128:3306 rr persistent 20 -> 192.168.1.115:3306 Route 1 0 0 成功!!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report