0
点赞
收藏
分享

微信扫一扫

Haproxy编译安装及简述全局配置段的各参数

1、Haproxy编译安装

Haproxy是法国的一个开发者威利塔罗(Willy Tarreau)在2000年使用C语言开发的一个开源软件,是一款具备高并发(1万以上)、高性能的TCP和HTTP负载均衡器,支持基于cookie的持久性,自动故障切换,支持正则表达式及web状态统计。
注意:编译haproxy的时候是需要用到lua的环境,并且lua的最低版本要求是要5.3以上。

1.1、基础环境准备

lua源码包下载的地址是http://www.lua.org/ftp/
image.png

#安装基础命令及编译依赖环境
root@ubuntu:~# apt -y install gcc iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev openssh-server libreadline-dev libsystemd-dev
#编译安装lua,为Haproxy支持基于其实现功能扩展。
注意:HAProxy要求的lua最低版本为5.3,ubuntu系统自带的版本是支持的,这里我就安装比较新又稳定的。
root@ubuntu:~# cd /usr/local/src/
root@ubuntu:/usr/local/src# wget http://www.lua.org/ftp/lua-5.4.4.tar.gz
root@ubuntu:/usr/local/src# tar xf lua-5.4.4.tar.gz
root@ubuntu:/usr/local/src# cd lua-5.4.4/
root@ubuntu:/usr/local/src/lua-5.4.4# make linux test

#查看编译完后lua的版本
root@ubuntu:/usr/local/src/lua-5.4.4# ./src/lua -v
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
#在ubuntu18.04的系统中是自带一个lua5.3的包,使用包安装的话就运行apt -y install lua5.3安装一下就好了。

1.2、编译安装haproxy

haproxy源码包下载地址http://www.haproxy.org/#down
image.png
找到想要下载的大版本后再打开Dir那个网址就可以找到源码包了。

#首先先下载haproxy源码包并解压后编译安装
root@ubuntu:~# cd /usr/local/src/
root@ubuntu:/usr/local/src# wget http://www.haproxy.org/download/2.4/src/haproxy-2.4.17.tar.gz
root@ubuntu:/usr/local/src# tar xf haproxy-2.4.17.tar.gz
root@ubuntu:/usr/local/src# cd haproxy-2.4.17/
root@ubuntu:/usr/local/src/haproxy-2.4.17# make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.4.4/src/ LUA_LIB=/usr/local/src/lua-5.4.4/src/ && make install PREFIX=/apps/haproxy

#创建软链接
root@ubuntu:/usr/local/src/haproxy-2.4.17# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
#查看编译安装完后的haproxy版本
root@ubuntu:~# haproxy -v
HAProxy version 2.4.17-9f97155 2022/05/13 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2026.
Known bugs: http://www.haproxy.org/bugs/bugs-2.4.17.html
Running on: Linux 4.15.0-167-generic #175-Ubuntu SMP Wed Jan 5 01:56:07 UTC 2022 x86_64

1.3、准备haproxy配置文件和启动文件

#准备haproxy.service文件
root@ubuntu:~# cat /lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network-online.target
Wants=network-online.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/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

#准备haproxy.cfg文件
root@ubuntu:~# mkdir /etc/haproxy
root@ubuntu:~# vim /etc/haproxy/haproxy.cfg
root@ubuntu:~# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /apps/haproxy
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    pidfile /var/lib/haproxy/haproxy.pid
    log 127.0.0.1 local2 info

defaults
    option http-keep-alive
    option forwardfor
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client 300000ms
    timeout server 300000ms
listen stats
    mode http
    bind 0.0.0.0:9999
    stats enable
    log global
    stats uri   /haproxy-status
    stats auth  admin:wm521314

#准备相关目录并创建haproxy用户
root@ubuntu:~# mkdir /var/lib/haproxy
root@ubuntu:~# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy

1.4、设置开机自启动并立即启动和查看haproxy的状态是否启动成功

root@ubuntu:~# systemctl daemon-reload
root@ubuntu:~# systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /lib/systemd/system/haproxy.service.
root@ubuntu:~# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2022-07-06 16:57:47 CST; 13s ago
  Process: 28075 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
 Main PID: 28078 (haproxy)
    Tasks: 3 (limit: 2284)
   CGroup: /system.slice/haproxy.service
           ├─28078 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
           └─28080 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid

Jul 06 16:57:47 ubuntu systemd[1]: Starting HAProxy Load Balancer...
Jul 06 16:57:47 ubuntu systemd[1]: Started HAProxy Load Balancer.
Jul 06 16:57:47 ubuntu haproxy[28078]: [NOTICE]   (28078) : New worker #1 (28080) forked

#验证是否开启管理端口9999
root@ubuntu:~# ss -tnlp | grep 9999 
LISTEN   0         20480               0.0.0.0:9999             0.0.0.0:*        users:(("haproxy",pid=28080,fd=8))

1.5、范围状态页

image.png
image.png

2、基础配置详解

Haproxy的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分。

  • global:全局配置段

进程及安全配置相关的参数
性能调整相关参数
Debug参数

  • proxie:代理配置段

defaults:为frontend,backend,listen提供默认配置
frontend:前端,相当于nginx中的server {}
backend:后端,相当于nginx中的upstream {}
listen:同时拥有前端和后端的配置

2.1、global配置参数

官方文档:https://cbonte.github.io/haproxy-dconv/2.4/intro.html

chroot  #指定运行的目录
deamon  #以守护进程运行
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1   #指定socket文件路劲
user,group,uid,gid  #指定运行haproxy服务的用户
nbproc n    #指定开启haproxy worker的进程数,默认是1个
#nbthread 1 #和多进程 nbproc配置互斥(版本有关,CentOS8的haproxy1.8无此问题),指定每个haproxy进程开启的线程数,默认为每个进程一个线程
#如果同时启用nbproc和nbthread 会出现以下日志的错误,无法启动服务
Jul  6 14:46:23 haproxy haproxy: [ALERT] 097/144623 (1454) : config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.
cpu-map 1 0   #绑定haproxy worker 进程至指定CPU,将第1个work进程绑定至0号CPU
cpu-map 2 1     #绑定haproxy worker 进程至指定CPU,将第2个work进程绑定至1号CPU
maxconn n   #每个haproxy进程的最大并发连接数
maxsslconn n   #每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
maxconnrate n   #每个进程每秒创建的最大连接数量
spread-checks n #后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间,默认值0
pidfile #指定pid文件路径
log 127.0.0.1 local2 info #定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个

2.2、Proxies配置

官方文档:https://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4

defaults [<name>] #默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有name
frontend <name>   #前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。
backend <name>   #后端服务器组,等于nginx的upstream和LVS中的RS服务器
listen   <name>   #将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用

注意:name字段只能使用大小写字母,数字,‘-’(dash),'_‘(underscore),'.' (dot)和 ':'(colon),并且严格区分大小写

2.2.1、Proxies配置-defaults

option redispatch   #当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发
option abortonclose #当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启
option http-keep-alive  #开启与客户端的会话保持
option forwardfor   #透传客户端真实IP至后端web服务器
mode http|tcp   #设置默认工作类型,使用TCP服务器性能更好,减少压力
timeout http-keep-alive 120s    #session会话保持超时时间,此时间段内会转发到相同的后端服务器
timeout connect 120s    #客户端请求从haproxy到后端server最长连接等待时间(TCP连接之前),默认单位ms
timeout server 600s #客户端请求从haproxy到后端服务端的请求处理超时时长(TCP连接之后),默认单位ms,如果超时,会出现502错误,此值建议设置较大些,访止502错误
timeout client 600s #设置haproxy与客户端的最长非活动时间,默认单位ms,建议和timeout server相同
timeout check   5s  #对后端服务器的默认检测超时时间
default-server inter 1000 weight 3  #指定后端服务器的默认设置

示例:

defaults
    option http-keep-alive
    option forwardfor
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client 300000ms
    timeout server 300000ms

2.2.2、Proxies配置-listen 简化配置

listen web_host
    bind 10.0.0.100:80
    mode http
    option forwardfor
    server rs1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server rs2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5

2.2.3、Proxies配置-frontend

bind:#指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中
#格式:
bind [<address>]:<port_range> [, ...] [param*]
#注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1
backlog <backlog> #针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度,注意:不支持backend

2.2.4、Proxies配置-backend

定义一组后端服务器,backend服务器将被frontend进行调用。
注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动

mode http|tcp   #指定负载协议类型,和对应的frontend必须一致
option  #配置选项
server  #定义后端real server,必须指定IP和端口

注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更多应用层检测功能。
server配置:

#针对一个server配置
check   #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没有其它配置也可以启用检查功能
 #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查
     addr <IP>  #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
     port <num> #指定的健康状态监测端口
     inter <num>    #健康状态检查间隔时间,默认2000 ms
     fall <num> #后端服务器从线上转为线下的检查的连续失效次数,默认为3
     rise <num> #后端服务器从下线恢复上线的检查的连续有效次数,默认为2
weight <weight> #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接
backup  #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry Server
disabled    #将后端服务器标记为不可用状态,即维护状态,除了持久模式,将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求
redirect prefix http://www.baidu.com/   #将请求临时(302)重定向至其它URL,只适用于http模式
redir http://www.baidu.com  #将请求临时(302)重定向至其它URL,只适用于http模式
maxconn <maxconn>   #当前后端server的最大并发连接数

2.2.5、frontend+backend配置

示例:

frontend test_http_port
    bind 10.0.0.100:80
    mode http
    balance roundrobin
    acl acl_php path_end -i .php
    acl acl_html path_end -i .html
    use_backend php_hosts if acl_php
    use_backend html_hosts if acl_html

backend php_hosts
    mode http
    server 10.0.0.101 10.0.0.101:80 check inter 2000 fall 3 rise 5
backend html_hosts
    mode http
    server 10.0.0.102 10.0.0.102:80 check inter 2000 fall 3 rise 5
举报

相关推荐

0 条评论