HaProxy的安装与配置

HAProxy是一款提供高可用性,负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,他是免费,快速并且可靠的一种解决方案.
1 安装教程

  • 环境
# lsb_release -a LSB Version::base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID: CentOS Description:CentOS release 6.7 (Final) Release:6.7 Codename:Final# 或者通过命令: uname -a Linux jms-master-01 3.10.0-514.16.1.el7.x86_64 #1 SMP Wed Apr 12 15:04:24 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

  • 安装包下载
    https://src.fedoraproject.org/repo/pkgs/haproxy/
  • 安装记录
# 解压 tar -xzvf haproxy-1.8.20.tar.gz -C ~/tools/ # 创建软链 ln -s haproxy-1.8.20 haproxy-current # 安装 make TARGET=linux1820 prefix=/usr/local/haproxy # root用户执行或sudo sudo make install prefix=/usr/local/haproxy

安装记录
[hadoop@jms-master-01 haproxy-current]$ make TARGET=linux1820 prefix=/usr/local/haproxy gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/ev_poll.o src/ev_poll.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/ebtree.o ebtree/ebtree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/eb32sctree.o ebtree/eb32sctree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/eb32tree.o ebtree/eb32tree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/eb64tree.o ebtree/eb64tree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/ebmbtree.o ebtree/ebmbtree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/ebsttree.o ebtree/ebsttree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/ebimtree.o ebtree/ebimtree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o ebtree/ebistree.o ebtree/ebistree.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/proto_http.o src/proto_http.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/cfgparse.o src/cfgparse.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/server.o src/server.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/stream.o src/stream.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/flt_spoe.o src/flt_spoe.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/stick_table.o src/stick_table.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/stats.o src/stats.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/mux_h2.o src/mux_h2.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/checks.o src/checks.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" \ -DBUILD_TARGET='"linux1820"' \ -DBUILD_ARCH='""' \ -DBUILD_CPU='"generic"' \ -DBUILD_CC='"gcc"' \ -DBUILD_CFLAGS='"-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv -Wno-unused-label"' \ -DBUILD_OPTIONS='"USE_POLL=default"' \ -c -o src/haproxy.o src/haproxy.c gcc -Iinclude -Iebtree -Wall-O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -fwrapv-Wno-unused-label-DENABLE_POLL-DCONFIG_HAPROXY_VERSION=\"1.8.20\" -DCONFIG_HAPROXY_DATE=\"2019/04/25\" -c -o src/log.o src/log.c ... [root@jms-master-01 haproxy-current]# make install prefix=/usr/local/haproxy install -d "/usr/local/sbin" install haproxy"/usr/local/sbin" install -d "/usr/local/share/man"/man1 install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1 install -d "/usr/local/doc/haproxy" for x in 51Degrees-device-detection architecture close-options configuration cookie-options DeviceAtlas-device-detection intro linux-syn-cookies lua management netscaler-client-ip-insertion-protocol network-namespaces peers peers-v2.0 proxy-protocol SPOE WURFL-device-detection; do \ install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \ done

安装验证
[hadoop@jms-master-01 haproxy-current]$ haproxy -v HA-Proxy version 1.8.20 2019/04/25 Copyright 2000-2019 Willy Tarreau

2 配置说明 【HaProxy的安装与配置】HAProxy的配置主要由全局配置和代理配置两部分组成.由五个配置构成.
模块 说明
global 进程级参数.通常和操作系统相关的.通常只设置一次
default 默认参数.缺省配置使用
frontend 前端相关配置
backend 后端相关配置
listen frontend和backend的组合
负载均衡算法 banlance
算法 说明
roundrobin 基于权重的轮询算法.当服务器的处理时间保持均匀分布时,最平衡最公平
static-rr 静态的基于权重的轮询算法.运行时调整权重不生效
leastconn(WLC) 适用于长连接会话,新的链接请求分配到最少连接数的服务器
source 将请求的原地址进行hash运算,并由后端的权重总数相除后分配至匹配的服务器
3 典型的一个配置 haproxy.cfg
global # 最大并发连接数 maxconn 81920 # 修改haproxy的工作目录至指定的目录 chroot /usr/usr/local/haproxy # 让给你haproxy以守护进程的方式工作于后台,当同于“-D”选项. daemon # 指定启动的haproxy的进程个数,只能用于守护进程模式的haproxy,默认只启动一个进程 nbproc 1 # pid文件路径 pidfile /usr/local/haproxy/logs/haproxy.pid # 指定运行haproxy的用户id uid 11 # 指定运行haproxy的用户名,同uid user hadoopdefault # 默认模式:tcp是四层,http是七层,health只会返回ok mode tcp # 重试最大次数 retries 2 # 当serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option redispatch # 当服务器负载很高时,自动结束调当前队列处理比较久的链接 option abortonclose # 最大连接数 maxconn 81920 # 连接超时时间 timeout connect 1d # 客户端超时时间 timeout client 1d # 服务器超时时间 timeout server 1d # 心跳检测超时时间 timeout check 2000 # 日志级别 log 127.0.0.1 local0 warning # 负载均衡算法 balance roundrobin# 定义管理界面 listen admin_stats # 管理界面访问的ip和port bind 0.0.0.0:9999 # 管理界面使用的协议 mode http # 日志格式采用http option httplog # 统计页面自动刷新时间 stats refresh 30s # 统计页面密码框提示文本 stats realm Haproxy Manager # 统计页面用户名密码 stats auth admin:admin# 定义一个监听 listen test bind 0.0.0.0:8088 mode tcp balance source maxconn 1024 server hiveserver2.cn01 128.128.128.11:10020 check inter 120000 rise 1 fall 2 server hiveserver2.cn01 128.128.128.12:10020 check inter 120000 rise 1 fall 2 server hiveserver2.cn01 128.128.128.13:10020 check inter 120000 rise 1 fall 2 server hiveserver2.cn01 128.128.128.14:10020 check inter 120000 rise 1 fall 2

    推荐阅读