一 HAProxy安装
官方链接:http://www.haproxy.org/ (国内可能无法打开)
下载连接:http://pkgs.fedoraproject.org/repo/pkgs/haproxy/
1 [root@HAProxy ~]# yum -y install gcc gcc-c++ make openssl-devel wget openssh-clients #安装编译工具
2 [root@HAProxy ~]# service iptables stop
3 [root@HAProxy ~]# chkconfig iptables off
4 [root@HAProxy ~]# vi /etc/selinux/config
5 SELINUX=disabled
1.1 yum安装
1 [root@HAProxy ~]# yum -y install haproxy
1.2 编译安装
[
1 [root@HAProxy ~]# tar -zxvf haproxy-1.7.9.tar.gz
2 [root@HAProxy ~]# cd haproxy-1.7.9/
3 [root@HAProxy haproxy-1.7.9]# make TARGET=linux2628 CPU=x86_64 PREFIX=/usr/local/haprpxy #编译uname -r #查看系统内核版本号
4 [root@HAProxy haproxy-1.7.9]# make install PREFIX=/usr/local/haproxy
参数解释:
TARGET=linux26:内核版本,使用uname -r可查看内核。
[root@HAProxy ~]# uname -r
2.6.32-642.el6.x86_64
如:2.6.18-371.el5,此时该参数就为linux26;当内核kernel为2.6.28或之上时采用TARGET=linux2628,更多参数可参考:https://github.com/haproxy/haproxy
ARCH=x86_64:系统位数。
PREFIX=/opt/haprpxy:opt/haprpxy为haprpxy安装路径
1.4 创建HAProxy相关配置文件
1 [root@HAProxy ~]# mkdir /usr/local/haproxy/conf #创建配置文件目录
2 [root@HAProxy ~]# mkdir -p /etc/haproxy #创建配置文件目录
3 [root@HAProxy ~]# touch /usr/local/haproxy/haproxy.cfg #创建配置文件
4 [root@HAProxy ~]# ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件软连接
5 [root@HAProxy ~]# cp -r /root/haproxy-1.7.9/examples/errorfiles /usr/local/haproxy/errorfiles #拷贝错误页面
6 [root@HAProxy ~]# ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加软连接
8 [root@HAProxy ~]# mkdir -p /usr/local/haproxy/log #创建日志文件目录
10 [root@HAProxy ~]# touch /usr/local/haproxy/log/haproxy.log #创建日志文件目录
12 [root@HAProxy ~]# ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加软连接
14 [root@HAProxy ~]# cp /usr/local/haproxy/sbin/haproxy /usr/sbin/ #拷贝HAProxy命令
16 [root@HAProxy ~]# cp /root/haproxy-1.7.9/examples/haproxy.init /etc/rc.d/init.d/haproxy #拷贝开机启动文件
18 [root@HAProxy ~]# chmod u+x /etc/rc.d/init.d/haproxy #添加脚本执行权限
20 [root@HAProxy ~]# chkconfig haproxy on #设置开机启动
二 常用配置
2.1 配置文件
默认配置内容见附件。
2.2 配置选项
1 global #设定全局参数,属于进程级的配置,通常和操作系统的配置有关
2 log 127.0.0.1 local2 #全局日志配置,local0表示日志设备,info表示日志级别,如err、warning、info、debug4中
3 chroot /usr/local/haproxy #chroot运行路径
4 pidfile /var/run/haproxy.pid #指定haproxy进程的PID文件,启动进程的用户必须有访问此文件的权限
5 maxconn 4000 #每个HAProxy进程可接受的最大并发连接数
6 user haproxy
7 group haproxy #设置运行HAProxy进程的用户和组,也可使用UID和GID值来替代
8 daemon #设置HAProxy进程进入后台运行。
9
10 defaults #配置默认参数,会自动被引用到frontend、backend和listen中
11 mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
12 log global #采用全局定义的日志
13 option httplog #日志类别http日志格式
14 option dontlognull #不记录健康检查的日志信息
15 option http-server-close #每次请求完毕后主动关闭http通道
16 option forwardfor except 127.0.0.0/8 #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
18 option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
19 retries 3 #3次连接失败就认为服务不可用,也可以通过后面设置
20 timeout http-request 10s
21 timeout queue 1m #默认队列超时时间
22 timeout connect 10s #连接超时时间
23 timeout client 1m #客户端超时时间
24 timeout server 1m #服务器超时时间
25 timeout http-keep-alive 10s #默认持久连接超时时间
26 timeout check 10s #心跳检测超时
27 maxconn 3000 #默认的最大连接数
28 balance roundrobin #设置默认负载均衡方式,轮询方式
29 #balance source #设置默认负载均衡方式,类似于nginx的ip_hash
30 #balnace leastconn #设置默认负载均衡方式,最小连接数
31
32 listen stats
33 bind 0.0.0.0:1080 #监听端口
34 mode http #监听的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
35 stats refresh 30s #设置统计页面自动刷新的时间
36 stats uri /stats #设置统计页面url
37 stats realm Haproxy Manager #设置登录HAProxy统计页面密码框上提示文本
38 stats auth admin:admin #设置登录HAProxy统计页面用户名和密码设置
39 #stats hide-version #隐藏统计页面上HAProxy的版本信息
40
41 frontend main *:5000
42 acl url_static path_beg -i /static /images /javascript /stylesheets
43 acl url_static path_end -i .jpg .gif .png .css .js
44
45 use_backend static if url_static #满足策略要求,则响应策略定义的backend页面
46 default_backend app #不满足则响应backend的默认页面
47
48 backend static
49 balance roundrobin #负载均衡模式轮询
50 server static 127.0.0.1:4331 check #后端服务器定义
51
52 backend app #定义名称为app的后端真实服务器组
53 balance roundrobin
54 server app1 127.0.0.1:5001 check #后端真实服务器,不能用的default和frontend部分
55 server app2 127.0.0.1:5002 check
56 server app3 127.0.0.1:5003 check
57 server app4 127.0.0.1:5004 check
后端服务器格式:server <name> <address> [:port] [param*]
parm*参数可有: