nginx跟haproxy的负载均衡比对(附源码)

96
DeanKano
2018.08.31 15:12* 字数 222

一文一言: 骑自行车的再努力也追不上路虎,说明,平台很重要

简介:

ha-ng-compare.png

测试目标:

  实现8081&8082 均衡 8083备份(在8081&8082都down机时执行)

  server 127.0.0.1:8081 weight=4 max_fails=2 fail_timeout=30s;
  server 127.0.0.1:8082 weight=4 max_fails=2 fail_timeout=30s;
  server 127.0.0.1:8083 backup;

环境:

haproxy官网: https://www.haproxy.org/
nginx官网: https://nginx.org/

1.安装: 选择对应os的版本离线安装,选择性配置环境变量

在线安装示例:
linux: # yum install haproxy
mac: # brew install haproxy

2.版本:

最终目的,成功使用cli命令客户端

root:haproxy mac$ haproxy -v
HA-Proxy version 1.8.7 2018/04/07
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>

root:haproxy mac$ nginx -V
nginx version: nginx/1.13.10
built by clang 9.0.0 (clang-900.0.39.2)
built with OpenSSL 1.0.2n  7 Dec 2017 (running with OpenSSL 1.0.2o  27 Mar 2018)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/....

3.配置:(详细配置见源码文件)

4.手动方式启动:

haproxy-config.jpeg

haproxy -f /usr/local/haproxy-loadbalance/ha01/haproxy.conf
如上述的配置:
stats-ui地址(stats uri & bind 配置): localhost:9099/haproxy-stats
输入用户名密码(stats auth配置):root/root

haproxy统计ui界面
nginx-config.jpeg

nginx -c /usr/local/nginx-loadbalance/ng01/nginx.conf

ha&ng结果比对:

roundrobin负载均衡

haproxy-roundrobin

nginx-roundrobin

backup 备份机制

haproxy备份测试

nginx备份测试

Req测试比对

haproxy-test.jpeg

nginx-test.png

源码见:

https://github.com/lingqibaobei/rangers-nginx-haproxy

  ./haproxy-loadbance.zip
  ./nginx-loadbance.zip
入坑
日记本
Gupao