编译安装haproxy
环境准备
由于CentOS7 之前版本自带的lua版本比较低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此需要编译安装较新版本的lua环境,然后才能编译安装HAProxy,过程如下:
[root@centos7 ~]
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 lua-5.4.4]
[root@centos7 lua-5.4.4]
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
1编译安装HAProxy
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 ~]
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
/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
1.1验证HAProxy版本
[root@centos7 haproxy-2.4.15]
/usr/sbin/haproxy
[root@centos7 haproxy-2.4.15]
HAProxy version 2.4.15-7782e23 2022/03/14 - 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.15.html
Running on: Linux 3.10.0-1160.el7.x86_64
[root@centos7 haproxy-2.4.15]
HAProxy version 2.4.15-7782e23 2022/03/14 - 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.15.html
Running on: Linux 3.10.0-1160.el7.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
-m limits the usable amount of memory (in MB)
-N sets the default, per-proxy maximum
-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
1.2准备HAProxy启动文件
[root@centos7 haproxy-2.4.15]
[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/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
[root@centos7 haproxy-2.4.15]
1.3配置文件
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
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.7:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
1.4启动 haproxy
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
[root@centos7 haproxy-2.4.15]
1.5验证 haproxy 状态
[root@centos7 haproxy-2.4.15]
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-04-17 14:10:51 CST; 1min 56s ago
Process: 3503 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
Main PID: 3506 (haproxy)
CGroup: /system.slice/haproxy.service
├─3506 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
└─3509 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
Apr 17 14:10:51 centos7 systemd[1]: Starting HAProxy Load Balancer...
Apr 17 14:10:51 centos7 systemd[1]: Started HAProxy Load Balancer.
Apr 17 14:10:51 centos7 haproxy[3506]: [NOTICE] (3506) : New worker
Apr 17 14:10:51 centos7 haproxy[3506]: [WARNING] (3509) : Server web_port/web1 is DOWN, reason: Layer4 connec...ueue.
Apr 17 14:10:51 centos7 haproxy[3506]: [NOTICE] (3509) : haproxy version is 2.4.15-7782e23
Apr 17 14:10:51 centos7 haproxy[3506]: [NOTICE] (3509) : path to executable is /usr/sbin/haproxy
Apr 17 14:10:51 centos7 haproxy[3506]: [ALERT] (3509) : proxy 'web_port' has no server available!
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7 haproxy-2.4.15]
|-haproxy(3506)---haproxy(3509)---{haproxy}(3510)
1.6查看haproxy的状态页面
|