Alibaba Cloud provides a type of Custom Resource Definition (CRD) object that you can use to reference image caches when you create pods on elastic container instances. This accelerates pod creation. This topic describes how to use image caches to accelerate pod creation for a Knative Service.
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.
- 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. Alibaba Cloud provides a type of CRD object named ImageCache that you can use to reference image caches when you create pods on elastic container instances.
- An ImageCache is a cluster-level resource in a Kubernetes cluster, and can be shared by all namespaces in the cluster.
Step 1: Create an ImageCache
- Create an imagecache-secrets-test.yaml file based on the following code block:
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: - centos:latest - busybox:latest imagePullSecrets: - default:secret1 - default:secret2 - kube-system:secret3 imageCacheSize: 25 retentionDays: 7Note 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.
- Run the following command to create an ImageCache:
kubectl create -f imagecache-secrets-test.yaml
- Run the following command to query an ImageCache:
kubectl get imagecache imagecache-sample-testExpected output:
NAME AGE CACHEID PHASE PROGRESS imagecache-sample-test 20h imc-2zeditzeoemfhqor**** Ready 100%
Step 2: Use an ImageCache to accelerate pod creation
An ImageCache is a cluster-level resource that you can use to accelerate the creation of pods in different namespaces.
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.
k8s.aliyun.com/eci-image-cacheat the same time, only k8s.aliyun.com/eci-image-snapshot-id takes effect.
- Method 1: Specify an ImageCache
When you create a Knative Service, you can use the
k8s.aliyun.com/eci-image-snapshot-idannotation to specify the ImageCache that you want to use.Notice 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
- Method 2: Automatically select an ImageCache
When you create a Knative Service, you can use the
k8s.aliyun.com/eci-image-cacheannotation 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:
- 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.
- 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
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.
- 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.
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
What to do next
- Run the following command to query all ImageCaches in a cluster:
kubectl get imagecache
- Run the following command to query information about an ImageCache:
kubectl get imagecache/<imagecache_name> -o yaml
<imagecache_name>with the name of the ImageCache that you want to query.
- Run the following command to delete an ImageCache:
kubectl delete imagecache/<imagecache_name>
<imagecache_name>with the name of the ImageCache that you want to delete.