在HAProxy服务器上安装HAProxy:
yum update
yum install haproxy
haproxy的配置文件位于/etc/haproxy/。为了防止出错,先备份原始配置文件:
cd /etc/haproxy/
mv haproxy.cfg haproxy.cfg.backup
编辑配置文件:
vim haproxy.cfg
写入如下内容:
#---------------------------------------------------------------------
全局设置
#---------------------------------------------------------------------
global
log 127.0.0.1 local2 # 日志
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy # Haproxy在haproxy用户和组下运行
group haproxy
daemon
开启 stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
基本设置
#---------------------------------------------------------------------
defaults
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
#---------------------------------------------------------------------
HAProxy Monitoring 配置
#---------------------------------------------------------------------
listen haproxy3-monitoring *:8080 # Haproxy Monitoring 的使用端口:8080
mode http
option forwardfor
option httpclose
stats enable
stats show-legends
stats refresh 5s
stats uri /stats # HAProxy monitoring的网址
stats realm Haproxy\ Statistics
stats auth testuser:test1234 # 登录Monitoring的用户和密码
stats admin if TRUE
default_backend app-main
#---------------------------------------------------------------------
FrontEnd 配置
#---------------------------------------------------------------------
frontend main
bind *:80
option http-server-close
option forwardfor
default_backend app-main
#---------------------------------------------------------------------
使用roundrobin做为负载均衡算法
#---------------------------------------------------------------------
backend app-main
balance roundrobin # 使用的负载均衡算法
option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost # 检查nginx服务器是否连通- 200状态码
server nginx1 192.168.0.108:80 check # Nginx1
server nginx2 192.168.0.109:80 check # Nginx2
配置rsyslog
我们需要使用rsyslog记录HAProxy的日志,编辑rsyslog.conf配置文件,打开UDP的514端口:
vim /etc/rsyslog.conf
去掉如下行的注释:
$ModLoad imudp
$UDPServerRun 514
如果你想指定特定IP,可以更改如下行:
$UDPServerAddress 127.0.0.1
保存退出。
创建rsyslog配置文件:
vim /etc/rsyslog.d/haproxy.conf
写入如下内容:
local2.=info /var/log/haproxy-access.log # 访问日志
local2.notice /var/log/haproxy-info.log # haproxy执行信息
重启rsyslog:
systemctl restart rsyslog
启动HAProxy:
systemctl start haproxy
systemctl enable haproxy
本文转自 Lee_吉 博客,原文链接: http://blog.51cto.com/12173069/2051294 如需转载请自行联系原作者