编译安装HAProxy

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编译安装HAProxy相关的知识,希望对你有一定的参考价值。

由于centos7之前版本自带的lua版本比较低并不符合HAproxy要求的lua最低版本(5.3)的要求,因此需要安装较新版本的lua环境,然后才能编译安装HAproxy,过程如下:

编译安装lua

#当前系统版本
[root@cen7_27 ~]# lua -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

#安装基本命令及编译依赖环境
[root@cen7_27 ~]# yum install gcc readline-devel
[root@cen7_27 ~]# wget http://www.lua.org/ftp/lua-5.3.6.tar.gz
[root@cen7_27 ~]# tar xvf lua-5.3.6.tar.gz -C /usr/local/src
[root@cen7_27 ~]# cd /usr/local/src/lua-5.3.6/
[root@cen7_27 lua-5.3.6]# make linux test

#查看编译安装的版本
[root@cen7_27 lua-5.3.6]# src/lua -v
Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
[root@cen7_27 lua-5.3.6]# 

编译安装haproxy

#安装基础命令,下载包和编译依赖环境
[root@cen7_27 ~]# yum install -y gcc openssl-devel pcre-devel systemd-devel zlib
[root@cen7_27 ~]# tar xvf haproxy-2.4.1.tar.gz -C /usr/local/src/
[root@cen7_27 ~]# 
[root@cen7_27 ~]# cd /usr/local/src/haproxy-2.4.1/

#查看安装方法
[root@cen7_27 haproxy-2.4.1]# ll Makefile 
-rw-rw-r-- 1 root root 45534 Jun 17 03:06 Makefile
[root@cen7_27 haproxy-2.4.1]# cat README
[root@cen7_27 haproxy-2.4.1]# cat INSTALL

#参看install文件进行编译安装
[root@cen7_27 haproxy-2.4.1]# make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.6/src/ LUA_LIB=/usr/local/src/lua-5.3.6/src/
[root@cen7_27 ~]# mkdir /apps/
[root@cen7_27 haproxy-2.4.1]# make install PREFIX=/apps/haproxy
[root@cen7_27 haproxy-2.4.1]# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/

#查看生成的文件
[root@cen7_27 haproxy-2.4.1]# tree /apps/haproxy/
/apps/haproxy/
├── doc
│   └── haproxy
│       ├── 51Degrees-device-detection.txt
│       ├── architecture.txt
│       ├── close-options.txt
│       ├── configuration.txt
│       ├── cookie-options.txt
│       ├── DeviceAtlas-device-detection.txt
│       ├── intro.txt
│       ├── linux-syn-cookies.txt
│       ├── lua.txt
│       ├── management.txt
│       ├── netscaler-client-ip-insertion-protocol.txt
│       ├── network-namespaces.txt
│       ├── peers.txt
│       ├── peers-v2.0.txt
│       ├── proxy-protocol.txt
│       ├── regression-testing.txt
│       ├── seamless_reload.txt
│       ├── SOCKS4.protocol.txt
│       ├── SPOE.txt
│       └── WURFL-device-detection.txt
├── sbin
│   └── haproxy
└── share
    └── man
        └── man1
            └── haproxy.1

6 directories, 22 files
[root@cen7_27 haproxy-2.4.1]# 

验证haproxy版本

#验证haproxy版本:
[root@cen7_27 ~]# which haproxy
/usr/sbin/haproxy
[root@cen7_27 ~]# haproxy -v
HAProxy version 2.4.1-1ce7d49 2021/06/17 - 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.1.html
Running on: Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64
[root@cen7_27 ~]# 

#大写-V选项显示版本和帮助用法
[root@cen7_27 ~]# haproxy -V
HAProxy version 2.4.1-1ce7d49 2021/06/17 - 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.1.html
Running on: Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64
Usage : haproxy [-f <cfgfile|cfgdir>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
        [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ] [-- <cfgfile>*]
        -v displays version ; -vv shows known build options.
        -d enters debug mode ; -db only disables background mode.
        -dM[<byte>] poisons memory with <byte> (defaults to 0x50)
        -V enters verbose mode (disables quiet mode)
        -D goes daemon ; -C changes to <dir> before loading files.
        -W master-worker mode.
        -Ws master-worker mode with systemd notify support.
        -q quiet mode : don't display messages
        -c check mode : only check config files and exit
        -n sets the maximum total # of connections (uses ulimit -n)
        -m limits the usable amount of memory (in MB)
        -N sets the default, per-proxy maximum # of connections (0)
        -L set local peer name (default to hostname)
        -p writes pids of all children to this file
        -de disables epoll() usage even when available
        -dp disables poll() usage even when available
        -dS disables splice usage (broken on old kernels)
        -dG disables getaddrinfo() usage
        -dR disables SO_REUSEPORT usage
        -dr ignores server address resolution failures
        -dV disables SSL verify on servers side
        -dW fails if any warning is emitted
        -dD diagnostic mode : warn about suspicious configuration statements
        -sf/-st [pid ]* finishes/terminates old pids.
        -x <unix_socket> get listening sockets from a unix socket
        -S <bind>[,<bind options>...] new master CLI

[root@cen7_27 ~]# 

[root@cen7_27 ~]# haproxy -vv
HAProxy version 2.4.1-1ce7d49 2021/06/17 - 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.1.html
Running on: Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64
Build options :
  TARGET  = linux-glibc
  CPU     = generic
  CC      = cc
  CFLAGS  = -m64 -march=x86-64 -O2 -g -Wall -Wextra -Wdeclaration-after-statement -fwrapv -Wno-unused-label -Wno-sign-compare -Wno-unused-parameter -Wno-clobbered -Wno-missing-field-initializers -Wtype-limits
  OPTIONS = USE_PCRE=1USE_OPENSSL=1 USE_LUA=1 USE_ZLIB=1 USE_SYSTEMD=1
  DEBUG   = 

Feature list : +EPOLL -KQUEUE +NETFILTER +PCRE -PCRE_JIT -PCRE2 -PCRE2_JIT +POLL -PRIVATE_CACHE +THREAD -PTHREAD_PSHARED +BACKTRACE -STATIC_PCRE -STATIC_PCRE2 +TPROXY +LINUX_TPROXY +LINUX_SPLICE +LIBCRYPT +CRYPT_H +GETADDRINFO -OPENSSL +LUA +FUTEX +ACCEPT4 -CLOSEFROM +ZLIB -SLZ +CPU_AFFINITY +TFO +NS +DL +RT -DEVICEATLAS -51DEGREES -WURFL +SYSTEMD -OBSOLETE_LINKER +PRCTL +THREAD_DUMP -EVPORTS -OT -QUIC -PROMEX -MEMORY_PROFILING

Default settings :
  bufsize = 16384, maxrewrite = 1024, maxpollevents = 200

Built with multi-threading support (MAX_THREADS=64, default=4).
Built with Lua version : Lua 5.3.6
Built with network namespace support.
Built with zlib version : 1.2.7
Running on zlib version : 1.2.7
Compression algorithms supported : identity("identity"), deflate("deflate"), raw-deflate("deflate"), gzip("gzip")
Built with transparent proxy support using: IP_TRANSPARENT IPV6_TRANSPARENT IP_FREEBIND
Built with PCRE version : 8.32 2012-11-30
Running on PCRE version : 8.32 2012-11-30
PCRE library supports JIT : no (USE_PCRE_JIT not set)
Encrypted password support via crypt(3): yes
Built with gcc compiler version 4.8.5 20150623 (Red Hat 4.8.5-44)

Available polling systems :
      epoll : pref=300,  test result OK
       poll : pref=200,  test result OK
     select : pref=150,  test result OK
Total: 3 (3 usable), will use epoll.

Available multiplexer protocols :
(protocols marked as <default> cannot be specified using 'proto' keyword)
              h2 : mode=HTTP       side=FE|BE     mux=H2       flags=HTX|CLEAN_ABRT|HOL_RISK|NO_UPG
            fcgi : mode=HTTP       side=BE        mux=FCGI     flags=HTX|HOL_RISK|NO_UPG
       <default> : mode=HTTP       side=FE|BE     mux=H1       flags=HTX
              h1 : mode=HTTP       side=FE|BE     mux=H1       flags=HTX|NO_UPG
       <default> : mode=TCP        side=FE|BE     mux=PASS     flags=
            none : mode=TCP        side=FE|BE     mux=PASS     flags=NO_UPG

Available services : none

Available filters :
	[SPOE] spoe
	[CACHE] cache
	[FCGI] fcgi-app
	[COMP] compression
	[TRACE] trace

[root@cen7_27 ~]# 

准备haproxy启动文件

#默认缺失配置文件,无法启动
#创建service文件
[root@cen7_27 ~]# vi /usr/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
[root@cen7_27 ~]# 

创建配置文件

#创建自定义的配置文件
[root@cen7_27 ~]# mkdir /etc/haproxy
[root@cen7_27 ~]# vim /etc/haproxy/haproxy.cfg
[root@cen7_27 ~]# 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  haadmin:123456

#listen web_port
#    bind 10.0.0.27:80
#    mode http
#    log global
#    server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5

启动haproxy

#准备socker文件目录
[root@cen7_27 ~]# mkdir /var/lib/haproxy

#设置用户和目录权限
[root@cen7_27 ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy

#启动和查看状态
[root@cen7_27 ~]# systemctl enable --now haproxy
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@cen7_27 ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-06-21 09:02:17 EDT; 8s ago
  Process: 2882 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
 Main PID: 2885 (haproxy)
   CGroup: /system.slice/haproxy.service
           ├─2885 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
           └─2888 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid

Jun 21 09:02:17 cen7_27.yzl systemd[1]: Starting HAProxy Load Balancer...
Jun 21 09:02:17 cen7_27.yzl systemd[1]: Started HAProxy Load Balancer.
Jun 21 09:02:17 cen7_27.yzl haproxy[2885]: [NOTICE]   (2885) : New worker #1 (2888) forked
[root@cen7_27 ~]# 

使用子配置文件保存配置

注意:子配置文件的文件后缀必须为.cfg

#创建子配置目录
[root@cen7_27 ~]# mkdir /etc/haproxy/conf.d/

#创建子配置文件,注意:必须为cfg后缀非.开头的配置文件
[root@cen7_27 ~]# vim /etc/haproxy/conf.d/test.cfg
listen web_port_80
   bind 10.0.0.27:80
   mode http
   balance roundrobin
   server web1 10.0.0.40:80 check inter 3000 fall 2 rise 5
   server web2 10.0.0.50:80 check inter 3000 fall 2 rise 5
[root@cen7_27 ~]# 

#添加子配置目录到unit文件中
[root@cen7_27 ~]# vim /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 -f /etc/haproxy/conf.d/ -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

[root@cen7_27 ~]# systemctl daemon-reload
[root@cen7_27 ~]# systemctl restart haproxy

 

以上是关于编译安装HAProxy的主要内容,如果未能解决你的问题,请参考以下文章

编译安装HAProxy

HAProxy1.8.20版本编译安装

编译安装HAProxy1.8.20

CentOS 7.2 编译安装Haproxy-1.7.4

编译安装HAProxy2.4.17

Linux编译安装HAProxy最新版本2.4.1