1:准备编译安装HAProxy的基础环境
apt install make gcc build-essential libssl-dev zlib1g-dev libpcre3 libpcre3-dev libsystemd-dev libreadline-dev -y
2:编译安装lua,为HAProxy支持基于其实现功能扩展。
注:HAProxy要求的lua最低版本为5.3
2.1 下载并且安装lua
wget -P /usr/local/src/ http://www.lua.org/ftp/lua-5.3.5.tar.gz
cd /usr/local/src/
tar xf lua-5.3.5.tar.gz
cd lua-5.3.5/src/
make linux
2.2 查看编译后的版本
root@template-ubuntu-20200606:/usr/local/src/lua-5.3.5/src# ./lua -v
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
3.编译安装haproxy
3.1安装基础环境
root@template-ubuntu-20200606:/usr/local/src# apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop unzip libreadline-dev libsystemd-dev
3.2 解压并安装
20210622的下载地址 wget https://www.haproxy.org/download/2.3/src/haproxy-2.3.10.tar.gz
cd /usr/local/src/
tar xf haproxy-2.3.10.tar.gz
cd haproxy-2.3.10
make -j `lscpu |awk 'NR==4{print $2}'` ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ PREFIX=/apps/haproxy && make install PREFIX=/apps/haproxy
注意路径 /apps/haproxy
ARM架构 make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_LUA=1 USE_PCRE=1 USE_SYSTEMD=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ PREFIX=/apps/haproxy && make install PREFIX=/apps/haproxy
3.3 查看编译安装好的版本信息
4.编写haproxy启动脚本
#创建文件
vim /lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/apps/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/apps/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
5:编写配置文件
mkdir /etc/haproxy
#创建haproxy的配置文件
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
daemon
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local3 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:molson
6:启动haproxy
groupadd haproxy
useradd -g haproxy haproxy
#创建文件并授权
mkdir /var/lib/haproxy #相关文件存放位置
chown 99.99 /var/lib/haproxy/ -R
#启动Haproxy并设置开机启动及检查Haproxy的状态
systemctl unmask haproxy
systemctl start haproxy
systemctl enable haproxy
systemctl status haproxy