清华镜像站

https://mirrors.tuna.tsinghua.edu.cn/ubuntu/pool/main/h/haproxy/

下载haproxy

https://mirrors.tuna.tsinghua.edu.cn/ubuntu/pool/main/h/haproxy/haproxy_1.6.3-1ubuntu0.3_amd64.deb

安装haproxy

sudo dpkg -i ./haproxy_1.6.3-1ubuntu0.3_amd64.deb

编辑配置文件

vim /etc/haproxy/haproxy.cfg

其默认配置如下

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # Default ciphers to use on SSL-enabled listening sockets.
        # For more information, see ciphers(1SSL). This list is from:
        #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
        ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
        ssl-default-bind-options no-sslv3

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

 

1. 开启stats page

添加如下配置开启haproxy的stats page页面,在页面中可以查看流程等信息

frontend stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s
#    stats admin if LOCALHOST
    stats auth user:123
#  stats hide-version # 可以隐藏haproxy的版本

参考文档

https://www.haproxy.com/blog/exploring-the-haproxy-stats-page/

访问localhost:8404可以查看haproxy状态

ubuntu16.04安装haproxy_参考文档

 

2. 给TCP端口配置load balancer,比如给hiveserver2配置负载均衡

参考文档

https://docs.cloudera.com/documentation/enterprise/5-15-x/topics/admin_ha_hiveserver2.html

添加如下配置开启haproxy作为load balancer

listen hiveserver2
    bind 0.0.0.0:10001
    mode tcp
    option tcplog
    balance source
    server hiveserver2_1 hs2-host-1.example.com:10000 check
    server hiveserver2_2 hs2-host-2.example.com:10000 check
    server hiveserver2_3 hs2-host-3.example.com:10000 check
    server hiveserver2_4 hs2-host-4.example.com:10000 check

比如

listen test
    bind 0.0.0.0:10001
    mode tcp
    option tcplog
    balance leastconn
    server es localhost:9200 check
    server cerebro localhost:9090 check

 haproxy的负载均衡策略有,中文请参考:关于haproxy负载均衡的算法整理

roundrobin
static-rr
leastconn
first
source
URI
URL parameter
HDR
rdp-cookie

参考:

https://d2c.io/post/haproxy-load-balancer-part-2-backend-section-algorithms