我有一个带有“Gateway”和“Httproute”资源的 GKE 集群,如下所示。
kind: Gateway
apiVersion: gateway.networking.k8s.io/v1beta1
metadata:
namespace: development
name: dev-example-gateway
spec:
gatewayClassName: gke-l7-global-external-managed
listeners:
- name: http
protocol: HTTP
port: 80
kind: HTTPRoute
apiVersion: gateway.networking.k8s.io/v1beta1
metadata:
name: store
namespace: development
spec:
parentRefs:
- kind: Gateway
name: dev-example-gateway
hostnames:
- "gateway.example.com"
rules:
- backendRefs:
- name: example-frontend-svc
port: 3000
- matches:
- path:
type: PathPrefix
value: /api
filters:
- type: URLRewrite
urlRewrite:
hostname: gateway.example.com
path:
type: ReplacePrefixMatch
replacePrefixMatch: /
backendRefs:
- name: example-backend-svc
port: 2828
我还有一个 GCP 存储桶,我想将流量传递到其中。我已经为名为“example-bucket”的后端创建了存储桶。将下面的代码添加到 httproute 资源`
- matches:
- path:
type: PathPrefix
value: /_next/static/
filters:
- type: URLRewrite
urlRewrite:
hostname: gateway.example.com
path:
type: ReplacePrefixMatch
replacePrefixMatch: /static/
backendRefs:
- name: projects/example/global/backendBuckets/example-bucket
port: 80
但它为此添加了另一个后端,而不是存储桶,一些自动生成的后端称为“gkegw1-bxdu-development-gw-serve404-80-s8c5ntyjhig9”。我使用的LB类型支持后端桶。
尝试过这个httproute配置。
kind: HTTPRoute
apiVersion: gateway.networking.k8s.io/v1beta1
metadata:
name: store
namespace: development
spec:
parentRefs:
- kind: Gateway
name: dev-example-gateway
hostnames:
- "gateway.example.com"
rules:
- backendRefs:
- name: example-frontend-svc
port: 3000
- matches:
- path:
type: PathPrefix
value: /api
filters:
- type: URLRewrite
urlRewrite:
hostname: gateway.example.com
path:
type: ReplacePrefixMatch
replacePrefixMatch: /
backendRefs:
- name: example-backend-svc
port: 2828
- matches:
- path:
type: PathPrefix
value: /_next/static/
filters:
- type: URLRewrite
urlRewrite:
hostname: gateway.example.com
path:
type: ReplacePrefixMatch
replacePrefixMatch: /static/
backendRefs:
- name: projects/example/global/backendBuckets/example-bucket
port: 80
其他后端按预期工作。
不幸的是,GKE Ingress 或 Gateway 都不支持使用 GCS 作为后端。您必须手动配置负载均衡器,然后使用添加 GKE 独立 NEG 作为后端以及 GCS 后端。