zoukankan      html  css  js  c++  java
  • HA绝大部分配置解析及示例

    haproxy 配置

    global:全局配置
    chroot #锁定运行目录
    deamon #以守护进程运行
    #stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin #socket文件
    user, group, uid, gid #运行haproxy的用户身份
    nbproc #开启的haproxy进程数,与CPU保持一致
    nbthread #指定每个haproxy进程开启线程数,默认为每个进程一个线程
    cpu-map 1 0 #绑定haproxy 进程至指定CPU
    maxconn #每个haproxy进程的最大并发连接数
    maxsslconn #每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
    maxconnrate #每个进程每秒创建的最大链接数量
    spread-checks #后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间
    pidfile #指定pid文件路径
    log 127.0.0.1 local3 info #定义全局的syslog服务器;最多可以定义两个

    proxies: 代理配置段
    defaults [<name>] #默认配置项,针对以下的frontend、 backend和lsiten生效,可以多个name也可以乜有name
    frontend <name> #前端servername,类似于nginx的一个虚拟主机 server
    backend <name> #后端服务器组,等于nginx的upstream
    listen <name> #将frontend和backend合并在一起配置

    proxies
    defaults配置参数
    option redispatch #当server ID对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发
    option abortonclose #当服务器负责很高的时候,自动结束掉当前队列处理比较久的连接,关闭
    option http-keep-alive #开启与客户端的会话保持
    option forwardfor #透传客户端真实IP至后端web服务器
    mode http #设置默认工作类型
    timeout http-keep-alive 120s #session 会话保持超时时间,范围内会转发到相同的后端服务器
    timeout connect 120s #客户端请求从haproxy到后端server的最长连接等待时间(TCP之前)
    timeout server 600s #客户端请求从haproxy到后端服务端的请求处理超时时长(TCP之后)
    timeout client 600s #设置haproxy与客户端的最长非活动时间
    timeout check 5s #对后端服务器的默认检测超时时间

    proxies
    frontend配置参数
    bind: #指定HAProxy 的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中
    bind [<address>]:<port_range>[, ...][param*]

    listen http_proxy #监听http的多个IP的多个端口和sock文件
    bind :80,:443,:8801-8810
    bind 10.0.0.1:10080,10.0.0.1:100443
    bind /var/run/ssl-frontend.sock user root mode 600 accept-proxy

    liten http_https_proxy #https监听
    bind :80
    bind :443 ssl crt /etc/haproxy/site.pem

    listen http_https_proxy_explicit #监听ipv6、 ipv4和unix sock文件
    bind ipv6@:80
    bind ipv4@public_ssl:443 ssl crt /etc/haproxy/site.pem
    bind unix@ssl-frontend.sock user root mode 600 accept-proxy

    listen external_bind_app1 #监听file descriptor
    bind "fd@${FD_APP1}"

    proxies
    backend: 定义一组后端服务器,backend服务器将被frontend进行调用
    mode http/tcp #指定负责协议类型
    option #配置选项
    server #定义后端real server
    注意:option后面加httpchk, smtpchk, mysql-check, pgsql-check, ssl-hello-chk方法,实现更多应用层检测功能
    server中配置:
    check #对指定real进行健康状态检查,默认不开启
    addr IP #可指定的健康状态检测IP
    port num #指定的健康状态检测端口
    inter num #健康状态检查间隔时间,默认200 ms
    fall num #后端服务器失效检查次数,默认为3
    rise num #后端服务器从下线恢复检查次数,默认为2
    weight #默认为1, 最大为256,0标识不参与负载均衡
    backup #将后端服务器标记为备份状态
    disabled #将后端服务器标记为不可用状态
    redirect prefix http://www.magedu.net/ #将请求临时重定向至其他URL,只适用于http模式
    maxconn <maxconn>: #当前后端server的最大并发连接数
    backlog <backlog>: #当server 的连接数达到上限后的后援队列长度

    frontend+backend配置实例:
    frontend WEB_PORT_80
    bind 192.168.7.248:80
    mode http
    use_backend web_prot_http_nodes

    backend web_prot_http_nodes
    mode http
    option forwardfor
    server 192.168.10.10:8080 check inter 3000 fall 3 rise 5
    server 192.168.10.11:8080 check inter 3000 fall 3 rise 5

    proxies
    listen代替frontend+backend:
    listen WEB_PORT_80
    bind 192.168.10.9:80
    mode http
    option forwardfor
    server web1 192.168.10.10:8080 check inter 3000 fall 3 rise 5
    server web2 192.168.10.11:8080 check inter 3000 fall 3 rise 5

    HAProxy调度算法:
    HAProxy 通过固定参数balance指明对后端服务器的调度算法,可以配置在listen或backend选项中
    HAProxy 调度算法分为静态和动态调度算法
    static-rr: 基于权重的轮训调度,不支持权重的运行时调整及后端服务器慢启动,其后端主机数量没有限制
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance static-rr
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    first: 根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限
    心情求才会分配给下一台服务,会忽略服务器的权重设置
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance first
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 1 check inter 3000 falll 2 rise 5

    roundrobin:基于权重的轮训动态调度算法,支持权重的运行时调整,不完全等于lvs中的rr轮训模式,
    HAProxy中的roundrobin支持慢启动(新加的服务器会捉奸增加转发数),其每个后端backend中最多支持4095个
    real server, roundrobbin为默认算法,支持对real server 权重动态调整
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance roundrobin
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    leastconn: 甲醛的最少连接的动态,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的有限调度(新客户连接)
    比较适合长连接的场景,mysql等
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance leastconn
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    其他算法:
    source: 源地址hash,基于用户源地址hash并将请求转发到后端服务器,默认为静态即取模方式,但是可以通过hash-type
    支持的选项更改,后续同一个源地址请求将被转发至同一个后端web服务器,适用于session保持/缓存业务
    源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,取模法和一致性hash
    map-base 取模法:map-based:取模法,基于服务器总权重的hash数组取模,该hash是静态的即不支持在线调整权重
    不支持慢启动,其对后端服务器调度均衡,缺点是当服务器的总权重发生变化时,服务器上线或下线,都会因权重
    变化导致调度结果整体改变。
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance source
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    一致性hash: 该hash是动态的,支持在线调整权重,支持慢启动,优点在于当服务器的总权重发生变化时,对调度
    结果影响是局部的,不会引起大的变动hash(o)modn
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode tcp
    log global
    balance source
    hash-type consistent
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    uri: 基于对用户请求的uri做hash并将请求转发到后端指定服务器,也可以通过map-based和consistent定义
    使用取模法还是一致性hash
    uri取模法配置
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance uri
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5
    uri一致性hash配置
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance uri
    hash-type consistent
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    url_param: 对用户请求的url中的params部分中的参数name作hash计算,并由服务器总权重相除以后派发至
    某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server
    例:url = http://www.magedu.com/foo/bar/index.php?k1=v1&k2=v2
    则: host = 'www.magedu.com'
    url_param = "k1=v1&k2=v2"
    url_param 取模法配置
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance url_param name,age #支持对单个及多个url_param 值 hash
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5
    url_param 一致性hash配置
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance url_param name, age #支持对单个及多个url_param 值 hash
    hash-type consistent
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    hdr: 针对用户每个http头部(header)请求中的指定信息做hash,此处由name指定的http首部将会被取出并
    做hash计算,然后由服务器总权重相除以后派发至某挑出的服务器,如无有效的值,则使用默认轮训调度
    hdr取模法配置
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance hdr(User-Agent)
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5
    一致性hash配置
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance hdr(User-Agent)
    hash-type consistent
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    rdp-cookie: 对远程windows桌面的负载,使用cookie保持会话
    rdp-cookie取模法配置
    listen RDP
    bind 192.168.10.9:3389
    balance rdp-cookie
    mode tcp
    server web1 192.168.10.10:3389 check fall 3 rise 5 inter 2000 weight 1
    rdp-cookie一致性hash配置
    listen web_host
    bind 192.168.10.9:3389
    mode tcp
    balance rdp-cookie
    hash-type consistent
    server web1 192.168.10.10:3389 check fall 3 rise 5 inter 2000 weight 1
    基于iptables实现:
    net.ipv4.ip_forward=1 必须开启ip 转发功能
    iptables -t nat -A PREROUTING -d 192.168.10.9 -p tcp --dport 3389 -j DNAT --to-destination 192.168.10.10:3389
    iptables -t nat -A POSTROUTING -s 192.168.0.0/21 -j SNAT --to-source 192.168.10.9

    random: 1.9 版本开始增加一个叫做 random 的负载平衡算法,其基于一个随机数作为一致性hash的key,随机负载平衡对于大型服务器
    或经常添加或删除服务器非常有用,因为它可以避免在这种情况下由roundrobin或leastconn导致的锤击效应
    listen web_host
    bind 192.168.10.9:80,:8801-8810,192.168.10.8:9001-9010
    mode http
    log global
    balance random
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 2 check inter 3000 fall 2 rise 5

    动态调整权限:
    yum -y install socat
    echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock
    echo "set weight web_host/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock
    echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock

    算法总结:
    static-rr --------> tcp/http 静态
    first --------> tcp/http 静态

    roundrobin--------> tcp/http 动态
    leastconn --------> tcp/http 动态
    random --------> tcp/http 动态

    source --------> tcp/http
    uri --------> http
    url_param --------> http #取决于hash_type是否consistent
    hdr --------> http
    rdp-cookie--------> tcp

    四层IP透传:
    haproxy配置
    listen web_prot_http_nodes
    bind 192.168.10.10:80
    mode tcp
    balance roundrobin
    server web1 blogs.studylinux.net:80 send-proxy check inter 3000 fall 3 rise 5
    nginx配置
    server {
    listen 80 proxy_protocol;
    #listen 80;
    server_name blogs.studylinex.net;
    }

    七层IP透传
    HAProxy配置
    defaults
    oprtion fowardfor
    #或者 option forwardfor header X-Forwarded-xxx 自定义传递IP参数,后端web服务器写X-Forwarded-xxx
    如果option forwardfor则后端服务器web格式为X-Forwarded-For

    listen配置
    listen web_host
    bind 192.168.10.10:80
    mode http
    log global
    balance random
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 1 check inter 3000 fall 2 rise 5

    web服务器日志格式配置
    配置web服务器,记录负载均衡透传的客户端IP地址
    apache配置:
    LogFormat "%{X-Forwarded-For}i %a %l %u %t "%r" %>s %b "%{Referer}i"%{User-Agent}i"" commbined
    tomcat 配置
    pattern='%{X-Forwarded-For}i %l %T %t *quot;%r&quot; %s %b &quot;%{User-Agent}i&quot;'/>
    nginx配置
    log_format main '"$http_x_forwarded_For" - $remote_user [$time_local] "$request"'
    '$status $body_bytes_sent "$http_referer"'
    '"$http_user_agent"';

    高级功能及配置
    cookie value:为当前server指定cookie值,实现基于cookie的会话粘性
    配置选项:
    cookie name [rewrite |insert | prefix] [ indirect ][ nocache ] [ postonly ] [ preserve ] [ httponly]
    [ secure ] [ domain ] [ maxidle <idle> ] [ maxlife ]
    name: cookie 的 key 名称,用于实现持久连接
    insert:如果没有就插入新的cookie
    indirect: 不会向客户端发送服务器已经处理过请求的cookie信息,间接
    nocache:当client和haproxy之间有缓存时,不缓存cookie

    配置示例:
    listen web_host
    bind 192.168.10.10:80
    mode http
    log global
    balance roundrobin
    cookie SERVER-COOKIE inserver indirect nocache
    server web1 192.168.10.10:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 weight 1 check inter 3000 fall 2 rise 5

    HAProxy状态页
    web界面
    https://cbonte.github.io/harpoxy-dconv/2.0/configuration.html$4-stats%20uri
    状态也配置项:
    stats enable #基于默认的参数启用stats page
    stats hide-version #隐藏版本
    stats refresh <delay> #设定自动刷新时间间隔
    stats uri <prefix> #自定义 stats page uri,默认值: /haproxy?stats
    stats realm <realm> #账户认证时的提示信息,示例; stats realm : HAProxy Statistics
    stats auth <user>:<passwd>#认证时的账户和密码,可使用多次,默认:no authentication
    stats admin { if | unless } <cond> #启用stats page 中的管理功能
    启用状态页
    listen stats
    bind :9009
    stats enable
    #stats hide-version
    stats uri /haproxy-status
    stats realm HAPorxy Stats Page
    stats auth haadmin:123456
    stats auth admin:123456
    #stats refresh 30s
    #stats admin if TRUE
    登录状态页
    pid = 3689 (process #2, nbproc = 2, nbthread =2) #pid为当前pid号,process为当前进程号,nbproc和nbthread为
    一共多少进程和每个进程多少个线程
    uptime = 0d 0h00m08s #启动了多长时间
    system limits: memmax = unlimited; ulimit-n = 1311124 #系统资源限制:内存/最大打开文件数/
    maxsock = 1311124; maxconn = 65535; maxpipes = 0; #最大socket连接数/单进程最大连接数/最大管道数
    maxpipes
    current conns = 1; current pipes =0/0; conn rate = 1/sec #当前连接数/当前管道输/当前连速率
    Running stasks: 1/9; idle = 100% #运行的任务/当前空闲率
    active UP: #现在服务器
    backup UP: #标记为backup的服务器
    active UP, going down: #监测未通过正在进入down过程
    backup UP, going down: #备份服务器正在进入down过程
    active DOWN, going up: #down的服务器正在进入up过程
    backup DOWN, going up: #备份服务器正在进入up过程
    active or backup DOWN: #在线的服务器或者是backup的服务器已经转换成了down状态
    not checked: #标记为不监测的服务器
    active or backup DOWN for maintenance (MAINT) #active或者backup服务器认为下线的
    active or backup SOFT STOPPED for maintenance #active或者backup被人为软下线(认为将weight改成0)

    报文修改
    在http模式下,基于实际需求修改客户端的请求报文与响应报文,通过reqadd和reqdel在请求报文添加删除字段,通过
    rspadd与rspdel在响应报文中添加与删除字段。
    在请求报文尾部添加指定首部
    reqadd <string> [{if | unless} <cond>]
    从请求报文中删除屁屁额正则表达式的首部
    reqdel <search> [{if | unless} <cond>]
    reqdel <search> [{if | unless} <cond>]
    在响应报文尾部添加指定首部
    repadd <string> [{if | unless} <cond>]
    示例:
    rspadd x-via: HAPorxy

    从响应报文中删除屁屁额正则表达式的首部
    reqdel <search> [{if | unless} <cond>]
    reqdel <search> [{if | unless} <cond>]
    示例:
    rspdel server.* #从响应报文删除server信息
    rspdel x-Powered-By:.* #从响应报文删除x-Powered-By信息

    web服务器状态检测:
    基于不同的检测方式,对后端real server进行状态检测
    option httpchk
    option httpchk <uri>
    option httpchk <method> <uri>
    option httpchk <method> <uri> <version>
    三种状态检测方式:
    基于四层的传输端口做状态检测
    基于指定URI做状态检测
    基于指定URI的request请求头部内容做状态检测
    配置示例:
    listen web_host
    bind 192.168.10.10:80
    mode http
    balance roundrobin
    log global
    option httplog
    #option httpchk GET /app/monitor/check.html HTTP/1.0
    option httpchk HEAD /app/monitor/check.html HTTP/1.o Host: 192.168.10.10
    cookie SERVER-COOKIE insert indirect nocache
    server web1 192.168.10.10:80 cookie web1 check inter 3000 fall 2 rise 5
    server web2 192.168.10.11:80 cookie web2 check inter 3000 fall 2 rise 5

    acl配置选项:
    acl <aclname> <criterion> <flags> <operator> [<value>]
    acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型
    ACL-Name
    acl image_service hdr_dom(host) -i img.magedu.com
    ACL名称,可以使用大写字母A-Z、小写字母a-z 数字0-9冒号: 点 . 中横线和下划线,并严格区分大小写,
    Image_site和image_site完全是两个acl

  • 相关阅读:
    Layui 数据表格显示图片,鼠标滑过图片放大
    ModuleNotFoundError: No module named redis
    Layui 数据表格特定数据行变色
    Qt下QMainWindow内部QTableView不能自适应大小
    android-启动另外一个Activity
    sqlite获取表字段
    Apache下更改.htaccess文件名称
    QTableView的indexAt使用方法
    Qt分割线
    QTableView排序
  • 原文地址:https://www.cnblogs.com/sqbk/p/14283059.html
Copyright © 2011-2022 走看看