1、安装lua
HAProxy 支持基于lua实现功能扩展,lua是一种小巧的脚本语言
#当前系统版本
[root@centos7 ~]#lua -v
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
#安装基础命令及编译依赖环境
[root@centos7 ~]# yum install gcc readline-devel
[root@centos7 ~]# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
[root@centos7 ~]# tar xvf lua-5.3.5.tar.gz -C /usr/local/src
[root@centos7 ~]# cd /usr/local/src/lua-5.3.5
[root@centos7 lua-5.3.5]# make linux test
#查看编译安装的版本
[root@centos7 lua-5.3.5]#src/lua -v
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
#查看安装方法
#HAProxy 2.0以上版本编译参数:
[root@centos37 ~]#yum -y install gcc openssl-devel pcre-devel systemd-devel
[root@centos37 ~]#tar xvf haproxy-2.1.3.tar.gz -C /usr/local/src
[root@centos37 ~]#cd /usr/local/src/haproxy-2.1.3/
[root@centos37 haproxy-2.1.3]#ll Makefile
-rw-rw-r-- 1 root root 40812 Feb 12 23:18 Makefile
[root@centos37 haproxy-2.1.3]#cat README
[root@centos37 haproxy-2.1.3]#cat INSTALL
#参考INSTALL文件进行编译安装
[root@centos37 haproxy-2.1.3]#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/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/
[root@centos7 haproxy-2.1.3]# make install PREFIX=/apps/haproxy
[root@centos7 haproxy-2.1.3]#ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
#查看生成的文件
[root@centos7 haproxy-2.1.3]#tree /apps/haproxy/
2.验证HAProxy版本
```cpp
[root@centos37 ~]#haproxy -v
HA-Proxy version 2.1.3 2020/02/12 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2021.
Known bugs: http://www.haproxy.org/bugs/bugs-2.1.3.html
[root@centos7 ~]#haproxy -vv
HA-Proxy version 2.1.3 2020/02/12 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2021
3.准备HAProxy启动文件
#默认缺少配置文件,无法启动
[root@centos37 ~]#systemctl daemon-reload
[root@centos37 ~]#systemctl start haproxy
Job for haproxy.service failed because the control process exited with error
code. See "systemctl status haproxy.service" and "journalctl -xe" for details
[root@centos37 ~]#systemctl stop haproxy #此处无法启动,记得要停掉服务
#创建service文件
[root@centos37 ~]#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
4、配置文件
#创建自定义的配置文件
[root@centos37 ~]# mkdir /etc/haproxy
[root@centos37 ~]# 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
listen web_port
bind 10.0.0.37:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
5、启动 haproxy
#准备socket文件目录
[root@centos37 ~]# mkdir /var/lib/haproxy
#设置用户和目录权限
[root@centos37 ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy
[root@centos37 ~]# systemctl enable --now haproxy
6、验证 haproxy 状态
[root@centos37 ~]#systemctl status haproxy
http://10.0.0.8:9999/haproxy-status
http://10.0.0.7:9999/haproxy-status