首页 > 其他分享 >knative serving 流量管理

knative serving 流量管理

时间:2023-10-08 16:48:06浏览次数:36  
标签:serving demo helloworld TCP 流量 knative wgs go

创建客户端

# kubectl run client --image=ikubernetes/admin-box -it --rm --restart=Never --command -n knative-demo -- /bin/bash
root@client /# 

创建应用

hello-world-v1.yaml

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
  namespace: knative-demo
spec:
  template:
    spec:
      containers:
        - image: ghcr.dockerproxy.com/knative/helloworld-go:latest
          env:
            - name: TARGET
              value: "Go Sample v1"

hello-world-v2.yaml

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
  namespace: knative-demo
spec:
  template:
    spec:
      containers:
        - image: ghcr.dockerproxy.com/knative/helloworld-go:latest
          env:
            - name: TARGET
              value: "Go Sample v2"

hello-world-v3.yaml

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
  namespace: knative-demo
spec:
  template:
    spec:
      containers:
        - image: ghcr.dockerproxy.com/knative/helloworld-go:latest
          env:
            - name: TARGET
              value: "Go Sample v3"

创建应用

# kubectl apply -f hello-word-v1.yaml 
# kubectl apply -f hello-word-v2.yaml 
# kubectl apply -f hello-word-v3.yaml 
service.serving.knative.dev/helloworld-go created
service.serving.knative.dev/helloworld-go configured
service.serving.knative.dev/helloworld-go configured

查看应用资源

# kubectl get svc,rt,vs -n knative-demo
NAME                                  TYPE           CLUSTER-IP       EXTERNAL-IP                                        PORT(S)                                              AGE
service/helloworld-go                 ExternalName   <none>           knative-local-gateway.istio-system.svc.wgs.local   80/TCP                                               14s
service/helloworld-go-00001           ClusterIP      10.100.212.167   <none>                                             80/TCP,443/TCP                                       17s
service/helloworld-go-00001-private   ClusterIP      10.100.63.173    <none>                                             80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   17s
service/helloworld-go-00002           ClusterIP      10.100.105.107   <none>                                             80/TCP,443/TCP                                       13s
service/helloworld-go-00002-private   ClusterIP      10.100.93.61     <none>                                             80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   13s
service/helloworld-go-00003           ClusterIP      10.100.170.32    <none>                                             80/TCP,443/TCP                                       10s
service/helloworld-go-00003-private   ClusterIP      10.100.157.99    <none>                                             80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   10s

NAME                                      URL                                               READY   REASON
route.serving.knative.dev/helloworld-go   http://helloworld-go.knative-demo.svc.wgs.local   True    

NAME                                                               GATEWAYS                                      HOSTS                                                                                                        AGE
virtualservice.networking.istio.io/helloworld-go-ingress           ["knative-serving/knative-local-gateway"]     ["helloworld-go.knative-demo","helloworld-go.knative-demo.svc","helloworld-go.knative-demo.svc.wgs.local"]   14s
virtualservice.networking.istio.io/helloworld-go-mesh              ["mesh"]                                      ["helloworld-go.knative-demo","helloworld-go.knative-demo.svc","helloworld-go.knative-demo.svc.wgs.local"]   14s
virtualservice.networking.istio.io/helloworld-go.wgs.com-ingress   ["knative-serving/knative-ingress-gateway"]   ["helloworld-go.wgs.com"]                                                                                    22h
virtualservice.networking.istio.io/helloworld.wgs.com-ingress      ["knative-serving/knative-ingress-gateway"]   ["helloworld.wgs.com"]                                                                                       22h

查看 Revision

# kubectl get revision -n knative-demo
NAME                  CONFIG NAME     K8S SERVICE NAME   GENERATION   READY   REASON   ACTUAL REPLICAS   DESIRED REPLICAS
helloworld-go-00001   helloworld-go                      1            True             0                 0
helloworld-go-00002   helloworld-go                      2            True             0                 0
helloworld-go-00003   helloworld-go                      3            True             1                 1

查看流量规则

默认流量路由到最新的Revision。
# kn route describe  helloworld-go -n knative-demo
Name:       helloworld-go
Namespace:  knative-demo
Age:        1m
URL:        http://helloworld-go.knative-demo.svc.wgs.local
Service:    helloworld-go

Traffic Targets:  
  100%  @latest (helloworld-go-00003)

Conditions:  
  OK TYPE                      AGE REASON
  ++ Ready                      1m 
  ++ AllTrafficAssigned         1m 
  ++ CertificateProvisioned     1m TLSNotEnabled
  ++ IngressReady               1m 

访问测试

root@client /# curl helloworld-go.knative-demo.svc.wgs.local
Hello Go Sample v3!

定义流量策略

hello-world-v3.yaml

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
  namespace: knative-demo
spec:
  template:
    spec:
      containers:
        - image: ghcr.dockerproxy.com/knative/helloworld-go:latest
          env:
            - name: TARGET
              value: "Go Sample v3"
  traffic:
  - percent: 0
    revisionName: helloworld-go-00001
    tag: staging
  - percent: 40
    revisionName: helloworld-go-00002
  - percent: 60
    revisionName: helloworld-go-00003

 

更新资源

# kubectl apply -f hello-word-v3.yaml 
service.serving.knative.dev/helloworld-go configured

查看流量规则

# kn route describe  helloworld-go -n knative-demo
Name:       helloworld-go
Namespace:  knative-demo
Age:        30m
URL:        http://helloworld-go.knative-demo.svc.wgs.local
Service:    helloworld-go

Traffic Targets:  
    0%  helloworld-go-00001 #staging
        URL:  http://staging-helloworld-go.knative-demo.svc.wgs.local
   40%  helloworld-go-00002
   60%  helloworld-go-00003

Conditions:  
  OK TYPE                      AGE REASON
  ++ Ready                      1m 
  ++ AllTrafficAssigned        30m 
  ++ CertificateProvisioned    30m TLSNotEnabled
  ++ IngressReady               1m 

访问测试

root@client /# while true;do sleep 0.5; curl helloworld-go.knative-demo.svc.wgs.local;done
Hello Go Sample v3!
Hello Go Sample v2!
Hello Go Sample v2!
Hello Go Sample v2!
Hello Go Sample v3!
Hello Go Sample v2!
Hello Go Sample v3!
Hello Go Sample v2!
Hello Go Sample v3!
Hello Go Sample v2!
Hello Go Sample v3!
Hello Go Sample v3!
root@client /# curl staging-helloworld-go.knative-demo.svc.wgs.local
Hello Go Sample v1!

使用 Knative CLI管理路由和流量

kn service update <service-name> --traffic <revision-name>=<percent>
<service-name>   是为其配置流量路由的 Knative 服务的名称。
<revision-name>  是要配置为接收流量百分比的修订版的名称。
<percent>        是要发送到指定<revision-name>的修订版本的流量百分比。

将流量完全发送至指定的某个Revision

kn service update helloworld-go --trafic helloworld-go-00003=100 -n knative-demo

将流量切分至不同的Revision

kn service update helloworld-go --traffic helloworld-go-00003=90 --traffic helloworld-go-00002=10 -n knative-demo

将流量完全发送至最新就绪版本的Revision

kn service update helloworld-go --traffic '@latest'=100 -n knative-demo

路由标签

设置标签

kn service update helloworld-go --tag helloworld-go-00002=green --tag @latest=blue -n knative-demo
Updating Service 'helloworld-go' in namespace 'knative-demo':

  0.051s The Route is still working to reflect the latest desired specification.
  0.177s Ingress has not yet been reconciled.
  0.248s Ready to serve.

Service 'helloworld-go' with latest revision 'helloworld-go-00003' (unchanged) is available at URL:
http://helloworld-go.knative-demo.svc.wgs.local

查看标签

# kn route describe  helloworld-go -n knative-demo
Name:       helloworld-go
Namespace:  knative-demo
Age:        55m
URL:        http://helloworld-go.knative-demo.svc.wgs.local
Service:    helloworld-go

Traffic Targets:  
    0%  helloworld-go-00001 #staging
        URL:  http://staging-helloworld-go.knative-demo.svc.wgs.local
   40%  helloworld-go-00002 #green
        URL:  http://green-helloworld-go.knative-demo.svc.wgs.local
   60%  helloworld-go-00003
    0%  @latest (helloworld-go-00003) #blue
        URL:  http://blue-helloworld-go.knative-demo.svc.wgs.local

Conditions:  
  OK TYPE                      AGE REASON
  ++ Ready                      9s 
  ++ AllTrafficAssigned        55m 
  ++ CertificateProvisioned    55m TLSNotEnabled
  ++ IngressReady               9s 

删除标签

kn service update helloworld-go --untag blue

将流量按照标签切分至不同的Revision

kn service update helloworld-go --traffic blue=100 --traffic green=0 -n knative-demo
Updating Service 'helloworld-go' in namespace 'knative-demo':

  0.046s The Route is still working to reflect the latest desired specification.
  0.090s Ingress has not yet been reconciled.
  0.198s Ready to serve.

Service 'helloworld-go' with latest revision 'helloworld-go-00003' (unchanged) is available at URL:
http://helloworld-go.knative-demo.svc.wgs.local

查看流量规则

# kn route describe  helloworld-go -n knative-demo
Name:       helloworld-go
Namespace:  knative-demo
Age:        59m
URL:        http://helloworld-go.knative-demo.svc.wgs.local
Service:    helloworld-go

Traffic Targets:  
    0%  helloworld-go-00001 #staging
        URL:  http://staging-helloworld-go.knative-demo.svc.wgs.local
    0%  helloworld-go-00002 #green
        URL:  http://green-helloworld-go.knative-demo.svc.wgs.local
  100%  @latest (helloworld-go-00003) #blue
        URL:  http://blue-helloworld-go.knative-demo.svc.wgs.local

Conditions:  
  OK TYPE                      AGE REASON
  ++ Ready                      6s 
  ++ AllTrafficAssigned        59m 
  ++ CertificateProvisioned    59m TLSNotEnabled
  ++ IngressReady               6s 

流量逐步迁移

ConfigurationName也可以作为路由项中的流量目标,意味着相关的流量部分由该Configurate下最新就绪的Revision承载。在新的Revision就绪之后,Configuration Target上的所有流量会立即转移至该Revision。
这可能会使 QP 或 Activator 处的请求队列过长,并导致请求过期或被 QP 拒绝。
Knative 提供了一个rollout-duration参数,可用于将流量逐渐转移到最新的 Revision,防止请求排队或被拒绝。新的Revision上线后,它会先从Configuration Target迁出1%的流量,随后再等分迁出余下的流量部分。

全局配置

# kubectl patch configmap config-network -n knative-serving -p '{"data":{"rollout-duration":"380"}}'

单独配置

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
  namespace: knative-demo
  annotations:
    serving.knative.dev/rollout-duration: "380s"  # 指定流量迁移过程的时长

参考文档

https://knative.dev/docs/serving/traffic-management

标签:serving,demo,helloworld,TCP,流量,knative,wgs,go
From: https://www.cnblogs.com/wangguishe/p/17747092.html

相关文章

  • Serverless平台knative第十章如何应用pod频繁抖动
    负载变动频繁时,Knative可能会因为响应负载变动而导致频繁创建或销毁Pod实例为避免服务规模“抖动”,AutoScaler支持两种扩缩容模式Stable稳定模式在稳定模式中,KPA会在默认的稳定窗口期(默认为60秒)内计算Pod的平均并发数。根据这个平均并发数,KPA会调整Pod的数量,以保持稳定的负载水......
  • Serverless平台knative第九章配置文件介绍
    knative配置文件[root@ip-172-17-11-227~]#kubectlgetcm-nknative-servingNAMEDATAAGEconfig-autoscaler12d2hconfig-defaults12d2hconfig-deployment22d2hconfig-domain22d2h......
  • knative serving 域名映射
    创建应用hello-world.yamlapiVersion:serving.knative.dev/v1kind:Servicemetadata:name:helloworld-gonamespace:knative-demospec:template:spec:containers:-image:ghcr.dockerproxy.com/knative/helloworld-go:latestenv......
  • TCP协议的秘密武器:流量控制与拥塞控制
    TCP可靠性传输相信大家都熟知TCP协议作为一种可靠传输协议,但它是如何确保传输的可靠性呢?要实现可靠性传输,需要考虑许多因素,比如数据的损坏、丢失、重复以及分片顺序混乱等问题。如果不能解决这些问题,就无法实现可靠传输。因此,TCP采用了序列号、确认应答、重发控制、连接管理和窗......
  • Serverless平台knative第八章流量管理
    准备环境创建一个服务两个版本[root@ip-172-17-11-227~]#catblue.yamlapiVersion:serving.knative.dev/v1kind:Servicemetadata:name:demoappspec:template:metadata:name:demoapp-01spec:containers:#-image:gcr.io/knative......
  • 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......
  • Sentinel系列之流量控制及熔断降级示例
    关于Sentinel的介绍网上很多,不再复制粘贴。本文主要演示Sentinel的两个重点功能:流量控制和熔断降级。示例基于Sentinel1.8.6, 同时使用JMeter进行并发请求(Postman无法并发)。当然也可以通过main方法,但这样就无法重复触发,并且无法学习Sentinel与Spring框架的集成另外需要注意的......