首页 > 其他分享 >Serverless平台knative第八章流量管理

Serverless平台knative第八章流量管理

时间:2023-10-06 23:32:19浏览次数:40  
标签:Serverless OK default 第八章 172 demoapp knative True spec

准备环境

创建一个服务两个版本

[root@ip-172-17-11-227 ~]# cat blue.yaml 
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: demoapp
spec:
  template:
    metadata:
      name: demoapp-01
    spec:
      containers:
        #- image: gcr.io/knative-samples/helloworld-go
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "blue"
[root@ip-172-17-11-227 ~]# cat green.yaml 
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: demoapp
spec:
  template:
    metadata:
      name: demoapp-02
    spec:
      containers:
        #- image: gcr.io/knative-samples/helloworld-go
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "green"
[root@ip-172-17-11-227 ~]# kubectl apply -f blue.yaml -f green.yaml 
Warning: Kubernetes default value is insecure, Knative may default this to secure in a future release: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation, spec.template.spec.containers[0].securityContext.capabilities, spec.template.spec.containers[0].securityContext.runAsNonRoot, spec.template.spec.containers[0].securityContext.seccompProfile
service.serving.knative.dev/demoapp created
service.serving.knative.dev/demoapp configured

可以看到流量都在最新版本

[root@ip-172-17-11-227 ~]# kn service list
NAME      URL                            LATEST       AGE   CONDITIONS   READY   REASON
demoapp   http://demoapp.yht.com   demoapp-02   7s    3 OK / 3     True    
[root@ip-172-17-11-227 ~]# kn revision list
NAME         SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-02   demoapp   100%             2            35s   4 OK / 4     True    
demoapp-01   demoapp                    1            35s   4 OK / 4     True   

通过命令行管理流量

[root@ip-172-17-11-227 ~]# kn service update demoapp --traffic demoapp-02=50 --traffic demoapp-01=50
Warning: Kubernetes default value is insecure, Knative may default this to secure in a future release: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation, spec.template.spec.containers[0].securityContext.capabilities, spec.template.spec.containers[0].securityContext.runAsNonRoot, spec.template.spec.containers[0].securityContext.seccompProfile
Updating Service 'demoapp' in namespace 'default':

  0.060s The Route is still working to reflect the latest desired specification.
  0.109s Ingress has not yet been reconciled.
  0.159s Waiting for load balancer to be ready
  0.378s Ready to serve.

Service 'demoapp' with latest revision 'demoapp-02' (unchanged) is available at URL:
http://demoapp.yht.com

验证流量

[root@ip-172-17-11-227 ~]# kn revision list
NAME         SERVICE   TRAFFIC   TAGS   GENERATION   AGE     CONDITIONS   READY   REASON
demoapp-02   demoapp   50%              2            4m23s   3 OK / 4     True    
demoapp-01   demoapp   50%              1            4m23s   3 OK / 4     True    
[root@ip-172-17-11-227 ~]# curl https://demoapp.yht.com 
Hello blue!
[root@ip-172-17-11-227 ~]# curl https://demoapp.yht.com
Hello green!
[root@ip-172-17-11-227 ~]# curl https://demoapp.yht.com
Hello blue!
[root@ip-172-17-11-227 ~]# 

通过资源清单管理流量

创建新版本并配置流量

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: demoapp
spec:
  template:
    #metadata:
    #  name: demoapp-01 # 不配置name则会通过自动配置成demoapp-00003
    spec:
      containers:
        #- image: gcr.io/knative-samples/helloworld-go
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "red"
  traffic:
  - latestRevision: true
    percent: 0
  - revisionName: demoapp-02
    percent: 70
  - revisionName: demoapp-01
    percent: 30

验证流量

[root@ip-172-17-11-227 ~]# kubectl apply -f  c.yaml    
Warning: Kubernetes default value is insecure, Knative may default this to secure in a future release: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation, spec.template.spec.containers[0].securityContext.capabilities, spec.template.spec.containers[0].securityContext.runAsNonRoot, spec.template.spec.containers[0].securityContext.seccompProfile
service.serving.knative.dev/demoapp configured
[root@ip-172-17-11-227 ~]# kn revision list                   
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-00003   demoapp                    3            6s    4 OK / 4     True    
demoapp-02      demoapp   70%              2            14m   3 OK / 4     True    
demoapp-01      demoapp   30%              1            14m   3 OK / 4     True    
[root@ip-172-17-11-227 ~]# 

更新KService时触发的操作

◆更新spec.template部分,将创建一个新的revision

⚫ 一个KService下可能同时存在多个revision,其中有一个是为Latest Revision

⚫ 默认情况下,Latest Revision接收该Service收到的全部请求

⚫ 也可以为不同的Revision指定不同的流量比例

◆更新spec.traffic部分,其Route将被修改

每创建一个service,service名字不需要改变,因为都是同一个服务,但是template.metadata.name每一次更新都需要更改名字,每一次变更都会根据这个名字创建一个新的Revision

注意: 只template部分字段更新了,才会创建一个新的revision出来

traffic 字段

  traffic字段是列表型数据,每个列表项代表一个路由配置;

  percent:该路由项切分到的流量比例

  lastestRevision: 显示指定最新版本的revision,与revisionName字段互斥

  configuationName:  流量的目标configuration,实际接收流量的为其最新版的revision

  revisionName: 流量的目标revision

通过标签访问指定revision

路由标签能够为特定的路由项创建基于tag的目标地址

附带的tag的路由项指向的Revision,可通过<tag-name>-<route-name>.<namespace>.<domain>的格式访问

无tag的路由项,仅可通过<route-name>.<namespace>.<domain>的格式访问

kn service update demoapp --tag demoapp-02=green

查看vs看到多了一个访问入口

[root@ip-172-17-11-227 ~]# kubectl get vs 
NAME                            GATEWAYS                                                                 HOSTS                                                                                                                                                                                                                               AGE
demoapp-ingress                 ["default/demoapp-3797421420","knative-serving/knative-local-gateway"]   ["demoapp.default","demoapp.default.svc","demoapp.default.svc.cluster.local","demoapp.yht.com","green-demoapp.default","green-demoapp.default.svc","green-demoapp.default.svc.cluster.local","green-demoapp.yht.com"]   36m
demoapp-mesh                    ["mesh"]                                                                 ["demoapp.default","demoapp.default.svc","demoapp.default.svc.cluster.local","green-demoapp.default","green-demoapp.default.svc","green-demoapp.default.svc.cluster.local"]                                                         36m
demoapp.yht.com-ingress   ["default/1538132039-3797421420","default/wildcard-53c2106b"]            ["demoapp.yht.com"]                                                                                                                                                                                                           4h1m
[root@ip-172-17-11-227 ~]# 

验证访问

随机进入一个pod里面访问


sh-4.2# curl green-demoapp.default.svc.cluster.local 
Hello green!
sh-4.2# curl green-demoapp.default.svc.cluster.local
Hello green!
sh-4.2# curl green-demoapp.default.svc.cluster.local
Hello green!

删除标签

[root@ip-172-17-11-227 ~]# kn service update demoapp untag green
Error: flag(s) not set
Usage: update NAME
Run 'kn --help' for usage
[root@ip-172-17-11-227 ~]# kn service update demoapp --untag green
Warning: Kubernetes default value is insecure, Knative may default this to secure in a future release: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation, spec.template.spec.containers[0].securityContext.capabilities, spec.template.spec.containers[0].securityContext.runAsNonRoot, spec.template.spec.containers[0].securityContext.seccompProfile
Updating Service 'demoapp' in namespace 'default':

  0.034s The Route is still working to reflect the latest desired specification.
  0.083s Ingress has not yet been reconciled.
  0.152s Waiting for load balancer to be ready
  0.343s Ready to serve.

Service 'demoapp' with latest revision 'demoapp-00003' (unchanged) is available at URL:
http://demoapp.yht.com
[root@ip-172-17-11-227 ~]# 

通过资源清单创建标签

定义了一个yellow标签

[root@ip-172-17-11-227 ~]# cat c.yaml 
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: demoapp
spec:
  template:
    spec:
      containers:
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "yellow"  
  traffic:
  - latestRevision: true
    percent: 20
    tag: yellow
  - revisionName: demoapp-00003
    percent: 10
  - revisionName: demoapp-02
    percent: 40
  - revisionName: demoapp-01
    percent: 30

查看验证

[root@ip-172-17-11-227 ~]# kubectl get vs
NAME                            GATEWAYS                                                                 HOSTS                                                                                                                                                                                                                                   AGE
demoapp-ingress                 ["default/demoapp-3797421420","knative-serving/knative-local-gateway"]   ["demoapp.default","demoapp.default.svc","demoapp.default.svc.cluster.local","demoapp.yht.com","yellow-demoapp.default","yellow-demoapp.default.svc","yellow-demoapp.default.svc.cluster.local","yellow-demoapp.yht.com"]   49m
demoapp-mesh                    ["mesh"]                                                                 ["demoapp.default","demoapp.default.svc","demoapp.default.svc.cluster.local","yellow-demoapp.default","yellow-demoapp.default.svc","yellow-demoapp.default.svc.cluster.local"]                                                          49m
demoapp.yht.com-ingress   ["default/1538132039-3797421420","default/wildcard-53c2106b"]            ["demoapp.yht.com"]                                                                                                                                                                                                               4h14m
[root@ip-172-17-11-227 ~]# kn revision list
NAME            SERVICE   TRAFFIC   TAGS     GENERATION   AGE    CONDITIONS   READY   REASON
demoapp-00004   demoapp   20%       yellow   4            5m5s   4 OK / 4     True    
demoapp-00003   demoapp   10%                3            35m    3 OK / 4     True    
demoapp-02      demoapp   40%                2            49m    3 OK / 4     True    
demoapp-01      demoapp   30%                1            49m    3 OK / 4     True    
sh-4.2# curl yellow-demoapp.default.svc.cluster.local
Hello yellow!
sh-4.2# curl yellow-demoapp.default.svc.cluster.local
Hello yellow!
sh-4.2# curl yellow-demoapp.default.svc.cluster.local
Hello yellow!

配置流量逐步迁移

在KService或Route上使用“serving.knative.dev/rollout-duration”注解来指定流量迁移过程的时长

配置此项目的因为之前切换流量这可能会导致QP或Activator的请求队列过长,以至于部分请求可能会被拒绝

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: demoapp
  annotations:
    serving.knative.dev/rollout-duration: "30s"
spec:
  template:
    metadata:
      name: demoapp-05
    spec:
      containers:
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "rollout-deuration"

可以看到最后流量在30s之内全部迁移完成

[root@ip-172-17-11-227 ~]# kubectl apply -f c.yaml 
Warning: Kubernetes default value is insecure, Knative may default this to secure in a future release: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation, spec.template.spec.containers[0].securityContext.capabilities, spec.template.spec.containers[0].securityContext.runAsNonRoot, spec.template.spec.containers[0].securityContext.seccompProfile
service.serving.knative.dev/demoapp configured
[root@ip-172-17-11-227 ~]# kn revision list
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-05      demoapp   16%              5            8s    4 OK / 4     True    
demoapp-00004   demoapp   84%              4            12m   3 OK / 4     True    
demoapp-00003   demoapp                    3            43m   3 OK / 4     True    
demoapp-02      demoapp                    2            57m   3 OK / 4     True    
demoapp-01      demoapp                    1            57m   3 OK / 4     True    
[root@ip-172-17-11-227 ~]# kn revision list                 
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-05      demoapp   31%              5            13s   4 OK / 4     True    
demoapp-00004   demoapp   69%              4            12m   3 OK / 4     True    
demoapp-00003   demoapp                    3            43m   3 OK / 4     True    
demoapp-02      demoapp                    2            57m   3 OK / 4     True    
demoapp-01      demoapp                    1            57m   3 OK / 4     True    
[root@ip-172-17-11-227 ~]# kn revision list
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-05      demoapp   34%              5            15s   4 OK / 4     True    
demoapp-00004   demoapp   66%              4            12m   3 OK / 4     True    
demoapp-00003   demoapp                    3            43m   3 OK / 4     True    
demoapp-02      demoapp                    2            57m   3 OK / 4     True    
demoapp-01      demoapp                    1            57m   3 OK / 4     True    
[root@ip-172-17-11-227 ~]# kn revision list
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-05      demoapp   40%              5            16s   4 OK / 4     True    
demoapp-00004   demoapp   60%              4            13m   3 OK / 4     True    
demoapp-00003   demoapp                    3            43m   3 OK / 4     True    
demoapp-02      demoapp                    2            57m   3 OK / 4     True    
demoapp-01      demoapp                    1            57m   3 OK / 4     True    
[root@ip-172-17-11-227 ~]# kn revision list
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-05      demoapp   43%              5            17s   4 OK / 4     True    
demoapp-00004   demoapp   57%              4            13m   3 OK / 4     True    
demoapp-00003   demoapp                    3            43m   3 OK / 4     True    
demoapp-02      demoapp                    2            57m   3 OK / 4     True    
demoapp-01      demoapp                    1            57m   3 OK / 4     True    
[root@ip-172-17-11-227 ~]# kn revision list
NAME            SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-05      demoapp   100%             5            41s   4 OK / 4     True    
demoapp-00004   demoapp                    4            13m   3 OK / 4     True    
demoapp-00003   demoapp                    3            43m   3 OK / 4     True    
demoapp-02      demoapp                    2            58m   3 OK / 4     True    
demoapp-01      demoapp                    1            58m   3 OK / 4     True    

标签:Serverless,OK,default,第八章,172,demoapp,knative,True,spec
From: https://blog.51cto.com/yht1990/7728921

相关文章

  • Serverless平台knative第六章配置最大并发数及更新操作讲解
    并发数配置apiVersion:serving.knative.dev/v1kind:Servicemetadata:name:hellospec:template:metadata:name:hello-world-002spec:containerConcurrency:10#单个pod允许的最大并发数,超过将扩容containers:#-image:gcr......
  • knative所有服务域名及单域名配置方法
    为所有服务配置域名kubectleditconfigmapconfig-domain-nknative-servingapiVersion:v1data:yht.com:""#写你要配置的域名查看域名在创建完应用之后会自动创建域名默认域名格式为:kservice名字+命名空间+二级域名。可修改,下面会给出教程[root@ip-172-17-11......
  • Serverless平台knative第三章部署
    社区版部署环境准备 事先准备Kubernetes集群用于部署knative 选定isitio用来路由和治理流量需要部署的Knative组件 Serving Eventing Kn(KnativeCLI)环境要求 单节点的Kubernetes集群,需要至少有6个CPU核心,6G内存和30G磁盘空间 多节点的Kubernetes集群,每个......
  • Serverless平台knative第四章资源创建
    应用创建  创建和使用KnativeService资源方式有两种:    knservice<sub_command>      资源配置文件      群组:serving.knative.dev/v1       template <Object>用于创建或更新configuation,任何更新,都将创建新的Revision......
  • 第八章 LaneAF解读(车道线感知)
    @目录一前言二背景三DLA343.1IDA(IterativeDeepAggregation)3.2HDA(HierarchicalDeepAggregation)3.3结合四BEV视角4.1逆透视变换(IPM)4.2完全分割掩模与脚印分割五LaneAF结构5.1三个检测头5.2Pipeline过程六亲合场(AffinityFields)6.1亲合场生成(计算亲和力场......
  • 苍穹外卖-第八章来单提醒
    1.SpringTask1.1介绍SpringTask是Spring框架提供的任务调度工具,可以按照约定的时间自动执行某个代码逻辑。定位:定时任务框架作用:定时自动执行某段Java代码为什么要在Java程序中使用SpringTask?应用场景:信用卡每月还款提醒银行贷款每月还款提醒火车票售票系统处理未......
  • 《Unix/Linux系统编程》教材学习笔记第七章、第八章
    chapter7文件操作级别文件操作分五个级别,从低到高排序如下:1.硬件级别:硬件级别的文件操作包括fdisk:将硬盘、U盘或SDC盘分区。mkfs:格式化磁盘分区,为系统做好准备。fsck:检查和维修系统。碎片整理:压缩文件系统中的文件。其中大多说是针对系统的实用程序。2.操作系统内核中......
  • 运行 Knative Serving demo 应用
    创建ns#kubectlcreatensknative-demonamespace/knative-democreated创建应用hello-world.yamlapiVersion:serving.knative.dev/v1kind:Servicemetadata:name:helloworld-gonamespace:knative-demospec:template:spec:containers:......
  • 【活动回顾】Serverless 数仓技术与挑战(内含 PPT 下载)
    由「3306π」社区主办,「Databend」参与协办的「数据库朋友圈」活动于9月16日在北京360大厦成功举办!该活动汇集了数据库领域的资深专家和企业家,共同探讨数据库技术变革。下午,DatabendLabs联合创始人张雁飞作为「Serverless数仓技术与挑战」专题的演讲嘉宾进行了分享。主......
  • 【活动回顾】Serverless 数仓技术与挑战(内含 PPT 下载)
    由「3306π」社区主办,「Databend」参与协办的「数据库朋友圈」活动于9月16日在北京360大厦成功举办!该活动汇集了数据库领域的资深专家和企业家,共同探讨数据库技术变革。下午,DatabendLabs联合创始人张雁飞作为「Serverless数仓技术与挑战」专题的演讲嘉宾进行了分享。主题......