阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

272次阅读
没有评论

共计 6335 个字符,预计需要花费 16 分钟才能阅读完成。

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.2310.10.105.24上都要进行

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

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

2、安装 haproxykeepalived
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.2310.10.105.24上都要进行

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

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

2、安装 haproxykeepalived
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

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计6335字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中