1、Haproxy下载地址(下载1.7.9版本):http://www.haproxy.org/
2、将haproxy-1.7.9.tar.gz上传至Centos下
3、安装
yum install -y gcc #安装gcc(CentOS没有安装gcc则需要先安装gcc,安装过gcc则忽略) tar zxvf haproxy-1.7.9.tar.gz cd haproxy-1.7.9 # yum安装SSL,如需要支持SSL yum install -y openssl openssl-devel readline-devel pcre-devel libssl-dev libpcre3 # 加入支持ssl的编译参数 #如不需要可去掉USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_CRYPT_H=1 USE_LIBCRYPT=1 make TARGET=linux3100 CPU=x86_64 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_CRYPT_H=1 USE_LIBCRYPT=1 make install PREFIX=/usr/local/haproxy #安装 #参数说明: #TARGET=linux3100 #使用uname -r查看CentOS内核信息,信息:2.6.32-642.el6.x86_64 #大于2.6.28的用:TARGET=linux2628 #CPU=x86_64 #PREFIX=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径
3、设置HAProxy
mkdir -p /usr/local/haproxy/conf #创建配置文件目录 mkdir -p /etc/haproxy #创建配置文件目录 touch /usr/local/haproxy/conf/haproxy.cfg #创建配置文件 ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件软连接 cp -r /usr/dev/haproxy/haproxy-1.7.9/examples/errorfiles /usr/local/haproxy/errorfiles #拷贝错误页面 ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加软连接 mkdir -p /usr/local/haproxy/log #创建日志文件目录 touch /usr/local/haproxy/log/haproxy.log #创建日志文件 ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加软连接 cp /usr/dev/haproxy/haproxy-1.7.9/examples/haproxy.init /etc/rc.d/init.d/haproxy #拷贝开机启动文件 chmod +x /etc/rc.d/init.d/haproxy #添加脚本执行权限 chkconfig haproxy on #设置开机启动 ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin #添加软连接
5、 配置haproxy.cfg参数
haproxy配置文件参数详细解析
chroot<jail dir> |
将工作目录切换到<jail dir>并执行chroot |
daemon |
后台工作模式 |
uid |
进程账户id,建议设置为haproxy专用账户 |
gid |
进程组id,建议设置为haproxy专用组 |
log<address><facility> |
配置全局syslog,可以设置两台日志服务器 |
nbproc<number> |
指定后台进程数量 |
pidfile<file> |
指定pid文件 |
ulimit-n<number> |
设置每个进程最大文件描述符数量 |
maxconn<number> |
每个进程支持的最大并发数 |
tune.bufsize<number> |
设置buffer大小,默认16384B |
mode |
可选tcp、http、health |
timeout check<timeout> |
设置检查超时时间 |
contimeout<timeout> |
设置连接超时时间 |
balance roundrobin |
设置轮询负载 |
bind<address>:port |
定义一个或者多个监听地址和端口 |
stats auth admin:admin |
设置监控界面的用户名和密码 |
stats refresh<number> |
统计页面刷新间隔时间 |
option httplog |
使用http日志 |
cookie<name> |
启用cookie的保持连接功能 |
option forwardfor |
允许插入这种数据包头,可以让后端服务器获取客户端ip |
option abortonclose |
负载高时,自动关闭处理时间长的请求 |
option allbackups |
后端服务器宕机,是否激活全部备机,默认启动第一个备机 |
option dontlognull |
不记录空连接日志,主要用于不记录健康检查日志 |
option redispatch |
后端某个机器宕机,强制把请求转发给健康机器 |
monitor-uri<URi> |
检查uri文件是否存在,依次判断主机的健康状态 |
monitor-fail if site_dead |
服务器宕机时,返回503代码 |
option httpchk<uri> |
使用http协议检查服务器健康状态 |
retries<value> |
服务器连接失败后的重试次数 |
timeout client |
客户端最大超时时间,单位毫秒 |
timeout server |
服务器最大超时时间,单位毫秒 |
timeout connect |
最大连接超时时间,单位毫秒 |
default_backend |
默认后端服务器组 |
use_backend |
当条件满足时,指定后端服务器组 |
acl<name><criterion> |
定义访问控制列表 |
vi /usr/local/haproxy/conf/haproxy.cfg #编辑\修改\保存
#--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global log 127.0.0.1 local2 maxconn 3000 ###最大连接数,默认4000 chroot /usr/local/haproxy ###改变目录 pidfile /usr/local/haproxy/conf/haproxy.pid ###haproxy的pid存放路径,将所有进程写入pid文件,启动进程的用户必须有权限访问此文件 daemon ###配置haproxy进入后台运行模式 nbproc 1 ###进程数,可以同时开启多个 #--------------------------------------------------------------------- # Defaults settings # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults log global ###采用全局定义的日志 mode http ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK option httplog ###日志类别http日志格式 option dontlognull ###不记录健康检查的日志信息 option httpclose ###每次请求完毕后主动关闭http通道 retries 3 ###3次连接失败就认为服务不可用,也可以通过后面设置 option redispatch ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option forwardfor ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip timeout connect 10000 #default 10 second timeout if a backend is not found timeout client 300000 ###客户端连接超时 timeout server 300000 ###服务器连接超时 maxconn 60000 ###最大连接数 #################################################################### listen stats bind 0.0.0.0:1080 #监听端口 stats refresh 30s #统计页面自动刷新时间 stats uri /stats #统计页面url stats realm Haproxy Manager #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 #stats hide-version #隐藏统计页面上HAProxy的版本信息 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main bind 0.0.0.0:80 acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js use_backend static if url_static ###满足策略要求,则响应策略定义的backend页面 default_backend dynamic ###不满足则响应backend的默认页面 #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static balance roundrobin ###负载均衡模式轮询 server static 127.0.0.1:80 check ###后端服务器定义 backend dynamic balance roundrobin server websrv1 10.118.66.69:8081 check maxconn 2000 server websrv2 10.118.66.69:8082 check maxconn 2000 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- #errorloc 503 http://www.osyunwei.com/404.html errorfile 403 /etc/haproxy/errorfiles/403.http errorfile 500 /etc/haproxy/errorfiles/500.http errorfile 502 /etc/haproxy/errorfiles/502.http errorfile 503 /etc/haproxy/errorfiles/503.http errorfile 504 /etc/haproxy/errorfiles/504.http
:wq! #保存退出
service haproxy start #启动
service haproxy stop #关闭
service haproxy restart #重启
7、防火墙设置
开放80、1080端口
8、测试
浏览器输入地址:http://192.168.117.128:1080/stats
输入:http://192.168.117.128,则可以访问配置的web应用负载
web应用1:10.118.66.69:8081
web应用2:10.118.66.69:8082
9、增加SSL配置
#--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global log 127.0.0.1 local2 ###[err warning info debug] maxconn 4000 ###最大连接数,默认4000 chroot /usr/local/haproxy ###改变目录 pidfile /usr/local/haproxy/conf/haproxy.pid ###haproxy的pid存放路径,将所有进程写入pid文件,启动进程的用户必须有权限访问此文件 daemon ###配置haproxy进入后台运行模式 nbproc 1 ###进程数,可以同时开启多个 tune.ssl.default-dh-param 2048 #--------------------------------------------------------------------- # Defaults settings # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults log global ###采用全局定义的日志 mode http ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK option httplog ###日志类别http日志格式 option dontlognull ###不记录健康检查的日志信息 option httpclose ###每次请求完毕后主动关闭http通道 retries 3 ###3次连接失败就认为服务不可用,也可以通过后面设置 option redispatch ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option forwardfor ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip timeout connect 10000 #default 10 second timeout if a backend is not found timeout client 300000 ###客户端连接超时 timeout server 300000 ###服务器连接超时 maxconn 60000 ###最大连接数 #################################################################### listen stats bind 0.0.0.0:1080 #监听端口 stats refresh 30s #统计页面自动刷新时间 stats uri /stats #统计页面url stats realm Haproxy Manager #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 #stats hide-version #隐藏统计页面上HAProxy的版本信息 frontend http_its bind :80 maxconn 32000 option http-keep-alive option forwardfor option httpclose default_backend http_ITS_WEB frontend https_its bind :443 ssl crt /usr/local/haproxy/conf/itsweb.pem maxconn 32768 option forwardfor option httpclose option http-keep-alive default_backend http_ITS_WEB backend http_ITS_WEB cookie http_ITS_WEB insert indirect hash-type consistent fullconn 32768 option httpclose balance leastconn server p_01 10.118.66.69:8081 cookie web1 check inter 2000 rise 3 fall 5 check port 8081 server p_02 10.118.66.69:8082 cookie web2 check inter 2000 rise 3 fall 5 check port 8082
测试效果: