负载均衡入门:HAProxy 双 Web 节点集群配置与验证

HAProxy安装

为了更好的快速掌握 HAProxy 的安装和使用,我们以一个案例来进行讲解。首先根据以下要求进行虚拟机的克隆。

编号主机IP软件系统
1lb01192.168.72.100haproxyredhat 9.7
2web1192.168.72.10nginxredhat 9.7
3web2192.168.72.20nginxredhat 9.7

1.1 搭建Web1

1、克隆一台虚拟机,初始化虚拟机(ip ,主机名,关闭防火墙,selinux)

[root@node1 ~]# hostnamectl set-hostname web1&&bash
[root@web1 ~]# systemctl disable firewalld.service 
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@web1 ~]# setenforce 0
[root@web1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=permissive/" /etc/selinux/config 

下载nginx

[root@web1 ~]# dnf install nginx -y
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.

BaseOS                                                                                      2.7 MB/s | 2.7 kB     00:00    
AppStream                                                                                   3.1 MB/s | 3.2 kB     00:00    
Package nginx-2:1.20.1-22.el9.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

修改欢迎页

[root@web1 ~]# echo "$(hostname) $(hostname -I)" > /usr/share/nginx/html/index.html

[root@web1 ~]# echo "health" > /usr/share/nginx/html/test.html

启动nginx并测试

[root@web1 ~]# systemctl start nginx
[root@web1 ~]# curl localhost
web1 192.168.72.10 
[root@web1 ~]# curl 192.168.72.10 
web1 192.168.72.10 
[root@web1 ~]# 


1.2 搭建Web2

1、克隆一台虚拟机,初始化虚拟机(ip ,主机名,关闭防火墙,selinux)

[root@node1 ~]# hostnamectl set-hostname web2&&bash
[root@web2 ~]# systemctl disable firewalld.service 
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@web2 ~]# setenforce 0
[root@web2 ~]# sed -i "s/SELINUX=enforcing/SELINUX=permissive/" /etc/selinux/config 

下载nginx

[root@web2 ~]# dnf install nginx -y
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.

BaseOS                                                                                      2.7 MB/s | 2.7 kB     00:00    
AppStream                                                                                   3.1 MB/s | 3.2 kB     00:00    
Package nginx-2:1.20.1-22.el9.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@web2 ~]# 
 

修改欢迎页,创建健康检测页面

[root@web2~]# echo "$(hostname) $(hostname -I)" > /usr/share/nginx/html/index.html

[root@web2 ~]# echo "health" > /usr/share/nginx/html/test.html

启动nginx并测试

[root@web2 ~]# systemctl start nginx
[root@web2~]# curl localhost
web1 192.168.72.20
[root@web2~]# curl 192.168.72.20
web1 192.168.72.20
[root@web2 ~]# 

1.3 搭建HAProxy

1、克隆一台虚拟机,初始化虚拟机(ip ,主机名,关闭防火墙,selinux)

[root@node1 ~]# hostnamectl set-hostname lb1&&bash
[root@lb1 ~]# systemctl disable firewalld.service 
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@lb1 ~]# setenforce 0
[root@lb1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=permissive/" /etc/selinux/config 

下载haproxy

[root@lb1 ~]# dnf install haproxy -y
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.

BaseOS                                                                                      2.7 MB/s | 2.7 kB     00:00    
AppStream                                                                                   3.1 MB/s | 3.2 kB     00:00    
Dependencies resolved.
============================================================================================================================
 Package                    Architecture              Version                            Repository                    Size
============================================================================================================================
Installing:
 haproxy                    x86_64                    2.4.22-4.el9                       AppStream                    2.2 M

Transaction Summary
============================================================================================================================
Install  1 Package

Total size: 2.2 M
Installed size: 6.6 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                    1/1 
  Running scriptlet: haproxy-2.4.22-4.el9.x86_64                                                                        1/1 
  Installing       : haproxy-2.4.22-4.el9.x86_64                                                                        1/1 
  Running scriptlet: haproxy-2.4.22-4.el9.x86_64                                                                        1/1 
  Verifying        : haproxy-2.4.22-4.el9.x86_64                                                                        1/1 
Installed products updated.

Installed:
  haproxy-2.4.22-4.el9.x86_64                                                                                               

Complete!
 

验证haproxy

[root@lb1 ~]# haproxy -v
HAProxy version 2.4.22-f8e3218 2023/02/14 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2026.
Known bugs: http://www.haproxy.org/bugs/bugs-2.4.22.html
Running on: Linux 5.14.0-570.12.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Apr 4 10:41:31 EDT 2025 x86_64

查看配置文件路径

[root@lb1 ~]# rpm -qc haproxy
/etc/haproxy/haproxy.cfg                # 核心配置文件路径
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy                # 启动选项所在文件

修改配置文件

[root@lb1 ~]# cp /etc/haproxy/haproxy.cfg{,.bak}
[root@lb1 ~]# vim /etc/haproxy/haproxy.cfg
[root@lb1 ~]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    stats socket /var/lib/haproxy/stats

    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend main
    bind *:80
    default_backend             webcluster

backend webcluster
    balance     roundrobin
    option httpchk GET /test.html
    server      web1 192.168.72.10:80 check inter 2000 rise 2 fall 2 weight 2
    server      web2 192.168.72.20:80 check inter 2000 rise 2 fall 2 weight 2
listen admin_status
    bind *:9129
    stats refresh 30s
    stats uri /admin
    stats auth admin:admin123
    stats hide-version
    stats admin if TRUE

启动haproxy


[root@lb1 ~]# systemctl start haproxy

[root@lb1 ~]# systemctl stop firewalld

打开浏览器访问

http://192.168.72.100:9129/admin

输出后,会弹出输入用户名和密码的窗口,我们输入在配置文件中配置的用户名(admin)和密码(admin123)后就可以登录成功。登录成功后就可以看到如下的界面了。

Read more

try/catch/Promise:前端错误处理实战|JS 基础语法与数据操作篇

try/catch/Promise:前端错误处理实战|JS 基础语法与数据操作篇

【try/catch/Promise】+【前端错误处理】:从【异常捕获逻辑】到【落地实操】,彻底搞懂前端错误处理的最佳写法,避开异步捕获、HTTP状态码判断高频坑! 📑 文章目录 * 开篇 * 一、先搞清楚:try/catch 到底能抓到啥 * 1.1 能抓到的:同步代码里的异常 * 1.2 抓不到的:异步里的错误 * 二、Ajax 错误:别只盯着 try/catch * 2.1 fetch 是什么?小白必读 * 2.2 常见误解 * 2.3 正确做法 * 三、JSON 解析错误:最容易漏掉的一类 * 3.1 常见场景

【保姆级教程】手把手教你安装OpenClaw并接入飞书,让AI在聊天软件里帮你干活

【保姆级教程】手把手教你安装OpenClaw并接入飞书,让AI在聊天软件里帮你干活

这里先做一下简单的科普: OpenClaw 的名字经历了三次变更,第一次叫做 ClawdBot,后来因为名字跟 Claude 太过相似,被 CLaude 告侵权,遂改名 MoltBot 。 但是后来在改名过程中遭遇域名和社交账号被抢注,甚至出坑同名加密货币割韭菜的情况,导致名称传播受阻。 最终定名为:OpenClaw。 所以,名字经历先后顺序为:ClawdBot -> MoltBot -> OpenClaw 大家不要因为名字困惑了,怀疑是不是自己下错软件了,他们都是同一个。 一、什么是 OpenClaw? OpenClaw(曾用名 Clawdbot)是一款 2026 年爆火的开源个人 AI 助手,GitHub 星标已超过 10 万颗。与传统 AI 聊天机器人的根本区别在于: * 真正的执行能力:不仅能回答问题,

前端部署:别让你的应用在上线后掉链子

前端部署:别让你的应用在上线后掉链子 毒舌时刻 这部署流程写得跟绕口令似的,谁能记得住? 各位前端同行,咱们今天聊聊前端部署。别告诉我你还在手动上传文件到服务器,那感觉就像在石器时代用石头砸坚果——能用,但效率低得可怜。 为什么你需要自动化部署 最近看到一个项目,部署时需要手动复制文件到服务器,每次部署都要花上几个小时。我就想问:你是在做部署还是在做体力活? 反面教材 # 反面教材:手动部署 # 1. 构建项目 npm run build # 2. 压缩文件 zip -r build.zip build # 3. 上传到服务器 scp build.zip user@server:/var/www/html # 4. 登录服务器 ssh user@server # 5. 解压文件 unzip

【硬核排查】挂了代里还是“裸奔”?深度解析 WebRTC 泄露与 Google 账号风控机制

【硬核排查】挂了代里还是“裸奔”?深度解析 WebRTC 泄露与 Google 账号风控机制

本文仅用于技术研究,禁止用于非法用途。 Author:枷锁 前言:一个“玄学”的网络故障 最近在进行网络环境配置时遇到了一个非常反直觉的现象: 我在本地开启了 戴笠,状态栏显示连接正常,访问Gemini毫无压力。但是,当我打开 ip138 或百度搜索 “IP” 时,显示的却依然是我本地的 ISP 真实 IP。更糟糕的是,我的 Google 账号开始频繁触发安全风控——要么是登录时无限弹出验证码,要么是刚登上去就被踢下线。 这不仅仅是“连不上”的问题,而是一个典型的网络协议泄露与安全风控案例。本着“知其然更要知其所以然”的精神,我深扒了其背后的技术原理,发现罪魁祸首主要有两个:路由分流策略与WebRTC 协议漏洞。 第一部分:为什么 ip138 “出卖”了你?—— 聊聊路由分流 (Split Tunneling) 很多新手判断 是否生效的标准是: