HAProxy + Keepalived实现MySQL的高可用负载均衡
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–p2、安装haproxy和keepalived
yum-yinstallhaproxykeepalived
3、配置keepalived.conf文件#vi/etc/haproxy/haproxy.cfg!ConfigurationFileforkeepalivedglobal_defs{notification_email{jinyan2049@163.com}notification_email_fromkeepalived@chtopnet.comsmtp_server127.0.0.1smtp_connect_timeout30router_idLVS_DEVEL}vrrp_instanceVI_1{stateMASTER#10.10.105.24设置为BACKUPinterfaceeth0virtual_router_id51realserver10.10.105.23#10.10.105.24改自己的ippriority90#10.10.105.24设置为80advert_int1authentication{auth_typePASSauth_pass111111}virtual_ipaddress{10.10.105.30}4、配置haproxy#vi/etc/haproxy/haproxy.cfg#thisconfigneedshaproxy-1.1.28orhaproxy-1.2.1global#log127.0.0.1local0log127.0.0.1local1noticemaxconn5000uid99gid99daemonpidfile/var/run/haproxy.piddefaultslogglobalmodehttp#optionhttplogoptiondontlognullretries3optionredispatchmaxconn2000contimeout5000clitimeout50000srvtimeout50000listenMYSQL10.10.105.30:3306modetcpmaxconn2000balanceroundrobinservermysql-10.10.99.10810.10.99.108:3306checkinter5000fall1rise2servermysql-10.10.105.1110.10.105.11:3306checkinter5000fall1rise2srvtimeout20000listenstats_auth10.10.105.23:80#listenstats_auth10.10.105.24:80#backupconfigstatsenablestatsuri/koreastatsauthadmin:12345statsadminifTRUE
5、分别启动haproxy和keepalived#/etc/init.d/haproxyrestart&&/etc/init.d/keepalivedrestartStoppinghaproxy:Startinghaproxy:Stoppingkeepalived:Startingkeepalived:
6、查看vip是否起来#ipaddr1:lo:<LOOPBACK,UP,LOWER_UP>mtu16436qdiscnoqueuestateUNKNOWNlink/loopback00:00:00:00:00:00brd00:00:00:00:00:00inet127.0.0.1/8scopehostloinet6::1/128scopehostvalid_lftforeverpreferred_lftforever2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000link/ether52:54:00:6c:d5:95brdff:ff:ff:ff:ff:ffinet10.10.105.23/24brd10.10.105.255scopeglobaleth0inet10.10.105.30/32scopeglobaleth0inet6fe80::5054:ff:fe6c:d595/64scopelinkvalid_lftforeverpreferred_lftforever
7、因为我们知道crontab的颗粒细化度只有1分钟,不能细化到秒,所以我们编写haproxy循环检测脚本,并且放入后台运行cd/home/ops/scriptsnohupshcheckhapid.sh&vicheckhapid.sh#!/bin/bashwhiletruedoHA=`ps-ef|grephaproxy|grep-vgrep|wc-l`if[$HA-eq0];then/etc/init.d/haproxystartecho"">/dev/nullsleep2if[$HA-eq0];then/etc/init.d/keepalivedstopfifisleep2done
8、高可用测试在10.10.105.23上执行ipaddr|grepeth0#ipaddr|grepeth0eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000inet10.10.105.23/24brd10.10.105.255scopeglobaleth0inet10.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服务器可以实现并发读操作#mysqlslap-h10.10.105.30--concurrency=100--iterations=1--create-schema='ultrax'--query='select*frompre_home_share;'--number-of-queries=10--debug-info-ubbs-piz3n3s0ftBenchmarkAveragenumberofsecondstorunallqueries:1.028secondsMinimumnumberofsecondstorunallqueries:1.028secondsMaximumnumberofsecondstorunallqueries:1.028secondsNumberofclientsrunningqueries:100Averagenumberofqueriesperclient:0
Usertime0.02,Systemtime0.08Maximumresidentsetsize6996,Integralresidentsetsize0Non-physicalpagefaults1584,Physicalpagefaults0,Swaps0Blocksin0out0,Messagesin0out0,Signals0Voluntarycontextswitches1280,Involuntarycontextswitches91
10、效果图可以访问http://10.10.105.23/korea用户名admin密码12345
花花世界卜必当真、卜管你们吻得多逼真。
页:
[1]