首页 > 解决方案 > 将一个自定义域映射到多个服务的正确方法是什么?

问题描述

使用 GKE 和 Cloud Run映射自定义域的说明适用于 1:1 域:服务映射。但是如果我想拥有 1:M domain:services 并与 URI 匹配,

myapp.com/login  >> login-service
myapp.com/logout >> logout-service

我试过的

第二个域映射创建语句将出错,因为域在服务中必须是唯一的:

$ gcloud beta run domain-mappings create --service login-service --domain myapp.com     --cluster mycluster     --cluster-location europe-west2-a
Creating......done.                                                                                                                                         
RECORD TYPE  CONTENTS
A            XX.XXX.XXX.XX

$ gcloud beta run domain-mappings create --service login-service --domain myapp.com     --cluster mycluster     --cluster-location europe-west2-a
ERROR: ... "message": domainmappings.domains.cloudrun.com \"myapp.com\" already exists ...

以前,当使用手动创建的 Knative 环境时,我可以使用 Istio 来实现VirtualService

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: entry-route
  namespace: default
spec:
  - knative-ingress-gateway.knative-serving.svc.cluster.local
  # Set host to the domain name that you own.
  hosts:
  - myapp.com
  http:
  - match:
    - uri:
        prefix: "/login"
    rewrite:
      authority: login-service.default.myapp.com
    route:
      - destination:
          host: knative-ingressgateway.istio-system.svc.cluster.local
        weight: 100
  - match:
    - uri:
        prefix: "/logout"
    rewrite:
      authority: logout-service.default.myapp.com
    route:
      - destination:
          host: knative-ingressgateway.istio-system.svc.cluster.local
        weight: 100

但是,虽然我可以通过 Cloud Run 在 GKE 上应用它,但所有内容都被路由到映射到域的服务。

我还尝试删除gcloud beta run domain-mappings创建的,将istio-ingressgatewayLoadBalancer 设置为保留的静态 IP,并将我的域指向 LoadBalancer。但是,这只会导致503s.

为什么我不能只指向istio-ingressgatewayLoadBalancer 并VirtualService为我提供一条路线?

标签: google-cloud-run

解决方案


Firebase Hosting 与 Cloud Run的集成允许您将不同的子路径重写到不同的 Cloud Run 服务。配置看起来像:

{
  "hosting": {
    "rewrites": [
      {"source": "/api/**", "run": {"serviceId": "api"}},
      {"source": "/charts/*.svg", "run": {"serviceId": "chartgen"}},
      {"source": "**", "run": {"serviceId": "ssr"}}
    ]
  }
}

推荐阅读