serving的工作模式

image.png

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

image.png

运行knative应用

  • yaml

    1. cat hello-world.yaml
    2. apiVersion: serving.knative.dev/v1
    3. kind: Service
    4. metadata:
    5. name: hello
    6. spec:
    7. template:
    8. metadata:
    9. # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name}
    10. name: hello-world
    11. spec:
    12. containers:
    13. #- image: gcr.io/knative-samples/helloworld-go
    14. - image: ikubernetes/helloworld-go
    15. ports:
    16. - containerPort: 8080
    17. env:
    18. - name: TARGET
    19. value: "World"
  • 资源部署

    1. kubectl apply -f hello-world.yaml
  • 资源查看 ``` root@k8s-master1:~# kubectl get pod #处于Terminating状态 NAME READY STATUS RESTARTS AGE hello-world-deployment-5c446fc685-9sz5t 2/2 Terminating 0 4m49s

root@k8s-master1:~# kubectl get configuration NAME LATESTCREATED LATESTREADY READY REASON hello hello-world hello-world True

root@k8s-master1:~# kubectl get revision NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON ACTUAL REPLICAS DESIRED REPLICAS hello-world hello 1 True 0 0

root@k8s-master1:~# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE hello-world-deployment 0/0 0 0 6m31s

root@k8s-master1:~# kubectl get kpa NAME DESIREDSCALE ACTUALSCALE READY REASON hello-world 0 0 False NoTraffic

root@k8s-master1:~# kubectl get route NAME URL READY REASON hello http://hello.default.example.com True

root@k8s-master1:~# curl -H “HOST: hello.default.example.com” 10.168.56.110 #请求之后会激活pod Hello World! root@k8s-master1:~# kubectl get pod NAME READY STATUS RESTARTS AGE hello-world-deployment-5c446fc685-sgg45 2/2 Running 0 11s

root@k8s-master1:~/knative-in-practise-main/serving/basic# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello ExternalName knative-local-gateway.istio-system.svc.cluster.local 80/TCP 9m42s

root@k8s-master1:~/knative-in-practise-main/serving/basic# kubectl get vs NAME GATEWAYS HOSTS AGE 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”] 10m hello-mesh [“mesh”] [“hello.default”,”hello.default.svc”,”hello.default.svc.cluster.local”] 10m

root@k8s-master1:~# kubectl run client-$RANDOM —image=ikubernetes/admin-box:v1.2 —restart=Never -it —command — /bin/bash #集群内访问 If you don’t see a command prompt, try pressing enter. root@client-10092 /# curl http://hello.default.svc.cluster.local Hello World!

root@k8s-master1:~/knative-in-practise-main/serving/basic# kubectl describe svc hello-world Name: hello-world Namespace: default Labels: app=hello-world networking.internal.knative.dev/serverlessservice=hello-world networking.internal.knative.dev/serviceType=Public serving.knative.dev/configuration=hello serving.knative.dev/configurationGeneration=1 serving.knative.dev/configurationUID=1af77209-76a2-4182-a645-36198952ae52 serving.knative.dev/revision=hello-world serving.knative.dev/revisionUID=a6129c41-ef8c-48ff-be07-a30b6107a530 serving.knative.dev/service=hello serving.knative.dev/serviceUID=2b115578-7155-443c-a57e-52cf04ed10ef Annotations: autoscaling.knative.dev/class: kpa.autoscaling.knative.dev serving.knative.dev/creator: admin Selector: Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.68.31.45 IPs: 10.68.31.45 Port: http 80/TCP TargetPort: 8012/TCP Endpoints: 172.20.107.196:8012 Session Affinity: None Events:

  1. - 通过kn查看资源

root@k8s-master1:~/knative-in-practise-main/serving/basic# kn service list NAME URL LATEST AGE CONDITIONS READY REASON hello http://hello.default.example.com hello-world 20m 3 OK / 3 True
root@k8s-master1:~/knative-in-practise-main/serving/basic# kn service describe hello Name: hello Namespace: default Age: 21m URL: http://hello.default.example.com

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

Conditions:
OK TYPE AGE REASON ++ Ready 17m ++ ConfigurationsReady 18m ++ RoutesReady 17m

root@k8s-master1:~/knative-in-practise-main/serving/basic# kn revision list NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON hello-world hello 100% 1 21m 3 OK / 4 True

root@k8s-master1:~/knative-in-practise-main/serving/basic# kn revision describe hello-world Name: hello-world Namespace: default Age: 22m Image: ikubernetes/helloworld-go (at 5ea96b) Replicas: 0/0 Port: 8080 Env: TARGET=World Service: hello

Conditions:
OK TYPE AGE REASON ++ Ready 19m ++ ContainerHealthy 19m ++ ResourcesAvailable 19m I Active 4m NoTraffic

root@k8s-master1:~/knative-in-practise-main/serving/basic# kn route list NAME URL READY hello http://hello.default.example.com True root@k8s-master1:~/knative-in-practise-main/serving/basic# kn route describe hello Name: hello Namespace: default Age: 23m URL: http://hello.default.example.com Service: hello

Traffic Targets:
100% @latest (hello-world)

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

  1. <a name="LRidk"></a>
  2. ## knative Service资源规范
  3. - Knative Service资源的spec配置段主要由ConfigurationSpec和RouteSpec组成
  4. - ConfigurationSpec:定义Configuration资源规范,其核心组成即为Revision Template
  5. - RouteSpec:定义Route资源规范,旨在定义流量目标(Traffic Target)
  6. - 查看资源类型

root@k8s-master1:~/knative-in-practise-main/serving# kubectl api-resources | grep knative metrics autoscaling.internal.knative.dev/v1alpha1 true Metric podautoscalers kpa,pa autoscaling.internal.knative.dev/v1alpha1 true PodAutoscaler images img caching.internal.knative.dev/v1alpha1 true Image certificates kcert networking.internal.knative.dev/v1alpha1 true Certificate clusterdomainclaims cdc networking.internal.knative.dev/v1alpha1 false ClusterDomainClaim ingresses kingress,king networking.internal.knative.dev/v1alpha1 true Ingress serverlessservices sks networking.internal.knative.dev/v1alpha1 true ServerlessService configurations config,cfg serving.knative.dev/v1 true Configuration domainmappings dm serving.knative.dev/v1beta1 true DomainMapping revisions rev serving.knative.dev/v1 true Revision routes rt serving.knative.dev/v1 true Route services kservice,ksvc serving.knative.dev/v1 true Service

  1. - 服务访问

root@k8s-master1:~/knative-in-practise-main/serving# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello ExternalName knative-local-gateway.istio-system.svc.cluster.local 80/TCP 8d hello-world ClusterIP 10.68.31.45 80/TCP 8d hello-world-private ClusterIP 10.68.31.74 80/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 8d kubernetes ClusterIP 10.68.0.1 443/TCP 9d root@k8s-master1:~/knative-in-practise-main/serving# kubectl run client-$RANDOM —image=ikubernetes/admin-box:v1.2 —restart=Never -it —command — /bin/bash root@client-10090 /# curl http://hello.default.svc.cluster.local Hello World!

  1. <a name="MkdyN"></a>
  2. ### DomainMapping
  3. - 域名映射依赖于ClusterDomainClaim,创建方法(二选一)
  4. - ~$ kubectl patch configmap config-network -n knative-serving -p '{"data":{"autocreate-cluster-domain-claims":"true"}}’
  5. - 全局配置参数,允许自动创建CDC资源
  6. - 手动创建相关的CDC资源,名称为要使用的外部映射域名
  7. - 基于ClusterDomainClaim资源规范创建,仅需要指定名称,且namespace要在spec字段中定义
  8. - 查看CDC资源
  9. - kubectl get cdc [name]
  10. - kubectl describe cdc [name]
  11. - DomainMapping的功能
  12. - 将外部域名映射至Knative Service或Knative Route
  13. - 支持跨Kubernetes Namespace对Knative Service进行名称映射
  14. - 创建domain Mapping

root@k8s-master1:~/knative-in-practise-main/serving# kn route list NAME URL READY hello http://hello.default.example.com True root@k8s-master1:~/knative-in-practise-main/serving# kn domain create hello.cropy.cn —ref ksvc:hello Domain mapping ‘hello.cropy.cn’ created in namespace ‘default’.

root@k8s-master1:~/knative-in-practise-main/serving# kn domain describe hello.cropy.cn Name: hello.cropy.cn Namespace: default Age: 27m

URL: http://hello.cropy.cn

Reference:
APIVersion: serving.knative.dev/v1 Kind: Service Name: hello

Conditions:
OK TYPE AGE REASON !! Ready 27m DomainAlreadyClaimed ?? CertificateProvisioned 27m !! DomainClaimed 27m DomainAlreadyClaimed ?? IngressReady 27m IngressNotConfigured ?? ReferenceResolved 27m

root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# cat cdc-hello.magedu.com.yaml apiVersion: networking.internal.knative.dev/v1alpha1 kind: ClusterDomainClaim metadata: name: hello.cropy.cn spec: namespace: default

root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# kubectl apply -f cdc-hello.cropy.cn.yaml root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# kubectl get cdc root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# kn domain list NAME URL READY KSVC hello.cropy.cn http://hello.cropy.cn False hello

  1. - 设置自动创建cdc

root@k8s-master1:~# kubectl edit cm config-network -n knative-serving apiVersion: v1 data: autocreate-cluster-domain-claims: “true”

root@k8s-master1:~# curl -H “Host: hello.cropy.cn” 10.168.56.110 Hello World!

  1. <a name="TVrmJ"></a>
  2. ### Revision
  3. - 关于Revision
  4. - 应用程序代码及相关容器配置某个版本的不可变快照
  5. - KService上的spec.template的每次变动,都会自动生成一个新的Revision
  6. - 通常不需要手动创建及维护
  7. - Revision的使用场景
  8. - 将流量切分至不同版本的应用程序间(Canary Deployment、Blue/Green Deployment)
  9. - 版本回滚
  10. - 设置revision实现版本管理

root@k8s-master1:~/knative-in-practise-main/serving/basic# cat hello-world-002.yaml apiVersion: serving.knative.dev/v1 kind: Service metadata: name: hello spec: template: metadata:

  1. # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name}
  2. name: hello-world-002
  3. spec:
  4. containerConcurrency: 10
  5. containers:
  6. #- image: gcr.io/knative-samples/helloworld-go
  7. - image: ikubernetes/helloworld-go
  8. ports:
  9. - containerPort: 8080
  10. env:
  11. - name: TARGET
  12. value: "World-002"

root@k8s-master1:~/knative-in-practise-main/serving/basic# kubectl apply -f hello-world-002.yaml root@k8s-master1:~# curl -H “Host: hi.cropy.cn” 10.168.56.110 Hello World-002!

root@k8s-master1:~# kn service list NAME URL LATEST AGE CONDITIONS READY REASON hello http://hello.default.example.com hello-world-002 8d 3 OK / 3 True
root@k8s-master1:~# kn route list NAME URL READY hello http://hello.default.example.com True

root@k8s-master1:~# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello ExternalName knative-local-gateway.istio-system.svc.cluster.local 80/TCP 8d hello-world ClusterIP 10.68.31.45 80/TCP 8d hello-world-002 ClusterIP 10.68.156.150 80/TCP 81s hello-world-002-private ClusterIP 10.68.143.38 80/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 81s hello-world-private ClusterIP 10.68.31.74 80/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 8d kubernetes ClusterIP 10.68.0.1 443/TCP 9d

root@k8s-master1:~# kn revision list #查看revision NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON hello-world-002 hello 100% 2 7m33s 3 OK / 4 True
hello-world hello 1 8d 3 OK / 4 True
```