部署策略
部署 safely 使用 rolling 更新, blue-green 部署, and canary 发布.
你将学到
- 常见部署策略 and 当到 use 每个
- Rolling 更新 for 零-downtime 部署
- Blue-green 部署 for 瞬时 rollbacks
- Canary 发布 for progressive rollouts
概念讲解
选择策略
How you deploy code to production is just as important as the code itself. A good deployment strategy minimizes downtime, reduces risk, and enables fast rollbacks. The simplest approach — stop the old version, start the new — causes downtime and risks exposing all users to bugs at once. Modern strategies like rolling updates, blue-green deployments, and canary releases address these problems in different ways.
Rolling Updates
A rolling update replaces instances of the old version gradually, one at a time. If you have five servers, the deploy updates one server to the new version, verifies it is healthy, then moves to the next. This achieves zero downtime because some servers always serve traffic. Kubernetes does rolling updates by default. The risk is that during the rollout, both old and new versions serve traffic simultaneously, so they must be compatible.
Blue-Green Deployments
In a blue-green deployment, you maintain two identical production environments: blue (current) and green (new). You deploy the new version to green, test it, then switch the router to send all traffic to green. If something goes wrong, you switch back to blue instantly — no redeployment needed. The tradeoff is that you need twice the infrastructure, and database migrations must be carefully designed to work with both versions.
Canary Releases
A canary release sends a small percentage of traffic to the new version — say 5% — while the rest goes to the old version. You monitor error rates and performance metrics. If everything looks good, you gradually increase the percentage: 5%, 25%, 50%, 100%. If problems appear, you stop the rollout. Canary releases limit the blast radius of bad deploys and are the gold standard for large-scale production systems.
Choosing the Right Strategy
For small applications with low traffic, a simple rolling update may suffice. For critical services with high traffic, canary releases provide the safest rollout. Blue-green is ideal when you need instant rollback capability and can afford double infrastructure. Feature flags offer another approach — deploy the new code but gate the feature behind a flag, enabling it for a subset of users. The right strategy depends on your traffic volume, infrastructure budget, and risk tolerance. Start simple and adopt more sophisticated strategies as your system grows.
示例代码
# Rolling update with Docker Swarm
docker service update --image myapp:v2 myapp_service
# Blue-green deployment script
docker run -d --name app_green -p 8081:80 myapp:v2
curl -f http://localhost:8081/health || exit 1
# Switch traffic: update nginx upstream to green
sudo nginx -s reload
# Stop the old blue version after verification
docker stop app_blue && docker rm app_blue
# Canary: route 10% of traffic to the new version
# (typically done with a load balancer or service mesh)
echo "Canary deploy: 10% traffic to v2"
Rolling 更新 replace 实例 one at 时间. Blue-green 运行 two 环境 and 切换 traffic. Canary 发送小 百分比的 traffic 到新 version 首先.
动手试一试
- Docker Compose Generator
Generate compose files for multi-环境部署.
- Nginx 配置 Generator
Generate nginx configs for traffic 切换.
- HTTP 状态 Codes
验证 health 检查响应 codes.
常见错误
错误写法
Deploying 不使用 health 检查
正确做法
总是验证新 version 是 healthy 之前 routing traffic 到它. Use /health 端点 and automated 检查. 不使用这个, 你 risk 发送 traffic 到 broken 部署.
错误写法
不有 rollback 计划
正确做法
之前 deploying, know 精确地如何 roll 后面. 使用 blue-green, 切换 router. 使用 versioned 镜像, 部署上一个标签. 练习 rollbacks 之前你 需要它们.
练习 部署策略
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。