You can create Elastic Container Instances (ECIs) for a Knative service. Knative and virtual kubelet then can use ECIs to create pods based on demands. This topic describes how to create an ECI in a Knative service in two ways.

Method 1: Directly create an ECI for a Knative service

  • If the virtual:kubelet label is not added to a namespace, you can create an Elastic Container Instance (ECI) by setting the following configurations when you create resource objects for Knative services.
    • Specify the ECI specification in an annotation.
    • Specify virtual-kubelet in the nodeAffinity parameter.
    • Specify virtual-kubelet.io/provider in the tolerations parameter.
    Set the parameters based on the following template:
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go
      namespace: vk
    spec:
      template:
        metadata:
          annotations:
            k8s.aliyun.com/eci-use-specs: "2-4Gi"
        spec:
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                - matchExpressions:
                  - key: type
                    operator: In
                    values: 
                    - virtual-kubelet   
          tolerations:
          - key: virtual-kubelet.io/provider
            operator: Exists                
          containers:
          - env:
            - name: TARGET
              value: "Knative"
            image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56
  • If the virtual-kubelet label is added to the namespace, you only need to specify the ECI specification in the annotation to directly create an ECI.
    kubectl label namespace vk virtual-node-affinity-injection=enabled
    Set the parameters based on the following template:
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go
      namespace: vk
    spec:
      template:
        metadata:
          annotations:
            k8s.aliyun.com/eci-use-specs: "2-4Gi"
        spec:         
          containers:
          - env:
            - name: TARGET
              value: "Knative"
            image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56

Method 2: Specify an ECS instance to automatically create an ECI for a Knative service

You can specify a labelled ECS instance in the nodeAffinity parameter to automatically create an ECI for a Knative service. This way, the pod is deployed in both the ECS instance and ECI.

  1. Attach a label to the ECS instance. For more information, see Manage labels.
    The resource-role:ecs label is attached to the ECS instance in this example.
  2. In the resource object template, set the nodeAffinity parameter.
    Specify the labelled ECS instance and virtual-kubelet in the nodeAffinity parameter.
    Set the parameters based on the following template:
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go
    spec:
      template:
        metadata:
          annotations:
            k8s.aliyun.com/eci-use-specs: "2-4Gi"
       spec:
          containerConcurrency: 10
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                - matchExpressions:
                  - key: resource-role
                    operator: In
                    values:
                    - ecs
                - matchExpressions:
                  - key: type
                    operator: In
                    values: 
                    - virtual-kubelet
          tolerations:
          - key: virtual-kubelet.io/provider
            operator: Exists                 
          containers:
          - env:
            - name: TARGET
              value: "Knative"
            image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56

Result

Run the following command to verify that two pods are created in the ECI.
[root@iZbp1djyqnxhjfv9h9s0edZ dmy]# kubectl -n vk get pod -o wide
NAME                                              READY   STATUS    RESTARTS   AGE   IP             NODE                 NOMINATED NODE   READINESS GATES
helloworld-go-dqqhv-deployment-6d54c9c8dc-hkjwn   2/2     Running   0          40s   192.168.1.72   virtual-node-eci-0   <none>           <none>