RedHat下安装HAProxy
首先查看系统内核版本号及系统名称
uname -a
Linux rh64pfcrm01kf 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
HAProxy安装
HAProxy官网下载源码安装包http://www.haproxy.org/download/1.7/src/haproxy-1.7.5.tar.gz, 并将源码包拷贝到linux某个目录下
执行以下命令安装haproxy
tar –xzvf haproxy-1.7.5.tar.gz #解压文件 cd haproxy-1.7.5 make TARGET=linux2628 make install PREFIX=/usr/local/haproxy
遇到问题: 执行make TARGET=linux2628 报错: make: gcc: Command not found
解决:gcc未安装,yum install gcc配置haproxy.cfg
安装包中有例子:examples/option-http_proxy.cfg
我的最终配置:global log 127.0.0.1 local3 info chroot /usr/local/haproxy pidfile /var/run/haproxy.pid user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats level admin maxconn 1024 defaults log global mode tcp option tcplog timeout connect 5000 timeout client 50000 timeout server 50000 balance roundrobin retries 3 listen mysql_proxy_1 bind ip.5451 #ip mode tcp server plfA ip1:3306 check weight 5 #mysql数据库服务器 server plfB ip2:3306 check weight 5 listen stats bind 0.0.0.0:1080 mode http maxconn 10 stats refresh 30s stats uri /stats stats realm HAPCloud\ Haproxy stats auth admin:** # 监控的用户名和密码 stats hide-version stats admin if TRUE
配置脚本启动与关闭haproxy
安装包中同样有例子:cp -p examples/haproxy.init /etc/rc.d/init.d/haproxy chmod +x /etc/rc.d/init.d/haproxy chkconfig –-add haproxy vi /etc/rc.d/init.d/haproxy #这里需要把BIN的值替换为BIN=haproxy安装目录/sbin/$BASENAME
启动
service haproxy start
关闭service haproxy stop
重启service haproxy restart
启动后可通过http://ip.1080/stats页面查看
常用指令
动态设置权重:权重值为0-256echo "set weight mysql_proxy_1/plfA 0" |socat stdio /var/lib/haproxy/stats set weight <backend>/<server> <weight>[%]
动态关闭和开启后台服务
echo "disable server mysql_proxy_1/plfA" | socat stdio /var/lib/haproxy/stats disable server <backend>/<server>
echo "enable server mysql_proxy_1/plfA" | socat stdio /var/lib/haproxy/stats enable server <backend>/<server>
mysql测试
mysql -uhaproxy -phaproxy123456 -h*.*.*.* -P5451 -D test -e "select count(1) from test;"
后续研究如何结合keepalived。