haproxy+keepalived来实现mariadb galera cluster的高可用架构

网络拓扑图如下:

mariadb galera cluster集群基于wsrep协议可以实现mysql多主复制架构,详细说明如下:

galera cluster复制:

galera cluster本身是基于wsrep协议工作在底层的文件复制通道而已,只需要指明复制的文件,它会自动在多个节点之间的检测文件状态并完成数据同步。并且galera cluster向上提供API,其他组件只需调用其API,即可完成所需的文件复制功能。

galera cluster向上提供API,mysql只需调用其API即可。所以原生的mysql版本是没有galera cluster功能的,需要下载附带galera的版本才可以,mysql官方也有提供支持galera的版本。

而haproxy是mariadb galera cluster前端的负载均衡组件,可以实现把mysql请求按照指定的调度算法分发给后端的mariadb galera cluster存储。而keepalived则可以实现haproxy的高可用。

详细的安排配置过程如下:

以下节点均为CentOS7.1,安装方式为“基础架构服务器”,配置好CentOS7的base和epel源,主机名,时间同步,关闭selinux和firewalld

192.168.43.201node1.c7.comnode1Mariadb-Galera-server1

192.168.43.202node2.c7.comnode2 Mariadb-Galera-server2

192.168.43.203node3.c7.comnode3 Mariadb-Galera-server3

192.168.43.204node4.c7.comnode4 haproxy1 + keepalived

192.168.43.205node5.c7.comnode5 haproxy2 + keepalived

192.168.43.206node6.c7.comnode6 mysql 客户端

一、配置mariadb galera cluster集群

1. 配置节点之间网络时间同步

[[email protected] ~]# crontab -l

*/5 * * * * /usr/sbin/ntpdate -u 129.6.15.28 &> /dev/null

[[email protected] ~]# crontab -l

*/5 * * * * /usr/sbin/ntpdate -u 129.6.15.28 &> /dev/null

[[email protected] ~]# crontab -l

*/5 * * * * /usr/sbin/ntpdate -u 129.6.15.28 &> /dev/null

2. 配置集群节点互信

[[email protected] ~]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.43.201 node1.c7.com node1

192.168.43.202 node2.c7.com node2

192.168.43.203 node3.c7.com node3

192.168.43.204 node4.c7.com node4

192.168.43.205 node5.c7.com node5

[[email protected] ~]# for i in {2..5}; do scp /etc/hosts node$i:/etc; done

[[email protected] ~]# ssh-keygen -t rsa -P ‘‘ -f ‘/root/.ssh/id_rsa‘

[[email protected] ~]# cat .ssh/id_rsa.pub >> .ssh/authorized_keys

[[email protected] ~]# chmod 600 .ssh/*

[[email protected] ~]# ll .ssh/*

-rw------- 1 root root  399 Dec 18 10:10 .ssh/authorized_keys

-rw------- 1 root root 1675 Dec 18 10:09 .ssh/id_rsa

-rw------- 1 root root  399 Dec 18 10:09 .ssh/id_rsa.pub

[[email protected] ~]# for i in {2..5};do ssh node$i "mkdir /root/.ssh -p" ;done

[[email protected] ~]# for i in {2..5};do scp -p .ssh/{id_rsa,authorized_keys} \

node$i:/root/.ssh ;done

[[email protected] ~]# for i in {1..5};do ssh node$i hostname;done

[[email protected] ~]# for i in {1..5};do ssh node$i hostname;done

[[email protected] ~]# for i in {1..5};do ssh node$i hostname;done

3. 安装配置mariadb galera cluster集群

1) 到http://yum.mariadb.org/5.5-galera/的其中一个目录下载下列软件包进行安装

(需要先配置好系统光盘yum源)

MariaDB-Galera-server

MariaDB-shared

MariaDB-client

MariaDB-common

galera

2) 配置http://yum.mariadb.org/5.5-galera/某目录为网络源进行安装,例如如下

vim galera.repo

[galera]

name=mysql_galera_centos7

baseurl=http://yum.mariadb.org/5.5-galera/centos7-amd64/

gpgcheck=0

[[email protected] ~]# yum -y install MariaDB-Galera-server (注意大小写)

[[email protected] ~]# yum -y install MariaDB-Galera-server

[[email protected] ~]# yum -y install MariaDB-Galera-server

[[email protected] ~]# rpm -ql galera | grep -i smm.so

/usr/lib64/galera/libgalera_smm.so

[[email protected] ~]# vim /etc/my.cnf.d/server.cnf

[galera]

# Mandatory settings

wsrep_provider=/usr/lib64/galera/libgalera_smm.so

wsrep_cluster_address="gcomm://192.168.43.201,192.168.43.202,192.168.43.203"

binlog_format=row

#虽然不依赖二进制日志来复制,但是不开启二进制日志,mysql的集群服务启动不了的

default_storage_engine=InnoDB

#目前只支持InnoDB存储引擎

innodb_autoinc_lock_mode=2

bind-address=0.0.0.0

wsrep_cluster_name=‘mycluster‘

[[email protected] ~]# scp /etc/my.cnf.d/server.cnf node2:/etc/my.cnf.d/

[[email protected] ~]# scp /etc/my.cnf.d/server.cnf node3:/etc/my.cnf.d/

mariadb galera cluster集群第一个节点启动方式

[[email protected] ~]# /etc/rc.d/init.d/mysql start --wsrep-new-cluster

Starting MySQL..... SUCCESS!

mariadb galera cluster集群其他节点启动方式

[[email protected] ~]# service mysql start

Starting MySQL....SST in progress, setting sleep higher. SUCCESS!

[[email protected] ~]# service mysql start

Starting MySQL....SST in progress, setting sleep higher. SUCCESS!

mysql_secure_installation 对mysql做进一步安全设定,本文设定root密码为mysql

使用shell脚本不停的插入数据,测试mysql能否同步数据

#!/bin/bash

a=$1

first=0

while true

do

if [ $# -eq 0 ];then

echo "需要一个参数"

exit 1

fi

if [ $first -eq 0 ];then

mysql -uroot -pmysql -e "CREATE DATABASE IF NOT EXISTS testdb CHARACTER SET utf8;"

mysql -uroot -pmysql testdb -e "CREATE TABLE IF NOT EXISTS students

(StuID INT PRIMARY KEY,Name char(10) NOT NULL,Age TINYINT NOT NULL)

ENGINE=InnoDB DEFAULT CHARSET=utf8;"

first=1

fi

mysql -uroot -pmysql testdb -e "INSERT INTO students (StuID,Name,Age) values

($a,‘name$a‘,$a % 100);"

a=$(($a+1))

sleep 1

done

查看数据是否能同步

至此mariadb galera cluster集群就构建完成

授权root用户远程登陆(生产环境中建议使用普通用户,而不是root用户)

MariaDB [test]> grant all on *.* to ‘root‘@‘192.168.43.%‘ identified by ‘‘;

建议每个访问IP单独授权  grant all on *.* to ‘root‘@‘192.168.43.206‘ identified by ‘mysql‘;

二、构建haproxy + keepalived高可用框架

[[email protected] ~]# yum -y install haproxy keepalived

[[email protected] ~]# yum -y install haproxy keepalived

[[email protected] ~]# cd /etc/haproxy/

[[email protected] haproxy]# ls

haproxy.cfg

[[email protected] haproxy]# cp haproxy.cfg{,.bak}

[[email protected] haproxy]# vim haproxy.cfg

defaults

......

listen mysql_proxy 0.0.0.0:3306

mode tcp

balance leastconn

#leastconn可以把请求调度给连接最少的mysql服务器

option tcpka

option tcp-check

server mysql1 192.168.43.201:3306 weight 1

server mysql2 192.168.43.202:3306 weight 1

server mysql3 192.168.43.203:3306 weight 1

#文档中下面的内容删除即可

[[email protected] haproxy]# systemctl start haproxy

[[email protected] haproxy]# ss -tanlp | grep haproxy

LISTEN     0      128                       *:3306                     *:*

users:(("haproxy",3684,5))

[[email protected] haproxy]# scp /etc/haproxy/haproxy.cfg node5:/etc/haproxy/

[[email protected] ~]# systemctl start haproxy

[[email protected] ~]# ss -tanlp | grep haproxy

LISTEN     0      128                       *:3306                     *:*

users:(("haproxy",3368,5))

[[email protected] ~]# cd /etc/keepalived/

[[email protected] keepalived]# ls

keepalived.conf

[[email protected] keepalived]# cp keepalived.conf{,.bak}

[[email protected] keepalived]# vim keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_haproxy {

script "/etc/keepalived/check_haproxy.sh"

interval 5

weight -5

}

vrrp_instance VI_1 {

state MASTER

interface eno16777736

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 111199

}

virtual_ipaddress {

192.168.43.50/24 dev eno16777736 label eno16777736:1

}

track_script {

chk_haproxy

}

}

[[email protected] keepalived]# vim check_haproxy.sh

#!/bin/bash

A=`ps -C haproxy --no-header | wc -l`

if [ $A -eq 0 ]; then

systemctl restart haproxy

echo "Start haproxy" &> /dev/null

sleep 3

if [ `ps -C haproxy --no-header | wc -l`  -eq 0 ];then

systemctl stop keepalived

echo "Stop keepalived" &> /dev/null

fi

fi

[[email protected] keepalived]# chmod +x check_haproxy.sh

[[email protected] keepalived]# scp ./* node5:/etc/keepalived/

[[email protected] keepalived]# systemctl start keepalived

[[email protected] keepalived]# ip a

2: eno16777736:

inet 192.168.43.204/24 brd 192.168.43.255 scope global eno16777736

inet 192.168.43.50/24 scope global secondary eno16777736:1

[[email protected] ~]# cd /etc/keepalived/

[[email protected] keepalived]# ls

check_haproxy.sh  keepalived.conf  keepalived.conf.bak

[[email protected] keepalived]# vim keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_haproxy {

script "/etc/keepalived/check_haproxy.sh"

interval 5

weight -5

}

vrrp_instance VI_1 {

state BACKUP

interface eno16777736

virtual_router_id 51

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass 111199

}

virtual_ipaddress {

192.168.43.50/24 dev eno16777736 label eno16777736:1

}

track_script {

chk_haproxy

}

}

[[email protected] keepalived]# vim check_haproxy.sh

#!/bin/bash

A=`ps -C haproxy --no-header | wc -l`

if [ $A -eq 0 ]; then

systemctl start haproxy

echo "Start haproxy" &> /dev/null

sleep 3

if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then

#systemctl stop keepalived

echo "Stop keepalived" &> /dev/null

fi

fi

[[email protected] keepalived]# systemctl start keepalived

[[email protected] keepalived]# ip a

2: eno16777736:

inet 192.168.43.205/24 brd 192.168.43.255 scope global eno16777736

(说明:没有192.168.43.50/24)

三、使用mysql客户端进行连接

[[email protected] ~]# yum -y install mariadb

[[email protected] ~]# mysql -uroot -h192.168.43.50

MariaDB [(none)]> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [test]> show tables;

+----------------+

| Tables_in_test |

+----------------+

| tb1            |

| tb2            |

+----------------+

2 rows in set (0.01 sec)

MariaDB [test]> select * from tb2;

+----+-------+

| id | name  |

+----+-------+

|  2 | hi    |

|  5 | hello |

|  8 | Mike  |

| 11 | Jack  |

| 12 | what  |

| 15 | is    |

| 18 | that  |

+----+-------+

7 rows in set (0.01 sec)

MariaDB [test]> desc tb2;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    6

Current database: test

+-------+------------------+------+-----+---------+----------------+

| Field | Type             | Null | Key | Default | Extra          |

+-------+------------------+------+-----+---------+----------------+

| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| name  | char(30)         | YES  |     | NULL    |                |

+-------+------------------+------+-----+---------+----------------+

2 rows in set (0.06 sec)

MariaDB [test]> insert into tb2(name) values (‘how‘),(‘do‘),(‘you‘),(‘do‘);

Query OK, 4 rows affected (0.02 sec)

Records: 4  Duplicates: 0  Warnings: 0

MariaDB [test]> select * from tb2;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    6

Current database: test

+----+-------+

| id | name  |

+----+-------+

|  2 | hi    |

|  5 | hello |

|  8 | Mike  |

| 11 | Jack  |

| 12 | what  |

| 15 | is    |

| 18 | that  |

| 20 | how   |

| 23 | do    |

| 26 | you   |

| 29 | do    |

+----+-------+

11 rows in set (0.04 sec)

此时,haproxy+keepalived来实现mariadb galera cluster的高可用架构完成。

时间: 2024-10-08 02:39:49

haproxy+keepalived来实现mariadb galera cluster的高可用架构的相关文章

Keepalived+LVS+MariaDB Galera Cluster10.0高可用负载均衡多主复制

一.概述 1.简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到各个节点上去.在数据方面完全兼容 MariaDB.Percona Server和MySQL. 2.特性: (1).同步复制 Synchronous replication (2).Active-active multi-master 拓扑逻辑 (3).可对集群中任一节

MariaDB基于MHA和Galera Cluster实现高可用

MySQL高可用 MMM:MySQL主主复制管理器是一套灵活的脚本程序,基于perl实现,用来对mysqk replication进行监控和故障迁移,并能管理mysql master-master复制的配置(同一时间只有一个节点是可写的) MHA:对主节点进行监控,可实现自动故障转移至其他从节点,通过提升某一节点为新的主节点,基于主从复制实现,还需要客户端配合时间,目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群汇总最少有三台数据库服务器,一主二从,即一太充当master,一台充

Haproxy+Keepalived(双机热备)搭建高可用web架构

1.目的搭建web高可用架构,用haproxy作为前段负载均衡分摊后端web服务器压力,Keepalived保证haproxy的存活(双机热备:一台haproxy挂了,自动切换到另外一台haproxy上)2.环境(系统均为centos7,防火墙与selinux都关闭)192.168.0.100:web1(端口7000).web2(端口8000)192.168.0.101:haproxy1.keepalived(MASTER)192.168.0.102:haproxy2.keepalived(BA

haproxy + keepalived 实现web 双主模型的高可用负载均衡

参考文章 http://xz159065974.blog.51cto.com/8618592/1405812 http://blog.chinaunix.net/uid-25266990-id-3989321.html http://my.oschina.net/davehe/blog/162560 http://www.osyunwei.com/archives/7512.html http://fighter.blog.51cto.com/1318618/1254815/ Haproxy+K

Percona XtraDB Cluster(PXC)-高可用架构设计说明(原创)

Mycat+PXC高可用集群 一.架构图 架构说明: 1.mysql 集群高可用部分: l 针对业务场景选用Percona XtraDB Cluter(PXC)复制集群.两个片集群 PXC-dataNode01.PXC-dataNode01 由Mycat 管理,实现分库分表.读写分离 l PXC 复制模式保证强一致性.不会存在数据复制延迟:当client 发起事务请求只有在集群节点都同步响应完成时才会返回成功给客户端,否则回滚.客户端报错 l 节点集群扩展方案: 增加新节点集群需要对现有的需要分

MariaDB Galera Cluster集群

一.MariaDB Galera Cluster概要: 1.简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到 各个节点上去.在数据方面完全兼容 MariaDB 和 MySQL. 2.特性: (1).同步复制 Synchronous replication (2).Active-active multi-master 拓扑逻辑 (

利用MariaDB Galera Cluster实现mariadb的多主复制

利用MariaDB Galera Cluster实现mariadb的多主复制 时间:2014年01月08日 | 分类:MariaDB | 评论:0 条 | 浏览:870 次 一.MariaDB Galera Cluster概要: 1.简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到 各个节点上去.在数据方面完全兼容 MariaD

MariaDB Galera Cluster应用实践

数据是互联网公司的核心资产,所以好多公司在架构设计上不仅要保证业务系统的高可用,同时还要考虑数据存储的高可用以及安全性.在职公司是一家创业型公司,之前的应用系统是由.Net 和SQLserver组合的架构,由于存在业务量的增长,技术部门采用Java重构整个应用系统.数据库选择开源数据库MYSQL,从刚开始都现在踩了相当多的坑,在此给大家分享一下. 环境介绍: 服务器:阿里云主机 磁盘类型:SSD 操作系统:CentOS6.5 64位 软件版本:5.5.50-MariaDB-wsrep 1.数据库

MariaDB Galera Cluster实现mariadb的多主复制

一.MariaDB Galera Cluster概要: 1.简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到各个节点上去.在数据方面完全兼容 MariaDB 和 MySQL. 2.特性: (1).同步复制 Synchronous replication (2).Active-active multi-master 拓扑逻辑 (3