# 部署 Wasm 插件

APISIX 支持遵循 [Proxy-Wasm 规范](https://github.com/proxy-wasm/spec)开发的 [WebAssembly（Wasm）](https://webassembly.org) 插件。该规范将 Wasm 能力扩展到代理场景。

本指南介绍如何在 Kubernetes 上将 WebAssembly（Wasm）插件加载到网关，并通过 APISIX Ingress Controller 在路由上启用该插件。

备注

本流程适用于 APISIX。API7 Dashboard 的自定义插件上传流程支持 Lua 插件，但不提供本指南使用的 Wasm 部署流程。

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

1. 完成[设置 Ingress Controller 和网关](https://docs.apiseven.com/ingress-controller/set-up-ingress-controller-and-gateway.md)。
2. 按照 Wasm 插件指南中的[使用 Go 编写插件逻辑](https://docs.apiseven.com/apisix/how-to-guide/custom-plugins/wasm-plugins.md#%E7%94%A8-go-%E7%BC%96%E5%86%99%E6%8F%92%E4%BB%B6%E9%80%BB%E8%BE%91)和[将代码编译为 Wasm](https://docs.apiseven.com/apisix/how-to-guide/custom-plugins/wasm-plugins.md#%E5%B0%86%E4%BB%A3%E7%A0%81%E7%BC%96%E8%AF%91%E4%B8%BA-wasm)步骤准备 `log.go.wasm` 示例插件。

## 加载 Wasm 插件[​](#加载-wasm-插件 "加载 Wasm 插件的直接链接")

基于 `log.go.wasm` 文件创建 ConfigMap：

```
kubectl create configmap wasm-log -n aic --from-file=log.go.wasm
```

如需挂载 ConfigMap，先导出所有 values（包括默认值）：

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

在导出的 values 文件中，将以下条目追加到现有的 `extraVolumes` 和 `extraVolumeMounts` 列表，然后更新现有的 `apisix.wasm` 映射。请保留其他值，不要创建重复的顶层键：

values.yaml

```
extraVolumes:

  - name: wasm-log

    configMap:

      name: wasm-log

extraVolumeMounts:

  - name: wasm-log

    mountPath: /usr/local/bin/log.go.wasm

    subPath: log.go.wasm

apisix:

  wasm:

    enabled: true

    plugins:

      - name: wasm_log

        priority: 7999

        file: /usr/local/bin/log.go.wasm
```

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

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

## 测试 Wasm 插件[​](#测试-wasm-插件 "测试 Wasm 插件的直接链接")

创建带有 `wasm_log` 插件的路由：

* Gateway API
* APISIX CRD

wasm-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: wasm-log-plugin-config

spec:

  plugins:

    - name: wasm_log

      config:

        conf: hello apisix

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: wasm-log-plugin

spec:

  parentRefs:

  - name: apisix

  rules:

  - matches:

    - path:

        type: Exact

        value: /anything

    filters:

    - type: ExtensionRef

      extensionRef:

        group: apisix.apache.org

        kind: PluginConfig

        name: wasm-log-plugin-config

    backendRefs:

    - name: httpbin-external-domain

      port: 80
```

wasm-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: wasm-log-plugin

spec:

  ingressClassName: apisix

  http:

    - name: wasm-log-plugin

      match:

        paths:

          - /anything

      plugins:

      - name: wasm_log

        enable: true

        config:

          conf: hello apisix

      upstreams:

      - name: httpbin-external-domain
```

将配置应用到集群：

```
kubectl apply -f wasm-route.yaml
```

向路由发送请求：

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

你应收到 `HTTP/1.1 200 OK` 响应.

在网关日志中，你应看到由 `wasm_log` 插件创建的如下日志条目：

```
2025/09/04 09:58:54 [warn] 53#53: *3331 run plugin ctx 1 with conf hello apisix in http ctx 2, client: 127.0.0.1, server: _, request: "GET /anything HTTP/1.1", host: "127.0.0.1:9080"
```
