PixiuClusterPolicy 是一个自定义资源扩展,用于为 Pixiu Gateway 中的后端服务配置集群级别的策略。
使用注册中心(例如:Zookeeper、Nacos)配置服务发现
apiVersion: pixiu.apache.org/v1alpha1
kind: PixiuClusterPolicy
metadata:
name: service-registry-policy
namespace: default
spec:
serviceRef:
- name: dubbo-backend
type: dynamic
registries:
zookeeper:
protocol: zookeeper
address: zookeeper:2181
timeout: 3s
nacos:
protocol: nacos
address: nacos:8848
namespace: public
group: DEFAULT_GROUP
在服务发现不可用时定义静态端点
apiVersion: pixiu.apache.org/v1alpha1
kind: PixiuClusterPolicy
metadata:
name: static-endpoints-policy
namespace: default
spec:
serviceRef:
- name: static-backend
type: static
endpoints:
- address: 10.0.0.1
port: 8080
protocolType: http
- address: 10.0.0.2
port: 8080
protocolType: http
为服务设置负载均衡策略
apiVersion: pixiu.apache.org/v1alpha1
kind: PixiuClusterPolicy
metadata:
name: loadbalancer-policy
namespace: default
spec:
serviceRef:
- name: backend-service
loadBalancer:
policy: round_robin
为后端服务启用健康检查
apiVersion: pixiu.apache.org/v1alpha1
kind: PixiuClusterPolicy
metadata:
name: healthcheck-policy
namespace: default
spec:
serviceRef:
- name: backend-service
healthCheck:
protocol: http
timeout: 5s
interval: 10s
healthyThreshold: 2
unhealthyThreshold: 3
httpHealthCheck:
path: /health
expectedStatuses:
- 200
- 201
为集群设置超时配置
apiVersion: pixiu.apache.org/v1alpha1
kind: PixiuClusterPolicy
metadata:
name: cluster-timeout-policy
namespace: default
spec:
serviceRef:
- name: backend-service
timeout:
connect: 10s
request: 30s
在网关级别配置集群
apiVersion: pixiu.apache.org/v1alpha1
kind: PixiuClusterPolicy
metadata:
name: gateway-cluster-policy
namespace: pixiu-gateway-system
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: pixiu-listeners
clusterRef:
- name: custom-cluster
type: static
loadBalancerPolicy: least_request
endpoints:
- address: 192.168.1.100
port: 8080
protocolType: http
PixiuClusterPolicy 可以应用于服务级别和网关级别的不同层级
当未指定 targetRef 或目标为 Service 时,策略按名称应用于服务。serviceRef 条目定义特定服务的集群配置。
当 targetRef 指定网关时:
clusterRef 条目应用于网关引用的集群serviceRef 条目应用于附加到网关的 HTTPRoute 引用的服务targetRef,策略应用于同一命名空间中的服务targetRef.kind 是 Gateway,策略在网关级别应用serviceRef 中的服务名称必须与 HTTPRoute backendRefs 中使用的服务名称匹配clusterRef 中的集群名称必须与网关配置中引用的集群名称匹配当多个 PixiuClusterPolicy 资源配置相同的服务或集群时:
targetRef 指向 Gateway 对网关引用的集群具有优先权targetRef 或目标为 Service)应用于 HTTPRoute 中引用的服务