All Products
Search
Document Center

Elastic Container Instance:Use ImageCache to accelerate the creation of pods

Last Updated:Nov 01, 2023

Alibaba Cloud provides the ImageCache feature of Elastic Container Instance in the manner of ImageCache CustomResourceDefinition (CRD). This way, you can use the ImageCache feature of Elastic Container Instance to accelerate the creation of pods in Kubernetes. This topic describes how to use ImageCache to accelerate the creation of pods.

Configuration description

ImageCache is a cluster-level resource. You can use ImageCache to accelerate the creation of pods in different namespaces in a cluster.

When you create a pod by using ImageCache, you can add annotations to the metadata in the pod configuration file to enable automatic match of image caches or specify the image cache that you want to use. The following table describes the annotations:

Note
  • 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 or imc-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 the imc-perfect-match parameter. If you configure both the two parameters, priority is given to the imc-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.

Important
  • 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.

Configuration examples

When you create a pod by using 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.

Example 1: Enable automatic match of image caches

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 image cache to create the pod

Important

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