阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

HAproxy 代理技术原理探究

259次阅读
没有评论

共计 3949 个字符,预计需要花费 10 分钟才能阅读完成。

HAproxy 技术分享

简介

HAProxy 是一款提供高可用性、负载均衡以及基于 TCP(第四层)和 HTTP(第七层)应用的代理软件


Features

1. 免费
2. 能够做到 4 层以上代理
3. 高性能
4. 高稳定性


使用案例

淘宝 CDN(HTTP 反向代理)


测试:

HTTP 代理 ab -i -c 500 -n 100000

       | --- node 8910 URL = /
HAproxy| --- node 8911 URL = /
       | --- node 8912 URL = /
       | --- node 8913 /test/ (reqisetbe ^[^\ ]*\ /(test|uri)/ server_uri_route) # 按照规则转发
####### haproxy :  (单独由 haproxy 进行均衡负载)

Concurrency Level:      500
Time taken for tests:   32.562 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      36606588 bytes
HTML transferred:       0 bytes
Requests per second:    3071.02 [#/sec] (mean)
Time per request:       162.812 [ms] (mean)
Time per request:       0.326 [ms] (mean, across all concurrent requests)
Transfer rate:          1097.85 [Kbytes/sec] received

 

####### nginx : (单独由 nginx 进行均衡负载)

Concurrency Level:      500
Time taken for tests:   36.539 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      38600000 bytes
HTML transferred:       0 bytes
Requests per second:    2736.82 [#/sec] (mean)
Time per request:       182.694 [ms] (mean)
Time per request:       0.365 [ms] (mean, across all concurrent requests)
Transfer rate:          1031.65 [Kbytes/sec] received

对比 Nginx

 

Name Nginx HAproxy
HTTP 代理性能 9 10
TCP 代理性能 0 10
稳定性 10 10
转发规则 10 7
HTTP 代理性能 9 10
平滑升级 10 8

原理 epoll 转发

验证 转发方式为如下
通过 TCP 代理请求 www.baidu.com

client                   HAProxy                     Backend
curl |----------------->| 
                         accept(client)
                         recvfrom(client)
                         sendto(Backent)   
                                          |-------->|
                                                     dowith(HAProxy)
                                          |<--------|
                         recvfrom(Backend)
                         sendto(Client)
     |<-----------------|

strace 查看系统调用

    zhangbo3@vm-222:/etc/haproxy$ sudo strace -p 7876
    
    epoll_wait(0, {{EPOLLIN, {u32=5, u64=5}}}, 7, 1000) = 1
    *** (事件循环,监听连接事件)
    
    accept(5, {
        sa_family=AF_INET, 
        sin_port=htons(56479), 
        sin_addr=inet_addr("127.0.0.1")
    }, [16]) = 1
    *** (来自客户端 [127.0.0.1] 的请求, 端口56479[随机端口])
    
    fcntl(1, F_SETFL, O_RDONLY|O_NONBLOCK)  = 0
    *** (设置连接 socket 为非阻塞)
    
    setsockopt(1, SOL_TCP, TCP_NODELAY, [1], 4) = 0
    *** (设置 TCP 连接为 NODELAY, 禁止 Nagle 算法)
    
    accept(5, 0x7fff8da2d7a0, [128]) = -1 EAGAIN 
    (Resource temporarily unavailable)
    *** (再次去 accept 连接失败,因为 accept 被循环包裹,需要返回 EAGAIN 才break)
    
    recvfrom(1, "GET HTTP://www.baidu.com HTTP/1."..., 8192) = 212
    *** (收到来自客户端的请求 curl -x 127.0.0.1:1180 www.baidu.com)
    
    recvfrom(1, 0x2560ac4, 7980, 0, 0, 0)   = -1 EAGAIN 
    (Resource temporarily unavailable)
    *** (返回 EAGAIN 后break, 表示收到完整的 TCP 数据, 开始处理请求数据)
    
    epoll_ctl(0, EPOLL_CTL_ADD, 1, {EPOLLIN, {u32=1, u64=1}}) = 0
    *** (将来自客户端的 socket 加入 epoll 监听队列)
    
    socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 2
    fcntl(2, F_SETFL, O_RDONLY|O_NONBLOCK)  = 0
    setsockopt(2, SOL_TCP, TCP_NODELAY, [1], 4) = 0
    connect(2, {
        sa_family=AF_INET, 
        sin_port=htons(8912), 
        sin_addr=inet_addr("127.0.0.1")
    }, 16) = -1 EINPROGRESS (Operation now in progress)
    *** (创建到 backend 的 socket 连接, 这里 backend 配置为8912)
    *** (server server3 127.0.0.1:8912)
    
    sendto(2, "GET HTTP://www.baidu.com HTTP/1."..., 
    212, MSG_DONTWAIT|MSG_NOSIGNAL, NULL, 0) = 212
    *** (发送数据到 backend)
    epoll_wait(0, {}, 7, 0)                 = 0
    *** (继续进入 epoll_wait, 等待来自 backend 的数据返回)
    
    gettimeofday({1413020002, 322710}, NULL) = 0
    recvfrom(2, 0x255c960, 16384, 0, 0, 0)  = -1 EAGAIN 
    (Resource temporarily unavailable)
    *** (Backend 数据来了)
    
    epoll_ctl(0, EPOLL_CTL_ADD, 2, {EPOLLIN, {u32=2, u64=2}}) = 0
    epoll_wait(0, {{EPOLLIN, {u32=2, u64=2}}}, 7, 1000) = 1
    gettimeofday({1413020003, 308930}, NULL) = 0
    
    recvfrom(2, "HTTP/1.1 200 OK\r\nDate: Sat, 11 O"...,16384) = 2896
    *** (backend 返回 HTTP 200, 这是标准 HTTP 协议头)
    
    recvfrom(2, 0x255d4b0, 13488, 0, 0, 0)  = -1 EAGAIN 
    (Resource temporarily unavailable)
    
    sendto(1, "HTTP/1.1 200 OK\r\nDate: Sat, 11 O"...) = 2896
    *** (将 backend 返回的数据发送到 front end)
    
    epoll_wait(0, {{EPOLLIN, {u32=2, u64=2}}}, 7, 1000) = 1
    *** (继续进入 epoll_wait)
    gettimeofday({1413020003, 309695}, NULL) = 0
    *** (客户端连接超时,关闭连接)
    shutdown(2, 1 /* send */)               = 0
    close(2)                                = 0
    shutdown(1, 1 /* send */)               = 0
    close(1)                                = 0

验证单进程模型 -> 查看线程数
cat /proc/7878/status

    Name:   haproxy
    State:  S (sleeping)
    ...
    Threads:        1
    SigQ:   0/15594
    SigPnd: 0000000000000000
    ShdPnd: 0000000000000000
    ...
文章属原创,转载请注明出处 联系作者:Email:zhangbolinux@sina.com QQ:513364476
 

Haproxy+Keepalived 搭建 Weblogic 高可用负载均衡集群 http://www.linuxidc.com/Linux/2013-09/89732.htm

Keepalived+HAProxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

使用 HAProxy 配置 HTTP 负载均衡器 http://www.linuxidc.com/Linux/2015-01/112487.htm

HAproxy 的详细介绍:请点这里
HAproxy 的下载地址:请点这里

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计3949字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中