天天看點

knative serving中的幾個controller

本文基于knative v0.10

1 gc

控制revision的數量。

knative controller關聯了一個configmap:knative-serving/config-gc,它包含了gc控制器的參數,用于控制revision數量。

2 serverlessservice

處理serverlessservices.networking.internal.knative.dev資源(簡寫sks)。

[[email protected] ~]# 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

[[email protected] ~]# 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。

[[email protected] ~]# 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

[[email protected] ~]# 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)
[[email protected] ~]# 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/[email protected]:5ea96ba4b872685ff4ddb5cd8d1a97ec18c18fae79ee8df0d29f446c5efe5f50
           
  • PA,建立podautoscalers(podautoscalers.autoscaling.internal.knative.dev )
[[email protected] ~]# 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。

[[email protected] ~]# 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)

[[email protected] ~]# 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。

[[email protected] ~]# 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

繼續閱讀