Haproxy编译安装

发布于:2025-05-27 ⋅ 阅读:(59) ⋅ 点赞:(0)

实验环境:

本次安装使用的主机是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
global
    maxconn 100000
    chroot /apps/haproxy
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    #uid 99
    #gid 99
    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    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 Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2025-05-26 11:32:42 EDT; 22s ago
  Process: 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.5M
   CGroup: /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.pid

May 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.

查看haproxy的状态页面:(账户密码在配置文件中)


网站公告

今日签到

点亮在社区的每一天
去签到