时间:2025-04-18 10:54
人气:
作者:admin
ApisixPluginConfig,无需逐个修改路由。Ingress 的 Annotations 中编写冗长的 JSON。定义插件的具体配置(例如限流插件 limit-count):
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
name: limit-config
spec:
plugins:
- name: limit-count
enable: true
config:
_meta:
disable: false #注意这行是必须的,否则在dashboard的路由>高级特性>插件模板配置中会出现白屏的情况
count: 100
time_window: 60
key: remote_addr
policy: local
通过 plugin_config_name 字段关联到路由:
ApisixRoute 中引用apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: my-route
spec:
http:
- name: rule1
match:
hosts: ["example.com"]
paths: ["/*"]
backends:
- serviceName: my-service
servicePort: 80
# 引用插件配置
plugin_config_name: limit-config
注意:如果apisix-ingress中使用了自已的域名,那网关的域名就失效了,这块需要注意一下
Ingress 中通过注解引用apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
k8s.apisix.apache.org/plugin-config-name: "limit-config" # 指定插件配置名称
spec:
ingressClassName: apisix
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80
kubectl get apisixpluginconfig limit-config -o yaml

如果是阿里云容器平台上,它会在自定义资源里出现

apisix-dashboard的路由》高级特性》插件模板会展示你的插件配置

通过 Admin API 检查路由是否关联了插件:
curl http://<APISIX_ADMIN_IP>:9180/apisix/admin/routes -H 'X-API-KEY: <ADMIN_KEY>'
预期输出中应包含 limit-count 插件的配置。
| 方式 | 维护成本 | 复用性 | 灵活性 | 适用场景 |
|---|---|---|---|---|
ApisixPluginConfig |
低 | 高 | 高 | 多个路由共享同一插件 |
Ingress Annotations |
高 | 低 | 中 | 简单插件、单路由配置 |
ApisixPluginConfig 是命名空间级别的资源,需确保路由和插件配置在同一命名空间。若需跨命名空间引用,需配置 APISIX Ingress Controller 的 scope 参数为 cluster。ApisixPluginConfig(v2.7+ 推荐使用)。ApisixRoute 和 ApisixPluginConfig 中定义了同名插件,ApisixRoute 中的配置会覆盖 ApisixPluginConfig。可以在一个 ApisixPluginConfig 中定义多个插件:
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
name: global-plugins
spec:
plugins:
- name: limit-count
enable: true
config:
_meta:
disable: false
count: 200
time_window: 60
- name: cors
enable: true
config:
_meta:
disable: false
allow_origins: "*"
allow_methods: "GET,POST"
通过 ApisixPluginConfig 可以实现插件的集中管理和复用,特别适合以下场景:
Ingress 中维护冗长的 JSON。如果此前通过 Ingress Annotations 管理插件,可以逐步迁移到 ApisixPluginConfig,提升配置的可维护性。
作者:仓储大叔,张占岭,
荣誉:微软MVP
QQ:853066980
支付宝扫一扫,为大叔打赏!

下一篇:勒索软件介绍