跳到主要内容

启用网关 Debug 模式

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

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

前提条件

  1. 完成设置 Ingress Controller 和网关

启用基础 Debug 模式

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

默认情况下,Debug 模式处于关闭状态。如需启用 Debug 模式,可以在 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
#END

将 ConfigMap 应用到集群:

kubectl apply -f apisix-debug-cm.yaml

如需挂载 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

升级 Helm release:

helm upgrade -n aic apisix apisix/apisix -f values.yaml
信息

debug.yaml 配置必须以 #END 结尾,否则网关不会加载该配置。这也意味着你可以将 #END 标记作为断点,让网关只加载到指定位置之前的配置。

验证

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

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 manifest,启用 prometheus 作为全局插件:

提示

在添加额外配置前,如需查看当前 GatewayProxy 配置,运行:

kubectl get gatewayproxy apisix-config -o yaml
gateway-proxy.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: GatewayProxy
metadata:
namespace: aic
name: apisix-config
spec:
provider:
type: ControlPlane
controlPlane:
auth:
adminKey:
value: edd1c9f034335f136f87ad84b625c8f1
type: AdminKey
service:
name: apisix-admin
port: 9180
plugins:
- name: prometheus
enabled: true

将配置应用到集群:

kubectl apply -f httpbin-route.yaml -f gateway-proxy.yaml

再次向路由发送请求:

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。