|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!比来用HAproxy+keepalived+mysql复制测试高可用性Linux体系集群。
HAProxy是一款收费的供给高可用性、负载平衡和基于TCP(第四层)和HTTP(第七层)运用的署理软件,借助HAProxy可以疾速而且靠得住的供给基于TCP和HTTP运用的署理处理计划。
Keepalived重要感化是LoadBalancemaster和LoadBalancebackup之间的安康检讨,完成毛病转换。
MysqlReplication重要感化是进步mysql并处置数据的才能和完成容灾备份的感化。
项目拓扑图:
Haproxy办事器及其IP地址计划:
项目实行:
1.Mysql主从复制设置装备摆设。
- 1)在master办事器上创立mysql用户(受权复制账户)。grantreplicationslaveon*.*torep@192.168.1.244identifiedbyrep123;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>showmasterstatus;+---------------+----------+--------------+--------------------------+|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|+---------------+----------+--------------+--------------------------+|binlog.022343|339244|blog|mysql,information_schema|+---------------+----------+--------------+--------------------------+1rowinset(0.00sec)4)在slave端创立数据库blog,导出master真个blog库,导入到此库,并修正mysql主设置装备摆设文件my.cnfserver-id=2重启mysql数据库。mysql>changemasterto->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主从同步测试,showslavestatusG;能看到Slave_IO_Running和Slave_SQL_Running都为YES便可。mysql>showslavestatusG;***************************1.row***************************Slave_IO_State:WaitingformastertosendeventMaster_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:01rowinset(0.00sec)
复制代码
2.Haproxy装置及其设置装备摆设,master和backup装置设置装备摆设都是完整一样的.
- wgethttp://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gztar-zxvfhaproxy-1.4.24.tar.gzcdhaproxy-1.4.24makeTARGET=linux26PREFIX=/usr/local/haproxymakeinstallPREFIX=/usr/local/haproxycd/usr/local/haproxymkdirconflogs//在此目次上面树立conf,logs目次分离寄存HAproxy的设置装备摆设文件,PID文件和日记文件。vimconf/haproxy.confglobalmaxconn50000chroot/usr/local/haproxyuid99gid99daemonquietnbproc2pidfile/usr/local/haproxy/logs/haproxy.pid#debugdefaultslogglobalmodehttpoptionhttplog#每次要求终了后自动封闭http通道optiondontlognull#不记载安康检讨的日记信息optionforwardforoptionredispatchoptionabortoncloseretries3log127.0.0.1local3maxconn20000contimeout5000clitimeout50000srvtimeout50000listen192.168.1.236bind*:80modehttpstatsuri/admin#后端办事器状况检查地址statsauthadmin:admin#状况检查页面上岸帐号暗码balancesource#调剂算法,source是和nginx的ip_hash同理,处理session成绩optionhttpcloseoptionforwardforserverweb1192.168.1.248:80weight5checkinter2000rise2fall5serverweb2192.168.1.249:8080weight5checkinter2000rise2fall5#启动Haproxy办事/usr/local/haproxy/sbin/haproxy-fhaproxy.conf#haproxy启动剧本#!/bin/bashBASE_DIR="/usr/local/haproxy"ARGV="$@"start(){echo"STARTHAPoxySERVERS"$BASE_DIR/sbin/haproxy-f$BASE_DIR/conf/haproxy.conf}stop(){echo"STOPHAPoxyListen"kill-TTOU$(cat$BASE_DIR/logs/haproxy.pid)echo"STOPHAPoxyprocess"kill-USR1$(cat$BASE_DIR/logs/haproxy.pid)}case$ARGVinstart)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-m0"#从新启动syslog办事/etc/init.d/syslogrestart
复制代码
4.Keepalived的装置设置装备摆设。
- wgethttp://www.keepalived.org/software/keepalived-1.2.5.tar.gzln-s/usr/src/kernels/2.6.18-164.el5-x86_64//usr/src/linuxtar-zxvfkeepalived-1.2.5.tar.gzcdkeepalived-1.2.5./configure--prefix=/usr/local/keepalivedmake&&makeinstallcpkeepalived/etc/init.d/keepalived.rh.init/etc/init.d/keepalivedchmod+x/etc/init.d/keepalivedcpkeepalived/etc/init.d/keepalived.sysconfig/etc/sysconfig/keepalivedchkconfig--addkeepalivedchkconfig--level35keepalivedoncp/usr/local/keepalived/sbin/keepalived/bin/mkdir-p/etc/keepalivedcp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/#Haproxymaster设置装备摆设文件vim/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{shifeng_zhang88@163.com}notification_email_fromshifeng_zhang88@163.comsmtp_serversmtp.163.comsmtp_connect_timeout30router_idLVS_Master}vrrp_scriptchk_http_port{script"/etc/keepalived/check_haproxy.sh"interval5#剧本履行距离weight-5#履行剧本后优先级变革:5表现优先级+5;-5则表现优先级-5}vrrp_instanceVI_A{stateMASTERinterfaceeth0virtual_router_id50priority100advert_int1authentication{auth_typePASSauth_passsfzhang1109}track_script{chk_http_port}virtual_ipaddress{192.168.1.236#haproxy虚拟IP}}#Haproxybackup设置装备摆设文件只需转变state和priority的值stateBACKUPpriority50#启动keepalived办事/etc/init.d/keepalivedstart
复制代码
5.编纂check_haproxy.sh剧本,须要将haproxy启动剧本放到/etc/init.d/外面。
- vim/etc/keepalived/check_haproxy.sh#!/bin/bashA=`ps-Chaproxy--no-header|wc-l`if[$A-eq0];then/etc/init.d/haproxyrestartecho"Starthaproxy"&>/dev/nullsleep3if[`ps-Chaproxy--no-header|wc-l`-eq0];then/etc/init.d/keepalivedstopecho"Stopkeepalived"&>/dev/nullfifi#chomd+x/etc/keepalived/check_haproxy.sh
复制代码
6.Haproxy+Keepalived高可用性测试。
1)Haproxy+Keepalived高可用测试。
测试办法:停滞master的keepalived办事,检查备用的keepalived的日记发明当主节点宕机时,备用节点脚色会立刻变成主节点,启用VIPS协定,并把VIP地址连忙绑定到eth0网卡下面,当主节点恢复时则做相反的任务。
2)Haproxy+Keepalived负载平衡测试
测试办法:当客户端拜访VIP的时刻,haproxy依据设置的调剂算法和权重把拜访要求分发到后真个WEB办事器下面,从而完成了负载平衡的功效。
3)Haproxy+Keepalived毛病转移测试
测试办法:当后真个WEB办事器down机时,haproxy会主动检测到并把要求发送到正常的办事器下面,经由过程haproxy监控页面(http://192.168.1.235/admin)可以检查。
解释:(愿望人人提出名贵的看法)
1)拓扑图只是项目标一个雏形,前期还要添加监控办事器和邮件等办事器。
2)Mysql主库担任用户数据的写入,slave担任用户数据的读取和数据的备份等,除要监控mysql主从外还要监控Seconds_Behind_Master选项,以避免发生主从数据分歧步或许延时。
3)当WEB的负载很年夜的时刻,可以在haproxy添加多台物理机便可,并斟酌把项目标图片分别出来做成零丁的图片办事器。
欢迎大家来到仓酷云论坛! |
|