Skip to content

03 · D1-P1:最小业务链路(10 台) ​

项目内容
所属阶段D1 下午
前置条件P0 完成:ansible ansible -m ping 三台通、DNS 可解析
预计耗时2–3 小时
完成标志Windows 浏览器打开 http://192.168.10.119 看到应用页面,页面上的访问计数每次刷新 +1
下一步04-D1-P15堡垒机.md

这一步建哪 10 台:

VMID主机名角色规格
110 / 111lvs-01 / lvs-02四层负载 + keepalived1C/1.5G/15G
120 / 121nginx-01 / nginx-02七层 + 蓝绿 upstream2C/2G/15G
130 / 131app-blue-01 / app-blue-02占位应用2C/3G/20G
150mysql-01主库2C/3G/30G
160redis-01缓存1C/1.5G/15G
190 / 191prom-01 / grafana-01监控2C/3G/25G、2C/2G/20G

本步需要的资料 ​

类型名称从哪来
仓库demo-app.py03-集群部署/
下载MySQL / Redis / Nginx / Python 包apt(已换中科大源)
账号MySQL edu / 【实验密码】本步创建
前置P0 的 Ansible 三节点02 的产物

总表(含版本、下载地址、账号规则)见 00-资料清单.md。


步骤 0 · 开始前检查与上传(5 分钟) ​

在哪台:PVE(root)+ Windows 目的:确认 P0 成果、把一个文件传到 bastion 开始前:P0 验收通过

操作:

  1. 在 PVE 上确认:
bash
# 在 PVE 上执行(root)
qm list | grep running | wc -l
pvesm status | grep nvme-lvm

应该看到:3(三台管理机);nvme-lvm active。

  1. 把应用代码传到 bastion(Windows → PVE → bastion):
powershell
# 在 Windows PowerShell 里执行
scp ".\ops-project-files\03-集群部署\demo-app.py" root@192.168.10.30:/root/
bash
# 在 PVE 上执行(root)
scp /root/demo-app.py ubuntu@10.0.0.100:/home/ubuntu/
  1. 在 bastion 上确认:
bash
# 在 bastion 上执行(ubuntu)
ls -l ~/demo-app.py && cd ~/ansible && ansible ansible -m ping | grep -c pong

应该看到:文件存在;3。

失败怎么办:

  • ansible: command not found → 回 P0 步骤 5
  • ping 少于 3 → 回 P0 步骤 5

步骤 1 · 建 10 台并初始化(20 分钟) ​

在哪台:PVE(root)建机 → bastion(ubuntu)初始化 目的:把 P1 需要的 10 台全部拉起来并统一配置 开始前:步骤 0 通过

操作:

  1. 建机(在 PVE 上):
bash
# 在 PVE 上执行(root)
bash /root/create-lab-vm.sh 110 1 1536 15
bash /root/create-lab-vm.sh 111 1 1536 15
bash /root/create-lab-vm.sh 120 2 2048 15
bash /root/create-lab-vm.sh 121 2 2048 15
bash /root/create-lab-vm.sh 130 2 3072 20
bash /root/create-lab-vm.sh 131 2 3072 20
bash /root/create-lab-vm.sh 150 2 3072 30
bash /root/create-lab-vm.sh 160 1 1536 15
bash /root/create-lab-vm.sh 190 2 3072 25
bash /root/create-lab-vm.sh 191 2 2048 20

应该看到:10 段 完成。首次开机 cloud-init...。

  1. 等 60 秒,确认:
bash
# 在 PVE 上执行(root)
qm list | grep -E "lab-(11[01]|12[01]|13[01]|15[0]|16[0]|19[01])" | wc -l

应该看到:10。

  1. 在 bastion 上批量初始化(一次搞定 10 台):
bash
# 在 bastion 上执行(ubuntu)
declare -A M=(
  [110]=lvs-01 [111]=lvs-02 [120]=nginx-01 [121]=nginx-02
  [130]=app-blue-01 [131]=app-blue-02 [150]=mysql-01 [160]=redis-01
  [190]=prom-01 [191]=grafana-01
)
for id in "${!M[@]}"; do
  h=${M[$id]}
  scp -q -o StrictHostKeyChecking=no ~/p0-node-init.sh ubuntu@10.0.0.$id:/tmp/
  ssh -o StrictHostKeyChecking=no ubuntu@10.0.0.$id "sudo bash /tmp/p0-node-init.sh $h"
done

应该看到:10 段 初始化完成:<主机名> 10.0.0.x。

  1. 用 Ansible 复核:
bash
# 在 bastion 上执行(ubuntu)
cd ~/ansible
ansible edge,app_blue,monitor,mysql-01,redis-01 -m ping | grep -c pong

应该看到:10。

失败怎么办:

  • 某台 ping 不通 → qm terminal <VMID> 看启动;多半是 cloud-init 还在跑,等 2 分钟
  • declare: -A: invalid option → 你不在 bash 里,bash 一下再试

步骤 2 · MySQL(20 分钟) ​

在哪台:mysql-01(10.0.0.150) 目的:建库建表,给应用提供数据 开始前:步骤 1 的 150 能 ping 通

操作:

  1. 从 bastion 登录:
bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.150
  1. 装 MySQL 并配置:
bash
# 在 mysql-01 上执行(ubuntu)
sudo apt install -y mysql-server

sudo tee /etc/mysql/mysql.conf.d/99-lab.cnf >/dev/null <<'EOF'
[mysqld]
bind-address = 0.0.0.0
server-id = 150
gtid_mode = ON
enforce_gtid_consistency = ON
log_bin = mysql-bin
binlog_format = ROW
EOF

sudo systemctl restart mysql
sudo systemctl status mysql --no-pager | head -3

应该看到:Active: active (running)。

  1. 建库、建用户、插数据:
bash
# 在 mysql-01 上执行(ubuntu)
sudo mysql <<'SQL'
CREATE DATABASE IF NOT EXISTS edu CHARACTER SET utf8mb4;
CREATE USER IF NOT EXISTS 'edu'@'10.0.0.%' IDENTIFIED BY '【实验密码】';
GRANT ALL PRIVILEGES ON edu.* TO 'edu'@'10.0.0.%';
FLUSH PRIVILEGES;

USE edu;
CREATE TABLE IF NOT EXISTS course (
  id    INT PRIMARY KEY AUTO_INCREMENT,
  name  VARCHAR(64) NOT NULL,
  price INT NOT NULL
);
INSERT INTO course(name, price) VALUES
  ('Linux 运维', 199), ('K8s 实战', 299), ('MySQL 调优', 259);
SQL
  1. 验证监听和权限:
bash
# 在 mysql-01 上执行(ubuntu)
ss -lntp | grep 3306
sudo mysql -e "SELECT COUNT(*) AS courses FROM edu.course;"

应该看到:0.0.0.0:3306;courses = 3。

失败怎么办:

  • ss 显示只有 127.0.0.1:3306 → 99-lab.cnf 没生效,sudo mysqld --verbose --help | grep bind 确认,或看 /etc/mysql/mysql.conf.d/ 是否被 include
  • 建用户报错 → 已有同名用户,先 DROP USER 'edu'@'10.0.0.%';

步骤 3 · Redis(10 分钟) ​

在哪台:redis-01(10.0.0.160) 目的:给应用做访问计数(验证缓存链路) 开始前:步骤 1 完成

操作:

  1. 登录并安装:
bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.160
bash
# 在 redis-01 上执行(ubuntu)
sudo apt install -y redis-server
sudo sed -i 's/^bind .*/bind 0.0.0.0/' /etc/redis/redis.conf
sudo sed -i 's/^protected-mode .*/protected-mode no/' /etc/redis/redis.conf
sudo systemctl restart redis-server
  1. 验证:
bash
# 在 redis-01 上执行(ubuntu)
redis-cli ping
ss -lntp | grep 6379

应该看到:PONG;0.0.0.0:6379。

失败怎么办:

  • Could not connect → sudo systemctl status redis-server --no-pager
  • 改完配置起不来 → sudo redis-server /etc/redis/redis.conf --test-memory 2 或看 journal

步骤 4 · 占位应用(20 分钟) ​

在哪台:app-blue-01(10.0.0.130)、app-blue-02(10.0.0.131) 目的:跑一个会读 MySQL、写 Redis 的页面,把链路串起来 开始前:步骤 2、3 完成

操作:

  1. 在 bastion 上,把代码推到两台 app:
bash
# 在 bastion 上执行(ubuntu)
for ip in 10.0.0.130 10.0.0.131; do
  scp -q ~/demo-app.py ubuntu@$ip:/tmp/
done
  1. 在 app-blue-01 上安装并启动(131 一模一样,只是主机不同):
bash
# 在 app-blue-01 上执行(ubuntu)—— 131 换成在 131 上重跑一遍
sudo apt install -y python3-venv python3-pip
sudo mkdir -p /opt/edu-app
sudo cp /tmp/demo-app.py /opt/edu-app/app.py

sudo python3 -m venv /opt/edu-app/venv
sudo /opt/edu-app/venv/bin/pip install -q \
  -i https://pypi.tuna.tsinghua.edu.cn/simple \
  flask pymysql redis

sudo tee /etc/systemd/system/edu-app.service >/dev/null <<'EOF'
[Unit]
Description=Edu demo app
After=network-online.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/opt/edu-app
Environment=APP_VERSION=v1
Environment=DB_HOST=10.0.0.150
Environment=DB_USER=edu
Environment=DB_PASS=【实验密码】
Environment=DB_NAME=edu
Environment=REDIS_HOST=10.0.0.160
ExecStart=/opt/edu-app/venv/bin/python /opt/edu-app/app.py
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOF

sudo chown -R www-data:www-data /opt/edu-app
sudo systemctl daemon-reload
sudo systemctl enable --now edu-app
sleep 2
curl -s localhost:8080/healthz

应该看到:ok。

  1. 在 bastion 上验证两台都能返回页面:
bash
# 在 bastion 上执行(ubuntu)
curl -s http://10.0.0.130:8080/ | grep -E "版本|MySQL|Redis"
curl -s http://10.0.0.131:8080/ | grep -E "版本|MySQL|Redis"

应该看到:两台都显示 v1、MySQL 课程数 3、Redis 访问计数 1/2。

失败怎么办:

  • healthz 不是 ok → sudo journalctl -u edu-app -n 30 --no-pager,多数是 DB/Redis 连不上
  • 页面显示 error: → 看错误里的 IP,回步骤 2/3 确认监听地址

步骤 5 · Nginx 七层(15 分钟) ​

在哪台:nginx-01(10.0.0.120)、nginx-02(10.0.0.121) 目的:把请求转发给 app,并预留蓝绿切换点 开始前:步骤 4 的两台 app 都能 curl 通

操作:

  1. 在 nginx-01 上装并配置(121 一样):
bash
# 在 nginx-01 上执行(ubuntu)—— 121 换成在 121 上重跑一遍
sudo apt install -y nginx

sudo tee /etc/nginx/conf.d/00-upstream.conf >/dev/null <<'EOF'
upstream app_pool {
    server 10.0.0.130:8080 max_fails=2 fail_timeout=5s;
    server 10.0.0.131:8080 max_fails=2 fail_timeout=5s;
}
EOF

sudo tee /etc/nginx/conf.d/10-server.conf >/dev/null <<'EOF'
server {
    listen 80 default_server;
    server_name _;

    access_log /var/log/nginx/edu-access.log;
    error_log  /var/log/nginx/edu-error.log;

    location = /healthz {
        access_log off;
        return 200 "nginx ok\n";
    }

    location / {
        proxy_pass http://app_pool;
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP       $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 2s;
        proxy_read_timeout    10s;
        proxy_next_upstream   error timeout http_502 http_503;
    }
}
EOF

sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
curl -s http://127.0.0.1/healthz

应该看到:nginx: configuration file ... test is successful;nginx ok。

  1. 在 bastion 上验证两台:
bash
# 在 bastion 上执行(ubuntu)
curl -s http://10.0.0.120/ | grep 应用节点
curl -s http://10.0.0.121/ | grep 应用节点

应该看到:返回的 应用节点 在 app-blue-01 / app-blue-02 之间随机。

失败怎么办:

  • unknown "app_pool" → 文件名前缀不对,确认是 00-upstream.conf(不是 upstream.conf)
  • 502 → curl 10.0.0.130:8080/healthz 通不通;看 sudo tail -20 /var/log/nginx/edu-error.log

步骤 6 · LVS + keepalived(20 分钟) ​

在哪台:nginx-01/02(RS)+ lvs-01/02(director) 目的:四层负载 + VIP 高可用 开始前:步骤 5 的两台 Nginx 都能返回页面

操作:

6.1 两台 Nginx 配 VIP 和 ARP 参数(RS 侧) ​

bash
# 在 bastion 上执行(ubuntu)
for ip in 10.0.0.120 10.0.0.121; do
  ssh -o StrictHostKeyChecking=no ubuntu@$ip 'bash -s' <<'EOS'
sudo tee /etc/sysctl.d/99-lvs-rs.conf >/dev/null <<'EOF'
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
EOF
sudo sysctl --system >/dev/null

sudo tee /etc/systemd/system/lvs-vip.service >/dev/null <<'EOF'
[Unit]
Description=Add LVS VIP to loopback
After=network-online.target

[Service]
Type=oneshot
ExecStart=-/sbin/ip addr add 10.0.0.119/32 dev lo
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now lvs-vip
ip -4 a show lo | grep 10.0.0.119
EOS
done

应该看到:两行 inet 10.0.0.119/32 scope host lo。

6.2 lvs-01 配 keepalived(MASTER) ​

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.110
bash
# 在 lvs-01 上执行(ubuntu)
sudo apt install -y keepalived ipvsadm
IFACE=$(ip -br link | awk '$1!="lo"{print $1; exit}')
echo "网卡是:$IFACE"

sudo tee /etc/keepalived/keepalived.conf >/dev/null <<EOF
global_defs { router_id LVS_01 }

vrrp_instance VI_1 {
    state MASTER
    interface $IFACE
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication { auth_type PASS; auth_pass lab2026 }
    virtual_ipaddress { 10.0.0.119/24 dev $IFACE }
}

virtual_server 10.0.0.119 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 10.0.0.120 80 {
        weight 1
        TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 }
    }
    real_server 10.0.0.121 80 {
        weight 1
        TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 }
    }
}
EOF

sudo systemctl enable --now keepalived
sleep 2
ip -4 a show $IFACE | grep 10.0.0.119
sudo ipvsadm -Ln

应该看到:VIP 在本机;ipvsadm 里两个 RS 都是 Route 1 0 0。

6.3 lvs-02 配 keepalived(BACKUP) ​

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.111
bash
# 在 lvs-02 上执行(ubuntu)
sudo apt install -y keepalived ipvsadm
IFACE=$(ip -br link | awk '$1!="lo"{print $1; exit}')
echo "网卡是:$IFACE"

sudo tee /etc/keepalived/keepalived.conf >/dev/null <<EOF
global_defs { router_id LVS_02 }

vrrp_instance VI_1 {
    state BACKUP
    interface $IFACE
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication { auth_type PASS; auth_pass lab2026 }
    virtual_ipaddress { 10.0.0.119/24 dev $IFACE }
}

virtual_server 10.0.0.119 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 10.0.0.120 80 {
        weight 1
        TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 }
    }
    real_server 10.0.0.121 80 {
        weight 1
        TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 }
    }
}
EOF

sudo systemctl enable --now keepalived
ip -4 a show $IFACE | grep 10.0.0.119 || echo "本机无 VIP(正常,备机)"

应该看到:本机无 VIP(正常,备机)。

失败怎么办:

  • VIP 两台都有(脑裂)→ journalctl -u keepalived -n 50;检查 VRRP 组播是否被防火墙拦
  • ipvsadm 里 RS 是 Route 0 → 后端 Nginx 没起或 80 不通
  • keepalived 起不来 → 配置里 $IFACE 是否为空

步骤 7 · 监控(20 分钟) ​

在哪台:bastion → 全部节点;prom-01;grafana-01 目的:能看到每台的 CPU/内存/磁盘 开始前:步骤 6 完成

操作:

  1. 所有节点装 node_exporter(bastion 上一条命令):
bash
# 在 bastion 上执行(ubuntu)
cd ~/ansible
ansible ansible,edge,app_blue,monitor,mysql-01,redis-01 \
  -m apt -a "name=prometheus-node-exporter state=present" --become

应该看到:全部 changed 或 ok,没有 FAILED。

  1. prom-01 装 Prometheus:
bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.190
bash
# 在 prom-01 上执行(ubuntu)
sudo apt install -y prometheus

sudo tee /etc/prometheus/prometheus.yml >/dev/null <<'EOF'
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: node
    static_configs:
      - targets:
          - 10.0.0.100:9100
          - 10.0.0.101:9100
          - 10.0.0.102:9100
          - 10.0.0.110:9100
          - 10.0.0.111:9100
          - 10.0.0.120:9100
          - 10.0.0.121:9100
          - 10.0.0.130:9100
          - 10.0.0.131:9100
          - 10.0.0.150:9100
          - 10.0.0.160:9100
          - 10.0.0.190:9100
          - 10.0.0.191:9100
EOF

sudo systemctl restart prometheus
sleep 5
curl -s localhost:9090/api/v1/targets | grep -o '"health":"up"' | wc -l

应该看到:13(13 个 target 全 up)。

  1. grafana-01 装 Grafana 并挂数据源:
bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.191
bash
# 在 grafana-01 上执行(ubuntu)
sudo apt install -y apt-transport-https software-properties-common wget gpg
sudo mkdir -p /etc/apt/keyrings
wget -q -O - https://apt.grafana.com/gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/grafana.gpg
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list >/dev/null
sudo apt update && sudo apt install -y grafana
sudo systemctl enable --now grafana-server

sudo mkdir -p /etc/grafana/provisioning/datasources
sudo tee /etc/grafana/provisioning/datasources/prom.yaml >/dev/null <<'EOF'
apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    url: http://10.0.0.190:9090
    isDefault: true
EOF
sudo systemctl restart grafana-server

失败怎么办:

  • target 少于 13 → curl localhost:9090/api/v1/targets 看哪个 down,多半是那台 node_exporter 没起
  • Grafana 装不上 → 换 https://mirrors.tuna.tsinghua.edu.cn/grafana/apt/ 源

步骤 8 · 端到端验收 ​

在哪台:bastion + Windows 目的:确认整条链路真的通了

操作:

  1. 在 bastion 上测 VIP:
bash
# 在 bastion 上执行(ubuntu)
curl -s http://10.0.0.119/ | grep -E "版本|应用节点|MySQL|Redis"

应该看到:v1、某个 app 主机名、3、一个数字。

  1. 从 Windows 浏览器打开:http://192.168.10.119

应该看到:同样的页面(经 EIP → VIP → Nginx → App → MySQL/Redis)。

  1. 刷新 3 次,观察:
  • Redis 访问计数 每次 +1
  • 应用节点 在 app-blue-01 / app-blue-02 之间轮换
  1. 打开 Grafana:http://192.168.10.191:3000,账号 admin / 【首次登录密码】(首次会要求改密)。

失败怎么办:

  • 浏览器打不开但 bastion 能 curl → Windows 的路由或 EIP 问题,回 D0 步骤 7/8
  • 页面 502 → 回步骤 5 的 Nginx 日志
  • 页面显示数据库 NO → 回步骤 2

步骤 9 · 故障演练(P1 验收,20 分钟) ​

在哪台:按每条的说明 目的:证明你搭的东西真的有高可用

演练 1:挂一台应用

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.130 "sudo systemctl stop edu-app"
curl -s http://10.0.0.119/ | grep 应用节点      # 应该只剩 app-blue-02
ssh ubuntu@10.0.0.130 "sudo systemctl start edu-app"

演练 2:挂一台 Nginx

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.121 "sudo systemctl stop nginx"
sleep 8
curl -s http://10.0.0.119/ | head -3            # 仍可用
ssh ubuntu@10.0.0.110 "sudo ipvsadm -Ln"        # 121 被剔除
ssh ubuntu@10.0.0.121 "sudo systemctl start nginx"

演练 3:挂主 LVS(VIP 漂移)

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.110 "sudo systemctl stop keepalived"
sleep 3
ssh ubuntu@10.0.0.111 "ip -4 a show | grep 10.0.0.119"   # VIP 已漂移
curl -s http://10.0.0.119/ | grep 版本                    # 仍可用
ssh ubuntu@10.0.0.110 "sudo systemctl start keepalived"

演练 4:挂数据库(预期失败)

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.150 "sudo systemctl stop mysql"
curl -s http://10.0.0.119/ | grep "数据库连通"   # 会显示 NO
ssh ubuntu@10.0.0.150 "sudo systemctl start mysql"

第 4 条失败是故意的:MySQL 现在是单点,P2 做一主两从后才能演"主库挂了还能读"。


P1 验收清单 ​

  • [ ] 10 台 VM 全部 running
  • [ ] ansible edge,app,data,monitor -m ping 10 台 pong
  • [ ] MySQL 0.0.0.0:3306,course 表 3 行
  • [ ] Redis 0.0.0.0:6379,PING 返回 PONG
  • [ ] 两台 app 的 /healthz 都是 ok
  • [ ] 两台 Nginx 的 /healthz 都是 nginx ok
  • [ ] ipvsadm -Ln 两个 RS 都是 Route 1
  • [ ] VIP 只在 lvs-01 上
  • [ ] Prometheus 13 个 target 全 up
  • [ ] Windows 打开 http://192.168.10.119 看到页面
  • [ ] 刷新计数递增、应用节点轮换
  • [ ] 四个演练都按预期(前三个通过、第四个失败)

全部打勾后 → 进入 04-D1-P15堡垒机.md。