跳到主要内容

启用网关调试模式

网关提供调试模式,帮助开发者更好地理解和排查网关运行时行为。调试模式配置可在已发布版本的 debug.yaml 中查看。

本指南介绍如何在 Kubernetes 环境中启用网关基础调试模式,以检查请求命中路由上启用了哪些插件。高级调试模式请参阅高级调试模式。

前置条件​

  1. 完成设置 Ingress Controller 和网关。

启用基础调试模式​

启用基础调试模式后,可以通过 Apisix-Plugins 响应头检查请求命中路由上启用了哪些插件。

默认情况下,调试模式处于关闭状态。如需启用调试模式,可以在 debug.yaml 文件中将 basic.enable 设置为 true。

创建 ConfigMap:

apisix-debug-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: apisix-debug
namespace: aic
data:
debug.yaml: |
basic:
enable: true
http_filter:
enable: false
hook_conf:
enable: false
#END

将 ConfigMap 应用到集群:

kubectl apply -f apisix-debug-cm.yaml

以下命令假定你使用设置指南中的 APISIX 本地评估环境。对于 API7 网关,请使用已安装网关版本对应的配置流程。

如需挂载 ConfigMap,先导出所有 values(包括默认值):

helm get values -n aic apisix --all > values.yaml

在 values 文件中按如下方式更新配置:

values.yaml
extraVolumes:
- name: debug-config
configMap:
name: apisix-debug
extraVolumeMounts:
- name: debug-config
mountPath: /usr/local/apisix/conf/debug.yaml
subPath: debug.yaml

使用已安装的 Chart 版本升级 Release。运行 helm list -n aic,从 CHART 列查找版本后缀:

helm upgrade -n aic apisix apisix/apisix --version <installed-chart-version> -f values.yaml
信息

debug.yaml 文件应以 #END 结尾,APISIX 在解析文件前会将其作为完成标记。

验证​

创建一条不带任何插件的路由进行验证:

httpbin-route.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: getting-started-anything
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
backendRefs:
- name: httpbin-external-domain
port: 80

将配置应用到集群:

kubectl apply -f httpbin-route.yaml

向路由发送请求:

curl -i "http://127.0.0.1:9080/anything"

你应收到 HTTP/1.1 200 OK 响应,并观察到如下响应头:

Content-Type: application/json
Content-Length: 390
Connection: keep-alive
Apisix-Plugins: no plugin
...

更新路由并添加插件:

httpbin-route.yaml
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: limit-count-plugin-config
spec:
plugins:
- name: limit-count
config:
count: 5
time_window: 10
rejected_code: 429
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: getting-started-anything
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: limit-count-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80

应用路由,然后向现有 GatewayProxy 添加 prometheus,不替换由 Chart 管理的 Provider 或 Admin API Secret 引用:

kubectl apply -f httpbin-route.yaml
kubectl patch gatewayproxy apisix-config \
--namespace aic \
--type merge \
--patch '{
"spec": {
"plugins": [
{
"name": "prometheus",
"enabled": true
}
]
}
}'

合并补丁会保留 Provider 和身份验证字段,但会替换当前的 spec.plugins 列表。如果 GatewayProxy 已配置其他全局插件,请将它们包含在列表中。后续 Helm 升级会恢复 release values 中声明的插件;如需持久保留变更,请将相同列表添加到 ingress-controller.gatewayProxy.plugins。

再次向路由发送请求:

curl -i "http://127.0.0.1:9080/anything"

你应收到带有类似如下响应头的 HTTP/1.1 200 OK 响应:

Content-Type: application/json
Content-Length: 388
Connection: keep-alive
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 4
X-RateLimit-Reset: 10
Apisix-Plugins: limit-count, prometheus
...
信息

如果插件信息无法包含在响应头中(例如 L4 stream 插件),Debug 信息会以 warn 级别记录到 error log。