当前位置:网站首页>HAproxy: load balancing

HAproxy: load balancing

2022-08-09 12:16:00 focus_lyh

简介


HAProxy 提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案.HAProxy 特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上.

摘录地址:https://www.bilibili.com/read/cv13811038

一、拓扑图


在这里插入图片描述

二、配置部署


2.1 安装

yum -y install haproxy

2.2 配置

2.2.1 基础配置

vim /etc/haproxy
...

配置文件详解

HAproxy配置文件(haproxy.cfg )主要由5部分组成,但有些部分并不是必须的

  • global Global configuration section process,安全配置,性能调整,Debug参数
  • defaults The default configuration section isfrontend, listen, backend提供默认配置
  • frontend ACLConfiguration Section This section is used to set the frontend virtual node that receives user requests,并根据ACL规则直接指定要使用的后端backend
  • backend Real Cluster Configuration Section This section is used to set the configuration of the cluster backend service cluster,Handle front-end user requests,Add real servers
  • listen The proxy configuration section is used for settingshaproxyProxy Services page,Monitor the status of the entire backend cluster

全局配置 global

global
log 127.0.0.1 local0  日志输出配置,所有日志都记录在本机,通过local0输出
#log 127.0.0.1 local0 notice local0Represents a log device,info,notice代表日志级别.
#log 127.0.0.1 local0 info

maxconn 4096           #每个HAproxyThe maximum number of concurrent connections for a process
chroot /usr/local/haproxy    #改变当前工作目录,软件安装目录

uid 99                       #所属运行的用户uid,Can also be set to runHAproxy进程的用户
gid 99        #所属运行的用户组gid,Can also be set to runHAproxy进程的用户和组
daemon                     #以后台形式运行ha-proxy
nbproc 3                   #启动3个ha-proxy进程,可以设置多个进程提高性能
       smaller than the serverCPU内核数,Too many processes can cause the process to crash
pidfile /usr/local/haproxy/run/haproxy.pid     #pid文件位置
debug               #调试模式,输出启动信息到标准输出 

默认的全局配置 defaults

defaults
log  global  #Integrate log format in global configuration
mode http     #所处理的类别(#7层 http;4层tcp) tcpmode is generally used,SSL,SSH,SMTP,MySQL等
option  httplog    #日志类别http日志格式
option  http-server-close  #每次请求完毕后主动关闭http通道
option  dontlognull      # 不记录健康检查的日志信息
option  forwardfor   except 127.0.0.1 如果后端服务器需要获得客户端真实ip需要配置的参数
            可以从http  header中获得客户端ip
option  redispatch  #serverID对应的服务器挂掉后,强制定向到其他健康的服务器
option  abortonclose   #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
 
maxconn 20480    #最大连接数
stats  refresh  30   统计页面刷新间隔
retries  3    3If this connection fails, the service is considered unavailable
balance  roundrobin     # rr 轮询负载均衡
#balance source # source 轮询负载均衡
#balance leastconn # Least connection load balancing method,推荐在Mysql、LDAP等情况下使用

timeout connect  5000   连接超时
       timeout client  50000   客户端超时
       timeout server 50000   后端服务器超时

timeout  check  2000  设置对后端服务器的检测超时时间,默认单位是毫秒
timeout http-request     10s

Virtual front end section frontend

frontend  www
bind *:80  | 0.0.0.0:80 只能在frontend和listen部分进行定义,用于定义一个或几个监听的套接字
   bind  主机名|IP地址|*:port   interface <ens33>(可选)
option httplog    #启用日志记录http请求
option forwardfor    #通过X-Forwarded-For信息,to log client originIP
option httpclose    #Indicates after the client and server complete a connection request,HAproxy将主动关闭此TCP连接
log      global
default_backend web #指定默认的后端服务器池,指定后端真实服务器.1234567

The real server cluster section backend

backend web    使用backendThe keyword definition is named web的后端真实服务器组
mode http
option loghttp
option redispatch If the backend server fails,pass this parameter,
   Force client requests to another healthy backend server,以保证服务正常
option abortonclose This parameter can be used under high server load,自动结束当前队列中处理时间比较长的连接
balance roundrobin 定义负载均衡算法
cookie  SERVERID  表示允许向cookie插入serverid.
   每台服务器的serverid可在下面的server关键字中使用cookie关键字定义

option  httpchk GET  /index.html 启用http的服务状态检测功能
option  httpchk HEAD /index.html  HTTP/1.1\r\nHost:\ www.xxx.com   Header detection format
server  web1  192.168.10.2:80  cookie  server1  weight  6 check inter 2000 rise 2 fall 3
server  web2  192.168.10.22:9000  cookie  server2  weight 6 check inter 2000 rise 2 fall 3

注释如下:
#服务器定义web1,cookie表示serverid为web1
#check inter 2000 Check the heartbeat frequency
#rise 3 is three times correct that the server is available fall 3 is three failures that the server is unavailable
#weight代表权重,backup为备份节点

Proxy page listening section listen

listen web_stats      #通过listen定义了一个名为“web_stats”的监控页面
bind 0.0.0.0:80   #Here you can set the port number of the listening page,Can be any port number
log    global    
 mode   http    
 stats  refresh 30s    #设置HAproxyMonitor the automatic refresh time of the statistics page
 stats  uri  /haproxy-stats #设置HAproxy监控统计页面的URL路径,
stats  realm  welcome  logn\ Haproxy #设置登录HAproxyWhen monitoring the statistics page,Text hint on the password box
stats auth admin:123.com #设置登录HAproxy统计页面的用户名和密码,Multiple usernames and passwords can be set for the monitoring page,每行一个
#stats auth shm:123.com
stats hide-version  #用来隐藏统计页面上HAproxy的版本信息
stats admin if TURE  #可以在监控页面上手工启用或禁用后端真实服务器. 

2.2.2 健康检查

httpHealth check method annotationGETclient请求服务器资源,服务器返回对应资源HEAD只请求响应报文中的HTTP头部POST将客户端的数据提交到服务器PUTThe data sent from the client to the server replaces the specified document contentDELETE删除,请求服务器删除 request-URI 标识资源MOVERequests the server to transfer the specified page to another network.

1、通过监听端口进行健康检测 Only the backend is checked by listening on the portserver的端口,There is no guarantee that the service will actually be available.

listen http_proxy 0.0.0.0:80
       mode http
       cookie SERVERID
       balance roundrobin
       option httpchk
       server web1 192.168.1.1:80 cookie server01 check
       server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 

2、通过URIGet a health check,用过去GET后端server的的web页面,Basically it can represent the availability of backend services.

listen http_proxy 0.0.0.0:80
       mode http
       cookie SERVERID
       balance roundrobin
       option httpchk GET /index.html
       server web1 192.168.1.1:80 cookie server01 check
       server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 

3、通过request获取的头部信息进行匹配进行健康检测 .这种检测方式,It is based on advanced,Some fine-grained monitoring needs.By matching the header information accessed by the backend service.

listen http_proxy 0.0.0.0:80
       mode http
       cookie SERVERID
       balance roundrobin
       option httpchk HEAD /index.jsp HTTP/1.1\r\nHost:\ www.xxx.com
       server web1 192.168.1.1:80 cookie server01 check
       server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 

2.3 启动服务

systemctl start haproxy
systemctl enable haproxy
原网站

版权声明
本文为[focus_lyh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091144273436.html