Knative serving基础

[root@master basic]# kubectl apply -f hello-world.yaml 
[root@master 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"

查看下状态和service

[root@master basic]# kn service list
NAME      URL                                  LATEST          AGE     CONDITIONS   READY   REASON
demoapp   http://demoapp.default.example.com   demoapp-00001   128m    3 OK / 3     True    
hello     http://hello.default.example.com     hello-world     2m51s   3 OK / 3     True    
[root@master 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                                               129m
demoapp-00001           ClusterIP      10.111.131.8     <none>                                                 80/TCP,443/TCP                                       129m
demoapp-00001-private   ClusterIP      10.98.248.135    <none>                                                 80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   129m
hello                   ExternalName   <none>           knative-local-gateway.istio-system.svc.cluster.local   80/TCP                                               2m6s
hello-world             ClusterIP      10.99.49.105     <none>                                                 80/TCP,443/TCP                                       3m
hello-world-private     ClusterIP      10.104.110.111   <none>                                                 80/TCP,443/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP   3m
kubernetes              ClusterIP      10.96.0.1        <none>                                                 443/TCP                                              21d

 查看deployment和kpa和pods,如果没有访问pods会为0

[root@master basic]# kubectl get deployment
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
demoapp-00001-deployment   0/0     0            0           133m
hello-world-deployment     1/1     1            1           7m29s
[root@master basic]# kubectl get kpa
NAME            DESIREDSCALE   ACTUALSCALE   READY   REASON
demoapp-00001   -1             0             False   NoTraffic
hello-world     1              1             True    
[root@master basic]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
hello-world-deployment-7bb7b4c4c9-jvpc4   3/3     Running   0          22s

 显示服务完整信息

[root@master basic]# kn service list hello -o yaml

查看hello的基本状态

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

Revisions:  
  100%  @latest (hello-world) [1] (13m)
        Image:     ikubernetes/helloworld-go (at 5ea96b)
        Replicas:  0/0

Conditions:  
  OK TYPE                   AGE REASON
  ++ Ready                  12m 
  ++ ConfigurationsReady    12m 
  ++ RoutesReady            12m 

更新hello

[root@master basic]# kubectl apply -f hello-world-002.yaml
[root@master 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@master basic]# kn service list
NAME      URL                                  LATEST          AGE    CONDITIONS   READY     REASON
demoapp   http://demoapp.default.example.com   demoapp-00001   145m   3 OK / 3     True      
hello     http://hello.default.example.com     hello-world     19m    1 OK / 3     Unknown   
[root@master basic]# kn service describe hello
Name:       hello
Namespace:  default
Age:        19m
URL:        http://hello.default.example.com

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

Conditions:  
  OK TYPE                   AGE REASON
  ++ Ready                  26s 
  ++ ConfigurationsReady    27s 
  ++ RoutesReady            26s 

[root@master basic]# kn revision list
NAME              SERVICE   TRAFFIC   TAGS   GENERATION   AGE    CONDITIONS   READY   REASON
demoapp-00001     demoapp   100%             1            146m   3 OK / 4     True    
hello-world-002   hello     100%             2            51s    4 OK / 4     True    
hello-world       hello                      1            20m    3 OK / 4     True

[root@master basic]# curl -H "Host: hello.default.example.com" 10.211.55.30
Hello World-002!

更新KService资源,副本数量最大数为10最小为1

[root@master basic]# kn service update hello --env TARGET=knative --scale-max=10 --scale-min=1 --revision-name=hello-knative
Updating Service 'hello' in namespace 'default':

  0.029s The Configuration is still working to reflect the latest desired specification.
  7.202s Traffic is not yet migrated to the latest revision.
  7.248s Ingress has not yet been reconciled.
  7.293s Waiting for load balancer to be ready
  7.484s Ready to serve.

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

查看revision

[root@master basic]# kn revision list
NAME              SERVICE   TRAFFIC   TAGS   GENERATION   AGE    CONDITIONS   READY   REASON
demoapp-00001     demoapp   100%             1            156m   3 OK / 4     True    
hello-knative     hello     100%             3            61s    4 OK / 4     True    
hello-world-002   hello                      2            10m    3 OK / 4     True    
hello-world       hello                      1            29m    3 OK / 4     True  

查看service

[root@master basic]# kn service list
NAME      URL                                  LATEST          AGE    CONDITIONS   READY   REASON
demoapp   http://demoapp.default.example.com   demoapp-00001   156m   3 OK / 3     True    
hello     http://hello.default.example.com     hello-knative   30m    3 OK / 3     True   

 直接curl访问进行测试

[root@master basic]# curl -H "Host: hello.default.example.com" 10.211.55.30
Hello knative!

删除service

[root@master basic]# kn service delete demoapp
Service 'demoapp' successfully deleted in namespace 'default'.

 

添加域名映射

[root@master domainmapping]# kubectl apply -f cdc-hello.ik8s.io.yaml 
clusterdomainclaim.networking.internal.knative.dev/hello.ik8s.io created

[root@master domainmapping]# kubectl get cdc
NAME            AGE
hello.ik8s.io   4s

[root@master domainmapping]# kn domain create hello.ik8s.io --ref ksvc:hello
Domain mapping 'hello.ik8s.io' created in namespace 'default'.

[root@master domainmapping]# kn domain list
NAME            URL                    READY   KSVC
hello.ik8s.io   http://hello.ik8s.io   True    hello

[root@master domainmapping]# curl -H "Host: hello.ik8s.io" 10.211.55.30
Hello knative!
[root@master domainmapping]# curl -H "Host: hello.ik8s.io" 10.211.55.30
Hello knative!

 域名映射,自动化创建CDC

autocreate-cluster-domain-claims: "true"

[root@master domainmapping]# kubectl edit cm config-network -n knative-serving

[root@master domainmapping]# kn domain create www.ik8s.io --ref ksvc:hello:default
Domain mapping 'www.ik8s.io' created in namespace 'default'.
[root@master domainmapping]# kn domain list
NAME            URL                    READY   KSVC
hello.ik8s.io   http://hello.ik8s.io   True    hello
www.ik8s.io     http://www.ik8s.io     True    hello
[root@master domainmapping]# kubectl get cdc
NAME            AGE
hello.ik8s.io   3h23m
www.ik8s.io     64s

 

Revision和流量管理

配置Private Kservice仅创建私有服务的方法

域名映射不会被阻断,外部名称会被阻断

[root@master domainmapping]# kubectl get vs
NAME                    GATEWAYS                                                                              HOSTS                                                                                              AGE
hello-ingress           ["knative-serving/knative-ingress-gateway","knative-serving/knative-local-gateway"]   ["hello.default","hello.default.svc","hello.default.svc.cluster.local","hello.default.yang.com"]   22h
hello-mesh              ["mesh"]                                                                              ["hello.default","hello.default.svc","hello.default.svc.cluster.local"]                            22h
hello.ik8s.io-ingress   ["knative-serving/knative-ingress-gateway"]                                           ["hello.ik8s.io"]                                                                                  3h45m
www.ik8s.io-ingress     ["knative-serving/knative-ingress-gateway"]                                           ["www.ik8s.io"]                                                                                    25m

[root@master domainmapping]# kubectl label kservice hello networking.knative.dev/visibility=cluster-local
service.serving.knative.dev/hello labeled

[root@master domainmapping]# kubectl get vs
NAME                    GATEWAYS                                      HOSTS                                                                     AGE
hello-ingress           ["knative-serving/knative-local-gateway"]     ["hello.default","hello.default.svc","hello.default.svc.cluster.local"]   22h
hello-mesh              ["mesh"]                                      ["hello.default","hello.default.svc","hello.default.svc.cluster.local"]   22h
hello.ik8s.io-ingress   ["knative-serving/knative-ingress-gateway"]   ["hello.ik8s.io"]                                                         3h47m
www.ik8s.io-ingress     ["knative-serving/knative-ingress-gateway"]   ["www.ik8s.io"]                                                           27m
[root@master domainmapping]# curl -H "Host: hello.ik8s.io" 10.211.55.30
Hello knative!
[root@master domainmapping]# curl -H "Host: www.ik8s.io" 10.211.55.30
Hello knative!
[root@master domainmapping]# curl -H "Host: hello.default.yang.com" 10.211.55.30

 把标签删除掉就又可以访问到了

[root@master domainmapping]# kubectl label kservice hello networking.knative.dev/visibility-
service.serving.knative.dev/hello unlabeled
[root@master domainmapping]# kubectl get vs
NAME                    GATEWAYS                                                                              HOSTS                                                                                              AGE
hello-ingress           ["knative-serving/knative-ingress-gateway","knative-serving/knative-local-gateway"]   ["hello.default","hello.default.svc","hello.default.svc.cluster.local","hello.default.yang.com"]   22h
hello-mesh              ["mesh"]                                                                              ["hello.default","hello.default.svc","hello.default.svc.cluster.local"]                            22h
hello.ik8s.io-ingress   ["knative-serving/knative-ingress-gateway"]                                           ["hello.ik8s.io"]                                                                                  4h10m
www.ik8s.io-ingress     ["knative-serving/knative-ingress-gateway"]                                           ["www.ik8s.io"]                                                                                    50m
[root@master domainmapping]# curl -H "Host: hello.default.yang.com" 10.211.55.30
Hello knative!

人为切换流量分发比例

[root@master revision-and-route]# kn revision list
NAME              SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
hello-knative     hello     100%             3            22h   4 OK / 4     True    
hello-world-002   hello                      2            22h   3 OK / 4     True    
hello-world       hello                      1            23h   3 OK / 4     True    
[root@master revision-and-route]# kn service update hello --traffic hello-world=100
Updating Service 'hello' in namespace 'default':

  0.037s The Route is still working to reflect the latest desired specification.
  0.115s Ingress has not yet been reconciled.
  0.143s Waiting for load balancer to be ready
  0.339s Ready to serve.

Service 'hello' with latest revision 'hello-knative' (unchanged) is available at URL:
http://hello.default.yang.com
[root@master revision-and-route]# kn revision list
NAME              SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
hello-knative     hello                      3            22h   3 OK / 4     True    
hello-world-002   hello                      2            22h   3 OK / 4     True    
hello-world       hello     100%             1            23h   3 OK / 4     True    
[root@master revision-and-route]# kn service describe hello
Name:       hello
Namespace:  default
Age:        23h
URL:        http://hello.default.yang.com

Revisions:  
     +  hello-knative (current @latest) [3] (22h)
        Image:     ikubernetes/helloworld-go (at 5ea96b)
        Replicas:  0/0
  100%  hello-world [1] (23h)
        Image:  ikubernetes/helloworld-go (at 5ea96b)

Conditions:  
  OK TYPE                   AGE REASON
  ++ Ready                  25s 
  ++ ConfigurationsReady    22h 
  ++ RoutesReady            25s

[root@master revision-and-route]# kn service update hello --traffic hello-world=90 --traffic hello-world-002=9 --traffic hello-knative=1
Updating Service 'hello' in namespace 'default':

  0.035s The Route is still working to reflect the latest desired specification.
  0.099s Ingress has not yet been reconciled.
  0.153s Waiting for load balancer to be ready
  0.426s Ready to serve.

Service 'hello' with latest revision 'hello-knative' (unchanged) is available at URL:
http://hello.default.yang.com
[root@master revision-and-route]# kn service describe hello
Name:       hello
Namespace:  default
Age:        23h
URL:        http://hello.default.yang.com

Revisions:  
    1%  hello-knative (current @latest) [3] (22h)
        Image:     ikubernetes/helloworld-go (at 5ea96b)
        Replicas:  1/1
    9%  hello-world-002 [2] (22h)
        Image:  ikubernetes/helloworld-go (at 5ea96b)
   90%  hello-world [1] (23h)
        Image:  ikubernetes/helloworld-go (at 5ea96b)

Conditions:  
  OK TYPE                   AGE REASON
  ++ Ready                   5s 
  ++ ConfigurationsReady    22h 
  ++ RoutesReady             5s 
[root@master revision-and-route]# kn revision list
NAME              SERVICE   TRAFFIC   TAGS   GENERATION   AGE   CONDITIONS   READY   REASON
hello-knative     hello     1%               3            22h   4 OK / 4     True    
hello-world-002   hello     9%               2            22h   3 OK / 4     True    
hello-world       hello     90%              1            23h   3 OK / 4     True

[root@master revision-and-route]# while true; do curl -H "Host: hello.default.yang.com" 10.211.55.30; sleep .2; done

 打tag,然后可以根据tag去访问

[root@master ~]# kn service update hello --tag hello-world-002=green
Updating Service 'hello' in namespace 'default':

  0.024s The Route is still working to reflect the latest desired specification.
  0.148s Ingress has not yet been reconciled.
  0.182s Waiting for load balancer to be ready
  0.362s Ready to serve.

Service 'hello' with latest revision 'hello-knative' (unchanged) is available at URL:
http://hello.default.yang.com
[root@master ~]# kn service update hello --tag hello-world=blue
Updating Service 'hello' in namespace 'default':

  0.033s The Route is still working to reflect the latest desired specification.
  0.076s Ingress has not yet been reconciled.
  0.128s Waiting for load balancer to be ready
  0.312s Ready to serve.

Service 'hello' with latest revision 'hello-knative' (unchanged) is available at URL:
http://hello.default.yang.com
[root@master ~]# kubectl get vs
NAME                    GATEWAYS                                                                              HOSTS                                                                                                                                                                                                                                                                                                                                        AGE
hello-ingress           ["knative-serving/knative-ingress-gateway","knative-serving/knative-local-gateway"]   ["blue-hello.default","blue-hello.default.svc","blue-hello.default.svc.cluster.local","blue-hello.default.yang.com","green-hello.default","green-hello.default.svc","green-hello.default.svc.cluster.local","green-hello.default.yang.com","hello.default","hello.default.svc","hello.default.svc.cluster.local","hello.default.yang.com"]   23h
hello-mesh              ["mesh"]                                                                              ["blue-hello.default","blue-hello.default.svc","blue-hello.default.svc.cluster.local","green-hello.default","green-hello.default.svc","green-hello.default.svc.cluster.local","hello.default","hello.default.svc","hello.default.svc.cluster.local"]                                                                                         23h
hello.ik8s.io-ingress   ["knative-serving/knative-ingress-gateway"]                                           ["hello.ik8s.io"]                                                                                                                                                                                                                                                                                                                            4h43m
www.ik8s.io-ingress     ["knative-serving/knative-ingress-gateway"]                                           ["www.ik8s.io"]                                                                                                                                                                                                                                                                                                                              83m

[root@master ~]# curl -H "Host: green-hello.default.yang.com" 10.211.55.30
Hello World-002!
[root@master ~]# curl -H "Host: green-hello.default.yang.com" 10.211.55.30
Hello World-002!
[root@master ~]# curl -H "Host: blue-hello.default.yang.com" 10.211.55.30
Hello World!
[root@master ~]# curl -H "Host: blue-hello.default.yang.com" 10.211.55.30
Hello World!

# 删除tag
[root@master ~]# kn service update hello --untag blue
Updating Service 'hello' in namespace 'default':

  0.044s The Route is still working to reflect the latest desired specification.
  0.101s Ingress has not yet been reconciled.
  0.137s Waiting for load balancer to be ready
  0.339s Ready to serve.

Service 'hello' with latest revision 'hello-knative' (unchanged) is available at URL:
http://hello.default.yang.com

 

配置实例并发测试 

[root@master autoscaling]# kubectl apply -f autoscaling-concurrency.yaml 
[root@master autoscaling]# cat autoscaling-concurrency.yaml 
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: hello
  namespace: default
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/target-utilization-percentage: "60"
        autoscaling.knative.dev/target: "10"
    spec:
      containers:
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "Knative Autoscaling Concurrency"
              
[root@master autoscaling]# kn service list
NAME    URL                             LATEST        AGE    CONDITIONS   READY   REASON
hello   http://hello.default.yang.com   hello-00001   3m7s   3 OK / 3     True 

[root@master autoscaling]# curl -H "Host: hello.default.yang.com" 10.211.55.30
Hello Knative Autoscaling Concurrency!

我们安装一个hey压测工具,我是mac,直接brew install hey

执行压测命令

hey -c 20 -z 60s -host "hello.default.yang.com" http://10.211.55.30?sleep=100&prime=10000&bloat=5

可以看到hello的pod会迅速扩容

 

配置revision的扩容边界,最大不超过3个pod

[root@master autoscaling]# kubectl apply -f autoscaling-scale-bounds.yaml
[root@master autoscaling]# cat autoscaling-scale-bounds.yaml 
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: hello
  namespace: default
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/target-utilization-percentage: "60"
        autoscaling.knative.dev/target: "10"
        autoscaling.knative.dev/max-scale: "3"
        autoscaling.knative.dev/initial-scale: "1"
        autoscaling.knative.dev/scale-down-delay: "1m"
        autoscaling.knative.dev/stable-window: "60s"
    spec:
      containers:
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "Knative Autoscaling Scale Bounds"

继续压力测试

hey -c 20 -z 60s -host "hello.default.yang.com" 'http://10.211.55.30?sleep=100&prime=10000&bloat=5'

 

rps测试,实例会扩展至多个,但最多不超过10个

[root@master autoscaling]# kubectl apply -f autoscaling-metrics-and-targets.yaml 
service.serving.knative.dev/hello created   
[root@master autoscaling]# kn service list
NAME    URL                             LATEST        AGE   CONDITIONS   READY   REASON
hello   http://hello.default.yang.com   hello-00001   5s    3 OK / 3     True    
[root@master autoscaling]# kubectl get pods 
NAME                                      READY   STATUS    RESTARTS   AGE
hello-00001-deployment-74bc458559-hb6pp   3/3     Running   0          4s
[root@master autoscaling]# cat autoscaling-metrics-and-targets.yaml 
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: hello
  namespace: default
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/target-utilization-percentage: "60"
        autoscaling.knative.dev/metric: "rps"
        autoscaling.knative.dev/target: "100"
        autoscaling.knative.dev/max-scale: "10"
        autoscaling.knative.dev/initial-scale: "1"
        autoscaling.knative.dev/stable-window: "2m"
    spec:
      containers:
        - image: ikubernetes/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "Knative Autoscaling Metrics and Targets"

posted @ 2022-10-19 20:29  Maniana  阅读(119)  评论(0编辑  收藏  举报