天天看點

OpenShift 4 - Knative-Tutorial(1) 建立Serverless運作環境說明用戶端環境配置Knative環境部署并測試Hello的Serverless應用

《OpenShift 4.x HOL教程彙總》

說明:本文已經在OpenShift 4.6環境中驗證

文章目錄

  • 說明
  • 用戶端環境
  • 配置Knative環境
    • 安裝OpenShift Serverless Operator
    • 用控制台建立Knative Serving應用運作環境
    • 用腳本建立Knative Serving應用運作環境
  • 部署并測試Hello的Serverless應用

說明

OpenShift Serverless Operator為無伺服器應用提供提供了Knative Serving和Knative Eventing的運作環境。隻需要以下步驟就可以在OpenShift上運作Knative應用:安裝所需的Operator和配置Knative Serving運作環境。

用戶端環境

從以下位址下載下傳對應環境的OpenShift Client和Knative Client,然後将oc和kn配置到PATH中。

  1. https://mirror.openshift.com/pub/openshift-v4/clients/oc/4.6/
  2. https://mirror.openshift.com/pub/openshift-v4/clients/serverless/latest/

執行指令下載下傳以下用戶端程式,它們會在後面操作中使用到。

$ curl -Lo stern https://github.com/wercker/stern/releases/download/1.9.0/stern_linux_amd64
$ curl -Lo hey https://hey-release.s3.us-east-2.amazonaws.com/hey_linux_amd64
$ curl -Lo yq https://github.com/mikefarah/yq/releases/download/3.2.1/yq_linux_amd64
$ chmod +x stern hey yq
$ sudo mv stern hey yq /usr/local/bin
           

配置Knative環境

我們可以用OpenShift控制台或OpenShift指令行建立Knative及其所需的運作環境。

安裝OpenShift Serverless Operator

注意:通過OpenShift Console安裝OpenShift Serverless Operator時候,OpenShift會自動安裝OpenShift Serverless Operator所依賴的Service Mesh Opeator,是以Operator的安裝過程非常簡單。

  1. 用叢集管理者登入OpenShift Console。
  2. 在Administrator視圖中進入Operators->OperatorHub,然後找到“Red Hat OpenShift Serverless”,點選進入。
  3. 在右滑視窗中接受所有預設配置,然後在“Install Operator”頁面中接受預設選項,再點選Install。。
  4. 完後上一步後,可以在Operators->Installed Operators中看到安裝好的Red Hat OpenShift Serverless。此時就完成了OpenShift Serverless Operator安裝。
    OpenShift 4 - Knative-Tutorial(1) 建立Serverless運作環境說明用戶端環境配置Knative環境部署并測試Hello的Serverless應用

用控制台建立Knative Serving應用運作環境

Knative Serving提供了Serverless應用的支撐運作環境。Knative Serving必須安裝在knative-serving項目中。

  1. 建立knative-serving項目。
$ oc new-project knative-serving
           
  1. 切換到knative-serving項目,再次進入Operators->Installed Operators,然後進入OpenShift Serverless的配置。
  2. 在Overview頁面中點選Knative Serving區域的Create Instance連結。
    OpenShift 4 - Knative-Tutorial(1) 建立Serverless運作環境說明用戶端環境配置Knative環境部署并測試Hello的Serverless應用
  3. 在Create Knative Serving界面中接受預設配置,然後點選Create。
  4. 然後進入名為knative-serving的Knative Serving配置。需要等一段時間,直到下圖中5個狀态全部為True,此時就成功建立了Knative Serving環境。
    OpenShift 4 - Knative-Tutorial(1) 建立Serverless運作環境說明用戶端環境配置Knative環境部署并測試Hello的Serverless應用

用腳本建立Knative Serving應用運作環境

  1. 建立knative-serving.yaml檔案,内容為以下部分:
apiVersion: v1
kind: Namespace
metadata:
  name: knative-serving
---
apiVersion: operator.knative.dev/v1alpha1
kind: KnativeServing
metadata:
  name: knative-serving
  namespace: knative-serving
           
  1. 執行指令建立項目和KnativeServing資源
$ oc apply -f knative-serving.yaml
           
  1. 檢視knative-serving的狀态,成功完成後顯示以下内容。
$ 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
VersionMigrationEligible =True
           

部署并測試Hello的Serverless應用

  1. 建立knative-demo項目。
$ oc new-project knative-demo
           
  1. 部署Knative的Hello應用。其中“–env TARGET=Knative”是将字元串傳給TARGET環境變量。
$ 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
           
  1. 檢視Knative的Service清單。
$ 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
           
4. 檢視knative-demo項目中的所有資源,其中包括Knative的configuration、service、revision、route對象,還有OpenShift的service、deployment、rs對象。**注意**:目前項目中還沒有pod資源。
           
$ 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
           
  1. 在OpenShift Console中進入knative-demo的Developer視圖,可以看到和hello應用相關的資源。
    OpenShift 4 - Knative-Tutorial(1) 建立Serverless運作環境說明用戶端環境配置Knative環境部署并測試Hello的Serverless應用
  2. 通路hello應用的位址,确認可以傳回“Hello Knative!”,其中“Knative”為傳進去的環境變量。
$  curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Knative!
           
  1. 此時立即執行以下指令,确認此時可以檢視項目在運作的Pod資源。不過在一分鐘以後會又看不到Pod了。
$ oc get pod
NAME                                          READY   STATUS    RESTARTS   AGE
pod/hello-rfgt4-1-deployment-a0cd0125-cfvny   2/2     Running   0          22s
           
  1. 更新名為hello的service的環境變量内容。
$ 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
           
  1. 通路hello應用的位址,确認可以傳回結果已經變化。
$  curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Kn!
           
  1. 檢視項目所有資源,确認OpenShift的deployment、rs對象和Knative的revision對象都已經有了一個新版的配置。
$ oc get all
NAME                                            READY   STATUS    RESTARTS   AGE
pod/hello-lnvkj-2-deployment-58fcc9d677-7kr9s   2/2     Running   0          48s
 
NAME                            TYPE           CLUSTER-IP       EXTERNAL-IP                                                  PORT(S)                             AGE
service/hello                   ExternalName   <none>           kourier-internal.knative-serving-ingress.svc.cluster.local   80/TCP                              10m
service/hello-lnvkj-2           ClusterIP      172.30.20.162    <none>                                                       80/TCP                              48s
service/hello-lnvkj-2-private   ClusterIP      172.30.234.104   <none>                                                       80/TCP,9090/TCP,9091/TCP,8022/TCP   48s
service/hello-rddql-1           ClusterIP      172.30.123.87    <none>                                                       80/TCP                              10m
service/hello-rddql-1-private   ClusterIP      172.30.5.1       <none>                                                       80/TCP,9090/TCP,9091/TCP,8022/TCP   10m
 
NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-lnvkj-2-deployment   1/1     1            1           48s
deployment.apps/hello-rddql-1-deployment   0/0     0            0           10m
 
NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-lnvkj-2-deployment-58fcc9d677   1         1         1       48s
replicaset.apps/hello-rddql-1-deployment-6479596766   0         0         0       10m

NAME                                      LATESTCREATED   LATESTREADY     READY   REASON
configuration.serving.knative.dev/hello   hello-lnvkj-2   hello-lnvkj-2   True
 
NAME                                         CONFIG NAME   K8S SERVICE NAME   GENERATION   READY   REASON
revision.serving.knative.dev/hello-lnvkj-2   hello         hello-lnvkj-2      2            True
revision.serving.knative.dev/hello-rddql-1   hello         hello-rddql-1      1            True
 
NAME                                URL                                                                                LATESTCREATED   LATESTREADY     READY   REASON
service.serving.knative.dev/hello   http://hello-knative-demo.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com   hello-lnvkj-2   hello-lnvkj-2   True
 
NAME                              URL                                                                                READY   REASON
route.serving.knative.dev/hello   http://hello-knative-demo.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com   True
           
  1. 檢視名為hello的service詳細資訊。确認最新的revision(hello-cfvny-2)接收了100%的請求。
$ kn service describe hello
Name:       hello
Namespace:  knative-demo
Age:        5h
URL:        http://hello-knative-demo.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com
 
Revisions:
  100%  @latest (hello-lnvkj-2) [2] (2m)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
 
Conditions:
  OK TYPE                   AGE REASON
  ++ Ready                   2m
  ++ ConfigurationsReady     2m
  ++ RoutesReady             2m
           
  1. 檢視Knative的revision對象。确認結果和(10)看到的項目包括的revision一樣。
$ kn revision list
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE     CONDITIONS   READY   REASON
hello-lnvkj-2   hello     100%             2            2m16s   3 OK / 4     True
hello-rddql-1   hello                      1            12m     3 OK / 4     True
           
  1. 執行指令,将請求平均發給以上2個revision。其中“hello-rddql-1”是GENERATION=1的revision名稱。
$ kn service update hello --traffic @latest=50 --traffic hello-rddql-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-lnvkj-2' (unchanged) and URL:
http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
           
  1. 檢視Knative的service對象,确認配置修改為2個revision各自接收50%的請求。
$ 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-lnvkj-2) [2] (12m)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
   50%  hello-rddql-1 [1] (21m)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
 
Conditions:
  OK TYPE                   AGE REASON
  ++ Ready                   6m
  ++ ConfigurationsReady    41m
  ++ RoutesReady             6m
           
  1. 在OpenShift控制台的Developer視圖中可以上可以看到hello應用的Toplogy。
    OpenShift 4 - Knative-Tutorial(1) 建立Serverless運作環境說明用戶端環境配置Knative環境部署并測試Hello的Serverless應用
  2. 點選上圖的Set Traffic Distribution按鈕,可以在彈出視窗中可以為不同的Revision配置設定流量比例。
    OpenShift 4 - Knative-Tutorial(1) 建立Serverless運作環境說明用戶端環境配置Knative環境部署并測試Hello的Serverless應用
  3. 多次通路hello應用,确認傳回結果部分是“Hello Knative!”、部分是“Hello Kn!”。
$ curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Knative!
$ curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Kn!