配置灰度流量切分
灰度流量切分通过逐步将一小部分流量路由到新上游,并将大部分流量保留在稳定的默认上游,实现对新上游的安全测试。渐进式方式将潜在问题限制在少量用户范围内,从而降低风险,让你能够识别和解决问题,而不影响更广泛的用户群体。
灰度流量切分与灰度发布不同,API 或服务版本不会改变。灰度发布指同一 API 或服务的两个版本同时运行并提供服务。
前提条件
- 安装 API7 企业版。
- 创建一条路径为
/headers的路由。
按权重切分流量
本示例将 10% 的流量路由到新上游,其余 90% 继续转发到默认上游。
测试新上游后,可以考虑使用新值更新默认上游配置,并删除插件,以结束灰度过程。
- 控制台
- ADC
- Ingress Controller
- 从侧边栏选择网关组的已发布服务,然后点击要修改的服务,例如版本为
1.0.0的httpbin。 - 在已发布服务下,从侧边栏选择上游。
- 在连接配置模块中点击编辑,选择
使用节点主机,然后点击保存。注意:
mock.api7.ai强制使用 HTTPS,因此上游需要为 HTTPS 端点配置端口443。必须将pass_host参数更改为 nodes,以确保与上游成功握手。请根据你的使用场景相应调整。 - 点击新增上游。
- 在对话框中,执行以下操作:
- 在上游名称字段中输入
new upstream。 - 点击新增节点,调整节点主机以指向新后端。例如,使用
mock.api7.ai作为主机,443作为端口。 - 点击新增。
- 在上游名称字段中输入
- 点击上游页头操作按钮下的查看 ID,复制该 ID 以供后续使用。
- 在已发布服务下,从侧边栏选择插件。
- 点击新增插件。
- 搜索
traffic-split插件,然后点击新增。 - 在对话框中,执行以下操作:
-
将以下配置添加到 JSON 编辑器:
{"rules": [{"weighted_upstreams": [{"upstream_id": "new_upstream_id", // 使用上游 ID,而不是上游名称"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"
}
}
一个请求将发送到新上游地址 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
upstreams:
- name: newupstream
nodes:
- host: mock.api7.ai
port: 443
weight: 100
scheme: https
routes:
- uris:
- /headers
name: getting-started-headers
methods:
- GET
plugins:
traffic-split:
rules:
- weighted_upstreams:
- upstream_id: new_upstream_id # 使用上游 ID,而不是上游名称
weight: 1
- weight: 9
将配置同步到 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"
}
}
一个请求将发送到 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 Ingress Controller 不支持在 traffic-split 插件中使用 upstream_id。
- Gateway API
- APISIX CRD
创建路由的 Kubernetes 清单文件(同时也会创建服务),在服务上启用 traffic-split,按 10%/90% 切分流量:
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: api7
name: traffic-split-plugin-config
spec:
plugins:
- name: traffic-split
config:
rules:
- weighted_upstreams:
- upstream:
scheme: https
pass_host: node
nodes:
mock.api7.ai:443: 1
weight: 1
- weight: 9
---
apiVersion: v1
kind: Service
metadata:
namespace: api7
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: api7
name: passhost-node
spec:
targetRefs:
- name: httpbin-external-domain
kind: Service
group: ""
passHost: node
scheme: https
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: api7
name: getting-started-ip
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /headers
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: traffic-split-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 443
weight: 1
创建路由的 Kubernetes 清单文件(同时也会创建服务),在服务上启用 traffic-split,按 10%/90% 切分流量:
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: api7
name: httpbin-external-domain
spec:
ingressClassName: apisix
scheme: https
passHost: node
externalNodes:
- type: Domain
name: httpbin.org
weight: 1
port: 443
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: api7
name: traffic-split-plugin-config
spec:
ingressClassName: apisix
plugins:
- name: traffic-split
enable: true
config:
rules:
- weighted_upstreams:
- upstream:
scheme: https
pass_host: node
nodes:
mock.api7.ai:443: 1
weight: 1
- weight: 9
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: api7
name: getting-started-headers
spec:
ingressClassName: apisix
http:
- name: getting-started-headers
match:
paths:
- /headers
plugin_config_name: traffic-split-plugin-config
upstreams:
- name: httpbin-external-domain
你也可以不使用 traffic-split 插件,而是为上游服务设置 weight。
将配置应用到集群:
kubectl apply -f traffic-split.yaml
注意,使用 Gateway API 时,插件会显示在服务级别;使用 APISIX CRD 时,插件会显示在路由级别。
按条件切分流量:请求头
本示例将带有 version = test 请求头的请求路由到新上游,其余流量继续发送到默认上游。灰度规则适用于服务中的所有路由,不能应用于单个路由。
- 控制台
- ADC
- Ingress Controller
- 从侧边栏选择网关组的已发布服务,然后点击要修改的服务,例如版本为
1.0.0的httpbin。 - 在服务下,从侧边栏选择上游。
- 点击新增上游。
- 在对话框中,执行以下操作:
- 在上游名称字段中输入
new upstream。 - 点击新增节点,调整节点主机以指向新后端。例如,使用
mock.api7.ai作为主机,443作为端口。 - 点击新增。
- 在上游名称字段中输入
- 点击上游页头操作按钮下的查看 ID,复制该 ID 以供后续使用。
- 在已发布服务下,从侧边栏选择插件。
- 点击新增插件。
- 搜索
traffic-split插件,然后点击新增。 - 在对话框中,执行以下操作:
-
将以下配置添加到 JSON 编辑器:
{"rules": [{"match": [{"vars": [["version","==","test"]]}],"weighted_upstreams": [{"upstream_id": "new_upstream_id", // 使用上游 ID,而不是上游名称"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
upstreams:
- name: newupstream
nodes:
- host: mock.api7.ai
port: 443
weight: 100
scheme: https
routes:
- uris:
- /headers
name: getting-started-headers
methods:
- GET
plugins:
traffic-split:
rules:
- match:
- vars:
- - version
- "=="
- test
weighted_upstreams:
- upstream_id: new_upstream_id # 使用上游 ID,而不是上游名称
weight: 1
- weight: 9
将配置同步到 API7 网关:
adc sync -f adc.yaml
发送带有 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"
}
}
使用新上游的值更新默认上游,完成灰度流量切分:
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
你将学习如何使用 traffic-split 插件按权重和条件切分流量。
- Gateway API
- APISIX CRD
创建路由的 Kubernetes 清单文件(同时也会创建服务),在服务上启用 traffic-split:
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: api7
name: traffic-split-plugin-config
spec:
plugins:
- name: traffic-split
config:
rules:
- match:
- vars:
- - version
- "=="
- test
weighted_upstreams:
- upstream:
scheme: https
pass_host: node
nodes:
mock.api7.ai:443: 1
weight: 1
- weight: 9
---
apiVersion: v1
kind: Service
metadata:
namespace: api7
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: api7
name: passhost-node
spec:
targetRefs:
- name: httpbin-external-domain
kind: Service
group: ""
passHost: node
scheme: https
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: api7
name: getting-started-ip
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /headers
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: traffic-split-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 443
weight: 1
创建路由的 Kubernetes 清单文件(同时也会创建服务),按以下方式在服务上配置 traffic-split:
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: api7
name: httpbin-external-domain
spec:
ingressClassName: apisix
scheme: https
passHost: node
externalNodes:
- type: Domain
name: httpbin.org
weight: 1
port: 443
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: api7
name: traffic-split-plugin-config
spec:
ingressClassName: apisix
plugins:
- name: traffic-split
enable: true
config:
rules:
- match:
- vars:
- - version
- "=="
- test
weighted_upstreams:
- upstream:
scheme: https
pass_host: node
nodes:
mock.api7.ai:443: 1
weight: 1
- weight: 9
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: api7
name: getting-started-headers
spec:
ingressClassName: apisix
http:
- name: getting-started-headers
match:
paths:
- /headers
plugin_config_name: traffic-split-plugin-config
upstreams:
- name: httpbin-external-domain
将配置应用到集群:
kubectl apply -f traffic-split.yaml
注意,使用 Gateway API 时,插件会显示在服务级别;使用 APISIX CRD 时,插件会显示在路由级别。
相关阅读
- 核心概念
- 插件中心