方便测试
systemctl stop firewalld && systemctl disable firewalld
yum -y install haproxy
systemctl start haproxy && systemctl enable haproxy
编辑 vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
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 to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following 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
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000
default_backend httpd_web
listen show
bind 0.0.0.0:5080
mode http
option httplog
log 127.0.0.1 local0 err
stats refresh 30s
maxconn 10 # 最大连接数
stats uri /admin # 状态页面 http//ip:5080/admin访问
stats realm Haproxy\ Statistics
stats auth admin:admin # 用户和密码:admin
stats hide-version # 隐藏版本信息
stats admin if TRUE # 设置手工启动/禁用
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend httpd_web
balance roundrobin
server web1 192.168.0.104:80 check
server web2 192.168.0.112:80 check
效果可通过 IP:5000端口 调度到 192.168.0.104:80, 192.168.0.112:80
管理后台通过 IP:5080端口/admin访问
haproxy支持的常用算法
roundrobin:动态算法,(加权)轮询;
static-rr:静态加权轮询;
leastconn:加权最少连接;动态算法;
first:根据服务器在列表中的位置,自上而下进行调度;
source:相当于nginx的ip-hash或lvs的sh算法,原地址哈希hash,默认为取模法;静态调度