《MysqL教程Keepalived+HAProxy实现MysqL高可用负载均衡的配置》要点:
本文介绍了MysqL教程Keepalived+HAProxy实现MysqL高可用负载均衡的配置,希望对您有用。如果有疑问,可以联系我们。
KeepalivedMysqL进修
由于在生产环境使用了MysqLcluster,必要实现高可用负载均衡,这里提供了keepalived+haproxy来实现.MysqL学习
keepalived主要功效是实现真实机器的故障隔离及负载均衡器间的失败切换.可在第3,4,5层交换.它通过VRRPv2(Virtual Router Redundancy Protocol) stack实现的.MysqL学习
Layer3:Keepalived会定期向服务器群中的服务器.发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便申报这台服务器失效,并将它从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机.Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准.MysqL学习
Layer4:主要以TCP端口的状态来决议服务器工作正常与否.如web server的服务端口一般是80,如果Keepalived检测到80端口没有启动,则Keepalived将把这台服务器从服务器群中剔除.MysqL学习
Layer5:在网络上占用的带宽也要大一些.Keepalived将根据用户的设定检查服务器法式的运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器群中剔除.MysqL学习
Software Design
MysqL进修
keepalived启动后会有单个过程MysqL学习
8352 ? Ss 0:00 /usr/sbin/keepalived8353 ? S 0:00 \_ /usr/sbin/keepalived8356 ? S 0:01 \_ /usr/sbin/keepalived
父进程:内存治理,子进程治理等等MysqL学习
子过程:VRRP子过程MysqL学习
子过程:Healthchecking 子过程
实例MysqL学习
2台MysqLcluster 10.1.6.203 master 10.1.6.205 backupMysqL进修
vip 10.1.6.173MysqL进修
目的拜访10.1.6.173 3366端口 分别轮询通过haproxy转发到10.1.6.203 3306 和10.1.6.205 3306MysqL学习
MysqLcluster搭建参照之前博客,这里在2台机上安装keepalivedMysqL进修
root@10.1.6.203:~# apt-get install keepalivedroot@10.1.6.203:~# cat /etc/keepalived/keepalived.conf vrrp_script chk_haproxy { script "killall -0 haproxy" # verify the pID existance interval 2 # check every 2 seconds weight -2 # add 2 points of prio if OK} vrrp_instance VI_1 { interface eth1 # interface to monitor state MASTER virtual_router_ID 51 # Assign one ID for this route priority 101 # 101 on master,100 on backup nopreempt deBUG virtual_ipaddress { 10.1.6.173 } track_script { #注意大括号空格 chk_haproxy } notify_master /etc/keepalived/scripts/start_haproxy.sh #表示当切换到master状态时,要执行的脚本 notify_fault /etc/keepalived/scripts/stop_keepalived.sh #故障时执行的脚本 notify_stop /etc/keepalived/scripts/stop_haproxy.sh #keepalived停止运行前运行notify_stop指定的脚本 }
VRRPD配置包含三个类:MysqL学习
VRRP同步组(synchroization group) VRRP实例(VRRP Instance) VRRP剧本这里使用了 VRRP实例,VRRP剧本MysqL学习
注意设置装备摆设选项:MysqL学习
stat:指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定,里如果这里设置为master,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送本身的优先级,另外一台发现优先级不如本身的高,那么他会就回抢占为masterMysqL学习
interface:实例绑定的网卡,因为在配置虚拟IP的时候必需是在已有的网卡上添加的 priority 101:设置本节点的优先级,优先级高的为master deBUG:deBUG级别 nopreempt:设置为不抢占vrrp_script chk_haproxy { script "killall -0 haproxy" # verify the pID existance interval 2 # check every 2 seconds 脚本执行间隔 weight -2 # add 2 points of prio if OK 脚本结果导致的优先级变更:2表示优先级+2;-2则表示优先级-2}
然后在实例(vrrp_instance)里面引用,有点类似脚本里面的函数引用一样:先定义,后引用函数名
MysqL学习
track_script { chk_haproxy }
注意:VRRP剧本(vrrp_script)和VRRP实例(vrrp_instance)属于同一个级别MysqL学习
root@10.1.6.203:scripts# cat start_haproxy.sh #!/bin/bash sleep 5get=`ip addr |grep 10.1.6.173 |wc -l`echo $get >> /etc/keepalived/scripts/start_ha.log if [ $get -eq 1 ]then echo "`date +%c` success to get vip" >> /etc/keepalived/scripts/start_ha.log /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfgelse echo "`date +%c` can not get vip" >> /etc/keepalived/scripts/start_ha.logfiroot@10.1.6.203:scripts# cat stop_keepalived.sh #!/bin/bash pID=`pIDof keepalived`if [ $pID == "" ]then echo "`date +%c` no keepalived process ID" >> /etc/keepalived/scripts/stop_keep.logelse echo "`date +%c` will stop keepalived " >> /etc/keepalived/scripts/stop_keep.log /etc/init.d/keepalived stopfi /etc/init.d/keepalived stop root@10.1.6.203:scripts# cat stop_haproxy.sh #!/bin/bash pID=`pIDof haproxy`echo "`date +%c` stop haproxy" >> /etc/keepalived/scripts/stop_ha.logkill -9 $pID
同理设置装备摆设10.1.6.205MysqL学习
root@10.1.6.205:~# cat /etc/keepalived/keepalived.conf vrrp_script chk_haproxy { script "killall -0 haproxy" # verify the pID existance interval 2 # check every 2 seconds weight 2 # add 2 points of prio if OK} vrrp_instance VI_1 { interface eth1 # interface to monitor state BACKUP virtual_router_ID 51 # Assign one ID for this route priority 100 # 101 on master,100 on backup virtual_ipaddress { 10.1.6.173 } track_script { chk_haproxy } notify_master /etc/keepalived/scripts/start_haproxy.shnotify_fault /etc/keepalived/scripts/stop_keepalived.shnotify_stop /etc/keepalived/scripts/stop_haproxy.sh }
HAProxyMysqL进修
下面再先容下haproxyMysqL学习
HAProxy是一款基于TCP(第四层)和http(第七层)应用的代理软件,它也可作为负载均衡器.可以支持数以万计的并发连接.同时可以掩护服务器不暴露到网络上,通过端口映射.它还自带监控服务器状态的页面.MysqL学习
安装haproxyMysqL进修
wget -O/tmp/haproxy-1.4.22.tar.gz http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gztar xvfz /tmp/haproxy-1.4.22.tar.gz -C /tmp/cd /tmp/haproxy-1.4.22make TARGET=linux26make install
haproxy必要对每一个MysqLcluster服务器进行健康检查MysqL学习
1.在2台主机分离配置haproxy.cfgMysqL学习
root@10.1.6.203:scripts# cat /etc/haproxy/haproxy.cfg global maxconn 51200 #默认最大连接数 #uID 99 #gID 99 daemon #以后台形式运行haproxy #quIEt nbproc 1 #进程数量(可以设置多个进程提高性能) pIDfile /etc/haproxy/haproxy.pID #haproxy的pID存放路径,启动进程的用户必须有权限访问此文件 defaults mode tcp #所处理的类别 (#7层 http;4层tcp ) option redispatch #serverID对应的服务器挂掉后,强制定向到其他健康的服务器 option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接 timeout connect 5000s #连接超时 timeout clIEnt 50000s #客户端超时 timeout server 50000s #服务器超时 log 127.0.0.1 local0 #错误日志记录 balance roundrobin #默认的负载均衡的方式,轮询方式 Listen proxy bind 10.1.6.173:3366 #监听端口 mode tcp #http的7层模式 option httpchk #心跳检测的文件 server db1 10.1.6.203:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3 #服务器定义,check inter 12000是检测心跳频率 rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重 server db2 10.1.6.205:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3 Listen haproxy_stats mode http bind 10.1.6.173:8888 option httplog stats refresh 5s stats uri /status #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200,不正常返回503 stats realm Haproxy Manager stats auth admin:p@a1SZs24 #账号暗码root@10.1.6.205:~$ cat /etc/haproxy/haproxy.cfg global maxconn 51200 #uID 99 #gID 99 daemon #quIEt nbproc 1 pIDfile /etc/haproxy/haproxy.pID defaults mode tcp option redispatch option abortonclose timeout connect 5000s timeout clIEnt 50000s timeout server 50000s log 127.0.0.1 local0 balance roundrobin Listen proxy bind 10.1.6.173:3366 mode tcp option httpchk server db1 10.1.6.203:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3 server db2 10.1.6.205:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3 Listen haproxy_stats mode http bind 10.1.6.173:8888 option httplog stats refresh 5s stats uri /status stats realm Haproxy Manager stats auth admin:p@a1SZs24
2.安装xinetdMysqL学习
root@10.1.6.203:~# apt-get install xinetd
3.在每个节点添加xinetd服务脚本和MysqLchk端标语MysqL学习
root@10.1.6.203:~# vim /etc/xinetd.d/MysqLchk # default: on# description: MysqLchkservice MysqLchk #必要在servive定义{ flags = REUSE socket_type = stream port = 9222 wait = no user = nobody server = /opt/MysqLchk log_on_failure += USERID disable = no per_source = UNliMITED bind = 10.1.6.173} root@10.1.6.203:~# vim /etc/services MysqLchk 9222/tcp # MysqLchk
4.编写MysqLchk监控服务脚本MysqL学习
root@10.1.6.203:~# ls -l /opt/MysqLchk -rwxr--r-- 1 nobody root 1994 2013-09-17 11:27 /opt/MysqLchkroot@10.1.6.203:~# cat /opt/MysqLchk #!/bin/bash## This script checks if a MysqL server is healthy running on localhost. It will# return:# "http/1.x 200 OK\r" (if MysqL is running smoothly)# - OR -# "http/1.x 500 Internal Server Error\r" (else)## The purpose of this script is make haproxy capable of monitoring MysqL properly# MysqL_HOST="localhost"MysqL_SOCKET="/var/run/MysqLd/MysqLd.sock"MysqL_USERname="MysqLchkusr" #该账户暗码需要在MysqL里添加MysqL_PASSWORD="secret"MysqL_OPTS="-N -q -A"TMP_file="/dev/shm/MysqLchk.$$.out"ERR_file="/dev/shm/MysqLchk.$$.err"FORCE_FAIL="/dev/shm/proxyoff"MysqL_BIN="/opt/MysqLcluster/MysqL-cluster-gpl-7.2.6-linux2.6-x86_64/bin/MysqL"CHECK_query="select 1" preflight_check(){ for I in "$TMP_file" "$ERR_file"; do if [ -f "$I" ]; then if [ ! -w $I ]; then echo -e "http/1.1 503 Service Unavailable\r\n" echo -e "Content-Type: Content-Type: text/plain\r\n" echo -e "\r\n" echo -e "Cannot write to $I\r\n" echo -e "\r\n" exit 1 fi fi done} return_ok(){ echo -e "http/1.1 200 OK\r\n" echo -e "Content-Type: text/HTML\r\n" echo -e "Content-Length: 43\r\n" echo -e "\r\n" echo -e "<HTML><body>MysqL is running.</body></HTML>\r\n" echo -e "\r\n" rm $ERR_file $TMP_file exit 0}return_fail(){ echo -e "http/1.1 503 Service Unavailable\r\n" echo -e "Content-Type: text/HTML\r\n" echo -e "Content-Length: 42\r\n" echo -e "\r\n" echo -e "<HTML><body>MysqL is *down*.</body></HTML>\r\n" sed -e 's/\n$/\r\n/' $ERR_file echo -e "\r\n" rm $ERR_file $TMP_file exit 1}preflight_checkif [ -f "$FORCE_FAIL" ]; then echo "$FORCE_FAIL found" > $ERR_file return_fail;fi$MysqL_BIN $MysqL_OPTS --host=$MysqL_HOST --socket=$MysqL_SOCKET --user=$MysqL_USERname --password=$MysqL_PASSWORD -e "$CHECK_query" > $TMP_file 2> $ERR_fileif [ $? -ne 0 ]; then return_fail;fireturn_ok;
测试MysqL学习
2个节点开启keepalived(主节点会得到vip,自动拉起haproxy),xinetdMysqL学习
root@10.1.6.203:~# ip add1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNowN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo2: eth0: <broADCAST,MulTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 00:26:b9:36:0f:81 brd ff:ff:ff:ff:ff:ff inet 211.151.105.186/26 brd 211.151.105.191 scope global eth03: eth1: <broADCAST,MulTICAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:26:b9:36:0f:83 brd ff:ff:ff:ff:ff:ff inet 10.1.6.203/24 brd 10.1.6.255 scope global eth1 inet 10.1.6.173/32 scope global eth14: eth2: <broADCAST,MulTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:26:b9:36:0f:85 brd ff:ff:ff:ff:ff:ff5: eth3: <broADCAST,MulTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:26:b9:36:0f:87 brd ff:ff:ff:ff:ff:ffroot@10.1.6.203:~# netstat -tunlp | grep hatcp 0 0 10.1.6.173:3366 0.0.0.0:* ListEN 1042/haproxy tcp 0 0 10.1.6.203:8888 0.0.0.0:* ListEN 1042/haproxy udp 0 0 0.0.0.0:56562 0.0.0.0:* 1042/haproxy root@10.1.6.203:~# netstat -tunlp | grep xinetcp 0 0 10.1.6.203:9222 0.0.0.0:* ListEN 30897/xinetd root@10.1.6.203:~# ps -ef | grep haproxyroot 1042 1 0 Sep17 ? 00:00:00 /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg
测试:MysqL进修
通过vip10.1.6.173 3366拜访cluster数据库(注意账户dave权限需要加3个ip10.1.6.203,10.1.6.205,10.1.6.173)MysqL学习
root@10.1.6.203:mgm# MysqL -udave -p -h 10.1.6.173 -P 3366Enter password: Welcome to the MysqL monitor. Commands end with ; or \g.Your MysqL connection ID is 1344316Server version: 5.5.22-ndb-7.2.6-gpl-log MysqL Cluster Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. MysqL> show databases;+--------------------+| Database |+--------------------+| information_schema | | dave | | test | +--------------------+3 rows in set (0.01 sec) MysqL>
手动分别使keepalive,haproxy,数据库挂掉.vip10.1.6.173会自动漂到10.1.6.205从上,并不影响vip的拜访MysqL学习
经由过程vip,haproxy查看各节点状态MysqL学习
http://10.1.6.173:8888/status
MysqL进修
欢迎参与《MysqL教程Keepalived+HAProxy实现MysqL高可用负载均衡的配置》讨论,分享您的想法,内存溢出PHP学院为您提供专业教程。
总结以上是内存溢出为你收集整理的MYSQL教程Keepalived+HAProxy实现MySQL高可用负载均衡的配置全部内容,希望文章能够帮你解决MYSQL教程Keepalived+HAProxy实现MySQL高可用负载均衡的配置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)