Alibaba Cloud offers the image cache feature of Elastic Container Instance by using the ImageCache CustomResourceDefinition (CRD). This feature accelerates the creation of elastic container instances in Kubernetes clusters. This topic describes the features, creation and use of ImageCache.
Introduction to ImageCache
An ImageCache represents a resource type defined by Elastic Container Instance, with each ImageCache corresponding to an image cache. Before you run containers, the elastic container instance must pull the specified images. Pulling large images over an unstable network can significantly delay the startup time of an elastic container instance. To mitigate this, Elastic Container Instance offers the image cache feature. You can create a cache snapshot from an image and then use the cache snapshot to create an elastic container instance. This eliminates or reduces the need to download image layers and therefore accelerates the creation of elastic container instances. For more information, see Overview of image caches.
The amount of time reduced varies based on the number of the images in the image cache, the size of the image used to create an elastic container instance, and the network over which the elastic container instance connects to the image repository.
By default, ACK Serverless clusters are deployed with the ImageCache CRD. Run the command kubectl get crd/imagecaches.eci.alibabacloud.com
to confirm if the cluster supports ImageCache.
If information about imagecaches.eci.alibabacloud.com is returned, the cluster supports ImageCache.
If an error message is returned, it indicates that the cluster does not support ImageCache. To resolve this, update the ACK Virtual Node component to the latest version.
Billing
Fees for associated resources may apply during the creation and use of ImageCache. For more information, see Image caches billing.
Create an ImageCache
You can use one of the following methods to create an ImageCache:
Method | Description |
Automatic creation | You can enable automatic match of ImageCaches when you create an elastic container instance. Then, if no ImageCache is matched, the system automatically creates an ImageCache while the system creates the elastic container instance. This cache can then be used to accelerate future creations of elastic container instances with the same image. |
Manual creation | If you are creating an elastic container instance for the first time and you want the creation to be as quick as possible, you can manually create an ImageCache in advance by using a YAML template. For more information, see Manage ImageCaches and ImageCache annotations. |
Except for acceleration during first-time creation of an elastic container instance, we recommend that you use automatically-created ImageCaches to save costs.
For more information about the differences between manually and automatically created ImageCaches, see Methods to create an ImageCache.
Use an ImageCache
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 by using an ImageCache, take note of the following items:
Specify an image in the image cache for containers in the pod to improve the match degree.
Set the image pulling policy (ImagePullPolicy) of containers in the pod to IfNotPresent to prevent repeated downloads of image layers.
When you create a pod from an ImageCache, you can add annotations to the metadata in the pod configuration file to enable automatic match of ImageCache or specify the ImageCache that you want to use. The following list describes the annotations:
We recommend that you enable automatic match of image caches. If you accelerate the creation of pods by enabling automatic match of image caches and specifying the image cache that you want to use, the latter takes precedence. For more information, see Overview of the ImageCache feature.
You can configure the
imc-perfect-match
orimc-match-count-request
parameter if you want to use ImageCache for your business by enabling automatic match of image caches. We recommend that you configure theimc-perfect-match
parameter. If you configure both the two parameters, priority is given to theimc-perfect-match
parameter.
Usage method of ImageCache | Annotation | Example value | Description |
Enable automatic match of image caches | k8s.aliyun.com/eci-auto-imc | "true" | Specifies whether to automatically match image caches. The default value is true, which indicates that the system automatically matches the most suitable image cache to create the pod based on the matching policy. The system selects the image cache based on the following order: the matching degree of the image, the size of the image, and the point in time when the image is created. If no image cache is exactly matched, the system automatically creates a corresponding image cache when the system creates the pod. |
k8s.aliyun.com/imc-perfect-match | "true" | Specifies whether all container images in the pod must match the image cache. Default value: false. | |
k8s.aliyun.com/imc-match-count-request | "2" | Specifies the number of container images in the pod that you want to exactly match the image cache. | |
Specify an image cache that you want to use | k8s.aliyun.com/eci-imc-id | imc-2zebxkiifuyzzlhl**** | Specifies an image cache to create the pod. |
Annotations must be added to the metadata in the configuration file of the pod. For example, when you create a Deployment, you must add annotations in the spec.template.metadata section.
To use features of Elastic Container Instance, you can add annotations only when you create Elastic Container Instance-based pods. If you add or modify annotations when you update pods, these annotations do not take effect.
Example 1: Automatically match an ImageCache
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
labels:
app: test
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
name: nginx-test
labels:
app: nginx
alibabacloud.com/eci: "true"
annotations:
k8s.aliyun.com/eci-auto-imc: "true" # Enables automatic match of image caches
k8s.aliyun.com/imc-perfect-match: "true" # Specifies that all container images in the pod must match the image cache.
spec:
containers:
- name: nginx
image: registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
ports:
- containerPort: 80
imagePullPolicy: IfNotPresent
- name: busybox
image: registry.cn-shanghai.aliyuncs.com/eci_open/busybox:1.30
command: ["sleep"]
args: ["999999"]
imagePullPolicy: IfNotPresent
Example 2: Specify an ImageCache
Make sure that the specified image cache is in the Ready state. Otherwise, the pod fails to be created.
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
labels:
app: test
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
name: nginx-test
labels:
app: nginx
alibabacloud.com/eci: "true"
annotations:
k8s.aliyun.com/eci-imc-id: imc-2ze5tm5gehgtiiga**** # Specifies an image cache.
spec:
containers:
- name: nginx
image: registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
ports:
- containerPort: 80
imagePullPolicy: IfNotPresent
The Elastic Container Instance Effect of eci-profile can automatically use the image cache feature by dynamically adding ImageCache-related annotations to the pods. For more information, see Configure eci-profile to automatically use the ImageCache feature.