Use an ImageCache to accelerate pod creation

更新时间:
复制 MD 格式

Alibaba Cloud offers the ECI image cache feature to Kubernetes users as an ImageCache Custom Resource Definition (CRD) to accelerate pod creation. This topic describes how to use an ImageCache to create pods more quickly.

Configuration description

An ImageCache is a cluster-level resource. It can be used to accelerate pod creation in different namespaces.

You can create pods using an ImageCache in two ways: automatic matching or explicit specification. You can configure this feature by adding annotations to the pod-level metadata. The relevant annotations are described below.

Note
  • We recommend using the automatic matching method. If both methods are configured, the explicit method takes precedence over the automatic matching method. For more information, see Image Cache Overview.

  • When you use the automatic matching method, if you want to use an ImageCache only when an exact match is found, set the imc-perfect-match or imc-match-count-request annotation. If you set both annotations, imc-perfect-match takes precedence.

Method

Configuration item

Example value

Description

Automatic matching

k8s.aliyun.com/eci-auto-imc

"true"

Specifies whether to automatically match an ImageCache. The default value is true. If this annotation is set to true, the system automatically matches the optimal ImageCache to create the pod based on a matching policy. The matching priorities are, in descending order: image match degree, matched image size, and creation time.

If no exact match is found, the system automatically creates a corresponding ImageCache when it creates the pod.

k8s.aliyun.com/imc-perfect-match

"true"

Specifies whether all container images in the pod must exactly match an ImageCache. The default value is false.

k8s.aliyun.com/imc-match-count-request

"2"

The number of container images in the pod that must exactly match an ImageCache.

Explicit specification

k8s.aliyun.com/eci-imc-id

imc-2zebxkiifuyzzlhl****

Explicitly specifies an ImageCache to use for pod creation.

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.

  • Elastic Container Instance-related annotations are only applied when a pod is created. Adding or modifying these annotations on an existing pod will have no effect.

Configuration examples

When you create a pod using an ImageCache, note the following:

  • Use images from an existing ImageCache for the containers in the pod to improve the match degree.

  • Set the image pull policy (ImagePullPolicy) for the containers in the pod to `IfNotPresent`. This prevents image layers from being repeatedly downloaded.

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"       # Enable automatic ImageCache matching
        k8s.aliyun.com/imc-perfect-match: "true"  # Require all container images in the pod to have an exact match
    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: Explicitly specify an ImageCache

Important

Make sure that the specified ImageCache is in the Ready state. If the ImageCache is in any other state, the pod creation fails.

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****  # Explicitly specify an ImageCache
    spec:
      containers:
      - name: nginx
        image: registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
        ports:
        - containerPort: 80
        imagePullPolicy: IfNotPresent