且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

haproxy

更新时间:2022-09-20 11:58:55

一致性哈希:

首先用服务器的某个特征,比如主机名或者IP哈希后除以2的32次方,会落到环上的某个点。

然后把客服请求的特征比如ip,hash后的值除以2的32次方,必定会落到环上,顺时针第一个服务器

就为其提供服务。一个服务器挂掉,自动顺时针找下个服务器提供服务。

偏斜:启用虚拟节点,一个服务器虚拟多个服务器到环上面。

LB:

tcp

lvs,haproxy,nginx

application layer:

http:haproxy,nginx,ats,apache

mysql:mysql-proxy

nginx:

proxy_pass

upstream

haproxy:

frontend

use_backend

default_backend

backend

balancer

server

server

listen:

server

default

配置文件:haproxy.cfg

基本配置

#---------------------------------------------------------------------

# main frontend which proxys to the backends

#---------------------------------------------------------------------

frontend  main *:80


default_backend             websrvs


#---------------------------------------------------------------------

# static backend for serving up images, stylesheets and such

#---------------------------------------------------------------------

backend websrvs

balance     roundrobin

server    web1  172.16.6.15:80  check

server    web2  172.16.6.16:80  check

server    web3  172.16.6.17:80  check

其他全局重要的参数:

nbproc:启动多个进程

ulimit-n:每个进程能打开最大文件描述符数目

maxconn:每个进程的最大并发连接数

spread-checks:时间间隔检查后端服务器

参数:

balance:指明调度算法:

动态:权重可动态调整

静态:调整权重不会实时生效,需要重启

roundrobin:轮询,可以动态调整,每个后端服务器最多4128个并发连接。

static-rr:基于权重进行轮询。属于静态方法,某个后端服务器支持的连接数量无限制

leastconn:最小连接数。仅适用于长连接的会话,动态方法

source:源地址哈希。

hash-type:map-based:取模法:静态

   consistent:一致性哈希;动态

uri:对URI的请求左半部分或者整个URI进行hash运算。同一个URI的请求发送到同一台 服务器,常用于缓存服务器。

hash-type:map-based:取模法:静态

   consistent:一致性哈希;动态

url_param:根据url指定的参数的值进行调度,把值做hash计算(如:同一个用户名称

调度到同一台,做到用户绑定),并除以总权重;

hash-type:map-based:取模法:静态

   consistent:一致性哈希;动态

hdr(name):根据请求报文中指定的header(如:user_agent,referer,hostname)进行

调度,把指定的内容hash后调度

hash-type:map-based:取模法:静态

   consistent:一致性哈希;动态

bind:指定端口

frontend  main

   bind *:80

   bind *:8080


mode:haproxy的工作模式

默认为TCP

tcp,http,health

log;定义日志

vim  /etc/rsyslog.cfg

1.启用UDP 514端口

# Provides UDP syslog reception

$ModLoad imudp

$UDPServerRun 514

2.添加:

local2.*             /var/log/haproxy.log

3.service rsyslog restart

maxconn:设置一个前段最大连接数,各个前段之和不能超过global段值

default_backend:

default_backend:指定默认后端

use_backend:

server:

server <name><address>[:port][param*]

check:健康状态检查

inter:检查间隔时间,默认为2000ms

fall:检查几次up--down,默认3次

rise:down-->up

cooke:为指定的server设置cookie

maxconn:此服务器的最大并发连接数

maxqueue:请求队列最大长度

observe:根据流量判断后端server的健康状态

weight:权重,默认1,最大256,0不被调度

redir:启用重定向功能

基于浏览器cookie实现session sticky

 balance roundrobin

cookie SERVERID insert nocache indirect

server    web1  172.16.6.15:80  check cookie websrv1

server    web2  172.16.6.16:80  check cookie websrv2

server    web3  172.16.6.17:80  check cookie websrv3

要点:每个server有自己唯一的cookie标识;

  在backend中定义为用户请求调用完成后操作其cookie

stats enable

listen statistics

   bind *:8888

   stats enable

   stats hide-version  #隐藏版本

   stats uri /haproxyadmin?stats  #访问路径

   stats realm "HAPorxy\ Statistics"  

   stats auth admin:admin  #登录用户名和密码

   stats admin if TRUE #添加管理服务器功能

   stats   refresh 30s #页面自动刷新时间

   

    option httplog:记录更详细的日志

option forwardfor:允许在发完服务器的请求首部中插入X-Forwarded-For首部

在后端httpd服务配置文件中修改为

LogFormat "%{X-Forwarded-For}i %l %u %t 。。。。。。。

errorfile:错误页 

errorfile 400 /etc/haproxy/errorpages/400.http

errorfile 403 /etc/haproxy/errorpages/403.http

errorfile 503 /etc/haproxy/errorpages/503.http

ACL

定义,及调用,实现动静分离。





本文转自阿伦艾弗森 51CTO博客,原文链接:http://blog.51cto.com/perper/1981365,如需转载请自行联系原作者