灰度流量
灰度流量切分允许逐步将少量流量转发给新上游,同时将大部分流量留在稳定的基准上游。这样可将潜在问题限制在少数用户中,在影响更多用户之前发现并解决问题。
灰度流量切分不同于灰度发布,因为 API/服务的版本保持不变。灰度发布指的是同一个 API/服务同时有两个版本都在运行并可被调用。
前提条件
按百分比切分流量
在本例中,先将 10% 的流量转发到灰度上游,其余 90% 继续转发到基准上游。随后逐步将灰度上游的流量比例提高到 50% 和 100%。
灰度规则适用于服务中的所有路由,无法单独应用到一条路由。
灰度上游测试通过后,可以把所有流量转发给它,使其成为新的基准上游,然后删除旧的基准上游。
- 控制台
- ADC
- Ingress Controller
- 从侧边栏选择网关组的 已发布服务,然后单击要灰度的服务,例如,版本为
1.0.0的httpbin。 - 在服务下,从侧边栏选择 上游。
- 在 连接配置 模块,点击 编辑,选择 使用节点 Host,然后点击 保存。
mock.api7.ai强制使用 HTTPS。上游需要使用端口443,并将pass_host设为node,以便成功完成与上游的 TLS 握手。请根据实际情况调整。 - 在 灰度规则 区域,点击 开始灰度。
- 在表单中执行以下操作:
- 关闭 条件 开关。
- 权重 填写
10。 - 点击 下一步。
- 灰度上游 选择
创建一个新的上游。
- 将新上游的名称修改为
newupstream。 - 点击 编辑,将节点主机指向新的后端,例如将主机设为
mock.api7.ai,端口设为443。 - 保持其他属性与基准上游相同。
-
确认显示的信息并点击 开始。灰度规则将立即生效。
-
通过发送 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%。
- 点击 保存。
- 发送更多 API 请求,测试灰度上游,直到它达到你的业务预期。
- 在 灰度规则 区域,点击 操作,然后点击 完成。
- 在表单中执行以下操作:
- 基准上游 选择
灰度上游:newupstream。 - 打开 删除未选择的上游 开关。
- 点击 完成。
以下交互演示展示了如何在 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
你也可以将旧的上游保留为权重为 0 的灰度上游,以便在新上游出现问题时回滚。
api7-traffic-split 插件需要当前尚不可用的 ApisixService 自定义资源。
按请求头转移流量
在本例中,你将把带有请求头 version = test 的请求引导到灰度上游,而其余流量将继续流向基准上游。灰度规则适用于服务中的所有路由,不能应用于单个路由。
- 控制台
- ADC
- Ingress Controller
- 从侧边栏选择网关组的 已发布服务,然后点击要灰度的服务,例如,版本为
1.0.0的httpbin。 - 在服务下,从侧边栏选择 上游。
- 在 灰度规则 区域中,点击 开始灰度。
- 在表单中执行以下操作:
- 打开 条件 开关。
- 填写 header 条件为
headerversion == test。 - 权重 填写
100。 - 点击 下一步。
- 选择或创建灰度上游 选择
创建一个新的上游。
- 将新上游的名称修改为
newupstream。 - 点击 编辑,将节点主机指向新的后端,例如将主机设为
mock.api7.ai,端口设为443。然后点击 保存。 - 保持其他属性与基准上游相同。
- 确认显示的信息并点击 开始。灰度规则将立即生效。
- 通过发送多个请求来验证灰度规则:
发送一个带有正确请求头的请求:
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"
}
}
- 发送更多请求以测试灰度上游,直到其符合你的期望。
- 在 灰度规则区域,点击 完成。
- 在表单中执行以下操作:
- 基准上游 选择
灰度上游: newupstream。 - 保持 删除未选中的上游 开启。
- 点击 完成。
以下交互演示展示了如何在 API7 企业版中完成此操作。
更新你的 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
发送一个带有正确请求头的请求:
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
你也可以将旧的上游保留为权重为 0 的灰度上游,以便在新上游出现问题时回滚。
api7-traffic-split 插件需要当前尚不可用的 ApisixService 自定义资源。