HAproxy七层负载详解

文章目录

  • 前言
  • 一、HAProxy Session亲缘性
  • 三、HAproxy负载均衡
    • 1、环境架构
    • 2、配置haproxy
    • 3、浏览器访问
  • 四、HAproxy动静分离
    • 1、环境架构
    • 2、配置Haproxy
    • 3、浏览器访问


前言

Haproxy是一款高性能的负载均衡软件。

7层负载均衡、应用程序负载均衡、URL负载均衡、动静分离技术
特点

• 支持tcp / http 两种协议层的负载均衡

• 支持8种左右的负载均衡算法

• 性能卓越

• 拥有功能出色的监控页面stats

• 功能强大的ACL支持


一、HAProxy Session亲缘性

haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式

①、用户IP 识别

haproxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令)

配置指令 balance source

②、cookie 识别

haproxy 将WEB服务端发送给客户端的cookie中插入(或添加前缀)haproxy定义的后端的服务器COOKIE ID

配置指令 cookie SESSION_COOKIE insert indirect nocache

③、session 识别

session / ˈseʃn 一场;一节;一段时间

haproxy 将后端服务器产生的session和后端服务器标识存在haproxy中的一张表里。客户端请求时先查询这张表

配置指令 appsession JSESSIONID len 64 timeout 5h request-learn

三、HAproxy负载均衡

1、环境架构

4台Centos7主机

             ip                                  role
      192.168.116.131                           client
      192.168.116.132                           haproxy
      192.168.116.133                            web1
      192.168.116.134                            web2

2、配置haproxy

# vim /etc/haproxy/haproxy.cfg

global                                                               # 全局配置
  log         127.0.0.1 local3 info								  # 日志记录
  user        haproxy                                                # 使用用户
  group       haproxy                                                # 使用组
  maxconn     4000                                                   # 最大连接数
  pidfile     /run/haproxy.pid                                       # 进程ID存储位置
  daemon                                                             # 守护进程运行
  nbproc      1                                                      # haproxy进程数
 
defaults                                                             # 默认配置
  log         global
  maxconn     2000
  mode        http                                                   # 7层LB模式
  option      httplog                                                # 美化日志
  retries     3                                                      # 健康监测,3次失败不可用
  option      redispatch                                             # 不可用二次发送
  timeout     connect   5000                                         # Client和Server连接时延
  timeout     client	50000                                        # 与Server连接时延
  timeout     server	50000                                        # 与Client连接时延
  option      abortonclose                                           # 结束久处理请求
  
listen stats                                                          # 前后端组合
  bind		 :8000                                                   # 绑定端口
  stats       uri /admin_stats                                        # 监控地址
  stats       realm Private loads                                     # 监控提示信息
  stats       auth  admin:111111                                      # 监控页面认证
  stats       hide-version                                            # 隐藏版本号

frontend      http-in                                                 # 前端定义
  bind        :80                                                     # 绑定80
  mode        http                                                    # 7层LB模式
  log         global
  option      httplog
  acl html    url_reg -i \.html$                                      # acl访问控制列表
  use_backend html-server if html                                     # 适用后端if命中acl
  default_backend html-server                                         # 默认后端

backend       html-server                                             # 后端定义
  mode        http                                                    # 7层LB模式
  balance     roundrobin                                              # 调度算法
  option      httpchk GET /index.html                                 # 健康检查
  cookie      SERVERID insert indirect  nocache                       # 插入cookie
  server      html-A 192.168.116.133:80 cookie 1 weight 1 check inter 2000 rise 2 fall 5   # 真实服务器 权重 cookie 检查间隔
  server      html-B 192.168.116.134:80 cookie 2 weight 1 check inter 2000 rise 2 fall 5   # 检查2次成功可用    检查5次失败不可用
# systemctl start haproxy.service
# systemctl enable haproxy.service

3、浏览器访问

http://192.168.116.132:80

http://192.168.116.132:8000/admin_stats

四、HAproxy动静分离

1、环境架构

6台Centos7主机

             ip                                  role
      192.168.116.131                           client
      192.168.116.132                           haproxy
      192.168.116.133                          httpd-html1
      192.168.116.134                          httpd-html2
      192.168.116.135                          nginx-php1
      192.168.116.136                          nginx-php1

2、配置Haproxy

# yum -y install haproxy
# vim /etc/haproxy/haproxy.cfg

global
  log         127.0.0.1 local3 info
  user        haproxy                                                
  group       haproxy                                                
  maxconn     4000                                                   
  pidfile     /run/haproxy.pid                                       
  daemon                                                             
  nbproc      1                                                      
 
defaults                                                            
  log         global
  maxconn     2000
  mode        http                                                  
  option      httplog                                               
  retries     3                                                     
  option      redispatch 
  timeout     connect   5000
  timeout     client	50000
  timeout     server	50000
  option      abortonclose
  
listen stats
  bind		 :8000
  stats       uri /admin_stats
  stats       realm Private loads
  stats       auth  admin:111111
  stats       hide-version

frontend      http-in
  bind        :80
  mode        http
  log         global
  option      httplog
  acl html    url_reg -i \.html$
  acl php     url_reg -i \.php$
  use_backend html-server if html
  use_backend php-server  if php
  default_backend html-server

backend       html-server
  mode        http
  balance     roundrobin
  option      httpchk GET /index.html
  cookie      SERVERID insert indirect  nocache
  server      html-A 192.168.116.133:80 cookie 1 weight 1 check inter 2000 rise 2 fall 5
  server      html-B 192.168.116.134:80 cookie 2 weight 1 check inter 2000 rise 2 fall 5

backend       php-server
  mode        http
  balance     roundrobin
  option      httpchk GET /index.php
  cookie      SERVERID insert indirect  nocache
  server      php-A 192.168.116.135:80 cookie 3 weight 1 check inter 2000 rise 2 fall 5
  server      php-B 192.168.116.136:80 cookie 4 weight 1 check inter 2000 rise 2 fall 5
# systemctl start haproxy.service
# systemctl enable haproxy.service

3、浏览器访问

http://192.168.116.132:80/index.html

http://192.168.116.132:80/index.php

http://192.168.116.132:8000/admin_stats

你可能感兴趣的:(proxy模式,负载均衡)