实现蓝绿发布
蓝绿发布是一种发布策略,它维护两个完全相同的生产环境(Blue 和 Green)。任意时刻只有一个环境承载线上流量。当新版本准备就绪时,流量会切换到另一个环境;如果出现问题,也可以立即回滚。
API7 网关通过 traffic-split 插件实现蓝绿发布,该插件可以按权重在多个上游之间分配流量。
提示
蓝绿发布会一次性切换 100% 流量。若要逐步转移流量(例如将 10% 流量转到新版本),请参阅实施灰度发布。
前置条件
- API7 企业版实例正在运行。
- 已创建网关组,并且网关实例正在运行。
- 从控制台获取 Token。
工作流概览
第 1 步:准备上游
创建一个已发布服务,其默认上游代表 Blue 环境。切换流量时,Green 环境会以内联形式定义在 traffic-split 插件中。
- Admin API
- ADC
创建 Blue 服务和路由:
curl -k "https://localhost:7443/apisix/admin/services/blue-green-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "blue-green-service",
"upstream": {
"type": "roundrobin",
"scheme": "https",
"pass_host": "node",
"nodes": [
{
"host": "mock.api7.ai",
"port": 443,
"weight": 100
}
]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/blue-green-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "blue-green-route",
"methods": ["GET"],
"paths": ["/headers"],
"service_id": "blue-green-service"
}'
现在,默认服务上游代表 Blue 环境。
在 ADC 配置中定义 Blue 服务和路由:
adc.yaml
services:
- name: blue-green-service
upstream:
scheme: https
pass_host: node
nodes:
- host: mock.api7.ai
port: 443
weight: 100
routes:
- name: blue-green-route
uris:
- /headers
methods:
- GET
labels:
docs-example: blue-green-service
预览本示例管理的资源变更 ,确认没有非预期的更新或删除:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=blue-green-service
审查变更后同步配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=blue-green-service
第 2 步:将流量切换到 Green
应用 traffic-split 插件,将 100% 流量路由到 Green 上游。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/routes/blue-green-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "blue-green-route",
"methods": ["GET"],
"paths": ["/headers"],
"service_id": "blue-green-service",
"plugins": {
"traffic-split": {
"rules": [
{
"weighted_upstreams": [
{
"weight": 100,
"upstream": {
"type": "roundrobin",
"nodes": [
{
"host": "httpbin.org",
"port": 443,
"weight": 1
}
]
}
},
{
"weight": 0
}
]
}
]
}
}
}'
| 字段 |
|---|