配置灰度流量切分
灰度流量切分逐步将少量流量路由到新上游,同时将大部分流量保持在稳定的默认上游,从而安全地测试新上游。这种渐进方式将潜在问题限制在少量用户范围内,降低风险,让你能够发现并解决问题,而不影响更多用户。
灰度流量切分不同于灰度发布,因为 API/服务的版本保持不变。灰度发布是指同一 API/服务的两个版本同时运行并可被访问。
前提条件
按权重百分比切分流量
在此示例中,你将把 10% 的流量导向新上游,其余 90% 继续转发到默认上游。
测试新上游后,可以使用新值覆盖默认上游配置,并删除插件以结束灰度过程。
- 控制台
- ADC
- Ingress Controller
- 从侧边导航栏中选择网关组的已发布服务,然后点击要修改的服务,例如版本为
1.0.0的httpbin。 - 在已发布服务下,从侧边导航栏中选择上游。
- 在连接配置模块中点击编辑,选择使用节点主机,然后点击保存。
注意:由于
mock.api7.ai强制使用 HTTPS 访问,上游需要配置为使用端口443连接 HTTPS 端点。pass_host参数必须改为 nodes,以确保与上游成功握手。请根据实际使用场景进行相应调整。 - 点击新增上游。
- 在对话框中执行以下操作:
- 在上游名称字段中输入
new upstream。 - 点击新增节点,将节点的主机调整为新后端。例如,主机使用
mock.api7.ai,端口使用443。 - 点击新增。
- 在上游名称字段中输入
- 点击上游标题处(操作按钮下方)的查看 ID,复制以供后续使用。
- 在已发布服务下,从侧边导航栏中选择插件。
- 点击新增插件。
- 搜索
traffic-split插件,然后点击新增。 - 在对话框中执行以下操作:
-
在 JSON 编辑器中添加以下配置:
{"rules": [{"weighted_upstreams": [{"upstream_id": new_upstream_id, // Use upstream id, not upstream name"weight": 1},{"weight": 9}]}]} -
点击新增。
验证
发送 10 个请求以验证灰度规则:
for i in {1..10}; do "curl 127.0.0.1:9080/headers"; done
9 个请求将发送到默认上游地址 httpbin.org,你会收到以下响应:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.74.0",
"X-Amzn-Trace-Id": "Root=1-6650ab7e-32c90eba787abbeb4e3dbb0c",
"X-Forwarded-Host": "127.0.0.1"
}
}
1 个请求将发送到新上游地址 mock.api7.ai:
{
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, br",
"cf-connecting-ip": "159.89.160.194",
"cf-ipcountry": "IN",
"cf-ray": "888e28733f9604aa",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"content-type": "application/json",
"host": "mock.api7.ai",
"user-agent": "curl/7.74.0",
"x-application-owner": "API7.ai",
"x-forwarded-for": "127.0.0.1",
"x-forwarded-host": "127.0.0.1",
"x-forwarded-port": "9080",
"x-forwarded-proto": "https",
"x-real-ip": "159.89.160.194",
"X-Application-Owner": "API7.ai",
"Content-Type": "application/json"
}
}
以下是此场景的交互式演示。点击并按照演示中的步骤操作,可以更好地了解如何在 API7 企业版中使用此功能。
更新 ADC 配置文件(adc.yaml),将新上游包含在内。完整配置如下:
services:
- name: httpbin
upstream:
name: Test Group
scheme: https
nodes:
- host: httpbin.org
port: 443
weight: 100
plugins:
api7-traffic-split:
rules:
- canary_upstreams:
- upstream_name: newupstream
weight: 10
- weight: 90
upstreams:
- name: newupstream
nodes:
- host: mock.api7.ai
port: 443
weight: 100
scheme: https
routes:
- uris:
- /headers
name: getting-started-headers
methods:
- GET
将配置同步到 API7 企业版:
adc sync -f adc.yaml
验证
发送 10 个请求以验证灰度规则:
for i in {1..10}; do curl "127.0.0.1:9080/headers"; done
9 个请求将发送到 httpbin.org,你会收到以下响应:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.74.0",
"X-Amzn-Trace-Id": "Root=1-6650ab7e-32c90eba787abbeb4e3dbb0c",
"X-Forwarded-Host": "127.0.0.1"
}
}
1 个请求将发送到 mock.api7.ai:
{
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, br",
"cf-connecting-ip": "159.89.160.194",
"cf-ipcountry": "IN",
"cf-ray": "888e28733f9604aa",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"content-type": "application/json",
"host": "mock.api7.ai",
"user-agent": "curl/7.74.0",
"x-application-owner": "API7.ai",
"x-forwarded-for": "127.0.0.1",
"x-forwarded-host": "127.0.0.1",
"x-forwarded-port": "9080",
"x-forwarded-proto": "https",
"x-real-ip": "159.89.160.194",
"X-Application-Owner": "API7.ai",
"Content-Type": "application/json"
}
}
现在,将权重更新为 50:50,使一半流量路由到新上游:
services:
- name: httpbin
upstream:
name: Test Group
scheme: https
nodes:
- host: httpbin.org
port: 443
weight: 100
plugins:
api7-traffic-split:
rules:
- canary_upstreams:
- upstream_name: newupstream
weight: 50
- weight: 50
upstreams:
- name: newupstream
nodes:
- host: mock.api7.ai
port: 443
weight: 100
scheme: https
routes:
- uris:
- /headers
name: getting-started-headers
methods:
- GET
发送更多请求以测试新上游。最后,更新默认上游以完成灰度流量切分:
services:
- name: httpbin
upstream:
name: Test Group
scheme: https
nodes:
- host: mock.api7.ai
port: 443
weight: 100
routes:
- uris:
- /headers
name: getting-started-headers
methods:
- GET
api7-traffic-split 插件需要 ApisixService 自定义资源,而该资源目前不可用。
按条件切分流量:请求头
在此示例中,你将把带有 version = test 请求头的请求导向新上游,其余流量继续流向默认上游。灰度规则适用于服务中的所有路由,不能应用于单个路由。
- 控制台
- ADC
- Ingress Controller
- 从侧边导航栏中选择网关组的已发布服务,然后点击要修改的服务,例如版本为
1.0.0的httpbin。 - 在该服务下,从侧边导航栏中选择上游。
- 点击新增上游。
- 在对话框中执行以下操作:
- 在上游名称字段中输入
new upstream。 - 点击新增节点,将节点的主机调整为新后端。例如,主机使用
mock.api7.ai,端口使用443。 - 点击新增。
- 在上游名称字段中输入
- 点击上游标题处(操作按钮下方)的查看 ID,复制以供后续使用。
- 在已发布服务下,从侧边导航栏中选择插件。
- 点击新增插件。
- 搜索
traffic-split插件,然后点击新增。 - 在对话框中执行以下操作:
-
在 JSON 编辑器中添加以下配置:
{"rules": [{"match": [{"vars": [["version","==","test"]]}],"weighted_upstreams": [{"upstream_id": new_upstream_id, // Use upstream id, not upstream name"weight": 1},{"weight": 9}]}]} -
点击新增。
验证
发送请求以验证灰度规则:
-
发送带有
version:test请求头的请求:curl 127.0.0.1:9080/headers -H "version:test"你应收到来自新上游的以下响应:
{"headers": {"accept": "*/*","accept-encoding": "gzip, br","cf-connecting-ip": "159.89.160.194","cf-ipcountry": "IN","cf-ray": "888e28733f9604aa","cf-visitor": "{\"scheme\":\"https\"}","connection": "Keep-Alive","content-type": "application/json","host": "mock.api7.ai","user-agent": "curl/7.74.0","x-application-owner": "API7.ai","x-forwarded-for": "127.0.0.1","x-forwarded-host": "127.0.0.1","x-forwarded-port": "9080","x-forwarded-proto": "https","x-real-ip": "159.89.160.194","X-Application-Owner": "API7.ai","Content-Type": "application/json"}} -
发送带有错误请求头的请求:
curl 127.0.0.1:9080/headers -H "version:new"你应收到来自默认上游的以下响应:
{"headers": {"Accept": "*/*","Host": "httpbin.org","User-Agent": "curl/7.74.0","X-Amzn-Trace-Id": "Root=1-6650ab7e-32c90eba787abbeb4e3dbb0c","X-Forwarded-Host": "127.0.0.1"}} -
发送不带请求头的请求:
curl 127.0.0.1:9080/headers -
你应收到来自默认上游的以下响应:
{"headers": {"Accept": "*/*","Host": "httpbin.org","User-Agent": "curl/7.74.0","X-Amzn-Trace-Id": "Root=1-6650ab7e-32c90eba787abbeb4e3dbb0c","X-Forwarded-Host": "127.0.0.1"}}
- 继续发送请求以测试新上游,直到其符合预期。
更新 ADC 配置文件(adc.yaml),将新上游包含在内。完整配置如下:
services:
- name: httpbin
upstream:
name: default
scheme: https
nodes:
- host: httpbin.org
port: 443
weight: 100
plugins:
api7-traffic-split:
rules:
- canary_upstreams:
- upstream_name: newupstream
weight: 100
match:
- exprs:
- - http_version
- ==
- test
upstreams:
- name: newupstream
nodes:
- host: mock.api7.ai
port: 443
weight: 100
scheme: https
routes:
- uris:
- /headers
name: getting-started-headers
methods:
- GET
将配置同步到 API7 网关:
adc sync -f adc.yaml
发送带有 version:test 请求头的请求,以验证灰度规则:
curl 127.0.0.1:9080/headers
你将收到来自新上游的响应:
{
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, br",
"cf-connecting-ip": "159.89.160.194",
"cf-ipcountry": "IN",
"cf-ray": "888e28733f9604aa",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"content-type": "application/json",
"host": "mock.api7.ai",
"user-agent": "curl/7.74.0",
"x-application-owner": "API7.ai",
"x-forwarded-for": "127.0.0.1",
"x-forwarded-host": "127.0.0.1",
"x-forwarded-port": "9080",
"x-forwarded-proto": "https",
"x-real-ip": "159.89.160.194",
"X-Application-Owner": "API7.ai",
"Content-Type": "application/json"
}
}
发送带有错误请求头的请求:
curl 127.0.0.1:9080/headers -H "version:new"
你应收到来自默认上游的以下响应:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.74.0",
"X-Amzn-Trace-Id": "Root=1-6650ab7e-32c90eba787abbeb4e3dbb0c",
"X-Forwarded-Host": "127.0.0.1"
}
}
发送不带请求头的请求:
curl 127.0.0.1:9080/headers
你应收到来自默认上游的以下响应:
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.74.0",
"X-Amzn-Trace-Id": "Root=1-6650ab7e-32c90eba787abbeb4e3dbb0c",
"X-Forwarded-Host": "127.0.0.1"
}
}
使用新上游的值更新默认上游,以完成灰度流量切分:
services:
- name: httpbin
upstream:
name: Test Group
scheme: https
nodes:
- host: mock.api7.ai
port: 443
weight: 100
routes:
- uris:
- /headers
name: getting-started-headers
methods:
- GET
api7-traffic-split 插件需要 ApisixService 自定义资源,而该资源目前不可用。