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 [iyunv@vm-105-23~]#/etc/init.d/haproxyrestart&&/etc/init.d/keepalivedrestart Stoppinghaproxy:[OK] Startinghaproxy:[OK] Stoppingkeepalived:[OK] Startingkeepalived:[OK]
6、查看vip是否起来 [iyunv@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 [iyunv@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服务器可以实现并发读操作 [iyunv@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
|