All Products
Search
Document Center

Use image caches to accelerate the creation of pods

Last Updated: May 19, 2022

Alibaba Cloud provides the image caching feature of Elastic Container Instance by using the ImageCache CustomResourceDefinition (CRD). You can use the image caching feature of Elastic Container Instance to accelerate the creation of pods in Kubernetes. This topic describes how to use image caches to accelerate the creation of pods.

Configuration description

An image cache is a cluster-level resource that you can use to accelerate the creation of pods in different namespaces.

You can enable automatic matching of an image cache or specify an image cache to accelerate the creation of pods by using annotations. You can add the following annotations to the metadata field in the pod configuration file:

  • k8s.aliyun.com/eci-image-cache: Automatically matches the most suitable image cache and uses it to accelerate the creation of the pod. If no image cache is matched, the system automatically creates an image cache when the system creates the pod.

  • k8s.aliyun.com/eci-image-snapshot-id: Specify an image cache to accelerate the creation of the pod.

Note

If both k8s.aliyun.com/eci-image-cache and k8s.aliyun.com/eci-image-snapshot-id are added, the k8s.aliyun.com/eci-image-snapshot-id annotation prevails. For more information, see Overview of the image caching feature.

When you create a pod by using an image cache, take note of the following items:

  • We recommend that you specify an image in an image cache for the container to improve the matching degree.

  • Set ImagePullPolicy of the container to IfNotPresent to prevent the system from repeatedly downloading image layers.

Automatic matching of an image cache

When you create a pod, you can add an annotation to enable automatic matching of an image cache to accelerate the creation of the pod. Elastic Container Instance selects the most suitable image cache from existing image caches based on the matching policies. 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.

Note

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

  • Deployment example

    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 matching of an image cache.
        spec:
          nodeName: virtual-kubelet
          containers:
          - name: nginx
            image: nginx:1.7.9
            imagePullPolicy: IfNotPresent
  • Pod example

    apiVersion: v1
    kind: Pod
    metadata:
      annotations:
        k8s.aliyun.com/eci-image-cache: "true"   # Enables automatic matching of an image cache.
      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 an image cache

When you create a pod, you can add an annotation to specify an image cache that you want to use to accelerate the creation of the pod.

Notice

Make sure that the specified image cache is in the Ready state. Otherwise, the pod fails to be created.

  • Deployment example

    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 image cache.
        spec:
          nodeName: virtual-kubelet
          containers:
          - name: nginx
            image: nginx:1.7.9
            imagePullPolicy: IfNotPresent
  • Pod example

    apiVersion: v1
    kind: Pod
    metadata:
      annotations:
        k8s.aliyun.com/eci-image-snapshot-id: imc-2ze5tm5gehgtiiga****  # Specifies an image cache.
      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