centos7编译安装haproxy
1.编译安装lua,因为系统版本低与5.3版本
[root@centos7:~]
curl -R -O http://www.lua.org/ftp/lua-5.4.4.tar.gz
[root@centos7:~]
tar zxf lua-5.4.4.tar.gz
[root@centos7:~]
cd lua-5.4.4
[root@centos7:~/lua-5.4.4]
make all test
[root@centos7:~/lua-5.4.4]
mkdir /usr/local/lua
[root@centos7:~/lua-5.4.4]
mv src/ /usr/local/lua
2.编译安装haproxy
1.安装编译安装工具包
[root@centos7:~]
yum -y install gcc openssl-devel pcre-devel systemd-devel
2.下载并解压安装包,
http://www.haproxy.org/download/2.4/src/haproxy-2.4.17.tar.gz
[root@centos7:~]
ll
total 3904
-rw-------. 1 root root 1579 Dec 28 2021 anaconda-ks.cfg
-rw-r--r-- 1 root root 3623854 Jul 8 14:33 haproxy-2.4.17.tar.gz
[root@centos7:~]
tar xf haproxy-2.4.17.tar.gz
[root@centos7:~]
cd haproxy-2.4.17
[root@centos7:~/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/lua/src/ LUA_LIB=/usr/local/lua/src/
[root@centos7:~/haproxy-2.4.17]
make install PREFIX=/apps/haproxy
3.创建软连接
[root@centos7:~/haproxy-2.4.17]
ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
[root@centos7:~/haproxy-2.4.17]
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 3.10.0-1160.el7.x86_64
4.准备service文件,
[root@centos7:~/haproxy-2.4.17]
vim /usr/lib/systemd/system/haproxy.service
[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.17]
systemctl daemon-reload
5.准备启动使用的配置文件
[root@centos7:~/haproxy-2.4.17]
mkdir /etc/haproxy
[root@centos7:~/haproxy-2.4.17]
vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /apps/haproxy
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.17:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
[root@centos7:~/haproxy-2.4.17]
mkdir /var/lib/haproxy
[root@centos7:~/haproxy-2.4.17]
useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy
6.启动服务
[root@centos7:~/haproxy-2.4.17]
systemctl enable --now haproxy
[root@centos7:~/haproxy-2.4.17]
systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-07-08 15:30:53 CST; 17s ago
......
[root@centos7:~/haproxy-2.4.17]
ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9999 *:*
[root@centos7:~/haproxy-2.4.17]
pstree -p |grep haproxy
|-haproxy(2338)---haproxy(2342)---{haproxy}(2343)
3.测试
http://10.0.0.17:9999/haproxy-status,详情看下图
[root@centos7:~/haproxy-2.4.17]
systemctl enable --now haproxy
Failed to execute operation: Bad message