Alibaba Cloud offers the image cache feature for Elastic Container Instance by using the ImageCache CustomResourceDefinition (CRD). This feature reduces the time required for pulling container images from remote repositories each time an elastic container instance is created in Kubernetes clusters. This topic describes how to create and use an ImageCache for faster elastic container instances creation.
What is an ImageCache
An ImageCache represents a resource defined by an elastic container instance that caches container image layers for faster instance creation. Each ImageCache corresponds to an image cache. When you create an elastic container instance, instead of downloading all image layers, ImageCache allows the instance to use pre-cached layers. This feature reduces the time required to start the container, especially for large images or in unstable network conditions. For more information, see Overview of image caches.
The amount of time reduced depends on the number and size of the images, and the network condition.
Billing
Fees for associated resources may apply during the creation and use of ImageCache. For more information, see Image caches billing.
Procedure
Step 1: Deploy ImageCache CRD
Run the following command to check if your cluster supports ImageCache:
kubectl get crd/imagecaches.eci.alibabacloud.com
If information about
imagecaches.eci.alibabacloud.com
is returned, your cluster supports ImageCache.If an error message is returned, update the ACK Virtual Node component to the latest version to enable ImageCache.
Step 2: Create an ImageCache
You can create an ImageCache in two ways: automatically or manually. For cost efficiency, we recommend that you use automatic ImageCache creation unless you need immediate speed up for new elastic container instances. For more information about the differences between manually and automatically created ImageCaches, see Methods to create an image cache.
Method | Description |
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 Image cache 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.
Step 3: Use an ImageCache to create an elastic container instance
When you create an elastic container instance from an ImageCache, you can either automatically match an ImageCache or manually specify one.
We recommend that you enable automatically match and configure the imc-perfect-match
parameter for stricter matching. If both imc-perfect-match
and imc-match-count-request
are configured, the former takes precedence. If both automatic matching and manual specification are configured, the latter takes precedence. For more information, see Overview of image caches.
The following table describes the key annotations:
Match method | Annotation | Example | Description |
Match method | Annotation | Example | Description |
Automatic matching | 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 ImageCache to create the elastic container instance based on the matching policy. The system selects the ImageCache 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 ImageCache is exactly matched, the system automatically creates one when the system creates the elastic container instance. |
k8s.aliyun.com/imc-perfect-match | "true" | Specifies whether all container images in the pod must fully match the ImageCache. Default value: false. | |
k8s.aliyun.com/imc-match-count-request | "2" | Specifies the number of container images in the pod that must exactly match the ImageCache. | |
Manual specification | k8s.aliyun.com/eci-imc-id | imc-2zebxkiifuyzzlhl**** | Specifies an image cache to create the pod. |
Example configurations
ImageCache is a cluster-level resource and can be used across different namespaces.
Annotations must be added to the
metadata
section in the pod configuration file. For example, in a Deployment, add annotations underspec.template.metadata
.These annotations are only effective when you create an elastic container instance. Adding or modifying annotations for an existing elastic container instance will not take effect.
The Elastic Container Instance Effect feature in eci-profile dynamically adds ImageCache-related annotations to pods based on predefined conditions. If these annotations do not exist in the pod YAML file, you can configure the eci-profile, and the system will automatically apply them to all matching pods without requiring manual updates of each pod YAML file. For more information, see Configure eci-profile to automatically use the ImageCache feature.
Image matching: Specify images with ImageCaches to improve the match degree and take full advantage of caching.
Pulling policy: Set the
ImagePullPolicy
toIfNotPresent
to avoid downloading the same image layers.
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
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