This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.5.4. 路由
Knative 利用 OpenShift Container Platform TLS 终止来为 Knative 服务提供路由。创建 Knative 服务时,会自动为该服务创建一个 OpenShift Container Platform 路由。此路由由 OpenShift Serverless Operator 管理。OpenShift Container Platform 路由通过与 OpenShift Container Platform 集群相同的域公开 Knative 服务。
您可以禁用 OpenShift Container Platform 路由的 Operator 控制,以便您可以配置 Knative 路由来直接使用 TLS 证书。
Knative 路由也可以与 OpenShift Container Platform 路由一起使用,以提供额外的精细路由功能,如流量分割。
5.4.1. 为 OpenShift Container Platform 路由自定义标签和注解 复制链接链接已复制到粘贴板!
OpenShift Container Platform 路由支持使用自定义标签和注解,您可以通过修改 Knative 服务的元数据规格来配置这些标签和注解
。自定义标签和注解从服务传播到 Knative 路由,然后传播到 Knative ingress,最后传播到 OpenShift Container Platform 路由。
先决条件
- 您必须已在 OpenShift Container Platform 集群中安装了 OpenShift Serverless Operator 和 Knative Serving。
-
安装 OpenShift CLI (
oc
) 。
流程
创建包含您要传播到 OpenShift Container Platform 路由的标签或注解的 Knative 服务:
使用 YAML 创建服务:
使用 YAML 创建的服务示例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 要使用 Knative (
kn
) CLI 创建服务,请输入:使用
kn
命令创建的服务示例kn service create <service_name> \ --image=<image> \ --annotation <annotation_name>=<annotation_value> \ --label <label_value>=<label_value>
$ kn service create <service_name> \ --image=<image> \ --annotation <annotation_name>=<annotation_value> \ --label <label_value>=<label_value>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
通过检查以下命令的输出来验证 OpenShift Container Platform 路由是否已使用您添加的注解或标签创建:
验证命令示例
oc get routes.route.openshift.io \ -l serving.knative.openshift.io/ingressName=<service_name> \ -l serving.knative.openshift.io/ingressNamespace=<service_namespace> \ -n knative-serving-ingress -o yaml \ | grep -e "<label_name>: \"<label_value>\"" -e "<annotation_name>: <annotation_value>"
$ oc get routes.route.openshift.io \ -l serving.knative.openshift.io/ingressName=<service_name> \
1 -l serving.knative.openshift.io/ingressNamespace=<service_namespace> \
2 -n knative-serving-ingress -o yaml \ | grep -e "<label_name>: \"<label_value>\"" -e "<annotation_name>: <annotation_value>"
3 Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.4.2. 为 Knative 服务配置 OpenShift Container Platform 路由 复制链接链接已复制到粘贴板!
如果要将 Knative 服务配置为在 OpenShift Container Platform 上使用 TLS 证书,则必须禁用 OpenShift Serverless Operator 为服务自动创建路由,而是手动为服务创建路由。
完成以下步骤时,不会创建 knative-serving-ingress
命名空间中的默认 OpenShift Container Platform 路由。但是,应用程序的 Knative 路由仍然在此命名空间中创建。
先决条件
- OpenShift Serverless Operator 和 Knative Serving 组件必须安装在 OpenShift Container Platform 集群中。
-
安装 OpenShift CLI (
oc
) 。
流程
创建包含
service.knative.openshift.io/disableRoute=true
注解的 Knative 服务:重要service.knative.openshift.io/disableRoute=true
注解指示 OpenShift Serverless 不自动为您创建路由。但是,该服务仍然会显示 URL 并达到Ready
状态。除非使用与 URL 中主机名相同的主机名创建自己的路由,此 URL 才能在外部工作。创建 Knative
Service
资源:资源示例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 应用
Service
资源:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 可选。使用
kn service create
命令创建 Knative 服务:kn
命令示例kn service create <service_name> \ --image=gcr.io/knative-samples/helloworld-go \ --annotation serving.knative.openshift.io/disableRoute=true
$ kn service create <service_name> \ --image=gcr.io/knative-samples/helloworld-go \ --annotation serving.knative.openshift.io/disableRoute=true
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
验证没有为服务创建 OpenShift Container Platform 路由:
示例命令
$ oc get routes.route.openshift.io \ -l serving.knative.openshift.io/ingressName=$KSERVICE_NAME \ -l serving.knative.openshift.io/ingressNamespace=$KSERVICE_NAMESPACE \ -n knative-serving-ingress
$ $ oc get routes.route.openshift.io \ -l serving.knative.openshift.io/ingressName=$KSERVICE_NAME \ -l serving.knative.openshift.io/ingressNamespace=$KSERVICE_NAMESPACE \ -n knative-serving-ingress
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 您将看到以下输出:
No resources found in knative-serving-ingress namespace.
No resources found in knative-serving-ingress namespace.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 在
knative-serving-ingress
命名空间中创建Route
资源:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 应用
Route
资源:oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.4.3. 将集群可用性设置为集群本地 复制链接链接已复制到粘贴板!
默认情况下,Knative 服务会发布到一个公共 IP 地址。被发布到一个公共 IP 地址意味着 Knative 服务是公共应用程序,并有一个公开访问的 URL。
可以从集群以外访问公开的 URL。但是,开发人员可能需要构建后端服务,这些服务只能从集群内部访问(称为 私有服务 )。开发人员可以使用 networking.knative.dev/visibility=cluster-local
标签标记集群中的各个服务,使其私有。
对于 OpenShift Serverless 1.15.0 及更新的版本,service.knative.dev/visibility
标签不再可用。您必须更新现有服务来改用 networking.knative.dev/visibility
标签。
先决条件
- 在集群中安装了 OpenShift Serverless Operator 和 Knative Serving。
- 您已创建了 Knative 服务。
流程
通过添加
networking.knative.dev/visibility=cluster-local
标签来设置服务的可见性:oc label ksvc <service_name> networking.knative.dev/visibility=cluster-local
$ oc label ksvc <service_name> networking.knative.dev/visibility=cluster-local
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
验证
输入以下命令并查看输出结果,检查您的服务的 URL 是否现在格式为
http://<service_name>.<namespace>.svc.cluster.local
:oc get ksvc
$ oc get ksvc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 输出示例
NAME URL LATESTCREATED LATESTREADY READY REASON hello http://hello.default.svc.cluster.local hello-tx2g7 hello-tx2g7 True
NAME URL LATESTCREATED LATESTREADY READY REASON hello http://hello.default.svc.cluster.local hello-tx2g7 hello-tx2g7 True
Copy to Clipboard Copied! Toggle word wrap Toggle overflow