# 启用网关调试模式

网关提供调试模式，帮助开发者更好地理解和排查网关运行时行为。调试模式配置可在已发布版本的 [`debug.yaml`](https://github.com/apache/apisix/blob/3.21.0/conf/debug.yaml) 中查看。

本指南介绍如何在 Kubernetes 环境中启用网关基础调试模式，以检查请求命中路由上启用了哪些插件。高级调试模式请参阅[高级调试模式](https://docs.apiseven.com/apisix/troubleshooting/debug-mode.md#%E9%85%8D%E7%BD%AE%E9%AB%98%E7%BA%A7%E8%B0%83%E8%AF%95%E6%A8%A1%E5%BC%8F)。

## 前置条件[​](#前置条件 "前置条件的直接链接")

1. 完成[设置 Ingress Controller 和网关](https://docs.apiseven.com/ingress-controller/set-up-ingress-controller-and-gateway.md)。

## 启用基础调试模式[​](#启用基础调试模式 "启用基础调试模式的直接链接")

启用基础调试模式后，可以通过 `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 在解析文件前会将其作为完成标记。

## 验证[​](#验证 "验证的直接链接")

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

* Gateway API
* APISIX CRD

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
```

httpbin-route.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: getting-started-anything

spec:

  ingressClassName: apisix

  http:

    - name: getting-started-anything

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain
```

将配置应用到集群：

```
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

...
```

* Gateway API
* APISIX CRD

更新路由并添加插件：

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`。

更新路由并添加插件：

httpbin-route.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: getting-started-anything

spec:

  ingressClassName: apisix

  http:

    - name: getting-started-anything

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain

      plugins:

      - name: limit-count

        enable: true

        config:

          count: 5

          time_window: 10

          rejected_code: 429
```

此外，创建一个全局 Prometheus 插件的 Kubernetes manifest 文件：

global-prometheus.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixGlobalRule

metadata:

  namespace: aic

  name: global-prometheus

spec:

  ingressClassName: apisix

  plugins:

    - name: prometheus

      enable: true
```

将配置应用到集群：

```
kubectl apply -f httpbin-route.yaml -f global-prometheus.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。
