HAproxy的安装配置
Posted ImJerryChan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HAproxy的安装配置相关的知识,希望对你有一定的参考价值。
本文尚未完结,待完善,估计今晚之前可以发出完整版
一、安装HAproxy
官方给出的文档如下(HAproxy1.7版本):
To build haproxy, you will need :
-GNU make
-GCC between 2.95 and 4.8.
-GNU ld
Also, you might want to build with libpcre support
If your system supports PCRE (Perl Compatible Regular Expressions), then you
really should build with libpcre which is between 2 and 10 times faster than
other libc implementations.
- USE_PCRE=1
It is also possible to include native support for zlib to benefit from HTTP
compression.
USE_ZLIB=1
查看Linux的内核版本:
1.uname -a 2.cat /proc/version
考虑到笔者虚拟机环境
步骤如下:
make TARGET=linux2628 USE_OPENSSL=1 USE_PCRE=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy
二、编写HAproxy启动脚本
vi /etc/init.d/haproxy #!/bin/sh # chkconfig 2345 on # description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments. if [ -f /etc/init.d/functions ]; then . /etc/init.d/functions elif [ -f /etc/rc.d/init.d/functions ] ; then . /etc/rc.d/init.d/functions else exit 0 fi # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 config="/etc/haproxy.cfg" exec="/usr/local/haproxy/sbin/haproxy" PID="/var/run/haproxy.pid" [ -f $config ] || exit 1 RETVAL=0
start() { daemon $exec -c -q -f $config if [ $? -ne 0 ]; then echo "Errors found in configuration file." return 1 fi echo -n "Starting HAproxy: " $exec -D -f $config -p $PID RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy return $RETVAL } stop() { echo -n "Shutting down HAproxy: " killproc haproxy -USR1 RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy [ $RETVAL -eq 0 ] && rm -f $PID return $RETVAL } restart() { $exec -c -q -f $config if [ $? -ne 0 ]; then echo "Errors found in configuration file, check it with ‘haproxy check‘." return 1 fi stop start } rhstatus() { status haproxy } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) rhstatus ;; *) echo $"Usage: haproxy {start|stop|restart|status}" RETVAL=1 esac exit $RETVAL
亲测可用
三、配置HAproxy
HAproxy主要参数包括:
global 全局参数配置
defaults 为所有其他配置段提供默认参数
listen 关联“前端”和“后端”,定义了完整的代理,只对TCP流量有用
frontend 用于定义一系列监听的套接字,可与接收客户端请求并与之连接
backend 用于定义一系列“后端服务器”,代理会将对应客户端的请求转发到这些服务器
官方文档如下:
A "defaults" section sets default parameters for all other sections following its declaration. Those default parameters are reset by the next "defaults" section. See below for the list of parameters which can be set in a "defaults" section. The name is optional but its use is encouraged for better readability. A "frontend" section describes a set of listening sockets accepting client connections. A "backend" section describes a set of servers to which the proxy will connect to forward incoming connections. A "listen" section defines a complete proxy with its frontend and backend parts combined in one section. It is generally useful for TCP-only traffic.
配置文件语法为以这几个主要参数开头,便可以配置
参考配置如下:
vi /etc/haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats #turn on stats unix socket default mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend main bind *:80 ##开启stats界面 stats enable stats hide-version stats uri /haproxyadmin default_backend dynamic #默认backend为dynamic acl url_static path_end -i .jpg #访问控制列表, 匹配结尾为.jpg的资源 use_backend static if url_static #如果结尾为.jpg, 则使用backend为static backend dynamic balance roundrobin #这里使用roundrobin算法 server dynamic 172.16.1.5:80 check backend static balance uri #这里使用uri算法 server static 172.16.1.4:80 check
这个代码是用来实现HAproxy的动静分离的
以上是关于HAproxy的安装配置的主要内容,如果未能解决你的问题,请参考以下文章