192.168.2.180 haproxy
192.168.2.181 web1
192.168.2.182 web2
以下操作全部在haproxy上操作:
tar -zxvf haproxy-1.6.6.tar.gz
cd haproxy-1.6.6
make TARGET=linux2628 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
groupadd -r -g 149 haproxy
useradd -g haproxy -r -s /sbin/nologin -u 149 haproxy
cd /usr/local/haproxy/
vim haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2
###[err warning info debug]
chroot
/usr/local/haproxy
pidfile
/var/run/haproxy
.pid
###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
maxconn 4000
###最大连接数,默认4000
user haproxy
group haproxy
daemon
###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
log global
###采用全局定义的日志
option dontlognull
###不记录健康检查的日志信息
option httpclose
###每次请求完毕后主动关闭http通道
option httplog
###日志类别http日志格式
option forwardfor
###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch
###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10000
#default 10 second timeout if a backend is not found
timeout client 300000
###客户端连接超时
timeout server 300000
###服务器连接超时
maxconn 60000
###最大连接数
retries 3
###3次连接失败就认为服务不可用,也可以通过后面设置
####################################################################
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 192.168.2.181:80 check maxconn 2000
server websrv2 192.168.2.182:80 check maxconn 2000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
测试:
访问192.168.2.180:1080/stats进入haproxy的web界面
访问192.168.2.180:80
会显示181的web界面
再次刷新会访问182的web界面