Skip to content

07 · P2-3:蓝绿发布(app-green + Nginx 切流) ​

项目内容
所属阶段P2(第二天)
前置条件P2-1、P2-2 完成;四台 app 的宿主机还有 ≥60G 空闲
预计耗时60 分钟
完成标志页面版本从 v1 切到 v2、节点从 blue 变 green;回滚一次;Redis 切主后页面不报错
下一步P3(GitLab / Jenkins / Harbor)

这一步建 2 台:

VMID主机名角色规格
132 / 133app-green-01/02新版本(v2,带哨兵)2C/3G/20G

蓝绿的本质:两套环境都在跑,Nginx 的 upstream 指向谁,流量就去谁。切换 = 改一个文件 + reload,秒级;回滚 = 改回去。


本步需要的资料 ​

类型名称从哪来
仓库demo-app.py(新版,含 REDIS_SENTINELS)03-集群部署/
前置4 台 app + Redis 哨兵03 / 05 的产物

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


步骤 0 · 准备新代码(5 分钟) ​

在哪台:Windows → PVE → bastion 目的:demo-app.py 已升级为"支持哨兵",需要重新传到 bastion 开始前:P2-2 验收通过

操作:

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/
bash
# 在 bastion 上执行(ubuntu)—— 确认新代码里有 REDIS_SENTINELS
grep -c REDIS_SENTINELS ~/demo-app.py

应该看到:2 或更多(不为 0)。

失败怎么办:

  • 0 → 传的是旧文件,重传

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

在哪台:PVE(root)建机 → bastion(ubuntu)初始化 目的:拉起 v2 的两台 开始前:步骤 0 完成

操作:

bash
# 在 PVE 上执行(root)
bash /root/create-lab-vm.sh 132 2 3072 20
bash /root/create-lab-vm.sh 133 2 3072 20
bash
# 在 bastion 上执行(ubuntu)
for pair in "10.0.0.132 app-green-01" "10.0.0.133 app-green-02"; do
  set -- $pair
  scp -q -o StrictHostKeyChecking=no ~/p0-node-init.sh ubuntu@$1:/tmp/
  ssh -o StrictHostKeyChecking=no ubuntu@$1 "sudo bash /tmp/p0-node-init.sh $2"
done

应该看到:两段 初始化完成:app-green-0x 10.0.0.13x。

失败怎么办:

  • 建机 no space → thin 池不够,先删掉不用的 VM

步骤 2 · 在 green 上部署 v2(20 分钟) ​

在哪台:app-green-01(132)、app-green-02(133) 目的:起 v2 应用,连哨兵而不是直连 redis-01 开始前:步骤 1 的两台能 ping 通

操作:

bash
# 在 bastion 上执行(ubuntu)
for ip in 10.0.0.132 10.0.0.133; do
  scp -q ~/demo-app.py ubuntu@$ip:/tmp/
done
bash
# 在 bastion 上执行(ubuntu)—— 两台执行同样的安装
for ip in 10.0.0.132 10.0.0.133; do
ssh -o StrictHostKeyChecking=no ubuntu@$ip "bash -s" <<'EOS'
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=v2
Environment=DB_HOST=10.0.0.150
Environment=DB_USER=edu
Environment=DB_PASS=【实验密码】
Environment=DB_NAME=edu
Environment=REDIS_SENTINELS=10.0.0.160:26379,10.0.0.161:26379,10.0.0.162:26379
Environment=REDIS_MASTER_NAME=mymaster
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
EOS
done

应该看到:两次 ok。

验证 v2 自己能读到数据:

bash
# 在 bastion 上执行(ubuntu)
curl -s http://10.0.0.132:8080/ | grep -E "版本|Redis 访问计数|数据库连通"

应该看到:v2、计数是个数字、YES。

失败怎么办:

  • 计数是 error: → 哨兵没起来,回 05 步骤 8
  • healthz 不是 ok → sudo journalctl -u edu-app -n 30 --no-pager

步骤 3 · 切流到 green(5 分钟) ​

在哪台:nginx-01(120)、nginx-02(121) 目的:把 upstream 从 blue 改成 green 开始前:步骤 2 的 132/133 都能返回 v2

操作:

bash
# 在 bastion 上执行(ubuntu)—— 两台 Nginx 都改
for ip in 10.0.0.120 10.0.0.121; do
ssh -o StrictHostKeyChecking=no ubuntu@$ip "bash -s" <<'EOS'
sudo cp /etc/nginx/conf.d/00-upstream.conf /etc/nginx/conf.d/00-upstream.conf.blue
sudo tee /etc/nginx/conf.d/00-upstream.conf >/dev/null <<'EOF'
upstream app_pool {
    server 10.0.0.132:8080 max_fails=2 fail_timeout=5s;
    server 10.0.0.133:8080 max_fails=2 fail_timeout=5s;
}
EOF
sudo nginx -t && sudo systemctl reload nginx
EOS
done

应该看到:两行 nginx: configuration file ... test is successful。

失败怎么办:

  • nginx -t 失败 → sudo cat /etc/nginx/conf.d/00-upstream.conf 看是否被写坏;用 .blue 备份还原

步骤 4 · 验证切换(5 分钟) ​

在哪台:bastion + Windows 目的:确认全链路已经是 v2 开始前:步骤 3 完成

操作:

bash
# 在 bastion 上执行(ubuntu)
for i in 1 2 3 4; do curl -s http://10.0.0.119/ | grep -E "版本|应用节点" | tr -d '\n'; echo; done

应该看到:四次都是 v2,节点在 app-green-01 / app-green-02 之间轮换。

浏览器打开 http://192.168.10.119,确认页面显示 v2。

失败怎么办:

  • 还是 v1 → Nginx 没 reload,或改的是另一台;sudo nginx -T | grep -A3 "upstream app_pool" 看实际生效配置

步骤 5 · 回滚演练(5 分钟) ​

在哪台:nginx-01/02 目的:证明回滚是秒级的 开始前:步骤 4 是 v2

操作:

bash
# 在 bastion 上执行(ubuntu)—— 用备份还原
for ip in 10.0.0.120 10.0.0.121; do
ssh -o StrictHostKeyChecking=no ubuntu@$ip \
  "sudo cp /etc/nginx/conf.d/00-upstream.conf.blue /etc/nginx/conf.d/00-upstream.conf && sudo nginx -t && sudo systemctl reload nginx"
done

curl -s http://10.0.0.119/ | grep 版本

应该看到:v1——回滚完成,全程没停服务。

再切回 v2(后面还要用):

bash
# 在 bastion 上执行(ubuntu)
for ip in 10.0.0.120 10.0.0.121; do
ssh ubuntu@$ip "sudo tee /etc/nginx/conf.d/00-upstream.conf >/dev/null <<'EOF'
upstream app_pool {
    server 10.0.0.132:8080 max_fails=2 fail_timeout=5s;
    server 10.0.0.133:8080 max_fails=2 fail_timeout=5s;
}
EOF
sudo nginx -t && sudo systemctl reload nginx"
done
curl -s http://10.0.0.119/ | grep 版本

应该看到:v2。


步骤 6 · 金丝雀 10%(10 分钟,可选) ​

在哪台:nginx-01/02 目的:新版本先接 10% 流量 开始前:步骤 5 已切回 v2

操作:

bash
# 在 bastion 上执行(ubuntu)
for ip in 10.0.0.120 10.0.0.121; do
ssh ubuntu@$ip "sudo tee /etc/nginx/conf.d/00-upstream.conf >/dev/null <<'EOF'
upstream app_pool {
    server 10.0.0.130:8080 weight=9;
    server 10.0.0.131:8080 weight=9;
    server 10.0.0.132:8080 weight=1;
    server 10.0.0.133:8080 weight=1;
}
EOF
sudo nginx -t && sudo systemctl reload nginx"
done

打 20 次,数版本:

bash
# 在 bastion 上执行(ubuntu)
for i in $(seq 20); do curl -s http://10.0.0.119/ | grep -o 'v[12]' | head -1; done | sort | uniq -c

应该看到:v1 大约 18 次、v2 大约 2 次(10%)。

失败怎么办:

  • 全是一边 → weight 写错,sudo nginx -T | grep weight

步骤 7 · 哨兵演练(应用不再中断)(5 分钟) ​

在哪台:bastion 目的:证明 v2 能跟着哨兵走 开始前:步骤 5 已全量 v2

操作:

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.160 "redis-cli -p 26379 sentinel failover mymaster"
sleep 8
curl -s http://10.0.0.119/ | grep -E "版本|Redis 访问计数|数据库连通"

应该看到:v2、计数是数字(不再是 error)、YES。

对比 05 步骤 9:v1 直连 redis-01,切主就报错;v2 走哨兵,切主后自动找到新主。


P2-3 验收清单 ​

  • [ ] 132/133 两台 running,/healthz 返回 ok
  • [ ] curl 10.0.0.119 显示 v2
  • [ ] 回滚到 v1 成功,再切回 v2 成功
  • [ ] 金丝雀 20 次里能看到约 10% 的 v1(如果做了步骤 6)
  • [ ] Redis 切主后页面仍然正常
  • [ ] ls /etc/nginx/conf.d/00-upstream.conf.blue 备份文件还在

P2 全部完成 ​

现在你的环境长这样:

            ┌── MySQL 一主两从(GTID + 半同步)
业务入口 ───┤
            ├── Redis 三节点 + 三哨兵
            └── app-blue(v1) / app-green(v2) ← Nginx upstream 切换

存储:MinIO 3 节点 6 盘(EC 4+2)
搜索:ES 3 节点(green)
监控:Prometheus 14 target + Grafana + Kibana

下一步 P3:GitLab → Jenkins(1 master + 2 agent)→ Harbor 双节点 → Ansible 部署串联,把"手工发布"变成"流水线发布"。