All Products
Search
Document Center

Container Service for Kubernetes:Use image caches to accelerate pod creation for Knative Services

Last Updated:Nov 05, 2024

When deploying services through images in Knative, the Container Service for Kubernetes (ACK) cluster pulls images from remote repositories and creates new pods. Large images can lead to extended image pulling times. We recommend that you use the ImageCache feature provided by Elastic Container Instance to create cached snapshots of the images you intend to use. Then, create pods (elastic container instances) based on these snapshots to reduce startup time.

Introduction

It requires a large amount of time to download an image during the pod creation process. To accelerate pod creation, Elastic Container Instance provides image caches. You can create a cache snapshot from an image and then use the cache snapshot to deploy a pod on an elastic container instance. This reduces the image layers that you need to download and therefore accelerates pod creation. For example, if you want to create a pod by using a Docker image for Apache Flink of about 386.26 MB, 50 seconds is required to pull the image from Docker Hub. If you create the pod by using an image cache, only 5 seconds is required to pull the image.

Important

These data is theoretical. The amount of time that is reduced varies based on the number and size of images that are required and the quality of the network connection to the image repository.

In an ACK cluster, elastic container instances are cached as a type of Custom Resource Definition (CRD) object named ImageCache. The ImageCache CRD is a cluster-level resource in a Kubernetes cluster and can be shared across all namespaces within the cluster.

For more information about how to create and use image caches and the billing methods, see Overview of image caches.

Step 1: Create an ImageCache

  1. Create an imagecache-secrets-test.yaml file based on the following code block to accelerate image pulling and reduce the instance startup time:

    apiVersion: eci.alibabacloud.com/v1
    kind: ImageCache
    metadata:
      name: imagecache-sample-test
      annotations:
        k8s.aliyun.com/eci-image-cache: "true" # Enable the reuse of ImageCaches. 
    spec:
      images:
      - registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
      - registry.cn-shanghai.aliyuncs.com/eci_open/busybox:1.30
      imageCacheSize:
       25
      retentionDays:
       7
    Note

    If the image that you want to cache contains image layers that can be found in existing ImageCaches, you can enable the reuse of ImageCaches to accelerate image caching.

    For more information about ImageCache and how to configure parameters in the preceding sample code, see Manage ImageCaches.

  2. Run the following command to create an ImageCache:

    kubectl create -f imagecache-secrets-test.yaml
  3. Run the following command to query an ImageCache:

    kubectl get imagecache imagecache-sample-test

    Expected output:

    NAME                              AGE   CACHEID                               PHASE   PROGRESS
    imagecache-sample-test            20h   imc-2zeditzeoemfhqor****              Ready    100%

Step 2: Use an ImageCache to accelerate pod creation

When you create a pod from an ImageCache, you can add annotations to the pod metadata to specify the ImageCache that you want to use or set the pod to automatically select an ImageCache. The following list describes the annotations:

  • k8s.aliyun.com/eci-image-snapshot-id: specifies the ImageCache that you want to use.

  • k8s.aliyun.com/eci-image-cache: automatically selects an optimal ImageCache.

Note

If you specify k8s.aliyun.com/eci-image-snapshot-id and k8s.aliyun.com/eci-image-cache at the same time, only k8s.aliyun.com/eci-image-snapshot-id takes effect.

Specify an ImageCache

When you create a Knative Service, you can use the k8s.aliyun.com/eci-image-snapshot-id annotation to specify the ImageCache that you want to use.

Important

Make sure that the specified ImageCache is in the Ready state. Otherwise, the pod fails to be created.

Example:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
spec:
  template:
    metadata:
      labels:
        app: helloworld-go
      annotations:
        k8s.aliyun.com/eci-image-snapshot-id: imc-2ze5tm5gehgtiiga****  # Specify the ImageCache that you want to use. 
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56

Automatically select an ImageCache

When you create a Knative Service, you can use the k8s.aliyun.com/eci-image-cache annotation to set the pod to automatically select an ImageCache.

After this annotation is specified, Elastic Container Instance selects an ImageCache based on the following rules:

  1. Elastic Container Instance filters all ImageCaches in the region. The size of each ImageCache is not greater than the size of the temporary storage space of an elastic container instance.

  2. Elastic Container Instance selects an optimal ImageCache based on the following attributes in descending order of priority: the match degree, the cache size, and the creation time.

    • Match degree: the match degree between an ImageCache and the specified image registry and image version. The ImageCache that has the highest match degree is used to create the pod.

    • Cache size: the size of the ImageCache. The ImageCache whose size is closest to the size of the specified image is used to create the pod.

    • Creation time: the time when the ImageCache is created. The most recently created ImageCache is used to create the pod.

    Note

    If no ImageCache is matched, the specified image is automatically cached when the pod is created. In this case, the system pulls the image from the specified registry. We recommend that you set the image pull policy to IfNotPresent. This prevents repetitive downloads of the same image layer and improves caching efficiency.

Example:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
spec:
  template:
    metadata:
      labels:
        app: helloworld-go
      annotations:
        k8s.aliyun.com/eci-image-cache: "true"    # Automatically select an ImageCache. 
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56