OpenShift Serverless Operator为无服务器应用提供提供了Knative Serving的运行环境。需要以下两步就可以在OpenShift上运行Knative应用:安装所需的Operator和配置Knative Serving运行环境。
从以下地址下载对应环境的OpenShift Client和Knative Client,然后将oc和kn配置到PATH中。
我们可以用OpenShift控制台或OpenShift命令行创建Knative及其所需的运行环境。下面分别介绍这两种方法。
注意:通过OpenShift Console安装OpenShift Serverless Operator时候,OpenShift会自动安装OpenShift Serverless Operator所依赖的Service Mesh Opeator,因此Operator的安装过程非常简单。
Knative Serving提供了Serverless应用的支撑运行环境。Knative Serving必须运行在knative-serving项目中。
$ oc new-project knative-serving
apiVersion: v1
kind: Namespace
metadata:
name: knative-serving
---
apiVersion: operator.knative.dev/v1alpha1
kind: KnativeServing
metadata:
name: knative-serving
namespace: knative-serving
$oc apply -f knative-serving.yaml
$ oc get knativeserving.operator.knative.dev/knative-serving -n knative-serving --template='{{range .status.conditions}}{{printf "%s =%s\n" .type .status}}{{end}}'
DependenciesInstalled=True
DeploymentsAvailable=True
InstallSucceeded=True
Ready=True
使用脚本创建Serverless环境参见《OpenShift 4.3 通过脚本配置ServiceMesh和Serverless运行环境》。
$ oc new-project knative-demo
$ kn service create hello --image gcr.io/knative-samples/helloworld-go --env TARGET=Knative
Creating service 'hello' in namespace 'knative-demo':
0.296s The Route is still working to reflect the latest desired specification.
0.393s Configuration "hello" is waiting for a Revision to become ready.
33.138s ...
33.369s Ingress has not yet been reconciled.
33.466s Configuration "hello" is waiting for a Revision to become ready.
33.543s Ingress has not yet been reconciled.
74.629s Ready to serve.
Service 'hello' created with latest revision 'hello-nvhqs-1' and URL:
http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
$ kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com hello-nvhqs-1 2m4s 3 OK / 3 True
$ oc get all -n knative-demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello ExternalName <none> cluster-local-gateway.knative-serving-ingress.svc.cluster.local <none> 10m
service/hello-nvhqs-1 ClusterIP 172.30.69.130 <none> 80/TCP 10m
service/hello-nvhqs-1-private ClusterIP 172.30.125.146 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 10m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-nvhqs-1-deployment 0/0 0 0 10m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-nvhqs-1-deployment-66db57b95f 0 0 0 10m
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON
revision.serving.knative.dev/hello-nvhqs-1 hello hello-nvhqs-1 1 True
NAME LATESTCREATED LATESTREADY READY REASON
configuration.serving.knative.dev/hello hello-nvhqs-1 hello-nvhqs-1 True
NAME URL READY REASON
route.serving.knative.dev/hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com True
NAME URL LATESTCREATED LATESTREADY READY REASON
service.serving.knative.dev/hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com hello-nvhqs-1 hello-nvhqs-1 True
$ curl http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
Hello Knative!
$ oc get pod
NAME READY STATUS RESTARTS AGE
pod/hello-rfgt4-1-deployment-a0cd0125-cfvny 2/2 Running 0 22s
$ kn service update hello --env TARGET=Kn
Updating Service 'hello' in namespace 'knative-demo':
10.760s Traffic is not yet migrated to the latest revision.
10.871s Ingress has not yet been reconciled.
12.231s Ready to serve.
Service 'hello' updated with latest revision 'hello-cfvny-2' and URL:
http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
$ curl http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
Hello Kn!
$ oc get all
NAME READY STATUS RESTARTS AGE
pod/hello-cfvny-2-deployment-d4cd7655-rfgt4 2/2 Running 0 22s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello ExternalName <none> cluster-local-gateway.knative-serving-ingress.svc.cluster.local <none> 5h6m
service/hello-cfvny-2 ClusterIP 172.30.69.131 <none> 80/TCP 6m22s
service/hello-cfvny-2-private ClusterIP 172.30.64.141 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 6m22s
service/hello-nvhqs-1 ClusterIP 172.30.69.130 <none> 80/TCP 5h6m
service/hello-nvhqs-1-private ClusterIP 172.30.125.146 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 5h6m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-cfvny-2-deployment 1/1 1 1 6m22s
deployment.apps/hello-nvhqs-1-deployment 0/0 0 0 5h6m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-cfvny-2-deployment-d4cd7655 1 1 1 6m22s
replicaset.apps/hello-nvhqs-1-deployment-66db57b95f 0 0 0 5h6m
NAME LATESTCREATED LATESTREADY READY REASON
configuration.serving.knative.dev/hello hello-cfvny-2 hello-cfvny-2 True
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON
revision.serving.knative.dev/hello-cfvny-2 hello hello-cfvny-2 2 True
revision.serving.knative.dev/hello-nvhqs-1 hello hello-nvhqs-1 1 True
NAME URL LATESTCREATED LATESTREADY READY REASON
service.serving.knative.dev/hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com hello-cfvny-2 hello-cfvny-2 True
NAME URL READY REASON
route.serving.knative.dev/hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com True
$ kn service describe hello
Name: hello
Namespace: knative-demo
Age: 5h
URL: http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
Revisions:
100% @latest (hello-cfvny-2) [2] (2m)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Conditions:
OK TYPE AGE REASON
++ Ready 2m
++ ConfigurationsReady 2m
++ RoutesReady 2m
$ kn revision list
NAME SERVICE GENERATION AGE CONDITIONS READY REASON
hello-cfvny-2 hello 2 45m 3 OK / 4 True
hello-nvhqs-1 hello 1 5h46m 3 OK / 4 True
$ kn service update hello --traffic @latest=50 --traffic hello-nvhqs-1=50
Updating Service 'hello' in namespace 'knative-demo':
0.066s The Route is still working to reflect the latest desired specification.
0.090s Ingress has not yet been reconciled.
1.450s Ready to serve.
Service 'hello' updated with latest revision 'hello-cfvny-2' (unchanged) and URL:
http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
$ kn service describe hello
Name: hello
Namespace: knative-demo
Age: 5h
URL: http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
Revisions:
50% @latest (hello-cfvny-2) [2] (41m)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
50% hello-nvhqs-1 [1] (5h)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Conditions:
OK TYPE AGE REASON
++ Ready 6m
++ ConfigurationsReady 41m
++ RoutesReady 6m
$ curl http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
Hello Knative!
$ curl http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
Hello Kn!