4.安装haproxy
1.rpm安装
https://repo.ius.io/
2.编译安装
1.准备依赖软件包:
yum install gcc readline-devel openssl-devel systemd-devel -y
2.安装lua
wget http://www.lua.org/ftp/lua-5.4.3.tar.gz
tar xf lua-5.4.3.tar.gz -C /usr/local/
cd /usr/local/lua-5.4.3/
make linux
make linux test
ln -s /usr/local/lua-5.4.3/ /usr/local/lua
3.编译haproxy
wget http://cdn.xuliangwei.com/haproxy-2.4.0.tar.gz
tar xf haproxy-2.4.0.tar.gz
cd haproxy-2.4.0/
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
make install PREFIX=/usr/local/haproxy-2.4
ln -s /usr/local/haproxy-2.4/ /usr/local/haproxy
验证版本
[root@proxy ~]# /usr/local/haproxy/sbin/haproxy -v
HAProxy version 2.4.0-6cbbecf 2021/05/14 - https://haproxy.org/
准备配置文件:
[root@proxy01 ~]# cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
# uid 99
# gid 99
user haproxy
group haproxy
daemon
log 127.0.0.1 local2 info
pidfile /var/lib/haproxy24/haproxy.pid
stats socket /var/lib/haproxy24/haproxy.sock mode 600 level admin
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:123456
listen web_port
bind *:80
mode http
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
启动命令软连接
ln -s /usr/local//usr/local/haproxy-2.4/sbin/haproxy /usr/sbin/haproxy
mkdir /etc/haproxy/conf.d/
启动文件:
cat /lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network-online.target
Wants=network-online.target
[Service]
EnvironmentFile=-/etc/sysconfig/haproxy
Environment=“CONFIG=/etc/haproxy/haproxy.cfg” “PIDFILE=/run/haproxy.pid”
Environment=“CONFIG_D=/etc/haproxy/conf.d/”
ExecStartPre=/usr/sbin/haproxy -f $CONFIG -f $CONFIG_D -c -q $OPTIONS
ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -f $CONFIG_D -p $PIDFILE $OPTIONS
ExecReload=/usr/sbin/haproxy -f $CONFIG -f $CONFIG_D -c -q $OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
SuccessExitStatus=143
Type=notify
[Install]
WantedBy=multi-user.target
初始环境:
useradd -r haproxy
mkdir /var/lib/haproxy24