完整实验命令解析:从集群搭建到负载均衡配置

  • 文档按 "集群初始化→网络配置→服务部署→负载均衡" 流程

一、集群基础配置(初始化节点)

复制代码
 #1. 配置 /etc/hosts(主机名映射)
 [root@centos7 ~ 10:50:43]# vim /etc/hosts
 [root@centos7 ~ 10:56:41]# cat /etc/hosts
 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 ​
 ########### cluster ################
 10.1.8.10   lb.lyk.cloud        lb
 10.1.8.11   web1.lyk.cloud  web1
 10.1.8.12   web2.lyk.cloud  web2
 10.1.8.13   web3.lyk.cloud  web3
 10.1.8.20   router.lyk.cloud    router
 10.1.8.21   client1.lyk.cloud   client1
 10.1.1.21   client2.lyk.cloud   client2
 ​
 ​
 #2. 上传并优化 sethost 脚本(批量配置节点)
 [root@centos7 bin 11:24:42]# cd /usr/local/bin/
 [root@centos7 bin 11:24:53]# rz -E
 rz waiting to receive.
 [root@centos7 bin 11:24:58]# mv sethost_for_exam_node.sh sethost
 [root@centos7 bin 11:25:09]# vim sethost 
 ​
 #3. sethost 脚本内容解析(批量配置的核心),脚本里的每个函数和命令都是为了 “自动给节点配 IP、改主机名”:
 [root@centos7 bin 11:29:46]# cat sethost 
 #!/bin/bash
 ​
 # 以 root 身份运行
 [ $UID -ne 0 ] && echo 'Please run as root.' && exit 1 
 ​
 # 指定接口名称DNS
 interface=ens33
 dns=223.5.5.5
 ​
 # 指定域名称
 domain=lyk.cloud
 ​
 # 脚本使用说明
 usage (){
   echo "Usage: $0 10-14 20 21"
   exit 1
 }
 ​
 # 设置 IP 地址
 function set_ip () {
   if [ $# -eq 0 ]; then
     usage   
   else
     nmcli connection modify ${interface} ipv4.method manual ipv4.addresses 10.1.8.$1/24 ipv4.gateway 10.1.8.2 ipv4.dns $dns 
     nmcli connection up ${interface} &>/dev/null
   fi
 }
 ​
 ​
 # 设置主机名
 function set_hostname () {
   # 获取主机名
   case $1 in
     10)
       HOSTNAME=lb.$domain
       ;;
     1[1-3])
       HOSTNAME=web$[ $1 - 10 ].$domain
       ;;
     20)
       HOSTNAME=router.$domain
       ;;
     21)
       HOSTNAME=client1.$domain
       ;;
     *)
       usage
       ;;
   esac
   # 设置主机名
   hostnamectl set-hostname $HOSTNAME
 }
 ​
 # 定义 main 函数调用功能函数
 function main() {
   set_ip $1
 ​
   set_hostname $1
 ​
   # 显示修改结果
   bash -c 'clear;hostname;echo;ip -br a;echo'
 ​
   # 关机打快照
   while true
   do
     echo -ne "Press the \033[1;31mEnter\033[0;39m key, and the system will shut down in 5 seconds.";read
     echo -e "Press \033[1;35mCTRL+C\033[0;39m to cancel the shutdown."
     for i in {5..1}
     do
       echo "The system will shut down in $i seconds."
       sleep 1
     done
     echo "Shutdown system Now." && init 0
   done 
   
 }
 ​
 # 执行 main 函数
 main $*
 ​
 [root@centos7 bin 11:29:57]# chmod +x sethost 
 ​
 #4. 用 sethost 批量初始化节点
 ​
 #进入虚拟机执行,sethost 10,看能不能创建地址  
 #init 0 重启 
 #保存快照
 #克隆,回车,回车 分别命名创建lb web1 web2 web3 client1 client2 router 虚拟机 
 ​

二、特殊节点网络配置(打通跨网段通信)

三台主机特殊配置

  • LB 和 router添加vm1(仅主机)

  • client2修改vim仅主机

特殊三台独立设置

复制代码
 #1. 给 lb 配置双网卡(连接两个网段)
 [root@lb ~ 11:53:21]# nmcli c
 NAME        UUID                                  TYPE      DEVICE 
 有线连接 1  83d688eb-f1f8-39fc-811f-f0ab0cec674e  ethernet  ens36  
 ens33       555eece5-af4c-45ae-bab9-c07e68d0e649  ethernet  ens33  
 ​
 [root@lb ~ 11:53:31]# nmcli connection modify 83d688eb-f1f8-39fc-811f-f0ab0cec674e connection.id ens36 ipv4.method manual ipv4.addresses 10.1.1.10/24
 ​
 [root@lb ~ 12:25:12]# nmcli connection up ens36
 连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/21)
 ​
 [root@lb ~ 12:25:27]# ip -br a
 lo               UNKNOWN        127.0.0.1/8 ::1/128 
 ens33            UP             10.1.8.10/24 fe80::20c:29ff:fe55:d621/64 
 ens36            UP             10.1.1.10/24 fe80::74d0:4a10:3b40:b67b/64 
 ​
 [root@lb ~ 12:25:36]# init 0
 ​
  • 核心目的:lb 需要同时连接两个网段(10.1.8.0/24 共享网、10.1.1.0/24 仅主机网),所以添加第二块网卡 ens36,配 10.1.1.10,实现跨网段访问。

sethost 批量初始化节点

复制代码
 #web虚拟机 执行
 web1 web2 web3分别进入虚拟机界面 分别执行sethost 11 sethost 12 sethost 13
 ​
 # 1. 给web1-3配置:分别进入3台web虚拟机,执行
 ​
 sethost 11  # web1→IP=10.1.8.11,主机名=web1.lyk.cloud
 sethost 12  # web2→IP=10.1.8.12,主机名=web2.lyk.cloud
 sethost 13  # web3→IP=10.1.8.13,主机名=web3.lyk.cloud
 # 3. 给client1配置:进入client1虚拟机,执行
 sethost 21  # client1→IP=10.1.8.21,主机名=client1.lyk.cloud
 ​
复制代码
 #2. 给 router 配置双网卡(路由节点核心)
 ​
 #router 虚拟机 执行
 nmcli connection modify   .....(ens36的UUID)  connection.id ens36 ipv4.method manual ipv4.addresses 10.1.1.20/24
 ​
 nmcli connection up ens36
 ​
 ip -br a
 ​
 init 0
 ​
 -- 作用:router 是两个网段的 “桥梁”,ens33 连 8 段(10.1.8.20),ens36 连 1 段(10.1.1.20),后续开启路由功能后,能让 8 段和 1 段互通。
复制代码
 #client1 虚拟机 执行
 sethost 21
 ​
 #3. 给 client2 配置 1 段 IP(仅主机网客户端)
 ​
 ​
 #client2 虚拟机 执行
 nmcli c
 nmcli connection modify ens33 ipv4.addresses 10.1.1.21/24
 hostnamectl set-hostname client2.lyk.cloud
 bash
 hostname
 ip -br a
 init 0
 ​

执行

复制代码
 #给除了router的虚拟机都打快照

复制代码
 #4. 配置 router 路由功能(打通跨网段)
 ​
 #Xshell执行
 [C:\~]$ ssh root@10.1.8.20
 #开启路由
 [root@router ~ 12:58:24]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
 [root@router ~ 12:59:13]# sysctl -p
 net.ipv4.ip_forward = 1
 ​
 #设置防火墙
 [root@router ~ 13:00:01]# firewall-cmd --add-masquerade --permanent 
 success
 ​
 [root@router ~ 12:59:15]# systemctl enable firewalld.service --now
 ​
 [root@router ~ 12:59:32]# firewall-cmd --set-default-zone=trusted 
 success
 ​
 ​
 [root@router ~ 13:01:17]# init 0
 ​
 #返回虚拟机 **保存router快照**
 ​
 关键作用:没有这步,1 段的 client2 无法访问 8 段的 web 节点;开启后,router 能转发两个网段的数据包,实现跨网段通信。
复制代码
 #5. 配置 client2 网关(指向 router)
 ​
 [C:\~]$ ssh root@10.1.1.21
 ​
 [root@client2 ~ 13:04:09]# ip r
 default via 10.1.8.2 dev ens33 proto static metric 100 
 10.1.1.0/24 dev ens33 proto kernel scope link src 10.1.1.21 metric 100 
 10.1.8.2 dev ens33 proto static scope link metric 100 
 [root@client2 ~ 13:04:31]# nmcli connection modify ens33 ipv4.gateway 10.1.1.20
 [root@client2 ~ 13:05:05]# nmcli connection up ens33 
 连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
 ​
 [root@client2 ~ 13:05:13]# ip r
 default via 10.1.1.20 dev ens33 proto static metric 100 
 10.1.1.0/24 dev ens33 proto kernel scope link src 10.1.1.21 metric 100
 ​
 [root@client2 ~ 13:05:34]# init 0
 ​
 #返回虚拟机 **保存client2快照**

三、部署 Web 服务(集群业务节点)

复制代码
 #1. 在 web1-3 上安装 Nginx 并配置首页
 ​
 [root@web1-3 ~]#
 ​
 # 部署 web
 yum install -y nginx
 echo Welcome to $(hostname) > /usr/share/nginx/html/index.html 
 systemctl enable nginx.service --now
 ​
 --作用:让 web1-3 成为 Web 服务器,各自的首页显示自己的主机名,后续测试负载均衡时能区分 “请求被分发到哪台 web
 ​
 #2. 测试 Web 服务是否正常
 [root@client1 ~ 15:23:23]# curl 10.1.8.11
 Welcome to web1.lyk.cloud
 [root@client1 ~ 15:24:49]# curl 10.1.8.12
 Welcome to web2.lyk.cloud
 [root@client1 ~ 15:24:51]# curl 10.1.8.13
 Welcome to web3.lyk.cloud
 ​
 ​
 ​
 #3. 给 web1-3 配置虚拟主机(多端口服务)
 [root@web1-3 ~]# cat > /etc/nginx/conf.d/vhost-test.conf <<'EOF'
 server {
     listen       81;
     root         /test;
 }
 EOF
 [root@web1-3 ~]# systemctl restart nginx
 ​
 # 准备测试文件
 [root@web1-3 ~]# mkdir /test
 [root@web1-3 ~]# echo hello txt from $(hostname -s) > /test/index.txt
 [root@web1-3 ~]# echo hello html from $(hostname -s) > /test/index.html
 ​
 #4. 测试虚拟主机是否正常
 [root@client1 ~ 15:24:54]# curl http://web1:81/
 hello html from web1
 [root@client1 ~ 15:27:29]# curl http://web2:81/
 hello html from web2
 [root@client1 ~ 15:27:34]# curl http://web3:81/
 hello html from web3
 ​

四、配置 HAProxy 负载均衡(TCP+HTTP 模式)

复制代码
 #1. 安装并备份 HAProxy 配置
 [root@lb ~ 15:32:55]# yum install -y haproxy
 ​
 [root@lb ~ 15:33:43]# cp /etc/haproxy/haproxy.cfg{,.ori}
 ​
 --作用:HAProxy 是专业的负载均衡工具,能把客户端请求分发到多台后端服务器,减轻单台压力。
 ​
 ​
 #2. 配置 HAProxy 基础 HTTP 负载均衡(80 端口)
 [root@lb ~ 15:36:27]# vim /etc/haproxy/haproxy.cfg
 #在最后添加代码
 frontend front_web
     bind *:80
     use_backend  back_web
 ​
 backend back_web
     balance     roundrobin
     server web1 10.1.8.11:80 check
     server web2 10.1.8.12:80 check
     server web3 10.1.8.13:80 check
 ​
 ​
 [root@lb ~ 15:36:27]# systemctl start haproxy
 ​
 --核心目的:客户端访问 lb 的 80 端口时,HAProxy 会按 “轮询” 规则把请求分给 web1-3 的 80 端口,实现 HTTP 请求的负载均衡。
 ​
 #3. 测试 HTTP 负载均衡(client1-2 验证)
 ​
 #client1测试  (10.1.8.21)
 [root@client1 ~ 15:27:37]# curl http://lb.lyk.cloud/
 Welcome to web1.lyk.cloud
 [root@client1 ~ 15:37:56]# curl http://lb.lyk.cloud/
 Welcome to web2.lyk.cloud
 [root@client1 ~ 15:38:04]# curl http://lb.lyk.cloud/
 Welcome to web3.lyk.cloud
 ​
 #client2测试  (10.1.1.21)
 [root@client2 ~ 15:38:58]# curl http://lb.lyk.cloud/
 Welcome to web1.lyk.cloud
 [root@client2 ~ 15:38:59]# curl http://lb.lyk.cloud/
 Welcome to web2.lyk.cloud
 [root@client2 ~ 15:39:03]# curl http://lb.lyk.cloud/
 Welcome to web3.lyk.cloud
 ​
 --作用:验证负载均衡是否生效 —— 单次访问能轮询到不同 web 节点,批量访问时每台节点的请求数基本相等(30 次),说明 “轮询算法” 正常。

for循环测试

复制代码
[root@client2 ~ 15:39:03]# for i in {1..90}
> do
> curl http://lb.lyk.cloud/
> done

Welcome to web1.lyk.cloud
Welcome to web2.lyk.cloud
Welcome to web3.lyk.cloud
Welcome to web1.lyk.cloud
Welcome to web2.lyk.cloud
Welcome to web3.lyk.cloud
Welcome to web1.lyk.cloud
Welcome to web2.lyk.cloud
Welcome to web3.lyk.cloud
......

#sort输出结果进行排序
[root@client2 ~ 15:42:35]# for i in {1..90}; do curl http://lb.lyk.cloud/; done | sort
Welcome to web1.lyk.cloud
....       web1...
Welcome to web2.lyk.cloud
....       web2...
Welcome to web3.lyk.cloud
....       web3...


#-s 是 curl 的选项,表示 “静默模式”(不显示进度条等冗余信息,只输出响应内容)
[root@client2 ~ 15:42:35]# for i in {1..90}; do curl -s http://lb.lyk.cloud/; done | sort

#uniq -c 统计每种相同内容的出现次数,并在内容前显示计数
[root@client2 ~ 15:44:16]# for i in {1..90}; do curl -s http://lb.lyk.cloud/; done | sort | uniq -c
     30 Welcome to web1.lyk.cloud
     30 Welcome to web2.lyk.cloud
     30 Welcome to web3.lyk.cloud
     
 --关键:
--  uniq -c 必须配合 sort 使用才有效(因为它只统计相邻的重复行),通过 sort 先将相同内容排在一起,才能准确统计总次数。
复制代码
[root@lb ~ 15:36:27]# systemctl start haproxy

#4. 配置 HAProxy URL 路由(按文件类型转发)
[root@lb ~ 15:37:33]# vim /etc/haproxy/haproxy.cfg    # 重新编辑配置
#把刚添加的删除

#echo添加
[root@lb ~ 15:59:15]# echo '
> ########### web 代理 ###########
> frontend front_web
>     bind *:80
>     default_backend  back_web
>     acl test url_reg -i \.txt$
>     use_backend test if test
> 
> backend back_web
>     balance     roundrobin
>     server web1 10.1.8.11:80 check
>     server web2 10.1.8.12:80 check
>     server web3 10.1.8.13:80 check
> 
> backend test								# 处理.txt请求,转发到web的81端口
>     balance    roundrobin
>     server test1 10.1.8.11:81 check
>     server test2 10.1.8.12:81 check
>     server test3 10.1.8.13:81 check
> ' >> /etc/haproxy/haproxy.cfg

[root@lb ~ 15:59:49]# systemctl enable haproxy.service --now

测试

复制代码
[root@client1 ~ 15:38:05]# for n in {1..90}; do curl http://10.1.8.10 -s; done | sort |uniq -c
     30 Welcome to web1.lyk.cloud
     30 Welcome to web2.lyk.cloud
     30 Welcome to web3.lyk.cloud

[root@client2 ~ 15:58:25]# for n in {1..90}; do curl http://10.1.1.10 -s; done | sort |uniq -c
     30 Welcome to web1.lyk.cloud
     30 Welcome to web2.lyk.cloud
     30 Welcome to web3.lyk.cloud

[root@client1 ~ 16:00:12]# for n in {1..90}; do curl http://10.1.8.10/index.txt -s; done | sort |uniq -c
......

tcp 模式

复制代码
#5. 配置 HAProxy TCP 模式(SSH 负载均衡)
#之前环境没变
[root@lb ~ 16:12:58]# echo '
> ########### ssh 代理 ###########
> listen ssh
>   mode tcp
>   bind *:1022
>   balance  roundrobin
>   server web1 10.1.8.11:22   check
>   server web2 10.1.8.12:22   check
>   server web3 10.1.8.13:22   check
> ' >> /etc/haproxy/haproxy.cfg

[root@lb ~ 16:15:14]# systemctl restart haproxy.service

--作用:客户端通过ssh root@lb -p 1022登录时,HAProxy 会轮询把 SSH 连接转发到 web1-3 的 22 端口,不用记多台 web 的 IP,简化 SSH 登录


[root@client1 ~ 16:16:41]# for i in {1..90}; do curl -s http://lb.lyk.cloud/index.txt; done | sort | uniq -c
     30 hello txt from web1
     30 hello txt from web2
     30 hello txt from web3

[root@client2 ~ 16:00:21]# for i in {1..90}; do curl -s http://lb.lyk.cloud/index.txt; done | sort | uniq -c
     30 hello txt from web1
     30 hello txt from web2
     30 hello txt from web3

#6. 测试 SSH 负载均衡,每次轮流显示
[root@client2 ~ 16:21:24]# ssh root@lb -p 1022 hostname
web1.lyk.cloud
[root@client2 ~ 16:21:37]# ssh root@lb -p 1022 hostname
web2.lyk.cloud
[root@client2 ~ 16:21:38]# ssh root@lb -p 1022 hostname
web3.lyk.cloud


#每次轮流登录web1 web2 web3
[root@client2 ~ 16:18:40]# ssh root@lb -p 1022
Warning: Permanently added '[lb]:1022,[10.1.8.10]:1022' (ECDSA) to the list of known hosts.
Last login: Fri Aug 22 15:09:29 2025 from 10.1.8.1
[root@web1 ~ 16:19:03]# ssh root@lb -p 1022
Warning: Permanently added '[lb]:1022,[10.1.8.10]:1022' (ECDSA) to the list of known hosts.
Last login: Fri Aug 22 15:13:13 2025 from 10.1.8.1
[root@web2 ~ 16:19:24]# ssh root@lb -p 1022
Warning: Permanently added '[lb]:1022,[10.1.8.10]:1022' (ECDSA) to the list of known hosts.
Last login: Fri Aug 22 15:13:22 2025 from 10.1.8.1
[root@web3 ~ 16:19:42]# 

五、用 Nginx 实现负载均衡(替代 HAProxy)

复制代码
#1. 切换到 Nginx 负载均衡(停止 HAProxy)
[root@lb ~ 16:45:50]# systemctl disable haproxy.service --now
[root@lb ~ 16:45:50]# yum install -y nginx

--作用:除了 HAProxy,Nginx 也能实现负载均衡,这里测试用 Nginx 替代 HAProxy 的 HTTP 负载功能


#2. 配置 Nginx 负载均衡(upstream 模块)
[root@lb ~ 16:45:50]# vim /etc/nginx/conf.d/proxy.conf
[root@lb ~ 16:52:19]# cat /etc/nginx/conf.d/proxy.conf
upstream web {
    server 10.1.8.11:80;
    server 10.1.8.12:80;
    server 10.1.8.13:80;
}
server {
    listen       80;
    server_name  www.lyk.cloud;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {
       # 添加如下语句
       proxy_pass http://web;
    }
}

[root@lb ~ 16:52:26]# systemctl restart nginx.service 

测试

复制代码
#3. 测试 Nginx 负载均衡(client1-2 验证)

[root@client1 ~ 16:53:21]# curl http://www.lyk.cloud
Welcome to web1.lyk.cloud
[root@client1 ~ 16:54:06]# curl http://www.lyk.cloud
Welcome to web2.lyk.cloud
[root@client1 ~ 16:54:08]# curl http://www.lyk.cloud
Welcome to web3.lyk.cloud

[root@client1 ~ 16:54:09]# for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c
     30 Welcome to web1.lyk.cloud
     30 Welcome to web2.lyk.cloud
     30 Welcome to web3.lyk.cloud



[root@client2 ~ 16:22:39]# echo "10.1.8.10 www.lyk.cloud www" >> /etc/hosts
[root@client2 ~ 16:54:33]# curl http://www.lyk.cloud
Welcome to web1.lyk.cloud
[root@client2 ~ 16:54:43]# curl http://www.lyk.cloud
Welcome to web2.lyk.cloud
[root@client2 ~ 16:54:44]# curl http://www.lyk.cloud
Welcome to web3.lyk.cloud


[root@client2 ~ 16:54:45]# for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c
     30 Welcome to web1.lyk.cloud
     30 Welcome to web2.lyk.cloud
     30 Welcome to web3.lyk.cloud

总结:整个实验的核心逻辑

  1. 搭集群 :用sethost脚本批量初始化节点,配置主机名和 IP,统一集群标识。

  2. 通网络:给 lb 和 router 配双网卡,开启 router 的路由功能,让 8 段和 1 段能跨网段通信。

  3. 布服务:在 web1-3 上部署 Nginx,提供 Web 服务(80 端口首页 + 81 端口测试文件)。

  4. 做负载:先用 HAProxy 实现 "HTTP 负载(80 端口)+URL 路由(.txt 转 81 端口)+SSH 负载(1022 端口)",再用 Nginx 实现 HTTP 负载,验证不同工具的负载均衡能力。

相关推荐
一乐小哥1 小时前
五分钟就能搭好的socks5为啥我装了一个小时😭 进来看小丑
linux·后端
qq_364371721 小时前
Docker 常见命令
运维·docker·容器
Insist7532 小时前
K8s--调度管理:node节点、Pod亲和性、污点与容忍
linux·容器·kubernetes
VVVVWeiYee4 小时前
TCP/UDP详解(一)
运维·网络·tcp/ip·udp·信息与通信
Xの哲學4 小时前
Linux PCI 子系统:工作原理与实现机制深度分析
linux·网络·算法·架构·边缘计算
谢尔登4 小时前
【计算机网络】 IPV4和IPV6区别
运维·服务器·计算机网络
@Demi4 小时前
vsCode或Cursor 使用remote-ssh插件链接远程终端
服务器·ide·vscode·ssh
he_xiao1235 小时前
centos配置ip地址不生效
linux
努力努力再努力wz5 小时前
【c++进阶系列】:万字详解多态
java·linux·运维·开发语言·c++
杭州泽沃电子科技有限公司5 小时前
工业环境电缆火灾预防的分布式光纤在线监测
运维·人工智能·科技·安全