1、基于Ubuntu18.04编译haproxy

1.1、基础环境

#安装基础命令及编译依赖环境
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/
#下载比较新的lua包
root@ubuntu:/usr/local/src# wget http://www.lua.org/ftp/lua-5.4.4.tar.gz
root@ubuntu:/usr/local/src# tar xvf 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.4系统自带一个lua5.3.3的版本

1.2、编译安装haproxy

#下载haproxy包并解压后编译安装
root@ubuntu:~# cd /usr/local/src/
root@ubuntu:/usr/local/src# wget http://www.haproxy.org/download/2.5/src/haproxy-2.5.6.tar.gz
root@ubuntu:/usr/local/src# tar xvf haproxy-2.5.6.tar.gz
root@ubuntu:/usr/local/src/haproxy-2.5.6# 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.5.6# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
#查看编译安装完haproxy的版本
root@ubuntu:~# haproxy -v
HAProxy version 2.5.6-ba44b43 2022/04/26 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2023.
Known bugs: http://www.haproxy.org/bugs/bugs-2.5.6.html
Running on: Linux 4.15.0-163-generic #171-Ubuntu SMP Fri Nov 5 11:55:11 UTC 2021 x86_64

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

#准备haproxy。service文件
root@ubuntu:~# 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 -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
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:6699                                                                                         
    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 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 Tue 2022-05-03 07:35:48 UTC; 1min 45s ago
  Process: 41558 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCES
 Main PID: 41560 (haproxy)
    Tasks: 2 (limit: 2287)
   CGroup: /system.slice/haproxy.service
           ├─41560 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
           └─41562 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid

May 03 07:35:48 ubuntu systemd[1]: Starting HAProxy Load Balancer...
May 03 07:35:48 ubuntu systemd[1]: Started HAProxy Load Balancer.
May 03 07:35:48 ubuntu haproxy[41560]: [NOTICE]   (41560) : New worker (41562) forked
May 03 07:35:48 ubuntu haproxy[41560]: [NOTICE]   (41560) : Loading success.

1.5、访问测试状态页

image.png image.png

2、基于Centos7编译haproxy

2.1、基础环境

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

#安装基础命令及编译依赖环境
[root@Centos7 ~]# yum -y install gcc readline-devel
[root@Centos7 ~]# wget http://www.lua.org/ftp/lua-5.4.4.tar.gz
[root@Centos7 ~]# tar zxf lua-5.4.4.tar.gz -C /usr/local/src
[root@Centos7 ~]# cd /usr/local/src/lua-5.4.4/
[root@Centos7 lua-5.4.4]# make linux test
[root@Centos7 lua-5.4.4]# src/lua -v
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio

2.2、开始编译安装haproxy

#下载编译需要的依赖包和haproxy源码包
[root@Centos7 ~]# yum -y install gcc openssl-devel pcre-devel systemd-devel
[root@Centos7 ~]# wget http://www.haproxy.org/download/2.5/src/haproxy-2.5.6.tar.gz
[root@Centos7 ~]# tar xvf haproxy-2.5.6.tar.gz -C /usr/local/src/
[root@Centos7 ~]# cd /usr/local/src/haproxy-2.5.6/

#查看编译安装的方法
[root@Centos7 haproxy-2.5.6]# ll Makefile 
-rw-rw-r-- 1 root root 46616 Apr 26 17:25 Makefile
[root@Centos7 haproxy-2.5.6]# cat README
[root@Centos7 haproxy-2.5.6]# cat INSTALL

#参考刚刚查看的INSTALL文件来进行编译安装
[root@Centos7 haproxy-2.5.6]# 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@Centos7 haproxy-2.5.6]# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/

#查看编译安装后生成的文件
[root@Centos7 haproxy-2.5.6]# 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

#验证haproxy的版本
[root@Centos7 ~]# which haproxy 
/usr/sbin/haproxy
[root@Centos7 ~]# haproxy -v
HAProxy version 2.5.6-ba44b43 2022/04/26 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2023.
Known bugs: http://www.haproxy.org/bugs/bugs-2.5.6.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
#大写-V选项显示版本和帮助用法
[root@Centos7 ~]# haproxy -V
HAProxy version 2.5.6-ba44b43 2022/04/26 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2023.
Known bugs: http://www.haproxy.org/bugs/bugs-2.5.6.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 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
        -cc check condition : evaluate a condition 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
        -dL dumps loaded object files after config checks
        -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

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

#查看配置文件范例
[root@Centos7 ~]# tree /usr/local/src/haproxy-2.5.6/examples/
/usr/local/src/haproxy-2.5.6/examples/
├── acl-content-sw.cfg
├── 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
├── option-http_proxy.cfg
├── quick-test.cfg
├── socks4.cfg
├── transparent_proxy.cfg
└── wurfl-example.cfg

1 directory, 17 files


#创建自定义的配置文件
[root@Centos7 ~]# mkdir /etc/haproxy
[root@Centos7 ~]# vim /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
    #nbproc 4
    #cpu-map 1 0
    #cpu-map 2 1
    #cpu-map 3 2
    #cpu-map 4 3
    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


#创建service文件
[root@Centos7 ~]# vim /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

2.4、启动haproxy并验证haproxy的状态

haproxy.cfg文件中定义了chroot、pidfile、user、group等参数,如果系统没有相应的资源会导致haproxy无法启动,具体参考日志文件/var/log/messages

#准备好相关文件目录
[root@Centos7 ~]# mkdir /var/lib/haproxy 
[root@Centos7 ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy
[root@Centos7 ~]# systemctl daemon-reload
[root@Centos7 ~]# systemctl enable --now haproxy
[root@Centos7 ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-05-03 14:52:45 CST; 15s ago
  Process: 2708 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
 Main PID: 2711 (haproxy)
    Tasks: 3
   Memory: 24.0M
   CGroup: /system.slice/haproxy.service
           ├─2711 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
           └─2717 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid

May 03 14:52:45 Centos7.localdomain systemd[1]: Starting HAProxy Load Balancer...
May 03 14:52:45 Centos7.localdomain systemd[1]: Started HAProxy Load Balancer.
May 03 14:52:45 Centos7.localdomain haproxy[2711]: [NOTICE]   (2711) : New worker (2717) forked
May 03 14:52:45 Centos7.localdomain haproxy[2711]: [NOTICE]   (2711) : Loading success.

2.5、查看haproxy的状态页面

image.png image.png