OpenShift 4.3 之Knative-Tutorial(3) 了解Knative Serving的核心对象

文章目录

  • Knative Serving的核心对象
  • 创建Knative Serving应用
    • 通过Configuration和Route实现蓝绿部署
    • 通过Service部署应用

Knative Serving的核心对象

一个 Knative 应用主要包括四类对象:Service(不同于Kubernetes的Service)、Route(类似但不是OpenShift的Route)、Configuration、Revision。
OpenShift 4.3 之Knative-Tutorial(3) 了解Knative Serving的核心对象_第1张图片

  1. Service(不同于Kubernetes的Service):它是一个集合,可以在Service中通过Template定义Knative的Route和Configuration对象。
  2. Configuration(类似OpenShift的DeploymentConfig): 创建并记录所有Revision的情况。用kn命令更新Service的时候实际就是在更新Configuration,而每次更新Configuration都会生成一个新的Revision。
  3. Revision:是一个特定的应用和配置环境的快照。该对象一旦生成就不可更改了(Immutable)。一个Revision对象对应一个Kubernetes Deployment对象。由于每次更新Configuration对象都会生成一个新的Revision,因此要不然每次都指定一个不同的Revision名称,要不就让Knative自动生成一个Revision名称。
  4. Route(类似但不是OpenShift的Route对象):Knative的Route是运行在也是用来做请求分发路由的,它是用Istio VirtualRoute实现,它定义了请求如何通过Traffic策略发给指定的Revision。蓝绿部署和金丝雀发布都是通过Route实现的。

无服务器应用的特点就是当没有外部请求的时候服务器是不需要运行Pod,只有在有请求的时候在启动Pod运行应用处理请求。在Knative中,这种按需的运行应用的效果是通过Activator和Autoscaler实现的。我们可以在OpenShift中的knative-serving项目中看到相关的支撑环境。
OpenShift 4.3 之Knative-Tutorial(3) 了解Knative Serving的核心对象_第2张图片

  1. 当首次通过Route访问Knative应用(假设此时还没有运行应用的Pod)的时候,请求将会先被Route发给Activator,Activator会激活目标Revision环境,Revision在通过Kubernetes Deployment启动Pod运行应用。这个过程完成了从无服务器到有服务器的过程,当有服务器运行Pod后就可以处理请求了。
  2. 当已经有运行应用的Pod的情况下,Route会直接将请求发给和Revsion对应的Pod。
  3. Autoscaler会监控Revision中的Pod运行其情况并收集Metrics数据,然后根据策略决定是扩展还是收缩Pod的数量。而扩展或收缩的动作是Revision通知Kubernetes的Deployment完成的。

创建Knative Serving应用

一个Knative Serving应用包括Service、Route、Configuration、Revision对象。他们的关系是Service=Route+Configuration,因此可以通过分别操作Route和Configuration或操作Service实现对Knative Serving应用的配置。

通过Configuration和Route实现蓝绿部署

  1. 创建新项目。
$ oc new-project kn-blue-green
  1. 创建内容如下的blue-config.yaml文件,其中定义了名为blue-green-demo的Configuration对象,该对象使用的镜像是“gcr.io/knative-samples/knative-route-demo:blue”。
apiVersion: serving.knative.dev/v1
kind: Configuration
metadata:
  name: blue-green-demo
spec:
  template:
    spec:
      containers:
        - image: gcr.io/knative-samples/knative-route-demo:blue 
          env:
            - name: T_VERSION
              value: "blue"
  1. 根据blue-config.yaml创建名为blue-green-demo的Configuration。
$ oc apply -f blue-config.yaml
configuration "blue-green-demo" configured
  1. 查看项目中的对象,可以看到伴随Configuration还生成了Revision、Deployment、Replicset和Pod。其中Revision名为自动生成的blue-green-demo-jvtg7。
$ oc get all
NAME                                                   READY   STATUS    RESTARTS   AGE
pod/blue-green-demo-jvtg7-deployment-8cb94b4d7-qhvg7   2/2     Running   0          44s
 
NAME                                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/blue-green-demo-jvtg7-deployment   1/1     1            1           44s
 
NAME                                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/blue-green-demo-jvtg7-deployment-8cb94b4d7   1         1         1       44s
 
NAME                                                 CONFIG NAME       K8S SERVICE NAME   GENERATION   READY     REASON
revision.serving.knative.dev/blue-green-demo-jvtg7   blue-green-demo                      1            Unknown   Deploying
 
NAME                                                LATESTCREATED           LATESTREADY   READY     REASON
configuration.serving.knative.dev/blue-green-demo   blue-green-demo-jvtg7                 Unknown
  1. 创建以下内容的blue-route.yaml文件,其中定义了Route对象。注意:需要将文件中的"revisionName"名称中的“blue-green-demo-XXX”替换为上一步创建的名为“blue-green-demo-jvtg7”的Revision。
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
  name: blue-green-demo # The name of our route; appears in the URL to access the app
spec:
  traffic:
    - revisionName: blue-green-demo-XXX #  replace XXX with your RevisionName
      percent: 100 # All traffic goes to this revision
  1. 执行blue-route.yaml文件创建Route对象。
$ oc apply -f blue-route.yaml
route "blue-green-demo" configured
  1. 访问通过blue-green-demo的Route访问应用。确认返回页面中有“
    App v1
    ”。
$ curl $(kn route list blue-green-demo | grep blue-green-demo | awk '{print $2}')
<!DOCTYPE html>                                                  
<html lang="en">                                                 
<head>                                                           
    <title>Knative Routing Demo</title>                          
    <link rel="stylesheet" type="text/css" href="/css/app.css" />
</head>                                                          
<body>                                                           
            <div class="blue">App v1</div>                                                                                       
    </div>                                                       
</body>                                                          
  1. 创建内容如下的green-config.yaml文件,其中定义了名为blue-green-demo的Configuration对象,该对象使用的镜像是“gcr.io/knative-samples/knative-route-demo:green”。
apiVersion: serving.knative.dev/v1
kind: Configuration
metadata:
  name: blue-green-demo # Configuration name is unchanged, since we're updating an existing Configuration
spec:
  template:
    spec:
      containers:
        - image: gcr.io/knative-samples/knative-route-demo:green # URL to the new version of the sample app docker image
          env:
            - name: T_VERSION
              value: "green" # Updated value for the T_VERSION environment variable
  1. 执行green-config.yaml文件,更新名为blue-green-demo的Configuration。
$ oc apply -f green-config.yaml
configuration.serving.knative.dev/blue-green-demo configured
  1. 查看项目中对象的变化情况。
$ oc get all
NAME                                    TYPE           CLUSTER-IP       EXTERNAL-IP                                                       PORT(S)                             AGE
service/blue-green-demo                 ExternalName   <none>           cluster-local-gateway.knative-serving-ingress.svc.cluster.local   <none>                              43m
service/blue-green-demo-d44kj           ClusterIP      172.30.202.156   <none>                                                            80/TCP                              32m
service/blue-green-demo-d44kj-private   ClusterIP      172.30.114.50    <none>                                                            80/TCP,9090/TCP,9091/TCP,8022/TCP   32m
service/blue-green-demo-jvtg7           ClusterIP      172.30.254.229   <none>                                                            80/TCP                              50m
service/blue-green-demo-jvtg7-private   ClusterIP      172.30.214.59    <none>                                                            80/TCP,9090/TCP,9091/TCP,8022/TCP   50m
 
NAME                                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/blue-green-demo-d44kj-deployment   0/0     0            0           32m
deployment.apps/blue-green-demo-jvtg7-deployment   0/0     0            0           50m
 
NAME                                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/blue-green-demo-d44kj-deployment-b8fb879f6    0         0         0       32m
replicaset.apps/blue-green-demo-jvtg7-deployment-5bf694ccf9   0         0         0       50m
 
NAME                                        URL                                                                                                READY     REASON
route.serving.knative.dev/blue-green-demo   http://blue-green-demo.knative-demo.apps.cluster-shanghai-c30f.shanghai-c30f.example.opentlc.com   Unknown   IngressNotConfigured
 
NAME                                                 CONFIG NAME       K8S SERVICE NAME        GENERATION   READY   REASON
revision.serving.knative.dev/blue-green-demo-d44kj   blue-green-demo   blue-green-demo-d44kj   2            True
revision.serving.knative.dev/blue-green-demo-jvtg7   blue-green-demo   blue-green-demo-jvtg7   1            True
 
NAME                                                LATESTCREATED           LATESTREADY             READY   REASON
configuration.serving.knative.dev/blue-green-demo   blue-green-demo-d44kj   blue-green-demo-d44kj   True
  1. 创建以下内容的green-route.yaml文件,其中更新了Route对象。注意:需要将文件中的2个"revisionName"名称中的“blue-green-demo-XXX”和““blue-green-demo-XXX””替换为上一步创建的名为“blue-green-demo-jvtg7”和“blue-green-demo-d44kj”的Revision。
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
  name: blue-green-demo # Route name is unchanged, since we're updating an existing Route
spec:
  traffic:
    - revisionName: blue-green-demo-XXX
      percent: 0 
    - revisionName: blue-green-demo-YYY
      percent: 100 
      tag: v2 # A named route
  1. 执行blue-green-route.yaml文件,更新名为blue-green-demo的Route。
$ oc apply -f blue-green-route.yaml
configuration.serving.knative.dev/blue-green-demo configured
  1. 访问通过blue-green-demo的Route访问应用。确认返回页面中有“
    App v2
    ”。
$ curl $(kn route list blue-green-demo | grep blue-green-demo | awk '{print $2}')
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Knative Routing Demo</title>
    <link rel="stylesheet" type="text/css" href="/css/app.css" />
</head>
<body>
            <div class="green">App v2</div>
    </div>
</body>
  1. 还可进一步编辑blue-green-route.yaml文件中的Traffic比例,从而从green回退到blue版本,同时还可实现金丝雀发布。

通过Service部署应用

  1. 创建定义Service的greeter-service.yaml文件,为以下内容。
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: greeter
spec:
  template:
    metadata:
      name: greeter-v1
    spec:
      containers:
      - image: quay.io/rhdevelopers/knative-tutorial-greeter:quarkus
  1. 执行命令创建Service对象。
$ oc apply -f greeter-service.yaml
service.serving.knative.dev/greeter created
  1. 确认名为greeter的Knative Route已经是READY为True的状态。
$ kn route list greeter
NAME      URL                                                                                        READY
greeter   http://greeter.knative-demo.apps.cluster-shanghai-c30f.shanghai-c30f.example.opentlc.com   True
  1. 通过Knative Route访问greeter应用。
$ curl $(kn route list greeter | grep greeter | awk '{print $2}')
Hi  greeter => '9861675f8845' : 1

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