3.haproxy负载均衡

haproxy负载均衡

一、haproxy介绍

开源、负载均衡器

同时支持4层负载、7层负载

1、负载均衡类型

  • 4层负载

    根据IP/port进行负载、调度

    LVS、nginx(stream模块)

  • 7层负载

    针对http/https协议进行负载, 灵活性高

    根据应用层数据(图片、视频、动画等)进行调度

    nginx(upstream模块)

二、haproxy配置文件

1、backend

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

2、frontend

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

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

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

2、安装配置haproxy

2.1 安装软件

bash 复制代码
[root@master_haproxy ~]# yum install -y haproxy 

2.2 编辑配置

bash 复制代码
[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

bash 复制代码
[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)
    以正则表达式的方式匹配主机名
bash 复制代码
acl test1 hdr_reg(host) -i download 
use_backend xxxxxx if test1
  • hdr_dom(host)
    精确匹配主机名
bash 复制代码
acl test2 hdr_dom(host) -i www.jd.com 
  • hdr_beg(host)
    匹配以xxxx开头的主机名
bash 复制代码
acl tes3 hdr_beg(host) -i blog.
  • path_end
    根据URL以xxxx结尾的请求
bash 复制代码
acl test4 path_end -i .jpg .jpeg .png .gif .css .js 
  • path_beg
    根据URL以xxx开头的请求
bash 复制代码
acl test5 path_beg -i https:// 
  • url_ip
    根据请求中的目的IP来匹配
bash 复制代码
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地址匹配请求
bash 复制代码
acl test8 src -i 12.32.34.43
http-request deny if test8 
  • method
    根据http的请求方法来匹配请求 GET/POST
bash 复制代码
acl test7 method -i POST 

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

2.1 编辑haproxy配置文件

bash 复制代码
[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
bash 复制代码
[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日志格式

bash 复制代码
    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
bash 复制代码
[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"
相关推荐
NiKo_W17 分钟前
Linux 深入理解权限
linux·运维·服务器
zzywxc7871 小时前
自动化测试框架是软件测试的核心基础设施,通过预设规则和脚本自动执行测试用例,显著提高测试效率和覆盖率。
运维·人工智能·自动化·prompt·测试用例·流程图
郝学胜-神的一滴1 小时前
深入探索 Python 元组:从基础到高级应用
运维·服务器·开发语言·python·程序人生
CheungChunChiu1 小时前
嵌入式 Linux 启动机制全解析:从 Boot 到 Rootfs
linux·运维·服务器·ubuntu·uboot·boot·extboot
白鹭1 小时前
nginx(介绍+源码安装+平滑升级和回滚)
linux·运维·服务器·nginx·回滚·平滑升级
wanhengidc2 小时前
云手机在办公领域中自动化的应用
运维·智能手机·自动化
知星小度S4 小时前
系统核心解析:深入操作系统内部机制——进程管理与控制指南(一)【进程/PCB】
linux·运维·服务器·进程
编码浪子7 小时前
趣味学RUST基础篇(异步)
服务器·rust·负载均衡
码农101号9 小时前
运维安全05 - iptables规则保存与恢复
运维·网络·安全
bug攻城狮10 小时前
解决Ubuntu中apt-get -y安装时弹出交互提示的问题
linux·运维·ubuntu