OpenShift 4.3 之Knative-Tutorial(2) 部署Hello应用并基于Revision/Tag/Traffic实现蓝绿部署和恢复发布

文章目录

  • 部署并测试Hello的Serverless应用
  • 蓝绿部署和恢复发布

部署并测试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
  1. 查看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.3 之Knative-Tutorial(2) 部署Hello应用并基于Revision/Tag/Traffic实现蓝绿部署和恢复发布_第1张图片
  2. 访问hello应用的地址,确认可以返回“Hello Knative!”,其中“Knative”为传进去的环境变量。
$ curl $(oc get ksvc hello | grep hello | awk '{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 $(oc get ksvc hello | grep hello | awk '{print $2}')
Hello Kn!
  1. 查看项目所有资源,确认OpenShift的deployment、rs对象和Knative的revision对象都已经有了一个新版的配置。
$ 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

蓝绿部署和恢复发布

  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-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
  1. 查看Knative的revision对象。确认结果和上面(10)看到的项目包括的revision一样。
$ 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
  1. 执行命令,通过指定–traffic参数,将请求按照revision名称平均发给以上2个revision。
$ 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
  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-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
  1. 在OpenShift控制台的Developer视图中可以看到名为hello的Knative Service的Topology。
    OpenShift 4.3 之Knative-Tutorial(2) 部署Hello应用并基于Revision/Tag/Traffic实现蓝绿部署和恢复发布_第2张图片
  2. 点击上图的Set Traffic Distribution按钮,可以在弹出窗口中可以为不同的Revision分配流量比例。
    OpenShift 4.3 之Knative-Tutorial(2) 部署Hello应用并基于Revision/Tag/Traffic实现蓝绿部署和恢复发布_第3张图片
  3. 多次访问hello应用,确认返回结果部分是“Hello Knative!”、部分是“Hello Kn!”。
$ curl $(oc get ksvc hello | grep hello | awk '{print $2}')
Hello Knative!
$ curl $(oc get ksvc hello -ojsonpath={.status.url})
Hello Kn!
  1. 除了像(13)那样,我们还可以为revision打tag,然后在指定–traffic参数的时候使用tag名称区分revision。执行以下命令,给hello-cfvny-2和hello-nvhqs-1打tag。
$ kn service update hello --tag hello-cfvny-2=latest,hello-nvhqs-1=stable
Updating Service 'hello' in namespace 'knative-demo':
 
  0.033s The Route is still working to reflect the latest desired specification.
  0.174s Ingress has not yet been reconciled.
  1.608s 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
  1. 查看hello的Knative Service信息,确认hello-cfvny-2和hello-nvhqs-1都有了tag。
$ kn service describe hello
Name:       hello
Namespace:  knative-demo
Age:        10h
URL:        http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
 
Revisions:
   50%  @latest (hello-cfvny-2) [2] (5h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
     +  hello-cfvny-2 (current @latest) #latest [2] (5h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
   50%  hello-nvhqs-1 #stable [1] (10h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
  1. 执行以下命令,再给2个revsion打tag。
$ kn service update hello --tag hello-cfvny-2=v2,hello-nvhqs-1=v1
Updating Service 'hello' in namespace 'knative-demo':
 
  0.158s The Route is still working to reflect the latest desired specification.
  0.326s Ingress has not yet been reconciled.
  1.500s 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
  1. 看hello的Knative Service信息,确认hello-cfvny-2和hello-nvhqs-1都多了新的tag。
$ kn service describe hello
Name:       hello
Namespace:  knative-demo
Age:        10h
URL:        http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
 
Revisions:
   50%  @latest (hello-cfvny-2) [2] (5h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
     +  hello-cfvny-2 (current @latest) #latest [2] (5h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
     +  hello-cfvny-2 (current @latest) #v2 [2] (5h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
   50%  hello-nvhqs-1 #stable [1] (10h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
     +  hello-nvhqs-1 #v1 [1] (10h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
  1. 根据revision的tag分配流量。
$ kn service update hello --traffic v1=10,v2=90
Updating Service 'hello' in namespace 'knative-demo':

  0.091s The Route is still working to reflect the latest desired specification.
  0.195s Ingress has not yet been reconciled.
  2.020s 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
  1. 查看hello的Knative Serivce的配置,确认流量分配。
$ kn service describe hello
Name:       hello
Namespace:  knative-demo
Age:        10h
URL:        http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
 
Revisions:
     +  hello-cfvny-2 (current @latest) #latest [2] (5h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
   90%  hello-cfvny-2 (current @latest) #v2 [2] (5h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
     +  hello-nvhqs-1 #stable [1] (10h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
   10%  hello-nvhqs-1 #v1 [1] (10h)
        Image:  gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
  1. 再次查看项目中的所有资源。我们可以从对应关系上看到每个Knative Revision对象对应一个OpenShift Deployment对象;每个实际的Knative Revision对应二个OpenShift Service对象(例如service/hello-cfvny-2和service/hello-cfvny-2-private);每个Knative Revision的Tag会对应一个OpenShift Service对象。
$ oc get all
NAME                            TYPE           CLUSTER-IP       EXTERNAL-IP                                                       PORT(S)                             AGE
service/hello                   ExternalName   <none>           cluster-local-gateway.knative-serving-ingress.svc.cluster.local   <none>                              29h
service/hello-cfvny-2           ClusterIP      172.30.69.131    <none>                                                            80/TCP                              24h
service/hello-cfvny-2-private   ClusterIP      172.30.64.141    <none>                                                            80/TCP,9090/TCP,9091/TCP,8022/TCP   24h
service/hello-nvhqs-1           ClusterIP      172.30.69.130    <none>                                                            80/TCP                              29h
service/hello-nvhqs-1-private   ClusterIP      172.30.125.146   <none>                                                            80/TCP,9090/TCP,9091/TCP,8022/TCP   29h
service/latest-hello            ExternalName   <none>           cluster-local-gateway.knative-serving-ingress.svc.cluster.local   <none>                              19h
service/stable-hello            ExternalName   <none>           cluster-local-gateway.knative-serving-ingress.svc.cluster.local   <none>                              19h
service/v1-hello                ExternalName   <none>           cluster-local-gateway.knative-serving-ingress.svc.cluster.local   <none>                              19h
service/v2-hello                ExternalName   <none>           cluster-local-gateway.knative-serving-ingress.svc.cluster.local   <none>                              19h
 
NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-cfvny-2-deployment   0/0     0            0           24h
deployment.apps/hello-nvhqs-1-deployment   0/0     0            0           29h
 
NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-cfvny-2-deployment-d4cd7655     0         0         0       24h
replicaset.apps/hello-nvhqs-1-deployment-66db57b95f   0         0         0       29h
 
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
 
NAME                                      LATESTCREATED   LATESTREADY     READY   REASON
configuration.serving.knative.dev/hello   hello-cfvny-2   hello-cfvny-2   True
  1. 先删除一个Knative Revision对象,然后再查看项目中的Revision对象列表,发现相同名称的Revsion又被创建出来,说明Revision对象是由OpenShift Deployment对象控制的。
$ kn revision delete hello-cfvny-2
$ kn revision list

你可能感兴趣的:(OpenShift,4,Knative)