haproxy env 安装与基础配置

1. 安装

Use docker、package or source installations to install
第三方仓库 https://pkgs.org/download/haproxy

1.1 use docker

docker run -it --rm haproxy:2.2 haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg
docker run -d --name my-running-haproxy -v /apps/etc/haproxy:/usr/local/etc/haproxy:ro --sysctl net.ipv4.ip_unprivileged_port_start=0 haproxy:2.2
docker run -d --name my-running-haproxy -v /apps/etc/haproxy:/usr/local/etc/haproxy:ro  haproxy:2.2
# Reloading config
docker kill -s HUP my-running-haproxy

1.2 use apt


# apt-get install --no-install-recommends software-properties-common
# add-apt-repository ppa:vbernat/haproxy-2.2
# apt update && apt-cache madison haproxy

# apt-get install haproxy=2.2.\*
# haproxy -v
# haproxy -vv

1.3 source installations

dnf -y install systemd-devel
wget https://www.haproxy.org/download/2.2/src/haproxy-2.2.13.tar.gz
tar xf haproxy-2.2.13.tar.gz -C /usr/local/src
cd /usr/local/src/haproxy-2.2.13/
# less INSTALL
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.4.3/src/ LUA_LIB=/usr/local/src/lua-5.4.3/src/
make install PREFIX=/apps/haproxy
ln -s /usr/local/src/haproxy-2.2.13/haproxy /usr/sbin/
# haproxy -v
# haproxy -vv

PS: 升级lua到5.4.3

yum install gcc readline-devel systemd-devel
# apt install gcc iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev openssh-server libreadline-dev libsystemd-dev
# apt install lua5.3=5.3.3

curl -R -O http://www.lua.org/ftp/lua-5.4.3.tar.gz
tar zxf lua-5.4.3.tar.gz -C /usr/local/src
cd lua-5.4.3/
make all test

2. 基本服务配置

http://cbonte.github.io/haproxy-dconv/2.2/configuration.html

2.1 使用systemctl配置管理

0 root@centos7:~# cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed

[Install]
WantedBy=multi-user.target

# systemctl daemon-reload
# systemctl start haproxy

2.2 haproxy.cfg

#---------------------------------------------------------------------
# Global settings HA-Proxy version 2.2.13-5f3eb59 2021/04/02 - https://haproxy.org/
#---------------------------------------------------------------------
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

listen status_page
    bind 172.16.0.100:8888
    stats enable
    stats uri /status
    stats auth    admin:123465
    stats auth    user:123465
    stats realm "Welcome to the haproxy load balancer status page"
    stats hide-version
    #stats admin if TRUE
    stats refresh 5s

listen front-end
    bind 192.168.1.100:80
    mode tcp
    balance roundrobin
    server 172.16.0.1 172.16.0.1:2000 check inter 2s fall 3 rise 5
    server 172.16.0.2 172.16.0.1:2000 check inter 2s fall 3 rise 5
    server 172.16.0.3 172.16.0.1:2000 check inter 2s fall 3 rise 5
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
    bind :80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:2000 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:2000 check
    server  app2 127.0.0.1:2001 check
    server  app3 127.0.0.1:2002 check
    server  app4 127.0.0.1:5004 check
# /etc/haproxy# haproxy -v
HA-Proxy version 2.2.13-5f3eb59 2021/04/02 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2025.
Known bugs: http://www.haproxy.org/bugs/bugs-2.2.13.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64


# 内核参数调整
echo 'net.ipv4.ip_nonlocal_bind = 1'>> /etc/sysctl.conf  # y允许绑定不存在的vip
ehco 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf  # 打开内核的转发功能

- PS:

Haproxy-1.8.20 编译安装
如何在Ubuntu系统中编译安装HAProxy
https://blog.csdn.net/zerocdn/article/details/110191570
从零开始掌握 HAProxy 负载均衡器

posted @ 2021-05-02 19:32  AlexG  阅读(174)  评论(0编辑  收藏  举报