8.2. 为 Ingress Controller 分片创建路由

通过使用路由,您可以通过 URL 托管应用程序。在这种情况下,主机名没有被设置,路由会使用子域。当您指定子域时,会自动使用公开路由的 Ingress Controller 域。对于由多个 Ingress Controller 公开路由的情况,路由由多个 URL 托管。

以下流程描述了如何为 Ingress Controller 分片创建路由,使用 hello-openshift 应用程序作为示例。

在一组 Ingress Controller 之间平衡传入的流量负载时,以及在将流量隔离到特定 Ingress Controller 时,Ingress Controller 分片会很有用处。例如,A 公司的流量使用一个 Ingress Controller,B 公司的流量则使用另外一个 Ingress Controller。

先决条件

  • 已安装 OpenShift CLI(oc)。
  • 您以项目管理员身份登录。
  • 您有一个 web 应用来公开端口,以及侦听端口流量的 HTTP 或 TLS 端点。
  • 您已为分片配置了 Ingress Controller。

流程

  1. 运行以下命令,创建一个名为 hello-openshift 的项目:

    $ oc new-project hello-openshift
  2. 运行以下命令,在项目中创建 pod:

    $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/hello-openshift/hello-pod.json
  3. 运行以下命令,创建名为 hello-openshift 的服务:

    $ oc expose pod/hello-openshift
  4. 创建名为 hello-openshift-route.yaml 的路由定义:

    为分片创建的路由的 YAML 定义:

    apiVersion: route.openshift.io/v1
    kind: Route
    metadata:
      labels:
        type: sharded 1
      name: hello-openshift-edge
      namespace: hello-openshift
    spec:
      subdomain: hello-openshift 2
      tls:
        termination: edge
      to:
        kind: Service
        name: hello-openshift

    1
    标签键及其对应标签值必须与 Ingress Controller 中指定的标签值匹配。在本例中,Ingress Controller 具有标签键和值 type: sharded
    2
    路由将使用 subdomain 字段的值公开。指定 subdomain 字段时,您必须保留主机名未设置。如果您同时指定了 hostsubdomain 字段,则路由将使用 host 字段的值,并忽略 subdomain 字段。
  5. 通过运行以下命令,使用 hello-openshift-route.yaml 创建到 hello-openshift 应用程序的路由:

    $ oc -n hello-openshift create -f hello-openshift-route.yaml

验证

  • 使用以下命令获取路由的状态:

    $ oc -n hello-openshift get routes/hello-openshift-edge -o yaml

    生成的 Route 资源应类似以下示例:

    输出示例

    apiVersion: route.openshift.io/v1
    kind: Route
    metadata:
      labels:
        type: sharded
      name: hello-openshift-edge
      namespace: hello-openshift
    spec:
      subdomain: hello-openshift
      tls:
        termination: edge
      to:
        kind: Service
        name: hello-openshift
    status:
      ingress:
      - host: hello-openshift.<apps-sharded.basedomain.example.net> 1
        routerCanonicalHostname: router-sharded.<apps-sharded.basedomain.example.net> 2
        routerName: sharded 3

    1
    Ingress Controller 或路由器的主机名用于公开路由。host 字段的值由 Ingress Controller 自动决定,并使用它的域。在本例中,Ingress Controller 的域为 <apps-sharded.basedomain.example.net>
    2
    Ingress Controller 的主机名。
    3
    Ingress Controller 的名称。在本例中,Ingress Controller 的名称为 sharded

其它资源