七叶笔记 » golang编程 » OpenShift 4 之Knative(2) – 第一个Serverless应用

OpenShift 4 之Knative(2) – 第一个Serverless应用

在完成了《OpenShift 4 之Knative(1) – 创建Knative无服务器架构环境)》后,我们开始部署一个基于Knative的Serverless应用。本文先部署一个Serverless应用。作为对比,然后部署一个非Serverless应用,最后对比两个应用的运行效果。

部署非Serverless应用(dumpy)

1. 创建项目和权限

 $ oc new-project kn-demo
$ oc create serviceaccount pipeline
$ oc adm policy add-scc-to-user privileged -z pipeline
$ oc adm policy add-role-to-user edit -z pipeline  

2. 允许在tekton中使用openshift-client

 $ oc create -f   

3. 为Tekton创建可以使用go S2I的Task

 $ oc create -f   

创建tekton的Pipeline和调用Rource

 $ oc create -f 
$ oc create -f 
$ oc create -f 
$ tkn tasks list
NAME               AGE
openshift-client   9 minutes ago
$ tkn pipeline list
NAME              AGE             LAST RUN   STARTED   DURATION   STATUS
deploy-pipeline   3 minutes ago   ---        ---       ---        ---
$ tkn resource list
NAME                TYPE    DETAILS
sourcecode-git      git     url: 
applicat io n-image   image   url: image-registry.openshift-image-registry.svc:5000/kn-demo/dumpy  

4. 启动运行Tekton Pipeline

 $ tkn pipeline start deploy-pipeline -s pipeline -r app-git=sourcecode- git  -r app-image=application-image
Pipelinerun started: deploy-pipeline-run-m65rw
Showing logs...
[build : create-dir-image-wj45n] {"level":"warn","ts":1576916492.4300456,"logger":"fallback-logger","caller":"logging/ config .go:69"," msg ":"Fetch GitHub commit ID from kodata failed: \"KO_DATA_PATH\" does not exist or is empty"}
[build : create-dir-image-wj45n] {"level":"info","ts":1576916492.4334698,"logger":"fallback-logger","caller":"bash/main.go:64","msg":"Successfully executed command \"sh -c mkdir -p /workspace/ output /image\"; output "}
[build : git-source-sourcecode-git-h5kt6] {"level":"warn","ts":1576916492.9045074,"logger":"fallback-logger","caller":"logging/config.go:69","msg":"Fetch GitHub commit ID from kodata failed: \"KO_DATA_PATH\" does not exist or is empty"}
[build : git-source-sourcecode-git-h5kt6] {"level":"info","ts":1576916496.1496713,"logger":"fallback-logger","caller":"git/git.go:103","msg":"Successfully cloned  @ master in path /workspace/source"}
[build : generate] Application dockerfile generated in /gen-source/Dockerfile.gen
[build : build] STEP 1: FROM registry.access.redhat.com/devtools/go-toolset-rhel7
。。。
[build : build] STEP 2: LABEL "io.openshift.s2i.build.image"="registry.access.redhat.com/devtools/go-toolset-rhel7"       "io.openshift.s2i.build.source-location"="."
。。。
[build : build] STEP 3: USER root
。。。
[build : build] STEP 4: COPY upload/src /tmp/src
。。。
[build : build] STEP 5: RUN chown -R 1001:0 /tmp/src
。。。
[build : build] STEP 6: USER 1001
。。。
[build : build] STEP 7: RUN /usr/libexec/s2i/assemble
。。。
[build : build] STEP 8: CMD /usr/libexec/s2i/run
[build : build] STEP 9: COMMIT image-registry.openshift-image-registry.svc:5000/kn-demo/dumpy
。。。
[build : image-digest-exporter-vk4tr] {"level":"warn","ts":1576916688.3277884,"logger":"fallback-logger","caller":"logging/config.go:69","msg":"Fetch GitHub commit ID from kodata failed: \"KO_DATA_PATH\" does not exist or is empty"}
[build : image-digest-exporter-vk4tr] {"level":"info","ts":1576916688.3279572,"logger":"fallback-logger","caller":"imagedigestexporter/main.go:58","msg":"ImageResource application-image doesn't have an index.json file: stat /builder/home/image-outputs/image/index.json: no such file or directory"}
[deploy : oc] deploymentconfig.apps.openshift.io/dumpy rolled out  

5. 也可以用一下方式查看Tekton Pipeline的日志。

 $ tkn pipeline list
NAME              AGE              LAST RUN                    STARTED          DURATION    STATUS
deploy-pipeline   19 minutes ago   deploy-pipeline-run-m65rw   15 minutes ago   7 minutes   Succeeded
$ tkn pipelinerun logs -f deploy-pipeline-run-m65rw  

6. Tekton Pipeline完成后可以在OpenShift Console的Developer视图中看到以下Pipelines中的deploy-pipeline运行状态。

7. 访问dumpy应用的Route地址,验证应用是可以访问的。

 $ oc get route
NAME    HOST/PORT                        PATH   SERVICES   PORT       TERMINATION   WILDCARD
dumpy   dumpy-kn-demo.apps-crc.testing          dumpy      8080-tcp                 None
$ curl 
                    888 888             888
                    888 888             888
                    888 888             888
888d888 .d88b.  .d88888 88888b.  8888b. 888888
888P"  d8P  Y8bd88" 888 888 "88b    "88b888
888    88888888888  888 888  888.d888888888
888    Y8b.    Y88b 888 888  888888  888Y88b.
888     "Y8888  "Y88888 888  888"Y888888 "Y888  

部署Knative Serverless应用

1. 基于上一步生成的dumpy的ImageStream创建名为dumpy-serverless的Tekton的Service。

 $ oc describe is dumpy
Name:dumpy
Namespace:kn-demo
Created:2 hours ago
Labels:app=dump
Annotations:<none>

Image Repository:default-route-openshift-image-registry.apps-crc.testing/kn-demo/dumpy

Image Lookup:local=false

Unique Images:1

Tags:1

latest
  no spec tag
 . image-registry.openshift-image-registry.svc:5000/kn-demo/dumpy@sha256:9c5b1dc90f9b9fc54b1dec625251340d8b774e15bcad6664869c3db8529314f0
$ kn service create dumpy-serverless --image=image-registry.openshift-image-registry.svc:5000/kn-demo/dumpy -l app.kubernetes.io/name=golang
Creating service 'dumpy-serverless' in namespace 'kn-demo':
  0.136s The Route is still working to reflect the latest desired specification.
  0.161s Configuration "dumpy-serverless" is waiting for a Revision to become ready.
 28.368s ...
 28.551s Ingress has not yet been reconciled.
 69.596s Ready to serve.  

2. 用OpenShift Console的Administrator或命令行查看Knative相关资源的状态。

其中“kn service list”命令等同“oc get ksvc”命令;”kn revision list”命令等同“oc get revision”命令;“kn route list”命令等同

 $ kn service list
NAME               URL                                                LATEST                     AGE   CONDITIONS   READY   REASON
dumpy-serverless      dumpy-serverless-tbcvz-1   79m   3 OK / 3     True    
$ oc get ksvc -n kn-demo
NAME               URL                                                LATESTCREATED              LATESTREADY                READY   REASON
dumpy-serverless      dumpy-serverless-tbcvz-1   dumpy-serverless-tbcvz-1   True    
$ kn revision list 
NAME                       SERVICE            GENERATION   AGE   CONDITIONS   READY   REASON
dumpy-serverless-tbcvz-1   dumpy-serverless   1            79m   3 OK / 4     True    
$ oc get revision -n kn-demo
NAME                       CONFIG NAME        K8S SERVICE NAME           GENERATION   READY   REASON
dumpy-serverless-tbcvz-1   dumpy-serverless   dumpy-serverless-tbcvz-1   1            True    
$ kn route list
NAME               URL                                                READY
dumpy-serverless      True
$ oc get route.serving.knative.dev -n kn-demo
NAME               URL                                                READY   REASON
dumpy-serverless      True  

3. 访问dumpy-serverless应用的Route地址,验证应用是可以访问的。

 $ kn route list
NAME               URL                                                READY
dumpy-serverless      True
$ curl 
                    888 888             888
                    888 888             888
                    888 888             888
888d888 .d88b.  .d88888 88888b.  8888b. 888888
888P"  d8P  Y8bd88" 888 888 "88b    "88b888
888    88888888888  888 888  888.d888888888
888    Y8b.    Y88b 888 888  888888  888Y88b.
888     "Y8888  "Y88888 888  888"Y888888 "Y888  

# 对比dumpy与dumpy-serverless

1. 将运行dumpy应用的 Pod 减到“0”个:

 $ oc idle dumpy
WARNING: idling when network policies are in place may cause connections to bypass network policy entirely
The service "kn-demo/dumpy" has been marked as idled
The service will unidle DeploymentConfig "kn-demo/dumpy" to 1 replicas once it receives traffic
DeploymentConfig "kn-demo/dumpy" has been idled  

2. 此时查看运行dumpy应用与dumpy-serverless应用的Pod情况。注意2个应用都没有对应的Pod,其中dumpy是手工减到“0”的,而dumpy-serverless是Knative自动减到”0″的。

 $ oc get pod -w
NAME                                                READY   STATUS      RESTARTS   AGE
deploy-pipeline-run-m65rw-build-zc926-pod-4c22ce    0/6     Completed   0          70m
deploy-pipeline-run-m65rw-deploy-wtz72-pod-6bf2ef   0/1     Completed   0          63m
dumpy-1-deploy                                      0/1     Completed   0          63m  

3. 此时在OpenShift Console的Developer视图的Toplogy看到的是下图状态:

4. 执行命令分别访问dumpy应用与dumpy-serverless应用。

 ```bash
$ curl 
                    888 888             888
                    888 888             888
                    888 888             888
888d888 .d88b.  .d88888 88888b.  8888b. 888888
888P"  d8P  Y8bd88" 888 888 "88b    "88b888
888    88888888888  888 888  888.d888888888
888    Y8b.    Y88b 888 888  888888  888Y88b.
888     "Y8888  "Y88888 888  888"Y888888 "Y888
$ curl 
                    888 888             888
                    888 888             888
                    888 888             888
888d888 .d88b.  .d88888 88888b.  8888b. 888888
888P"  d8P  Y8bd88" 888 888 "88b    "88b888
888    88888888888  888 888  888.d888888888
888    Y8b.    Y88b 888 888  888888  888Y88b.
888     "Y8888  "Y88888 888  888"Y888888 "Y888
```  

5. 用(2)持续监控Pod的情况,其中运行dumpy应用的Pod在增加到”1″后不会自动减到“0”,而dumpy-servless的应用在响应用户请求后会自动减到“0”。此时在OpenShift Console上确认dumpy-servless应用的状态如下图。

相关文章