3.haproxy负载均衡

作者 : admin 本文共3477个字,预计阅读时间需要9分钟 发布时间: 2024-06-8 共3人阅读

haproxy负载均衡

  • 一、haproxy介绍
    • 1、负载均衡类型
  • 二、haproxy配置文件
    • 1、backend
    • 2、frontend
  • 三、haproxy实现MySQL负载均衡 —— 4层
    • 1、后端两台MySQL配置双主复制
    • 2、安装配置haproxy
      • 2.1 安装软件
      • 2.2 编辑配置
      • 2.3 启动haproxy
      • 2.4 测试通过haproxy正常连接MySQL
  • 四、haproxy实现七层调度
    • 1、匹配请求的方法
    • 2、配置haproxy实现web 7层负载
    • 2.1 编辑haproxy配置文件
    • 2.2 所有网站域名统一解析到haproxy上,测试访问
    • 2.3 修改nginx日志格式

一、haproxy介绍

开源、负载均衡器
同时支持4层负载、7层负载

1、负载均衡类型

  • 4层负载
    根据IP/port进行负载、调度
    LVS、nginx(stream模块)

  • 7层负载
    针对http/https协议进行负载, 灵活性高
    根据应用层数据(图片、视频、动画等)进行调度
    nginx(upstream模块)

二、haproxy配置文件

1、backend

  • 定义后端业务服务器
backend xxxx
	balance roundrobin			// 调度算法
	mode {tcp|http}				// tcp:4层负载、http: 7层负载
	server 名称 IP:port check
	server 名称 IP:port check

2、frontend

  • 定义虚拟服务
frontend 虚拟服务名称
	bind IP:port
	mode {tcp|http}
	use_backend backend后端服务器

三、haproxy实现MySQL负载均衡 —— 4层

3.haproxy负载均衡插图

1、后端两台MySQL配置双主复制

2、安装配置haproxy

2.1 安装软件

[root@master_haproxy ~]# yum install -y haproxy 

2.2 编辑配置

[root@master_haproxy ~]# vim /etc/haproxy/haproxy.cfg 
frontend MySQL_Service
   bind 0.0.0.0:3306
   mode tcp
   use_backend DB

backend DB
   balance source  
   mode tcp
   server db01 192.168.140.13:3306
   server db02 192.168.140.14:3306

2.3 启动haproxy

[root@master_haproxy ~]# systemctl enable --now haproxy
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@master_haproxy ~]# 
[root@master_haproxy ~]# netstat -tunlp | grep haproxy 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1500/haproxy        
udp        0      0 0.0.0.0:58409           0.0.0.0:*                           1499/haproxy     

2.4 测试通过haproxy正常连接MySQL

四、haproxy实现七层调度

1、匹配请求的方法

ACL, 访问控制列表, 作用用于匹配请求,便于7层调度
语法: acl acl名称 -i 条件

  • hdr_reg(host)
    以正则表达式的方式匹配主机名
acl test1 hdr_reg(host) -i download 
use_backend xxxxxx if test1
  • hdr_dom(host)
    精确匹配主机名
acl test2 hdr_dom(host) -i www.jd.com 
  • hdr_beg(host)
    匹配以xxxx开头的主机名
acl tes3 hdr_beg(host) -i blog.
  • path_end
    根据URL以xxxx结尾的请求
acl test4 path_end -i .jpg .jpeg .png .gif .css .js 
  • path_beg
    根据URL以xxx开头的请求
acl test5 path_beg -i http:// 
  • url_ip
    根据请求中的目的IP来匹配
acl test6 url_ip 10.0.0.0/8
acl test6 url_ip 172.12.0.0/12
acl test6 url_ip 192.168.0.0/16
http-request deny if test6 
  • src
    根据源IP地址匹配请求
acl test8 src -i 12.32.34.43
http-request deny if test8 
  • method
    根据http的请求方法来匹配请求 GET/POST
acl test7 method -i POST 

2、配置haproxy实现web 7层负载

3.haproxy负载均衡插图(1)

2.1 编辑haproxy配置文件

[root@master_haproxy ~]# vim /etc/haproxy/haproxy.cfg 
frontend web_service
   bind 0.0.0.0:80
   mode http
   option forwardfor					// 让后端服务器日志记录真实客户端地址

   acl blog_acl hdr_reg(host) blog.
   use_backend blog if blog_acl

   acl cart_acl hdr_dom(host) opencart.linux.com
   use_backend opencart if cart_acl

   default_backend opencart     // 定义默认后端,通过IP访问haproxy时默认为503 


backend blog
   balance roundrobin
   mode http
   server blog01 192.168.140.12:80 check
   server blog02 192.168.140.13:80 check

backend opencart
   balance roundrobin
   mode http
   server cart01 192.168.140.12:80 check
   server cart02 192.168.140.13:80 check
[root@master_haproxy ~]# systemctl restart haproxy
[root@master_haproxy ~]# 
[root@master_haproxy ~]# netstat -tunlp | grep haproxy
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1909/haproxy        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1909/haproxy        
udp        0      0 0.0.0.0:42707           0.0.0.0:*                           1908/haproxy   

2.2 所有网站域名统一解析到haproxy上,测试访问

2.3 修改nginx日志格式

    log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';$remote_addr修改为$http_x_forwarded_for
[root@web01 ~]# tail -n 3 /usr/local/nginx/logs/opencart_access.log 
192.168.140.1 - - [08/Jun/2024:15:30:35 +0800] "GET /image/cache/catalog/demo/product/product-5-300x300.png HTTP/1.1" 404 153 "http://opencart.linux.com/index.php?route=product/category&path=20_26" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0"
192.168.140.1 - - [08/Jun/2024:15:30:35 +0800] "GET /image/cache/catalog/demo/slideshow/banner-2-287x403.png HTTP/1.1" 404 153 "http://opencart.linux.com/index.php?route=product/category&path=20_26" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0"
本站无任何商业行为
个人在线分享 » 3.haproxy负载均衡
E-->