用HAproxy+keepalived+mysql Replication 构建基于企业级负载均衡

最近用HAproxy+keepalived+mysql复制测试高可用性Linux系统集群。 HAProxy是一款免费的提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,借助HAProxy可以快速并且可靠的提供基于TCP和HTTP应用的代理解决方案。 Keepalived主要作用是LoadBalance master和LoadBalance backup之间的健康检查,实现故障转换。 Mysql Replication主要作用是提高mysql并处理数据的能力以及实现容灾备份的作用。

项目拓扑图:

Haproxy服务器及其IP地址规划:

项目实施:

1.Mysql主从复制配置。

1)在master服务器上创建mysql用户(授权复制账户)。grant replication slave on *.* to’rep’@’192.168.1.244’identified by ‘rep123′;2)编辑master服务器的mysql配置文件my.cnf。server-id = 1 //指定服务器的IDlog-bin = mysql-bin //开启二进制日志binlog-ignore = mysql //忽略mysql和information_schema 数据库binlog-ignore = information_schemabinlog-do-db = blog //同步数据库,默认同步所有数据库3)查看master状态。mysql> show master status;+—————+———-+————–+————————–+| File| Position | Binlog_Do_DB | Binlog_Ignore_DB|+—————+———-+————–+————————–+| binlog.022343 | 339244 | blog| mysql,information_schema |+—————+———-+————–+————————–+1 row in set (0.00 sec)4)在slave端创建数据库blog,导出master端的blog库,导入到此库,并修改mysql主配置文件my.cnf server-id = 2 重启mysql数据库。mysql> change master to-> master_host=’192.168.1.243′,-> master_port=3306,-> master_user=’rep’,-> master_password=’rep123′,-> master_log_file=’binlog.022343′,-> master_log_pos=339244;5)mysql主从同步测试,show slave status\G;能看到Slave_IO_Running和Slave_SQL_Running都为YES即可。mysql> show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.1.243Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog.022343Read_Master_Log_Pos: 339110Relay_Log_File: relaylog.005481Relay_Log_Pos: 339244Relay_Master_Log_File: binlog.022343Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB: mysql,test,information_schemaReplicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 339110Relay_Log_Space: 339244Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 01 row in set (0.00 sec)

wget tar -zxvf haproxy-1.4.24.tar.gzcd haproxy-1.4.24make TARGET=linux26 PREFIX=/usr/local/haproxymake install PREFIX=/usr/local/haproxycd /usr/local/haproxymkdir conf logs //在此目录下面建立conf,香港虚拟主机,logs目录分别存放HAproxy的配置文件,PID文件和日志文件。vim conf/haproxy.confglobalmaxconn 50000chroot /usr/local/haproxyuid 99gid 99daemonquietnbproc 2pidfile /usr/local/haproxy/logs/haproxy.pid#debugdefaultslogglobalmode httpoption httplog #每次请求完毕后主动关闭http通道option dontlognull #不记录健康检查的日志信息option forwardforoption redispatchoption abortoncloseretries 3log 127.0.0.1 local3maxconn 20000contimeout5000clitimeout50000srvtimeout50000listen 192.168.1.236bind *:80mode httpstats uri /admin#后端服务器状态查看地址stats auth admin:admin#状态查看页面登陆帐号密码balance source #调度算法,source是和nginx的ip_hash同理,解决session问题option httpcloseoption forwardforserver web1 192.168.1.248:80 weight 5 check inter 2000 rise 2 fall 5server web2 192.168.1.249:8080 weight 5 check inter 2000 rise 2 fall 5#启动Haproxy服务/usr/local/haproxy/sbin/haproxy -f haproxy.conf#haproxy启动脚本#!/bin/bashBASE_DIR=”/usr/local/haproxy”ARGV=”$@”start(){echo “START HAPoxy SERVERS”$BASE_DIR/sbin/haproxy -f $BASE_DIR/conf/haproxy.conf}stop(){echo “STOP HAPoxy Listen”kill -TTOU $(cat $BASE_DIR/logs/haproxy.pid)echo “STOP HAPoxy process”kill -USR1 $(cat $BASE_DIR/logs/haproxy.pid)}case $ARGV instart)startERROR=$?;;stop)stopERROR=$?;;restart)stopstartERROR=$?;;*)echo “hactl.sh [start|restart|stop]”esacexit $ERROR

3.Haproxy开启系统日志支持。

vim /etc/syslog.conf#添加:local3.*/var/log/haproxy.loglocal0.*/var/log/haproxy.logvim /etc/sysconfig/syslog#修改:SYSLOGD_OPTIONS=”-r -m 0″#重新启动syslog服务/etc/init.d/syslog restart

4.Keepalived的安装配置。

wget ln -s /usr/src/kernels/2.6.18-164.el5-x86_64/ /usr/src/linuxtar -zxvf keepalived-1.2.5.tar.gzcd keepalived-1.2.5./configure –prefix=/usr/local/keepalivedmake && make installcp keepalived/etc/init.d/keepalived.rh.init /etc/init.d/keepalivedchmod +x /etc/init.d/keepalivedcp keepalived/etc/init.d/keepalived.sysconfig /etc/sysconfig/keepalivedchkconfig –add keepalivedchkconfig –level 35 keepalived oncp /usr/local/keepalived/sbin/keepalived /bin/mkdir -p /etc/keepalivedcp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/#Haproxy master配置文件vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {shifeng_zhang88@163.com}notification_email_from shifeng_zhang88@163.comsmtp_server smtp.163.comsmtp_connect_timeout 30router_id LVS_Master}vrrp_script chk_http_port {script “/etc/keepalived/check_haproxy.sh”interval5#脚本执行间隔weight-5#执行脚本后优先级变更:5表示优先级+5;-5则表示优先级-5}vrrp_instance VI_A {state MASTERinterface eth0virtual_router_id 50priority 100advert_int 1authentication {auth_type PASSauth_pass sfzhang1109}track_script {chk_http_port}virtual_ipaddress {192.168.1.236 #haproxy虚拟IP}}#Haproxy backup配置文件只需改变state和priority的值state BACKUPpriority 50#启动keepalived服务/etc/init.d/keepalived start

5.编辑check_haproxy.sh脚本,香港服务器,需要将haproxy启动脚本放到/etc/init.d/里面。

只是微笑地固执自己的坚持,

用HAproxy+keepalived+mysql Replication 构建基于企业级负载均衡

相关文章:

你感兴趣的文章:

标签云: