7.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。
流程
运行以下命令,创建一个名为
hello-openshift
的项目:$ oc new-project hello-openshift
运行以下命令,在项目中创建 pod:
$ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/hello-openshift/hello-pod.json
运行以下命令,创建名为
hello-openshift
的服务:$ oc expose pod/hello-openshift
创建名为
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
通过运行以下命令,使用
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