部署 Wasm 插件
APISIX 支持遵循 Proxy-Wasm 规范开发的 WebAssembly(Wasm) 插件。该规范将 Wasm 能力扩展到代理场景。
本指南介绍如何在 Kubernetes 上将 WebAssembly(Wasm)插件加载到网关,并通过 APISIX Ingress Controller 在路由上启用该插件。
备注
API7 企业版不支持自定义 Wasm 插件。
前提条件
- 完成设置 Ingress Controller 和网关。
- 按照 Wasm 插件指南准备
log.go.wasm示例插件。
加载 Wasm 插件
基于 log.go.wasm 文件创建 ConfigMap:
kubectl create configmap wasm-log --from-file=log.go.wasm
如需挂载 ConfigMap,先导出所有 values(包括默认值):
helm get values -n aic apisix --all > values.yaml
在 values 文件中按如下方式更新配置:
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
升级 Helm release:
helm upgrade -n aic apisix apisix/apisix -f values.yaml
测试 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"