本文共 7120 字,大约阅读时间需要 23 分钟。
最近用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 event Master_Host: 192.168.1.243 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.022343 Read_Master_Log_Pos: 339110 Relay_Log_File: relaylog.005481 Relay_Log_Pos: 339244 Relay_Master_Log_File: binlog.022343 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql,test,information_schema Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 339110 Relay_Log_Space: 339244 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_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)
2.Haproxy安装及其配置,master和backup安装配置都是完全一样的。
wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gztar -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.confglobal maxconn 50000 chroot /usr/local/haproxy uid 99 gid 99 daemon quiet nbproc 2 pidfile /usr/local/haproxy/logs/haproxy.pid #debugdefaults log global mode http option httplog #每次请求完毕后主动关闭http通道 option dontlognull #不记录健康检查的日志信息 option forwardfor option redispatch option abortonclose retries 3 log 127.0.0.1 local3 maxconn 20000 contimeout 5000 clitimeout 50000 srvtimeout 50000listen 192.168.1.236 bind *:80 mode http stats uri /admin #后端服务器状态查看地址 stats auth admin:admin #状态查看页面登陆帐号密码 balance source #调度算法,source是和nginx的ip_hash同理,解决session问题 option httpclose option 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 http://www.keepalived.org/software/keepalived-1.2.5.tar.gzln -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.com smtp_server smtp.163.com smtp_connect_timeout 30 router_id LVS_Master}vrrp_script chk_http_port { script "/etc/keepalived/check_haproxy.sh" interval 5 #脚本执行间隔 weight -5 #执行脚本后优先级变更:5表示优先级+5;-5则表示优先级-5}vrrp_instance VI_A { state MASTER interface eth0 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_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/里面。
vim /etc/keepalived/check_haproxy.sh#!/bin/bashA=`ps -C haproxy --no-header |wc -l`if [ $A -eq 0 ];then/etc/init.d/haproxy restartecho "Start haproxy" &> /dev/nullsleep 3if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then/etc/init.d/keepalived stopecho "Stop keepalived" &> /dev/nullfifi#chomd +x /etc/keepalived/check_haproxy.sh
6.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添加多台物理机即可,并考虑把项目的图片分离出来做成单独的图片服务器。
转载地址:http://xemna.baihongyu.com/