knative serving中的几个controller

  • Post author:
  • Post category:其他


本文基于knative v0.10



1 gc

控制revision的数量。

knative controller关联了一个configmap:knative-serving/config-gc,它包含了gc控制器的参数,用于控制revision数量。



2 serverlessservice

处理serverlessservices.networking.internal.knative.dev资源(简写sks)。

[root@k8s-1 ~]# kubectl get sks helloworld-go-external-sfp8c -oyaml
apiVersion: networking.internal.knative.dev/v1alpha1
kind: ServerlessService
metadata:
  annotations:
    autoscaling.knative.dev/class: kpa.autoscaling.knative.dev
    serving.knative.dev/creator: kubernetes-admin
  creationTimestamp: "2019-12-05T07:11:48Z"
  generation: 2
  labels:
    app: helloworld-go-external-sfp8c
    serving.knative.dev/configuration: helloworld-go-external
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/revision: helloworld-go-external-sfp8c
    serving.knative.dev/revisionUID: eaa0e7aa-3c44-45d9-8c97-467a51716793
    serving.knative.dev/service: helloworld-go-external
  name: helloworld-go-external-sfp8c
  namespace: default
  ownerReferences:
  - apiVersion: autoscaling.internal.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: PodAutoscaler
    name: helloworld-go-external-sfp8c
    uid: 2ab93862-b921-4c59-872c-508aedc9bf19
  resourceVersion: "141981"
  selfLink: /apis/networking.internal.knative.dev/v1alpha1/namespaces/default/serverlessservices/helloworld-go-external-sfp8c
  uid: 4640a2c6-9699-4bea-819a-26baaf416d88
spec:
  ProtocolType: http1
  mode: Proxy
  objectRef:
    apiVersion: apps/v1
    kind: Deployment
    name: helloworld-go-external-sfp8c-deployment
status:
  conditions:
  - lastTransitionTime: "2019-12-05T07:26:32Z"
    message: Revision is backed by Activator
    reason: ActivatorEndpointsPopulated
    severity: Info
    status: "True"
    type: ActivatorEndpointsPopulated
  - lastTransitionTime: "2019-12-09T09:54:10Z"
    message: K8s Service is not ready
    reason: NoHealthyBackends
    status: Unknown
    type: EndpointsPopulated
  - lastTransitionTime: "2019-12-09T09:54:10Z"
    message: K8s Service is not ready
    reason: NoHealthyBackends
    status: Unknown
    type: Ready
  observedGeneration: 2
  privateServiceName: helloworld-go-external-sfp8c-private
  serviceName: helloworld-go-external-sfp8c



2.1 创建一个同名service

[root@k8s-1 ~]# kubectl get svc -oyaml helloworld-go-external-sfp8c
apiVersion: v1
kind: Service
metadata:
  annotations:
    autoscaling.knative.dev/class: kpa.autoscaling.knative.dev
    serving.knative.dev/creator: kubernetes-admin
  creationTimestamp: "2019-12-05T07:11:48Z"
  labels:
    app: helloworld-go-external-sfp8c
    networking.internal.knative.dev/serverlessservice: helloworld-go-external-sfp8c
    networking.internal.knative.dev/serviceType: Public
    serving.knative.dev/configuration: helloworld-go-external
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/revision: helloworld-go-external-sfp8c
    serving.knative.dev/revisionUID: eaa0e7aa-3c44-45d9-8c97-467a51716793
    serving.knative.dev/service: helloworld-go-external
  name: helloworld-go-external-sfp8c
  namespace: default
  ownerReferences:
  - apiVersion: networking.internal.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: ServerlessService
    name: helloworld-go-external-sfp8c
    uid: 4640a2c6-9699-4bea-819a-26baaf416d88
  resourceVersion: "111055"
  selfLink: /api/v1/namespaces/default/services/helloworld-go-external-sfp8c
  uid: 13f1a086-8e76-48c6-b131-d0547f8a5b7c
spec:
  clusterIP: 10.96.36.111
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8012
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

注意到该svc没有

selector

。同时创建一个ep。

[root@k8s-1 ~]# kubectl get ep helloworld-go-external-sfp8c -oyaml
apiVersion: v1
kind: Endpoints
metadata:
  annotations:
    autoscaling.knative.dev/class: kpa.autoscaling.knative.dev
    serving.knative.dev/creator: kubernetes-admin
  creationTimestamp: "2019-12-05T07:11:48Z"
  labels:
    app: helloworld-go-external-sfp8c
    networking.internal.knative.dev/serverlessservice: helloworld-go-external-sfp8c
    networking.internal.knative.dev/serviceType: Public
    serving.knative.dev/configuration: helloworld-go-external
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/revision: helloworld-go-external-sfp8c
    serving.knative.dev/revisionUID: eaa0e7aa-3c44-45d9-8c97-467a51716793
    serving.knative.dev/service: helloworld-go-external
  name: helloworld-go-external-sfp8c
  namespace: default
  ownerReferences:
  - apiVersion: networking.internal.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: ServerlessService
    name: helloworld-go-external-sfp8c
    uid: 4640a2c6-9699-4bea-819a-26baaf416d88
  resourceVersion: "141888"
  selfLink: /api/v1/namespaces/default/endpoints/helloworld-go-external-sfp8c
  uid: ab174892-34b3-48a6-9bb8-72b1fba5d21c
subsets:
- addresses:
  - ip: 192.138.200.204
    nodeName: k8s-2
    targetRef:
      kind: Pod
      name: activator-77fc555665-78vgs
      namespace: knative-serving
      resourceVersion: "141858"
      uid: d0856f3f-f6cd-4a39-9e69-24655aa5e166
  ports:
  - name: http
    port: 8012
    protocol: TCP

对应的ep是activator-77fc555665-78vgs。



2.2 创建private svc

[root@k8s-1 ~]# kubectl get svc helloworld-go-external-sfp8c-private -oyaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    autoscaling.knative.dev/class: kpa.autoscaling.knative.dev
    serving.knative.dev/creator: kubernetes-admin
  creationTimestamp: "2019-12-05T07:11:48Z"
  labels:
    app: helloworld-go-external-sfp8c
    networking.internal.knative.dev/serverlessservice: helloworld-go-external-sfp8c
    networking.internal.knative.dev/serviceType: Private
    serving.knative.dev/configuration: helloworld-go-external
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/revision: helloworld-go-external-sfp8c
    serving.knative.dev/revisionUID: eaa0e7aa-3c44-45d9-8c97-467a51716793
    serving.knative.dev/service: helloworld-go-external
  name: helloworld-go-external-sfp8c-private
  namespace: default
  ownerReferences:
  - apiVersion: networking.internal.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: ServerlessService
    name: helloworld-go-external-sfp8c
    uid: 4640a2c6-9699-4bea-819a-26baaf416d88
  resourceVersion: "111050"
  selfLink: /api/v1/namespaces/default/services/helloworld-go-external-sfp8c-private
  uid: d9b5f2c0-9e77-4f1d-beb6-f5fc070edfec
spec:
  clusterIP: 10.96.169.88
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8012
  - name: queueadm-port
    port: 8022
    protocol: TCP
    targetPort: 8022
  selector:
    serving.knative.dev/revisionUID: eaa0e7aa-3c44-45d9-8c97-467a51716793
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

这个svc是带selector,其ep的targetPort对应应用pod中的queue容器。



3 revision

处理revision资源,对于每个revision要经过如下的处理。

phases := []struct {
		name string
		f    func(context.Context, *v1alpha1.Revision) error
	}{{
		name: "image digest",
		f:    c.reconcileDigest,
	}, {
		name: "user deployment",
		f:    c.reconcileDeployment,
	}, {
		name: "image cache",
		f:    c.reconcileImageCache,
	}, {
		name: "PA",
		f:    c.reconcilePA,
	}}
  • image digest,设置rev.Status.ImageDigest,内容是“镜像名称@镜像id”
  • user deployment,创建deployment
  • image cache,创建image(images.caching.internal.knative.dev)
[root@k8s-1 ~]# kubectl get image helloworld-go-external-sfp8c-cache -oyaml
apiVersion: caching.internal.knative.dev/v1alpha1
kind: Image
metadata:
  annotations:
    serving.knative.dev/creator: kubernetes-admin
  creationTimestamp: "2019-12-05T07:11:48Z"
  generation: 1
  labels:
    app: helloworld-go-external-sfp8c
    serving.knative.dev/configuration: helloworld-go-external
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/revision: helloworld-go-external-sfp8c
    serving.knative.dev/revisionUID: eaa0e7aa-3c44-45d9-8c97-467a51716793
    serving.knative.dev/service: helloworld-go-external
  name: helloworld-go-external-sfp8c-cache
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: Revision
    name: helloworld-go-external-sfp8c
    uid: eaa0e7aa-3c44-45d9-8c97-467a51716793
  resourceVersion: "111029"
  selfLink: /apis/caching.internal.knative.dev/v1alpha1/namespaces/default/images/helloworld-go-external-sfp8c-cache
  uid: c94f9ec3-998f-406c-8723-ad19a02221a9
spec:
  image: registry.cn-beijing.aliyuncs.com/zhangoic/helloworld-go@sha256:5ea96ba4b872685ff4ddb5cd8d1a97ec18c18fae79ee8df0d29f446c5efe5f50
  • PA,创建podautoscalers(podautoscalers.autoscaling.internal.knative.dev )
[root@k8s-1 ~]# kubectl get PodAutoscaler -oyaml helloworld-go-external-sfp8c
apiVersion: autoscaling.internal.knative.dev/v1alpha1
kind: PodAutoscaler
metadata:
  annotations:
    autoscaling.knative.dev/class: kpa.autoscaling.knative.dev
    autoscaling.knative.dev/metric: concurrency
    serving.knative.dev/creator: kubernetes-admin
  creationTimestamp: "2019-12-05T07:11:48Z"
  generation: 12
  labels:
    app: helloworld-go-external-sfp8c
    serving.knative.dev/configuration: helloworld-go-external
    serving.knative.dev/configurationGeneration: "1"
    serving.knative.dev/revision: helloworld-go-external-sfp8c
    serving.knative.dev/revisionUID: eaa0e7aa-3c44-45d9-8c97-467a51716793
    serving.knative.dev/service: helloworld-go-external
  name: helloworld-go-external-sfp8c
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: Revision
    name: helloworld-go-external-sfp8c
    uid: eaa0e7aa-3c44-45d9-8c97-467a51716793
  resourceVersion: "141986"
  selfLink: /apis/autoscaling.internal.knative.dev/v1alpha1/namespaces/default/podautoscalers/helloworld-go-external-sfp8c
  uid: 2ab93862-b921-4c59-872c-508aedc9bf19
spec:
  protocolType: http1
  reachability: Reachable
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: helloworld-go-external-sfp8c-deployment
status:
  actualScale: 0
  conditions:
  - lastTransitionTime: "2019-12-09T09:54:08Z"
    message: The target is not receiving traffic.
    reason: NoTraffic
    status: "False"
    type: Active
  - lastTransitionTime: "2019-12-09T09:54:08Z"
    message: The target is not receiving traffic.
    reason: NoTraffic
    status: "False"
    type: Ready
  desiredScale: 0
  metricsServiceName: helloworld-go-external-sfp8c-metrics
  observedGeneration: 12
  serviceName: helloworld-go-external-sfp8c



4 config

处理config资源。根据config创建revision,更新config.status.conditions,该condition记录了revison的状态。config跟踪的是最新的revison。

[root@k8s-1 ~]# kubectl get config helloworld-go-external -oyaml
apiVersion: serving.knative.dev/v1
kind: Configuration
metadata:
  annotations:
    serving.knative.dev/creator: kubernetes-admin
    serving.knative.dev/lastModifier: kubernetes-admin
  creationTimestamp: "2019-12-05T07:11:43Z"
  generation: 1
  labels:
    serving.knative.dev/route: helloworld-go-external
    serving.knative.dev/service: helloworld-go-external
  name: helloworld-go-external
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: Service
    name: helloworld-go-external
    uid: 3b074cc8-417e-4b3a-afa2-979e59eb9c03
  resourceVersion: "111096"
  selfLink: /apis/serving.knative.dev/v1/namespaces/default/configurations/helloworld-go-external
  uid: bd90054a-4e84-45ef-a74a-465100d5c8aa
spec:
  template:
    metadata:
      creationTimestamp: null
    spec:
      containerConcurrency: 0
      containers:
      - env:
        - name: TARGET
          value: Go Sample v1
        image: registry.cn-beijing.aliyuncs.com/zhangoic/helloworld-go:latest
        imagePullPolicy: IfNotPresent
        name: user-container
        readinessProbe:
          successThreshold: 1
          tcpSocket:
            port: 0
        resources: {}
      timeoutSeconds: 300
status:
  conditions:
  - lastTransitionTime: "2019-12-05T07:26:30Z"
    status: "True"
    type: Ready
  latestCreatedRevisionName: helloworld-go-external-sfp8c
  latestReadyRevisionName: helloworld-go-external-sfp8c
  observedGeneration: 1



5 route

处理route资源。配置traffic,创建ExternalName类型的svc,猜测是把这个名称占住。

创建ingress(ingresses.networking.internal.knative.dev)

[root@k8s-1 ~]# kubectl get ingresses.networking.internal.knative.dev helloworld-go -oyaml
apiVersion: networking.internal.knative.dev/v1alpha1
kind: Ingress
metadata:
  annotations:
    networking.knative.dev/ingress.class: istio.ingress.networking.knative.dev
    serving.knative.dev/creator: kubernetes-admin
    serving.knative.dev/lastModifier: kubernetes-admin
  creationTimestamp: "2019-12-10T04:09:36Z"
  generation: 5
  labels:
    serving.knative.dev/route: helloworld-go
    serving.knative.dev/routeNamespace: default
    serving.knative.dev/service: helloworld-go
  name: helloworld-go
  namespace: default
  ownerReferences:
  - apiVersion: serving.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: Route
    name: helloworld-go
    uid: a55462e0-cd93-4021-9d9d-0ea11df71618
  resourceVersion: "200237"
  selfLink: /apis/networking.internal.knative.dev/v1alpha1/namespaces/default/ingresses/helloworld-go
  uid: c65ad5bb-06b6-4f13-ba34-ed827362c98d
spec:
  rules:
  - hosts:
    - helloworld-go.default.svc.cluster.local
    - helloworld-go.default.example.com
    http:
      paths:
      - retries:
          attempts: 3
          perTryTimeout: 10m0s
        splits:
        - appendHeaders:
            Knative-Serving-Namespace: default
            Knative-Serving-Revision: helloworld-go-v1
          percent: 50
          serviceName: helloworld-go-v1
          serviceNamespace: default
          servicePort: 80
        - appendHeaders:
            Knative-Serving-Namespace: default
            Knative-Serving-Revision: helloworld-go-v2
          percent: 50
          serviceName: helloworld-go-v2
          serviceNamespace: default
          servicePort: 80
        timeout: 10m0s
    visibility: ExternalIP
  - hosts:
    - candidate-helloworld-go.default.svc.cluster.local
    - candidate-helloworld-go.default.example.com
    http:
      paths:
      - retries:
          attempts: 3
          perTryTimeout: 10m0s
        splits:
        - appendHeaders:
            Knative-Serving-Namespace: default
            Knative-Serving-Revision: helloworld-go-v2
          percent: 100
          serviceName: helloworld-go-v2
          serviceNamespace: default
          servicePort: 80
        timeout: 10m0s
    visibility: ExternalIP
  - hosts:
    - current-helloworld-go.default.svc.cluster.local
    - current-helloworld-go.default.example.com
    http:
      paths:
      - retries:
          attempts: 3
          perTryTimeout: 10m0s
        splits:
        - appendHeaders:
            Knative-Serving-Namespace: default
            Knative-Serving-Revision: helloworld-go-v1
          percent: 100
          serviceName: helloworld-go-v1
          serviceNamespace: default
          servicePort: 80
        timeout: 10m0s
    visibility: ExternalIP
  - hosts:
    - latest-helloworld-go.default.svc.cluster.local
    - latest-helloworld-go.default.example.com
    http:
      paths:
      - retries:
          attempts: 3
          perTryTimeout: 10m0s
        splits:
        - appendHeaders:
            Knative-Serving-Namespace: default
            Knative-Serving-Revision: helloworld-go-v2
          percent: 100
          serviceName: helloworld-go-v2
          serviceNamespace: default
          servicePort: 80
        timeout: 10m0s
    visibility: ExternalIP
  visibility: ExternalIP
status:
  conditions:
  - lastTransitionTime: "2019-12-09T19:33:26Z"
    status: "True"
    type: LoadBalancerReady
  - lastTransitionTime: "2019-12-09T17:51:09Z"
    status: "True"
    type: NetworkConfigured
  - lastTransitionTime: "2019-12-09T19:33:26Z"
    status: "True"
    type: Ready
  loadBalancer:
    ingress:
    - domainInternal: istio-ingressgateway.istio-system.svc.cluster.local
  observedGeneration: 5
  privateLoadBalancer:
    ingress:
    - domainInternal: cluster-local-gateway.istio-system.svc.cluster.local
  publicLoadBalancer:
    ingress:
    - domainInternal: istio-ingressgateway.istio-system.svc.cluster.local



6 service

处理ksvc资源,根据定义创建config和route。

[root@k8s-1 ~]# kubectl get ksvc -oyaml helloworld-go
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"serving.knative.dev/v1","kind":"Service","metadata":{"annotations":{},"name":"helloworld-go","namespace":"default"},"spec":{"template":{"metadata":{"name":"helloworld-go-v2"},"spec":{"containers":[{"env":[{"name":"TARGET","value":"Go Sample v2"}],"image":"registry.cn-beijing.aliyuncs.com/zhangoic/helloworld-go:latest","imagePullPolicy":"IfNotPresent"}]}},"traffic":[{"percent":50,"revisionName":"helloworld-go-v1","tag":"current"},{"percent":50,"revisionName":"helloworld-go-v2","tag":"candidate"},{"latestRevision":true,"percent":0,"tag":"latest"}]}}
    serving.knative.dev/creator: kubernetes-admin
    serving.knative.dev/lastModifier: kubernetes-admin
  creationTimestamp: "2019-12-10T04:09:28Z"
  generation: 5
  name: helloworld-go
  namespace: default
  resourceVersion: "200240"
  selfLink: /apis/serving.knative.dev/v1/namespaces/default/services/helloworld-go
  uid: 5df3ebe6-0447-47c2-a062-fa2aaef4099c
spec:
  template:
    metadata:
      creationTimestamp: null
      name: helloworld-go-v2
    spec:
      containerConcurrency: 0
      containers:
      - env:
        - name: TARGET
          value: Go Sample v2
        image: registry.cn-beijing.aliyuncs.com/zhangoic/helloworld-go:latest
        imagePullPolicy: IfNotPresent
        name: user-container
        readinessProbe:
          successThreshold: 1
          tcpSocket:
            port: 0
        resources: {}
      timeoutSeconds: 300
  traffic:
  - latestRevision: false
    percent: 50
    revisionName: helloworld-go-v1
    tag: current
  - latestRevision: false
    percent: 50
    revisionName: helloworld-go-v2
    tag: candidate
  - latestRevision: true
    percent: 0
    tag: latest
status:
  address:
    url: http://helloworld-go.default.svc.cluster.local
  conditions:
  - lastTransitionTime: "2019-12-09T17:55:35Z"
    status: "True"
    type: ConfigurationsReady
  - lastTransitionTime: "2019-12-09T19:33:26Z"
    status: "True"
    type: Ready
  - lastTransitionTime: "2019-12-09T19:33:26Z"
    status: "True"
    type: RoutesReady
  latestCreatedRevisionName: helloworld-go-v2
  latestReadyRevisionName: helloworld-go-v2
  observedGeneration: 5
  traffic:
  - latestRevision: false
    percent: 50
    revisionName: helloworld-go-v1
    tag: current
    url: http://current-helloworld-go.default.example.com
  - latestRevision: false
    percent: 50
    revisionName: helloworld-go-v2
    tag: candidate
    url: http://candidate-helloworld-go.default.example.com
  - latestRevision: true
    percent: 0
    revisionName: helloworld-go-v2
    tag: latest
    url: http://latest-helloworld-go.default.example.com
  url: http://helloworld-go.default.example.com

参考:https://juejin.im/post/5ce3af5ff265da1bc41427a5



版权声明:本文为zhangoic原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。