六.基础配置阶段 1.安装haproxy
[root@hap ~]# yum install -y haproxy
[root@hap ~]# cd /etc/haproxy/
[root@hap haproxy]# cp haproxy.cfg{,.bak}
[root@hap haproxy]# ls
haproxy.cfg haproxy.cfg.bak 2.开启haproxy的系统日志
[root@hap haproxy]# vim/etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log 重新启动rsyslog服务:
[root@hap haproxy]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ] 3.编辑配置文件,添加后端web服务器
[root@hap haproxy]# vim/etc/haproxy/haproxy.cfg
global
#to have these messages end up in /var/log/haproxy.log you will
#need to:
#
#1) configure syslog to accept network log events. This is done
# by adding the '-r' option tothe SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
#2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like thefollowing can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
#turn on 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
frontend main *:80
default_backend websrvs
backend websrvs
balance roundrobin
server node7 172.16.31.30:80check
server node8 172.16.31.31:80check 4.启动服务:
[root@hap haproxy]# service haproxy start
Starting haproxy: [ OK ] 5.访问测试: 是基于轮询调度算法的。
七.常用配置解析: 1.cookie会话保持
backend websrvs
balance roundrobin
cookie SRV insert indirectnocache
server node7 172.16.31.30:80cookie node7 check rise 1 fall 2
server node8 172.16.31.31:80cookie node8 check 重启haproxy服务:
[root@hap haproxy]# service haproxy restart
Stopping haproxy: [ OK ]
Starting haproxy: [ OK ] 访问测试: 记录了cookie,实现了会话保持:
2.启用反向服务器状态信息页面
backend websrvs
balance roundrobin
server node7 172.16.31.30:80cookie node7 check rise 1 fall 2
server node8 172.16.31.31:80cookie node8 check
stats enable 重启haproxy服务,访问测试:
状态页安全性配置:
backend websrvs
balance roundrobin
server node7 172.16.31.30:80cookie node7 check rise 1 fall 2
server node8 172.16.31.31:80cookie node8 check
stats enable
stats uri /haproxyadm?stats
stats hide-version
stats realm HAProxy\ Status
stats auth admin:admin
stats admin if TRUE 重启haproxy服务,访问测试: