配置示例
API7 Ingress Controller 支持使用 Ingress 资源和 Gateway API 在 Kubernetes 中进行流量管理。虽然两者都受支持,但 Gateway API 提供了更大的灵活性和可扩展性。建议新用户在未来的部署中采用 Gateway API。
除了这些标准的 Kubernetes API 之外,API7 Ingress Controller 还支持一组专为 APISIX 原生功能设计的 CRD(自定义资源定义)。
本文档提供了涵盖如何以及何时使用这些资源的常见配置示例。请确保将任何占位符值(例如命名空间、路由 URI 和凭证)替换为与你自己环境匹配的值。
目前,APISIX Ingress Controller 和 API7 Ingress Controller 之间没有功能差异,尽管它们的发布时间表可能不同。
配置 CP 端点和 Admin Key
要在运行时更新控制平面端点和 Admin Key 以实现 API7 Ingress Controller 与控制平面之间的连接:
apiVersion: apisix.apache.org/v1alpha1
kind: GatewayProxy
metadata:
namespace: api7
name: apisix-proxy-config
spec:
provider:
type: ControlPlane
controlPlane:
endpoints:
- https://xxx.xxx.xxx.xxx:7443 # 替换为你的 CP 端点
auth:
type: AdminKey
adminKey:
value: xxxxxxxxxxx # 替换为你的 admin key
同一网关组内的所有资源必须使用相同的 IngressClass(针对 Ingress / APISIX CRD)或 Gateway(针对 Gateway API),并且每个资源都指向同一个 GatewayProxy。
对于单个网关组,使用多个 GatewayProxy、IngressClass 或 Gateway 资源可能会导致冲突和意外的资源覆盖。
定义控制器和网关
要在应用进一步配置之前指定负责处理资源的控制器:
- Gateway API
- Ingress
- APISIX CRD
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: apisix
spec:
controllerName: "apisix.apache.org/apisix-ingress-controller"
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: api7
name: apisix
spec:
gatewayClassname: apisix
listeners:
- name: http
protocol: HTTP
port: 80
infrastructure:
parametersRef:
group: apisix.apache.org
kind: GatewayProxy
name: apisix-proxy-config
❶ 如果你在同一个集群中运行 APISIX Ingress Controller 的多个不同实例(而不是具有多个副本的单个实例),则应自定义控制器名称。每个 ingress controller 实例必须在其配置文件中使用唯一的 controllerName,并且相应的 GatewayClass 应引用该值。
❷ 引用资源的 API 组。
❸ 引用资源的 Kind。
❹ 引用资源的名称。应与 GatewayProxy 资源的 metadata.name 匹配。
Gateway 监听器中的 port 是必需的,但会被忽略。这是由于数据面的限制:它无法动态打开新端口。由于 Ingress Controller 不管理数据面部署,因此它无法自动更新配置或重启数据面以应用端口更改。
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: apisix
spec:
controller: "apisix.apache.org/apisix-ingress-controller"
parameters:
apiGroup: apisix.apache.org
kind: GatewayProxy
name: apisix-proxy-config
namespace: api7
scope: Namespace
❶ 如果你在同一个集群中运行 APISIX Ingress Controller 的多个不同实例(而不是具有多个副本的单个实例),则应自定义控制器名称。每个 ingress controller 实例必须在其配置文件中使用唯一的 controllerName,并且相应的 IngressClass 应引用该值。
❷ 引用资源的 API 组。
❸ 引用资源的 Kind。
❹ 引用资源的名称。应与 GatewayProxy 资源的 metadata.name 匹配。
❺ 定义引用资源的命名空间。
❻ 引用资源的作用域。
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: apisix
spec:
controller: "apisix.apache.org/apisix-ingress-controller"
parameters:
apiGroup: apisix.apache.org
kind: GatewayProxy
name: apisix-proxy-config
namespace: api7
scope: Namespace
❶ 如果你在同一个集群中运行 APISIX Ingress Controller 的多个不同实例(而不是具有多个副本的单个实例) ,则应自定义控制器名称。每个 ingress controller 实例必须在其配置文件中使用唯一的 controllerName,并且相应的 IngressClass 应引用该值。
❷ 引用资源的 API 组。
❸ 引用资源的 Kind。
❹ 引用资源的名称。应与 GatewayProxy 资源的 metadata.name 匹配。
❺ 定义引用资源的命名空间。
❻ 引用资源的作用域。
路由到 K8s 服务
要创建一个将请求代理到 K8s 上服务的路由:
- Gateway API
- Ingress
- APISIX CRD
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: api7
name: httpbin
spec:
parentRefs:
- name: apisix
hostnames:
- httpbin.example.com
rules:
- matches:
- path:
type: Exact
value: /ip
backendRefs:
- name: httpbin
port: 80
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: api7
name: httpbin
spec:
ingressClassName: apisix
rules:
- host: httpbin.example.com
http:
paths:
- path: /ip
pathType: Exact
backend:
service:
name: httpbin
port:
number: 80
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: api7
name: httpbin
spec:
ingressClassName: apisix
http:
- name: httpbin
match:
paths:
- /ip
backends:
- serviceName: httpbin
servicePort: 80
路由到外部服务
要创建一个将请求代理到公开托管服务的路由:
- Gateway API
- Ingress
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: api7
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: api7
name: get-ip
spec:
parentRefs:
- name: apisix
hostnames:
- httpbin.external
rules:
- matches:
- path:
type: Exact
value: /ip
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: v1
kind: Service
metadata:
namespace: api7
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: api7
name: get-ip
spec:
rules:
- host: httpbin.external
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpbin-external-domain
port:
number: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: api7
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: api7
name: get-ip
spec:
ingressClassName: apisix
http:
- name: get-ip
match:
hosts:
- httpbin.external
paths:
- /ip
upstreams:
- name: httpbin-external-domain
配置加权服务
要创建一个按权重将流量代理到上游服务的路由:
- Gateway API
- APISIX CRD
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: api7
name: httpbin
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /ip
backendRefs:
- name: httpbin-1
port: 80
weight: 3
- name: httpbin-2
port: 80
weight: 7
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: api7
name: httpbin
spec:
ingressClassName: apisix
http:
- name: httpbin
match:
hosts:
- httpbin
paths:
- /ip
backends:
- serviceName: httpbin-1
servicePort: 80
weight: 3
- serviceName: httpbin-2
servicePort: 80
weight: 7
Ingress 资源不支持此配置。
配置上游
要配置上游相关的配置,包括负载均衡算法、请求头如何传递到上游、服务超时等:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: api7
name: httpbin
spec:
targetRefs:
- name: httpbin
kind: Service
group: ""
timeout:
send: 10s
read: 10s
connect: 10s
scheme: http
retries: 10
loadbalancer:
type: roundrobin
passHost: rewrite
upstreamHost: httpbin.example.com
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: api7
name: httpbin
spec:
ingressClassName: apisix
timeout:
send: 10s
read: 10s
connect: 10s
scheme: http
retries: 10
loadbalancer:
type: roundrobin
passHost: rewrite
upstreamHost: httpbin.example.com