Knative - Service及应用【二】

Knative Serving工作模式

Queue Proxy

#Knative Serving会为每个Pod注入一个称为Queue Proxy的容器
    ◼ 为业务代码提供代理功能
        ◆Ingress GW接收到请求后,将其发往目标Pod实例上由Queue Proxy监听的8012端口
        ◆而后,Queue Proxy再将请求转发给业务代码容器监听的端口
    ◼ 报告实例上的多个有关客户端请求的关键指标为给Autoscaler
    ◼ Pod健康状态检测(Kubernetes探针)
    ◼ 为超出实例最大并发量的请求提供缓冲队列

#Queue Proxy预留使用了如下几个端口
8012: HTTP协议的代理端口
8013: HTTP/2端口,用于代理gRPC
8022:管理端口,如健康状态检测等
9090: 暴露给Autoscaler进行指标采集的端口
9091: 暴露给Prometheus进行监控指标采集的端口

Knative 运行应用

#在Serving上,可通过创建Knative Service对象来运行应用;该Service资源会自动创建
    ◼ 一个Configuration对象,它会创建一个Revision,并由该Revision自动创建如下两个对象
        ◆一个Deployment对象
        ◆一个PodAutoscaler对象
    ◼ 一个Route对象,它会创建
        ◆一个Kubernetes Service对象
        ◆一组Istio VirtualService对象
            ⚫ {kservice-name}-ingress
            ⚫ {kservice-name}-mesh

#Knative Service资源(简称kservice或ksvc)的资源规范主要有两个字段
    ◼ template:用于创建或更新Configuration,任何更新,都将创建新的Revision对象
    ◼ traffic:用于创建或更新Route对象
Knatvie Service 
    Configuration --> Revision 
        Deployment 
        PodAutoScaler(KPA)
    Route 
        Kubernetes Service 
        (istio network layer):一组VirtualService资源对象
            {serivce-name}-ingress: 访问该Knative Service的来自于集群外部流量 
            {service-name}-mesh: 访问该Knative Service的来自于集群内部流量
                启用了网格功能时:VirtualService
                未启用网格功能时:流量转给istio-system/knative-local-service 

资源配置 KService 实战

创建和使用Knative Service资源方式有两种:
   kn service <sub_command> 
   资源配置文件
   群组:serving.knative.dev/v1 
   template  <Object>
   traffic <[]Object> 

1.Service简单定义

[root@xianchaonode1 ~]# crictl pull ikubernetes/helloworld-go

[root@xianchaomaster1 basic]# cat hello-world.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: hello
spec:
  template:
    metadata:
      # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name}
      name: hello-world
    spec:
      containers:
        #- image: gcr.io/knative-samples/helloworld-go
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "World"
[root@xianchaomaster1 basic]# kubectl apply -f hello-world.yaml
service.serving.knative.dev/hello created

#Service
[root@xianchaomaster1 basic]# kn service list
NAME      URL                                  LATEST          AGE   CONDITIONS   READY   REASON
demoapp   http://demoapp.default.example.com   demoapp-00001   64m   3 OK / 3     True
hello     http://hello.default.example.com     hello-world     7s    3 OK / 3     True

[root@xianchaomaster1 basic]# kubectl get service
NAME                    TYPE           CLUSTER-IP     EXTERNAL-IP                                            PORT(S)                                              AGE
demoapp                 ExternalName   <none>         knative-local-gateway.istio-system.svc.cluster.local   80/TCP                                               64m
demoapp-00001           ClusterIP      10.96.0.74     <none>                                                 80/TCP,443/TCP                                       64m
demoapp-00001-private   ClusterIP      10.96.92.180   <none>                                                 80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   64m
hello                   ExternalName   <none>         knative-local-gateway.istio-system.svc.cluster.local   80/TCP                                               37s
hello-world             ClusterIP      10.96.18.118   <none>                                                 80/TCP,443/TCP                                       38s
hello-world-private     ClusterIP      10.96.208.82   <none>                                                 80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   38s
kubernetes              ClusterIP      10.96.0.1      <none>                                                 443/TCP                                              10d

#VS
[root@xianchaomaster1 basic]# kubectl get vs
NAME              GATEWAYS                                                                              HOSTS                                                                                                         AGE
demoapp-ingress   ["knative-serving/knative-ingress-gateway","knative-serving/knative-local-gateway"]   ["demoapp.default","demoapp.default.example.com","demoapp.default.svc","demoapp.default.svc.cluster.local"]   65m
demoapp-mesh      ["mesh"]                                                                              ["demoapp.default","demoapp.default.svc","demoapp.default.svc.cluster.local"]                                 65m
hello-ingress     ["knative-serving/knative-ingress-gateway","knative-serving/knative-local-gateway"]   ["hello.default","hello.default.example.com","hello.default.svc","hello.default.svc.cluster.local"]           77s
hello-mesh        ["mesh"]                                                                              ["hello.default","hello.default.svc","hello.default.svc.cluster.local"]                                       77s

[root@xianchaomaster1 basic]# kubectl get kpa
NAME            DESIREDSCALE   ACTUALSCALE   READY   REASON
demoapp-00001   0              0             False   NoTraffic
hello-world     0              0             False   NoTraffic

#默认拉起服务 会创建一个服务 后面没人访问就会消失 停止
[root@xianchaomaster1 basic]# curl -H "Host: hello.default.example.com" 192.168.40.190
Hello World!
[root@xianchaomaster1 basic]# kubectl get deploy
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
demoapp-00001-deployment   0/0     0            0           67m
hello-world-deployment     1/1     1            1           3m43s

2.Service - containerConcurrency

#revision为代码和配置的不可变快照,因此,修改其配置,也必须改变revision的名称;
#此时,Configuration为创建一个新的revision作为Latest Revision,并通过其接收客户端流量;

#这里Servie name和上面hello一样 但是后端多了一个hello-world-002服务 并且100%转给hello-world-002
[root@xianchaomaster1 basic]# cat hello-world-002.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: hello
spec:
  template:
    metadata:
      # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name}
      name: hello-world-002
    spec:
      containerConcurrency: 10
      containers:
        #- image: gcr.io/knative-samples/helloworld-go
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "World-002"

[root@xianchaomaster1 basic]# kubectl apply -f hello-world-002.yaml
service.serving.knative.dev/hello configured

#说明现在hello 有两个版本 但是现在再hello-world-002版本上 流量100%到hello-world-002上
[root@xianchaomaster1 basic]# kn service list
NAME      URL                                  LATEST            AGE     CONDITIONS   READY   REASON
demoapp   http://demoapp.default.example.com   demoapp-00001     73m     3 OK / 3     True
hello     http://hello.default.example.com     hello-world-002   9m53s   3 OK / 3     True

[root@xianchaomaster1 basic]# kn service describe hello
Name:       hello
Namespace:  default
Age:        10m
URL:        http://hello.default.example.com

Revisions:
  100%  @latest (hello-world-002) [2] (46s)
        Image:     ikubernetes/helloworld-go (at 5ea96b)
        Replicas:  1/1

Conditions:
  OK TYPE                   AGE REASON
  ++ Ready                  42s
  ++ ConfigurationsReady    42s
  ++ RoutesReady            42s
  
[root@xianchaomaster1 basic]# kn revision list
NAME              SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-00001     demoapp   100%             1            74m   3 OK / 4     True
hello-world-002   hello     100%             2            58s   4 OK / 4     True
hello-world       hello                      1            10m   3 OK / 4     True

#访问到002上
[root@xianchaomaster1 basic]# curl -H "Host: hello.default.example.com" 192.168.40.190
Hello World-002!

命令式资源管理-Knative CLI和Serving相关子命令

#◼ service:管理Knative services
    ◆apply 
    ◆create、delete、update 
    ◆list, describe 
    ◆export, import 
#◼ revision:管理Knative revisions 
    ◆delete 
    ◆list、describe 
#◼ route:管理Knative routes 
    ◆delete 
    ◆list 、 describe 
#◼ domain:管理域名映射 
    ◆create、delete、update 
    ◆list、describe 
#◼ container:管理KService的容器 
    ◆add

#更新KService资源
    ◼ 使用apply或update命令进行资源更新
        ◆~$ kn service update hello --env TARGET=Knative-CLI --scale-max 10 --scale-min 1 --revision-name hello-knative
    ◼ 更新KService时触发的操作
        ◆更新spec.template部分,将创建一个新的revision
            ⚫ 一个KService下可能同时存在多个revision,其中有一个是为Latest Revision
            ⚫ 默认情况下,Latest Revision接收该Service收到的全部请求
            ⚫ 也可以为不同的Revision指定不同的流量比例
        ◆更新spec.traffic部分,其Route将被修改
#删除KService资源
    ◼ 使用delete命令删除资源
        ◆~$ kn service delete hello
    ◼ 删除KService时,Kubernetes将基于terminator和ownerReferences收集其子资源


#kn更新版本
[root@xianchaomaster1 basic]# kn service update hello --env TARGET=Knative-CLI --scale-max 10 --scale-min 1 --revision-name hello-knative
Updating Service 'hello' in namespace 'default':

  0.018s The Configuration is still working to reflect the latest desired specification.
  4.114s Traffic is not yet migrated to the latest revision.
  4.133s Ingress has not yet been reconciled.
  4.160s Waiting for load balancer to be ready
  4.366s Ready to serve.

Service 'hello' updated to latest revision 'hello-knative' is available at URL:
http://hello.default.example.com

[root@xianchaomaster1 basic]# kn revision list
NAME              SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
demoapp-00001     demoapp   100%             1            97m   3 OK / 4     True
hello-knative     hello     100%             3            48s   4 OK / 4     True
hello-world-002   hello                      2            24m   3 OK / 4     True
hello-world       hello                      1            33m   3 OK / 4     True

#此时hello Service 指向了 hello-knative
[root@xianchaomaster1 basic]# kn service list
NAME      URL                                  LATEST          AGE   CONDITIONS   READY   REASON
demoapp   http://demoapp.default.example.com   demoapp-00001   99m   3 OK / 3     True
hello     http://hello.default.example.com     hello-knative   35m   3 OK / 3     True

[root@xianchaomaster1 basic]# curl -H "Host: hello.default.example.com" 192.168.40.190
Hello Knative-CLI!
posted @ 2023-06-21 13:33  しみずよしだ  阅读(124)  评论(0)    收藏  举报