目录

简介

基于HAProxy2.1.2编译安装

主要是lua的编译安装、HAProxy的编译安装、systemd配置。

lua

扩展模块集成LUA脚本语言,下载
http://www.lua.org/download.html

安装lua环境

yum -y install libtermcap-devel ncurses-devel libevent-devel readline-devel gcc gcc-c++

mkdir /opt/lua
cd /opt/lua
curl -Ok http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar -zxvf lua-5.3.5.tar.gz
cd /opt/lua/lua-5.3.5
简单的安装
make linux 或 make linux test
#查看版本[切换到安装目录中]
./src/lua -v

HAProxy

准备编译依赖环境

download dir links: http://www.haproxy.org/download/2.1/src/

wget http://www.haproxy.org/download/2.1/src/haproxy-2.1.2.tar.gz

#准备编译依赖环境

yum -y install gcc gcc-c++ glibc glibc-devel  pcre-devel  openssl-devel systemd-devel  zlib-devel 

#编译
make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1  ADDLIB=-lz USE_ZLIB=1 USE_LUA=1 USE_PCRE=1 \
USE_LUA=1 LUA_INC=/opt/lua/lua-5.3.5/src LUA_LIB=/opt/lua/lua-5.3.5/src \
USE_SYSTEMD=1  

#编译后安装
make install PREFIX=/usr/local/haproxy  #参数无需要定义,默认的为/usr/local,最终主程序安装在/usr/local/sbin/haproxy

#执行程序复制到sbin下
cp /usr/local/haproxy/sbin/haproxy /usr/sbin/

#查看版本
haproxy -v

#配置systemd

#将源码路径中的contrib/systemd/haproxy.service.in复制到/usr/lib/systemd/system/haproxy.service
#替换其中的变量:
sed -i -e 's:@SBINDIR@:/usr/local/sbin:' /usr/lib/systemd/system/haproxy.service

cat /usr/lib/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=network.target

[Service]
EnvironmentFile=-/etc/default/haproxy
EnvironmentFile=-/etc/sysconfig/haproxy
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock"
ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=/usr/local/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify

# The following lines leverage SystemD's sandboxing options to provide
# defense in depth protection at the expense of restricting some flexibility
# in your setup (e.g. placement of your configuration files) or possibly
# reduced performance. See systemd.service(5) and systemd.exec(5) for further
# information.

# NoNewPrivileges=true
# ProtectHome=true
# If you want to use 'ProtectSystem=strict' you should whitelist the PIDFILE,
# any state files and any other files written using 'ReadWritePaths' or
# 'RuntimeDirectory'.
# ProtectSystem=true
# ProtectKernelTunables=true
# ProtectKernelModules=true
# ProtectControlGroups=true
# If your SystemD version supports them, you can add: @reboot, @swap, @sync
# SystemCallFilter=~@cpu-emulation @keyring @module @obsolete @raw-io

[Install]
WantedBy=multi-user.target

systemctl enable haproxy

haproxy.cfg配置文件

[root]# mkdir /etc/haproxy
[root]# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /usr/local/haproxy
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    uid 99
    gid 99
    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 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 haadmin:q1w2e3r4ys

listen web_port
    bind 192.168.99.118:80
    mode http  
    log global  
    server web1 192.168.99.119:80 check inter 3000 fall 2 rise 5  

配置文件语法检测与debug

#配置文件语法检测
[root] /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid  #语法正解。此时只要不与已有服务监听端口冲突,即可启动服务
[root] systemctl start haproxy
#debug模式,可以查看haproxy后台的处理过程
[root]# /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -d