Haproxy编译安装
实验环境:
本次安装使用的主机是Rocky 8,安装的haproxy版本为3.0.8,更多源码包下载地址:http://www.haproxy.org/download/
编译lua环境:
yum -y install gcc openssl-devel pcre-devel systemd-devel make
wget http://www.lua.org/ftp/lua-5.4.7.tar.gz
tar xf lua-5.4.7.tar.gz -C /opt
cd /opt/lua-5.4.7/
make all test
编译安装haproxy:
# 下载源码包
wget https://www.haproxy.org/download/3.0/src/haproxy-3.0.8.tar.gz
tar xf haproxy-3.0.8.tar.gz -C /usr/local/src
cd /usr/local/src/haproxy-3.0.8/# 参考INSTALL文件进行编译安装
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
make install PREFIX=/apps/haproxy
ln -s /apps/haproxy/sbin/haproxy /usr/sbin/# 查看生成的文件
[root@localhost haproxy-3.0.8]# tree /apps/haproxy/
/apps/haproxy/
├── doc
│ └── haproxy
│ ├── 51Degrees-device-detection.txt
│ ├── architecture.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
haproxy配置文件:
# 查看配置文件范例
[root@localhost ~]# tree /usr/local/src/haproxy-3.0.8/examples/
/usr/local/src/haproxy-3.0.8/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
├── option-http_proxy.cfg
├── quick-test.cfg
├── socks4.cfg
├── transparent_proxy.cfg
└── wurfl-example.cfg# 创建自定义的配置文件
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
globalmaxconn 100000chroot /apps/haproxystats socket /var/lib/haproxy/haproxy.sock mode 600 level admin#uid 99#gid 99user haproxygroup haproxydaemon#nbproc 4#cpu-map 1 0#cpu-map 2 1#cpu-map 3 2#cpu-map 4 3pidfile /var/lib/haproxy/haproxy.pidlog 127.0.0.1 local2 infodefaultsoption http-keep-aliveoption forwardformaxconn 100000mode httptimeout connect 300000mstimeout client 300000mstimeout server 300000mslisten statsmode httpbind 0.0.0.0:9999stats enablelog globalstats uri /haproxy-statusstats auth haadmin:123456
haproxy启动脚本:
[root@localhost ~]# 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[Install]
WantedBy=multi-user.target
启动haproxy:
[root@localhost ~]# mkdir /var/lib/haproxy
[root@localhost ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy
[root@localhost ~]# systemctl enable --now haproxy
验证haproxy状态:
[root@Rocky-1 haproxy-3.0.8]# systemctl status haproxy
● haproxy.service - HAProxy Load BalancerLoaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)Active: active (running) since Mon 2025-05-26 11:32:42 EDT; 22s agoProcess: 18574 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)Main PID: 18577 (haproxy)Tasks: 3 (limit: 10928)Memory: 21.5MCGroup: /system.slice/haproxy.service├─18577 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid└─18579 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pidMay 26 11:32:42 Rocky-1 systemd[1]: Starting HAProxy Load Balancer...
May 26 11:32:42 Rocky-1 systemd[1]: Started HAProxy Load Balancer.
May 26 11:32:42 Rocky-1 haproxy[18577]: [NOTICE] (18577) : haproxy version is 3.0.8-6036c31
May 26 11:32:42 Rocky-1 haproxy[18577]: [NOTICE] (18577) : path to executable is /usr/sbin/haproxy
May 26 11:32:42 Rocky-1 haproxy[18577]: [ALERT] (18577) : config : parsing [/etc/haproxy/haproxy.cfg:15] : 'pidfile' already specified. Cont>
May 26 11:32:42 Rocky-1 haproxy[18577]: [NOTICE] (18577) : New worker (18579) forked
May 26 11:32:42 Rocky-1 haproxy[18577]: [NOTICE] (18577) : Loading success.