前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HAProxy + Keepalived实现MySQL的高可用负载均衡

HAProxy + Keepalived实现MySQL的高可用负载均衡

作者头像
星哥玩云
发布2022-06-13 08:14:28
6370
发布2022-06-13 08:14:28
举报
文章被收录于专栏:开源部署

HAProxy+Keepalived实现MySQL的高可用负载均衡

当前环境说明

A:10.10.99.180(MySQLMaster)----|

|________VIP(10.10.99.103) B:10.10.99.108(MySQLSlave)------| C:10.10.105.11(MySQLSlave) D:10.10.105.23(HAProxy+Keepalived)

E:10.10.105.24(HAProxy+Keepalived) VIP:10.10.105.30

如下操作在10.10.105.23和10.10.105.24上都要进行

1、调整内核参数,添加非本地IP绑定支持

#vi/etc/sysctl.conf net.ipv4.ip_nonlocal_bind=1 #sysctl–p

2、安装haproxy和keepalived yum-yinstallhaproxykeepalived

3、配置keepalived.conf文件

#vi/etc/haproxy/haproxy.cfg

!ConfigurationFileforkeepalived

global_defs{

notification_email{

jinyan2049@163.com

}

notification_email_fromkeepalived@chtopnet.com

smtp_server127.0.0.1

smtp_connect_timeout30

router_idLVS_DEVEL

}

vrrp_instanceVI_1{

stateMASTER#10.10.105.24设置为BACKUP

interfaceeth0

virtual_router_id51

realserver10.10.105.23#10.10.105.24改自己的ip

priority90#10.10.105.24设置为80

advert_int1

authentication{

auth_typePASS

auth_pass111111

}

virtual_ipaddress{

10.10.105.30

}

4、配置haproxy

#vi/etc/haproxy/haproxy.cfg

#thisconfigneedshaproxy-1.1.28orhaproxy-1.2.1

global

#log127.0.0.1local0

log127.0.0.1local1notice

maxconn5000

uid99

gid99

daemon

pidfile/var/run/haproxy.pid

defaults

logglobal

modehttp

#optionhttplog

optiondontlognull

retries3

optionredispatch

maxconn2000

contimeout5000

clitimeout50000

srvtimeout50000

listenMYSQL10.10.105.30:3306

modetcp

maxconn2000

balanceroundrobin

servermysql-10.10.99.10810.10.99.108:3306checkinter5000fall1rise2

servermysql-10.10.105.1110.10.105.11:3306checkinter5000fall1rise2

srvtimeout20000

listenstats_auth10.10.105.23:80

#listenstats_auth10.10.105.24:80#backupconfig

statsenable

statsuri/korea

statsauthadmin:12345

statsadminifTRUE

5、分别启动haproxy和keepalived

[root@vm-105-23~]#/etc/init.d/haproxyrestart&&/etc/init.d/keepalivedrestart

Stoppinghaproxy:[OK]

Startinghaproxy:[OK]

Stoppingkeepalived:[OK]

Startingkeepalived:[OK]

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2013-10/92062p2.htm

推荐阅读:

Haproxy+Keepalived搭建Weblogic高可用负载均衡集群 http://www.linuxidc.com/Linux/2013-09/89732.htm

Keepalived+HAProxy配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

CentOS 6.3下Haproxy+Keepalived+Apache配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现WEB群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Haproxy+Keepalived构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

6、查看vip是否起来

[root@vm-105-23~]#ipaddr

1:lo:<LOOPBACK,UP,LOWER_UP>mtu16436qdiscnoqueuestateUNKNOWN

link/loopback00:00:00:00:00:00brd00:00:00:00:00:00

inet127.0.0.1/8scopehostlo

inet6::1/128scopehost

valid_lftforeverpreferred_lftforever

2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000

link/ether52:54:00:6c:d5:95brdff:ff:ff:ff:ff:ff

inet10.10.105.23/24brd10.10.105.255scopeglobaleth0

inet10.10.105.30/32scopeglobaleth0

inet6fe80::5054:ff:fe6c:d595/64scopelink

valid_lftforeverpreferred_lftforever

7、因为我们知道crontab的颗粒细化度只有1分钟,不能细化到秒,所以我们编写haproxy循环检测脚本,并且放入后台运行

cd/home/ops/scripts

nohupshcheckhapid.sh&

vicheckhapid.sh

#!/bin/bash

whiletrue

do

HA=`ps-ef|grephaproxy|grep-vgrep|wc-l`

if[$HA-eq0];

then

/etc/init.d/haproxystart

echo"">/dev/null

sleep2

if[$HA-eq0];

then

/etc/init.d/keepalivedstop

fi

fi

sleep2

done

8、高可用测试

在10.10.105.23上执行ipaddr|grepeth0

[root@vm-105-23~]#ipaddr|grepeth0

eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000

inet10.10.105.23/24brd10.10.105.255scopeglobaleth0

inet10.10.105.30/32scopeglobaleth0

关闭haproxy再打开一个终端继续执行ipaddr|grepeth0,观测

可以发现当无法迅速启动haproxy进程之后,循环脚本会杀死keepalived进程,实现vip的迁移

10.10.105.30迅速转移到10.10.105.24这台备机上!

9、MySQL测试

程序在读写分离之后,所有的select语句可以直接通过访问10.10.105.303306来进行工作,后端的mysql服务器可以实现并发读操作

[root@vm-105-23scripts]#mysqlslap-h10.10.105.30--concurrency=100--iterations=1--create-schema='ultrax'--query='select*frompre_home_share;'--number-of-queries=10--debug-info-ubbs-piz3n3s0ft

Benchmark

Averagenumberofsecondstorunallqueries:1.028seconds

Minimumnumberofsecondstorunallqueries:1.028seconds

Maximumnumberofsecondstorunallqueries:1.028seconds

Numberofclientsrunningqueries:100

Averagenumberofqueriesperclient:0

Usertime0.02,Systemtime0.08

Maximumresidentsetsize6996,Integralresidentsetsize0

Non-physicalpagefaults1584,Physicalpagefaults0,Swaps0

Blocksin0out0,Messagesin0out0,Signals0

Voluntarycontextswitches1280,Involuntarycontextswitches91

10、效果

可以访问http://10.10.105.23/korea

用户名admin

密码12345

HAProxy+Keepalived实现MySQL的高可用负载均衡

当前环境说明

A:10.10.99.180(MySQLMaster)----|

|________VIP(10.10.99.103) B:10.10.99.108(MySQLSlave)------| C:10.10.105.11(MySQLSlave) D:10.10.105.23(HAProxy+Keepalived)

E:10.10.105.24(HAProxy+Keepalived) VIP:10.10.105.30

如下操作在10.10.105.23和10.10.105.24上都要进行

1、调整内核参数,添加非本地IP绑定支持

#vi/etc/sysctl.conf net.ipv4.ip_nonlocal_bind=1 #sysctl–p

2、安装haproxy和keepalived yum-yinstallhaproxykeepalived

3、配置keepalived.conf文件

#vi/etc/haproxy/haproxy.cfg

!ConfigurationFileforkeepalived

global_defs{

notification_email{

jinyan2049@163.com

}

notification_email_fromkeepalived@chtopnet.com

smtp_server127.0.0.1

smtp_connect_timeout30

router_idLVS_DEVEL

}

vrrp_instanceVI_1{

stateMASTER#10.10.105.24设置为BACKUP

interfaceeth0

virtual_router_id51

realserver10.10.105.23#10.10.105.24改自己的ip

priority90#10.10.105.24设置为80

advert_int1

authentication{

auth_typePASS

auth_pass111111

}

virtual_ipaddress{

10.10.105.30

}

4、配置haproxy

#vi/etc/haproxy/haproxy.cfg

#thisconfigneedshaproxy-1.1.28orhaproxy-1.2.1

global

#log127.0.0.1local0

log127.0.0.1local1notice

maxconn5000

uid99

gid99

daemon

pidfile/var/run/haproxy.pid

defaults

logglobal

modehttp

#optionhttplog

optiondontlognull

retries3

optionredispatch

maxconn2000

contimeout5000

clitimeout50000

srvtimeout50000

listenMYSQL10.10.105.30:3306

modetcp

maxconn2000

balanceroundrobin

servermysql-10.10.99.10810.10.99.108:3306checkinter5000fall1rise2

servermysql-10.10.105.1110.10.105.11:3306checkinter5000fall1rise2

srvtimeout20000

listenstats_auth10.10.105.23:80

#listenstats_auth10.10.105.24:80#backupconfig

statsenable

statsuri/korea

statsauthadmin:12345

statsadminifTRUE

5、分别启动haproxy和keepalived

[root@vm-105-23~]#/etc/init.d/haproxyrestart&&/etc/init.d/keepalivedrestart

Stoppinghaproxy:[OK]

Startinghaproxy:[OK]

Stoppingkeepalived:[OK]

Startingkeepalived:[OK]

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档