All Products
Search
Document Center

Use ImageCache to accelerate the creation of pods

Last Updated: Dec 21, 2021

Alibaba Cloud provides the image cache feature of Elastic Container Instance in the manner of ImageCache CustomResourceDefinition (CRD). This way, you can use the image cache 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.

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 based on 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. Take note of the following annotations:

  • k8s.aliyun.com/eci-image-cache: Enables automatic match of ImageCache and select the most suitable ImageCache based on the match policy to create the pod. If no ImageCache is matched, the system automatically creates an ImageCache when the system creates the pod.

  • k8s.aliyun.com/eci-image-snapshot-id: specifies an ImageCache to create the pod.

Note

If both k8s.aliyun.com/eci-image-cache and k8s.aliyun.com/eci-image-snapshot-id are configured, k8s.aliyun.com/eci-image-snapshot-id is used. For more information, see Overview of the image cache feature.

When you create a pod by using ImageCache, we recommend that you take the following measures:

  • Specify an image in the ImageCache for the container to improve the match degree.

  • Set the image pulling policy (ImagePullPolicy) of the container to IfNotPresent to prevent repeated downloads of image layers.

Automatically match ImageCache

When you create a pod, you can add an annotation to enable automatic match of ImageCache to accelerate the creation of the pod. Then, Elastic Container Instance selects the most suitable ImageCache from your ImageCache list based on the match policies. ImageCache is matched based on the following elements in descending order of priority: the match degree of the ImageCache, the size of the ImageCache, and the point in time when the ImageCache is created.

Note

If no ImageCache is matched, the system automatically creates an ImageCache when the system creates the pod.

  • Sample Deployment

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
          annotations:
            k8s.aliyun.com/eci-image-cache: "true"   # Enables automatic match of ImageCache.
        spec:
          nodeName: virtual-kubelet
          containers:
          - name: nginx
            image: nginx:1.7.9
            imagePullPolicy: IfNotPresent
  • Sample pod

    apiVersion: v1
    kind: Pod
    metadata:
      annotations:
        k8s.aliyun.com/eci-image-cache: "true"  # Enables automatic match of ImageCache.
      name: nginx-auto-match
    spec:
      containers:
      - image: nginx:1.7.9
        imagePullPolicy: IfNotPresent
        name: nginx
        resources:
          limits:
            cpu: 300m
            memory: 200Mi
          requests:
            cpu: 200m
            memory: 100Mi
      nodeName: virtual-kubelet

Specify the ImageCache that is used to create the pod

When you create a pod, you can add an annotation to specify the ImageCache that is used to accelerate the creation of the pod.

Notice

Make sure that the specified ImageCache is in the Ready state. If the ImageCache is in another state, you cannot create the pod.

  • Sample Deployment

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
          annotations:
            k8s.aliyun.com/eci-image-snapshot-id: imc-2ze5tm5gehgtiiga****  # Specifies an ImageCache.
        spec:
          nodeName: virtual-kubelet
          containers:
          - name: nginx
            image: nginx:1.7.9
            imagePullPolicy: IfNotPresent
  • Sample pod

    apiVersion: v1
    kind: Pod
    metadata:
      annotations:
        k8s.aliyun.com/eci-image-snapshot-id: imc-2ze5tm5gehgtiiga****  # Specifies an ImageCache.
      name: nginx-imagecache-id
    spec:
      containers:
      - image: nginx:1.7.9
        imagePullPolicy: IfNotPresent
        name: nginx
        resources:
          limits:
            cpu: 300m
            memory: 200Mi
          requests:
            cpu: 200m
            memory: 100Mi
      nodeName: virtual-kubelet