注解 (Annotations)
注解(Annotations)是键值对,允许控制器配置无法通过标准 Kubernetes 资源字段实现的功能。在 API7 Ingress Controller 中,注解通常用于配置网关行为、路由规则、上游设置、插件和其他功能。或者,你可以使用 APISIX CRD 来配置这些功能,以获得更好的体验。
本文档描述了所有可用的注解及其用途。
Ingress 注解
| 注解 |
|---|
kubernetes.io/ingress.class |
k8s.apisix.apache.org/use-regex |
k8s.apisix.apache.org/enable-websocket |
k8s.apisix.apache.org/plugin-config-name |
k8s.apisix.apache.org/upstream-scheme |
k8s.apisix.apache.org/upstream-retries |
k8s.apisix.apache.org/upstream-connect-timeout |
k8s.apisix.apache.org/upstream-read-timeout |
k8s.apisix.apache.org/upstream-send-timeout |
k8s.apisix.apache.org/enable-cors |
k8s.apisix.apache.org/cors-allow-origin |
k8s.apisix.apache.org/cors-allow-headers |
k8s.apisix.apache.org/cors-allow-methods |
k8s.apisix.apache.org/enable-csrf |
k8s.apisix.apache.org/csrf-key |
k8s.apisix.apache.org/http-to-https |
k8s.apisix.apache.org/http-redirect |
k8s.apisix.apache.org/http-redirect-code |
k8s.apisix.apache.org/rewrite-target |
k8s.apisix.apache.org/rewrite-target-regex |
k8s.apisix.apache.org/rewrite-target-regex-template |
k8s.apisix.apache.org/enable-response-rewrite |
k8s.apisix.apache.org/response-rewrite-status-code |
k8s.apisix.apache.org/response-rewrite-body |
k8s.apisix.apache.org/response-rewrite-body-base64 |
k8s.apisix.apache.org/response-rewrite-add-header |
k8s.apisix.apache.org/response-rewrite-set-header |
k8s.apisix.apache.org/response-rewrite-remove-header |
k8s.apisix.apache.org/auth-uri |
k8s.apisix.apache.org/auth-ssl-verify |
k8s.apisix.apache.org/auth-request-headers |
k8s.apisix.apache.org/auth-upstream-headers |
k8s.apisix.apache.org/auth-client-headers |
k8s.apisix.apache.org/allowlist-source-range |
k8s.apisix.apache.org/blocklist-source-range |
k8s.apisix.apache.org/http-allow-methods |
k8s.apisix.apache.org/http-block-methods |
k8s.apisix.apache.org/auth-type |
k8s.apisix.apache.org/svc-namespace |
IngressClass 注解
| 注解 |
|---|
apisix.apache.org/parameters-namespace |
注解详情
请注意,注解的键和值只能是字符串。
Ingress Class
kubernetes.io/ingress.class 注解指定哪个 Ingress Controller 应处理给定的 Ingress 资源。当在同一个集群中部署了多个 Ingress Controller 时,这非常有用。
例如:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: httpbin
annotations:
kubernetes.io/ingress.class: "apisix"
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpbin
port:
number: 80
响应重写 (Response Rewrite)
这些注解使你能够在将上游服务的响应发送到客户端之前修改该响应。它们对应于 APISIX 中 response-rewrite 插件的功能。
| 注解 | 描述 |
|---|---|
k8s.apisix.apache.org/enable-response-rewrite | 启用响应重写功能。设置为 true 以便为此资源启用响应重写。 |
k8s.apisix.apache.org/response-rewrite-status-code | 为响应指定一个新的 HTTP 状态码。 |
k8s.apisix.apache.org/response-rewrite-body | 使用指定的纯文本内容替换响应体。 |
k8s.apisix.apache.org/response-rewrite-body-base64 | 使用 Base64 编码的内容替换响应体。 |
k8s.apisix.apache.org/response-rewrite-add-header | 向响应中添加新的请求头。你可以将多个请求头指定为逗号分隔的列表。 |
k8s.apisix.apache.org/response-rewrite-set-header | 设置或覆盖响应中的现有请求头。你可以将多个请求头指定为逗号分隔的列表。 |
k8s.apisix.apache.org/response-rewrite-remove-header | 从响应中删除指定的请求头。你可以将多个请求头指 定为逗号分隔的列表。 |
例如:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: httpbin-response-rewrite
annotations:
k8s.apisix.apache.org/enable-response-rewrite: "true"
k8s.apisix.apache.org/response-rewrite-status-code: "403"
k8s.apisix.apache.org/response-rewrite-body: "Access denied"
k8s.apisix.apache.org/response-rewrite-add-header: "X-Reason:Forbidden,X-Env:Test"
k8s.apisix.apache.org/response-rewrite-remove-header: "header1,header2"
spec:
ingressClassName: apisix
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpbin
port:
number: 80
IP 限制 (IP Restriction)
这些注解控制基于 IP 地址范围的客户端访问。它们对应于 APISIX 中 ip-restriction 插件的功能。
| 注解 | 描述 |
|---|---|
k8s.apisix.apache.org/allowlist-source-range | 允许访问资源的 CIDR 范围的逗号分隔列 表。所有其他 IP 均被拒绝。 |
k8s.apisix.apache.org/blocklist-source-range | 被阻止访问资源的 CIDR 范围的逗号分隔列表。 |
例如:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ip-restriction
annotations:
k8s.apisix.apache.org/allowlist-source-range: "10.0.0.0/24,192.168.1.0/24"
spec:
ingressClassName: apisix
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpbin
port:
number: 80