HAProxy各种调度算法介绍

HAProxy 的调度算法比较多,在没有设置 mode 或者其它选项时,HAProxy 默认对
后端服务器使用 roundrobin 算法来分配请求处理。对后端服务器指明使用的算法
时使用balance关键字,该关键字可在listenbackend中出现。在 HAProxy
运行时支持动态调整后端服务器权重、并且考虑后端服务器负载等情况的算法,我们
称之为动态算法;相反,不能动态调整后端服务器权重也不考虑服务器负载的
算法叫静态算法。指定调度策略的典型配置如下:

backend srv_group1
    mode http
    log global
    # balance <algorithm> [<arguments>]
    balance static-rr
    server web1 ip:port weight 2 check inter 300000ms fall 2 rise 5

调度算法官方文档说明

本文介绍 HAProxy 算法的同时,使用一台 HAProxy 服务器和四台后端服务器来验证各
调度算法的效果。另外,客户端使用172.20.2.195 client-node1,使用该客户端访
问 HAProxy 服务器(http://172.20.2.189:8080)来验证。

HAProxy 服务器
172.20.2.189 ubuntu-suosuoli-node1
系统:Ubuntu1804
四台后端服务器
172.20.2.37 node1
172.20.2.43 node2
172.20.2.44 node3
172.20.2.45 node4
系统都为 CentOS7.7
客户端
172.20.2.195 client-node1
系统为 CentOS7.7

准备环境

root@ubuntu-suosuoli-node1:~# vim /etc/ansible/ansible.cfg
......
[backend]
172.20.2.37
172.20.2.43
172.20.2.44
172.20.2.45
......
root@ubuntu-suosuoli-node1:~# ansible backend -m shell -a "nginx -t"
172.20.2.43 | SUCCESS | rc=0 >>
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

172.20.2.45 | SUCCESS | rc=0 >>
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

172.20.2.37 | SUCCESS | rc=0 >>
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful

172.20.2.44 | SUCCESS | rc=0 >>
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

root@ubuntu-suosuoli-node1:~# ansible backend -m shell -a "nginx -s reload"
172.20.2.37 | SUCCESS | rc=0 >>


172.20.2.45 | SUCCESS | rc=0 >>


172.20.2.43 | SUCCESS | rc=0 >>


172.20.2.44 | SUCCESS | rc=0 >>

root@ubuntu-suosuoli-node1:~# ansible backend -a "hostname"
172.20.2.43 | SUCCESS | rc=0 >>
node2

172.20.2.44 | SUCCESS | rc=0 >>
node3

172.20.2.45 | SUCCESS | rc=0 >>
node4

172.20.2.37 | SUCCESS | rc=0 >>
node1

使用的核心配置

global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    stats socket /run/haproxy/admin.sock2 mode 660 level admin expose-fd listeners
    #stats socket /run/haproxy/admin.sock1 mode 660 level admin expose-fd listeners process 1
    #stats socket /run/haproxy/admin.sock2 mode 660 level admin expose-fd listeners process 2
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

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

    # See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
    ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
    ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
    ssl-default-bind-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    option  http-keep-alive
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms
    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

listen stats
    mode http
    bind 172.20.2.189:9999
    stats enable
    log global
    stats uri    /haproxy_status
    stats auth   haadmin:stevenux

frontend  WEB_PORT_8080
    bind 172.20.2.189:8080
    mode http
    use_backend  web_prot_http_nodes

backend web_prot_http_nodes
    mode   http
    # balance roundrobin 默认使用该调度策略
    option forwardfor
    server node1 172.20.2.37:80 weight 1 check inter 3000 fall 3 rise 5
    server node2 172.20.2.43:80 weight 1 check inter 3000 fall 3 rise 5
    server node3 172.20.2.44:80 weight 1 check inter 3000 fall 3 rise 5
    server node4 172.20.2.45:80 weight 1 check inter 3000 fall 3 rise 5

访问 WEB 管理界面http://172.20.2.189:9999/haproxy_status
在这里插入图片描述

一.静态调度算法

静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数
和相应速度等,且无法实时修改权重,只能靠重启 HAProxy 生效。

HAProxy 运行时,要动态修改服务器权重,需要安装额外的工具socat。Socat 是 Linux
下的命令行工具,全名叫 Socket CAT,Socat 的主要工作原理是建立一个双向的通道,
并在这个通道传输比特流。其支持众多协议和链接方式。如 IP、TCP、UDP、IPv6、Socket
文件等。由于其功能强大,可以有多种用途,由于其可以转发 TCP 流,甚至有人将它配置为

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值