HaProxy源码安装(Rocky8)

[复制链接]
发表于 2025-10-29 22:59:34 | 显示全部楼层 |阅读模式
haproxy具有高性能、高可用性、机动的负载均衡计谋和强大的将恐和日记功能,是法国开辟者 威利塔罗(Willy Tarreau)在2000年使用C语言开辟的一个开源软件,是一款具 备高并发(一万以上)、高性能的TCP和HTTP负载均衡器,支持基于cookie的恒久性,自动故障切换,支 持正则表达式及web状态统计。
这里对安装社区免费的版本举行先容(3.1):Index of /download/3.1/src
  1. wget https://www.haproxy.org/download/3.1/src/haproxy-3.1.3.tar.gz
复制代码
解压到/etc/local/src
  1. tar -zxvf haproxy-3.1.3.tar.gz -C /usr/local/src/
  2. [root@localhost ~]# ll /usr/local/src/
  3. total 4
  4. drwxrwxr-x. 13 root root 4096 Jan 29 21:20 haproxy-3.1.3
复制代码
 我们可以在安装好的这个目次中找到INSTALL这个文件,more INSTALL,这里提供的是官方文档,我们找到Build environment,这里提到了编译环境的要求几个软件包的版本,比如:
   
HAProxy requires a working GCC or Clang toolchain and GNU make :
    - GNU make >= 3.80. Note that neither Solaris nor OpenBSD's make work with
    the GNU Makefile. If you get many syntax errors when running "make", you
    may want to retry with "gmake" which is the name commonly used for GNU make
    on BSD systems.
    - GCC >= 4.2 (up to 14 tested). Older versions can be made to work with a
    few minor adaptations if really needed. Newer versions may sometimes break
    due to compiler regressions or behaviour changes. The version shipped with
    your operating system is very likely to work with no trouble. Clang >= 3.0
    is also known to work as an alternative solution. Recent versions may emit
    a bit more warnings that are worth reporting as they may reveal real bugs.
    TCC (https://repo.or.cz/tinycc.git) is also usable for developers but will
    not support threading and was found at least once to produce bad code in
    some rare corner cases (since fixed). But it builds extremely quickly
    (typically half a second for the whole project) and is very convenient to
    run quick tests during API changes or code refactoring.
    - GNU ld (binutils package), with no particular version. Other linkers might
    work but were not tested.
  我们查抄一下本身的安装的make和gcc版本
  1. [root@localhost haproxy-3.1.3]# yum list make
  2. Installed Packages
  3. make.x86_64                           1:4.2.1-11.el8
  4. [root@localhost haproxy-3.1.3]# yum list gcc
  5. Installed Packages
  6. gcc.x86_64                         8.5.0-23.el8_10                          @appstream
复制代码
可以看到是满足上述要求的,未安装的话可以yum安装,这里还发起安装openssl-devel(haproxy加密通讯),pcre-devel(正则哀求匹配和处理惩罚),systemd-devel(使用systemd管理 haproxy)这几个软件,提供额外的功能支持:yum install openssl-devel pcre-devel systemd-devel -y
haproxy是对lua是有要求的,比如以下信息阐明至少要大于即是5.3,这在编译haproxy时应该是须要的,我们要指定USE_LUA=1,然后在默认环境下haproxy会探求lua5.4,lua54,lua5.3这种名字,找不到的话大概会报错,全部这里在编译的时间官方也发起使用变量'LUA_LIN_NAME'。假如读者的体系为CentOS7 之前版本自带的lua版本比力低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此须要编译安装较新版本的lua环境,然后才华编译安装HAProxy

查察本地默认lua版本信息
  1. [root@localhost haproxy-3.1.3]# yum list lua
  2. Available Packages
  3. lua.x86_64                           5.3.4-12.el8                            appstream
复制代码
 可以yum直接安装,大概源码安装,这里我选择源码安装5.4版本的。官网地点:Lua: download
我们下载下来之后,根据上图haproxy的提示的extracted and built步调大概lua官网提示步调,举行操纵
  1. tar -zxvf lua-5.4.7.tar.gz -C /opt/
  2. ll /opt
  3. #total 0
  4. #drwxr-xr-x. 4 1026 1000 58 Jun 14  2024 lua-5.4.7
  5. cd /opt/lua-5.4.7/
  6. make all test
  7. #-------------------------------------------------
  8. sed -n '/lua-5.4.6/ p' /usr/local/src/haproxy-3.1.3/INSTALL
  9.   $ cd /opt/lua-5.4.6
  10.     USE_LUA=1 LUA_INC=/opt/lua-5.4.6/src LUA_LIB=/opt/lua-5.4.6/src
  11. cd /usr/local/src/haproxy-3.1.3/
  12. 编译
  13. make  ARCH=x86_64 TARGET=linux-glibc  USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1  USE_SYSTEMD=1  USE_LUA=1 LUA_INC=/opt/lua-5.4.7/src LUA_LIB=/opt/lua-5.4.7/src
  14. #上面将我们之前安装的软件对应的服务打开,在编译的时候使用变量名=1
  15. 安装
  16. make -p /app/haproxy
  17. make install PREFIX=/apps/haproxy
  18. [root@localhost haproxy]# tree
  19. .
  20. ├── doc
  21. │   └── haproxy
  22. │       ├── 51Degrees-device-detection.txt
  23. │       ├── configuration.txt
  24. │       ├── cookie-options.txt
  25. │       ├── DeviceAtlas-device-detection.txt
  26. │       ├── intro.txt
  27. │       ├── linux-syn-cookies.txt
  28. │       ├── lua.txt
  29. │       ├── management.txt
  30. │       ├── netscaler-client-ip-insertion-protocol.txt
  31. │       ├── network-namespaces.txt
  32. │       ├── peers.txt
  33. │       ├── peers-v2.0.txt
  34. │       ├── proxy-protocol.txt
  35. │       ├── regression-testing.txt
  36. │       ├── seamless_reload.txt
  37. │       ├── SOCKS4.protocol.txt
  38. │       ├── SPOE.txt
  39. │       └── WURFL-device-detection.txt
  40. ├── sbin
  41. │   └── haproxy
  42. └── share
  43.     └── man
  44.         └── man1
  45.             └── haproxy.1
复制代码
 末了添加到环境变量或在创建软毗连
  1. ln -s /app/haproxy/sbin/haproxy /usr/sbin/
复制代码
验证是否安装乐成:
  1. [root@localhost ~]# haproxy -v
  2. HAProxy version 3.1.3-929bedf 2025/01/29 - https://haproxy.org/
  3. Status: stable branch - will stop receiving fixes around Q1 2026.
  4. Known bugs: http://www.haproxy.org/bugs/bugs-3.1.3.html
  5. Running on: Linux 4.18.0-553.el8_10.x86_64 #1 SMP Fri May 24 13:05:10 UTC 2024 x86_64
复制代码
 安装就正式完成了,但是启动还须要肯定的设置,我们现在还须要设置一下pid文件和体系设置文件让它可以或许通过systemctl启动,然后再设置下haproxy的设置文件
起首先创建pid的目次,这个目次应该是haproxy可以读写的,以是这里须要创建一个用户haproxy,它的家目次就是pid文件所在目次。
  1. mkdri /var/lib/haproxy
  2. useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy#-r表示系统用户-s用于设置登录权限-d指定家目录
复制代码
 haproxy的设置文件
  1. make /etc/haproxy
  2. vim /etc/haproxy/haproxy.cfg
  3. global
  4.         maxconn 100000
  5.         chroot /app/haproxy
  6.         stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
  7.         user  haproxy
  8.         group haproxy
  9.         daemon
  10.         pidfile /var/lib/haproxy/haproxy.pid
  11.         log 127.0.0.1 local2 info
  12. defaults
  13.         option http-keep-alive
  14.         option  forwardfor
  15.         maxconn 100000
  16.         mode http
  17.         timeout connect 300000ms
  18.         timeout client  300000ms
  19.         timeout server  300000ms
  20. listen stats
  21.         mode http
  22.         bind 0.0.0.0:9999#监听端口设置为9999
  23.         stats enable
  24.         log global
  25.         stats uri    /haproxy#后面用于登录的路由
  26.         stats auth   haproxy:123456#用户和密码
复制代码
 上面这个设置内容是参考官方给的示例设置的:比如下面的都是参考内容
   [root@localhost haproxy]# tree /usr/local/src/haproxy-3.1.3/examples/
/usr/local/src/haproxy-3.1.3/examples/
├── basic-config-edge.cfg
├── content-sw-sample.cfg
├── errorfiles
│   ├── 400.http
│   ├── 403.http
│   ├── 408.http
│   ├── 500.http
│   ├── 502.http
│   ├── 503.http
│   ├── 504.http
│   └── README
├── haproxy.init
├── lua
│   ├── event_handler.lua
│   ├── mailers.lua
│   └── README
├── mptcp-backend.py
├── mptcp.cfg
├── option-http_proxy.cfg
├── quick-test.cfg
├── socks4.cfg
├── traces.cfg
├── transparent_proxy.cfg
└── wurfl-example.cfg
  创建体系启动haproxy的设置文件:vim /usr/lib/systemd/system/haproxy.service
  1. [Unit]
  2. Description=HAProxy Load Balancer
  3. After=syslog.target network.target
  4. [Service]
  5. ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg  -c -q
  6. ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
  7. ExecReload=/bin/kill -USR2 $MAINPID
  8. [Install]
  9. WantedBy=multi-user.target
复制代码
 重新加载一下守卫进程:systemctl daemon-reload
  1. systemctl start haproxy
  2. systemctl status haproxy
复制代码

 关闭防火墙和selinux之后进入192.168.118.130:9999/haproxy

输入设置文件中的设置的用户和暗码,即可完成登录:



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表