YUM源安装HAProxy
centos7默认
[root@centos7 ~]#yum install haproxy -y
[root@centos7 ~]#haproxy -v
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
centos8默认
[root@centos8 ~]#yum install -y haproxy
[root@centos8 ~]#haproxy -v
第三方安装HAProxy
地址:https://pkgs.org/download/haproxy
下载对应操作系统的安装包文件,相当于安装了第三方的yum源,从他们的源里边安装HAProxy
[root@centos7 ~]#wget http://www.nosuchhost.net/~cheese/fedora/packages/epel-7/x86_64/cheese-release-7-1.noarch.rpm
[root@centos7 ~]#ll /etc/yum.repos.d/
total 8
drwxr-xr-x. 2 root root 220 Jan 6 00:38 backup
-rw-r--r--. 1 root root 648 Feb 22 10:21 CentosOS-Base.repo
-rw-r--r--. 1 root root 636 Jul 8 2014 cheese.repo ##安装后产生
[root@centos7 ~]#yum install haproxy
[root@centos7 ~]#haproxy -v
HA-Proxy version 1.8.14-52e4d43 2018/09/20
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>
编译安装HAProxy2.0
1、安装相关依赖包
[root@centos7 src]#yum install gcc gcc-c++ openssl openssl-devel glibc glibc-devel prce prce-devel zip unzip zlib-devel lsof systemd-devel -y
2、解压haproxy-2.0.20.tar.gz
[root@centos7 src]#tar xf haproxy-2.0.20.tar.gz
3、编译参数
HAProxy2.0支持的Lua需5.3版本以上,系统自带的是5.1,需从官网下载lua
[root@centos7 src]#tar xf lua-5.3.6.tar.gz
[root@centos7 src]#cd lua-5.3.6
[root@centos7 lua-5.3.6]#ll
total 12
drwxr-xr-x. 2 1026 guan 189 Aug 25 21:45 doc
-rw-r--r--. 1 1026 guan 3273 Jul 14 2020 Makefile
-rw-r--r--. 1 1026 guan 151 Sep 14 23:35 README
drwxr-xr-x. 2 1026 guan 4096 Sep 14 23:34 src
[root@centos7 lua-5.3.6]#make linux test
gcc -std=gnu99 -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_LINUX -c -o lua.o lua.c
lua.c:82:31: fatal error: readline/readline.h: No such file or directory
#include <readline/readline.h>
报错:缺少readline-devel
依赖包,安装后编译完成
HAProxy能自动对lua编译,需指定LUA_INC和LUA_LIB库
#可以查看INSTALL
[root@centos7 haproxy-2.0.20]#make 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.6/src LUA_LIB=/usr/local/src/lua-5.3.6/src PREFIX=/usr/local/haproxy
[root@centos7 haproxy-2.0.20]#make install PREFIX=/usr/local/haproxy
#验证版本
[root@centos7 haproxy-2.0.20]#/usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 2.0.20-f004859 2021/01/08 - https://haproxy.org/
4、编辑HAproxy启动脚本
[root@centos7 examples]#mkdir /etc/haproxy
[root@centos7 examples]#cp /usr/local/src/haproxy-2.0.20/examples/option-http_proxy.cfg /etc/haproxy/haproxy.cfg
[root@centos7 ~]#cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/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
[root@centos7 haproxy-2.0.20]#systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-02-26 15:04:59 CST; 1s ago
Process: 3225 ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
Main PID: 3227 (haproxy)
CGroup: /system.slice/haproxy.service
├─3227 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
└─3230 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
Feb 26 15:04:59 centos7.magedu.org systemd[1]: Starting HAProxy Load Balancer...
Feb 26 15:04:59 centos7.magedu.org systemd[1]: Started HAProxy Load Balancer.
Feb 26 15:04:59 centos7.magedu.org haproxy[3227]: [NOTICE] 056/150459 (3227) : New worker #1 (3230) forked
踩过的坑:
USE_SYSTEMD=1
指定为systemd模式,否则不能通过systemd进行启动,报错信息,需要配合systemd-devel
依赖包。
配置USE_SYSTEMD=1
时写成了USE_SYSTEM=1
,编译时未报错,导致编译后的启动脚本死活起不来
Feb 26 14:32:48 centos7 haproxy: [ALERT] 056/143248 (2072) : master-worker mode with systemd support (-Ws) requested, but not compiled. Use master-worker mode (-W) if you are not using Type=notify in your unit file or recompile with USE_SYSTEMD=1.