服务器信息

IP Hostname module
1.1.1.1 Haproxy 调度器
1.1.1.2 web web源站
1.1.1.3 web web源站
1.1.1.4 client 客户端

配置web1服务器

1
2
yum -y install httpd
echo "No.1" > /var/www/html/index.html

启动服务

1
systemctl start httpd

放行防火墙

1
2
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

配置web2服务器

1
2
yum -y install httpd
echo "No.2" > /var/www/html/index.html

启动服务

1
systemctl start httpd

放行防火墙

1
2
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

部署Haproxy

安装依赖包

1
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

创建账户

1
useradd -r -M -s /sbin/nologin haproxy

解压软件包

1
2
3
tar xf haproxy-2.3.0.tar.gz 
cd haproxy-2.3.0
make clean //清除临时文件; 清理临时文件

编译

1
2
3
4
5
6
7
8
make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
TARGET=linux-glibc \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 \
USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/

配置内核

1
2
3
4
5
6
echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf

sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

创建配置文件目录

1
mkdir /etc/haproxy

编写配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
maxconn 10000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server web01 1.1.1.2:80 check inter 2000 fall 5
server web02 1.1.1.3:80 check inter 2000 fall 5

编写haproxy.service文件

1
2
3
4
5
6
7
8
9
10
11
vi /usr/lib/systemd/system/haproxy.service 
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid

[Install]
WantedBy=multi-user.target

重启服务

1
2
systemctl daemon-reload 
systemctl restart haproxy

验证

1
2
3
4
curl 1.1.1.1
No.1
curl 1.1.1.1
No.2

配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
####################全局配置######################## 
#######参数是进程级的,通常和操作系统(OS)相关#########
global
maxconn 20480 #默认最大连接数
log 127.0.0.1 local0 #日志输出配置,所有日志都记录在本机系统日志,通过local0输出
log 127.0.0.1 local1 notice #notice 为日志级别,通常有24个级别(error warring info debug)
chroot /var/haproxy #chroot服务自设置的根目录,一般需将此行注释掉
uid 99 #所属运行的用户uid
gid 99 #所属运行的用户组
daemon #守护进程。以后台形式运行haproxy
nbproc 1 #进程数量(可以设置多个进程提高性能)
pidfile /var/run/haproxy.pid #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
ulimit-n 65535 #ulimit的数量限制


#####################默认设置######################
##这些参数可以被利用配置到frontend,backend,listen组件##
defaults
log global #定义日志为global配置中的日志定义
mode http #所处理的类别 (#7层 http;4层tcp )
maxconn 20480 #最大连接数
option httplog #日志类别http日志格式
option httpclose #每次请求完毕后主动关闭http通道
option dontlognull #不记录健康检查的日志信息
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
stats refresh 30 #统计页面刷新间隔
retries 3 #检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
balance roundrobin #默认的负载均衡的方式,轮询方式
#balance source #默认的负载均衡的方式,类似nginx的ip_hash
#balance leastconn #默认的负载均衡的方式,最小连接
contimeout 5000 #连接的超时时间
clitimeout 50000 #客户端的超时时间
srvtimeout 50000 #服务器的超时时间
timeout check 2000 #心跳检测的超时时间
####################监控页面的设置#######################
listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称
bind 0.0.0.0:8189 #监听端口
mode http #http的7层模式
log 127.0.0.1 local3 err #错误日志记录
stats refresh 5s #每隔5秒自动刷新监控页面
stats uri /admin?stats #监控页面的url
stats realm itnihao\ itnihao #监控页面的提示信息
stats auth admin:admin #监控页面的用户和密码admin,可以设置多个用户名
stats auth admin1:admin1 #监控页面的用户和密码admin1
stats hide-version #隐藏统计页面上的HAproxy版本信息
stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)
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