All Products
Search
Document Center

Elastic Container Instance:Manage ImageCaches

Last Updated:Feb 29, 2024

Alibaba Cloud provides the ImageCache feature of Elastic Container Instance by using the ImageCache CustomResourceDefinition (CRD). This way, you can use the ImageCache feature of Elastic Container Instance to accelerate the creation of Elastic Container Instance-based pods in Kubernetes. This topic describes ImageCaches and how to create, query, and delete an ImageCache.

Make preparations

Log on to your Kubernetes cluster and run the following command to check whether the Kubernetes cluster supports ImageCaches:

kubectl get crd/imagecaches.eci.alibabacloud.com
  • If an error is returned, the cluster does not support ImageCaches. Sample output:

    Error from server (NotFound): customresourcedefinitions.apiextensions.k8s.io "imagecaches.eci.alibabacloud.com" not found

    If your cluster does not support ImageCaches, update the ack-virtual-node component to a version that supports ImageCaches.

    Note

    We recommend that you update the ack-virtual-node component to the latest version to use new features.

  • If information about imagecaches.eci.alibabacloud.com is returned, your Kubernetes cluster supports ImageCaches. Sample output:

    NAME                               CREATED AT
    imagecaches.eci.alibabacloud.com   2024-01-23T05:54:44Z

    If the ImageCache CRD in your cluster is not of the latest version, we recommend that you run the kubectl apply command to update the ImageCache CRD to the latest version.

    kubectl apply -f imagecache-crd-sample.yaml

    Unfold to view imagecache-crd-sample.yaml

    apiVersion: apiextensions.k8s.io/v1beta1
    kind: CustomResourceDefinition
    metadata:
      name: imagecaches.eci.alibabacloud.com
    spec:
      group: eci.alibabacloud.com
      version: v1
      names:
        kind: ImageCache
        plural: imagecaches
        shortNames:
        - ic
        categories:
        - all
      scope: Cluster
      subresources:
        status: {}
      validation:
        openAPIV3Schema:
          required:
          - spec
          properties:
            spec:
              type: object
              required:
              - images
              properties:
                imagePullSecrets:
                  type: array
                  items:
                    type: string
                images:
                  minItems: 1
                  type: array
                  items:
                    type: string
                imageCacheSize:
                  type: integer
                retentionDays:
                  type: integer
      additionalPrinterColumns:
      - name: Age
        type: date
        JSONPath: .metadata.creationTimestamp
      - name: CacheId
        type: string
        JSONPath: .status.imageCacheId
      - name: Phase
        type: string
        JSONPath: .status.phase
      - name: Progress
        type: string
        JSONPath: .status.progress

Configure an ImageCache

The following code provides the YAML configuration file of an ImageCache. For more information, see Kubernetes ImageCache API.

apiVersion: eci.alibabacloud.com/v1
kind: ImageCache
metadata:
  name: imagecache-sample
  annotations:
    k8s.aliyun.com/imc-enable-reuse: "true" # Enables the ImageCache reuse feature.
spec:
  images:
  - centos:latest
  - busybox:latest
  imagePullSecrets:
  - default:secret1
  - default:secret2
  - kube-system:secret3
  imageCacheSize:
   25
  retentionDays:
   7

Parameter description:

Parameter

Type

Required

Description

spec.images

String[]

Yes

The container images that can be used to create the ImageCache.

spec.imagePullSecrets

String[]

No

The secrets of private image repositories.

  • If an image specified by the spec.images parameter resides in a private image repository, you must configure a secret for the private image repository in the namespace:secretName format.

  • If all images in spec.images are public images, you do not need to specify this parameter.

spec.imageCacheSize

int

No

The size of the ImageCache. Unit: GiB. Default value: 20. Valid values: 20 to 32768.

spec.retentionDays

int

No

The retention period of the ImageCache. When the specified retention period ends, the ImageCache is deleted. Unit: days. This parameter is left empty by default, which indicates that the ImageCache never expires.

Note

You can add annotations to the YAML configuration file of the ImageCache to make full use of the ImageCache feature. For example, in the preceding YAML file, the k8s.aliyun.com/imc-enable-reuse: "true" annotation is added to enable the ImageCache reuse feature. For more information, see ImageCache annotations.

Create an ImageCache

  1. Create an ImageCache.

    kubectl create -f imagecache-test.yaml

    Sample imagecache-test.yaml file:

    apiVersion: eci.alibabacloud.com/v1
    kind: ImageCache
    metadata:
      name: imagecache-sample-test
      annotations:
        k8s.aliyun.com/imc-enable-reuse: "true" # Enables the ImageCache reuse feature.
    spec:
      images:
      - registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
      - registry.cn-shanghai.aliyuncs.com/eci_open/busybox:1.30
      imageCacheSize:
       25
      retentionDays:
       7
  2. View the status of the ImageCache.

    kubectl get imagecache imagecache-sample-test

    The following sample code provides a sample output. If the ImageCache is in the Ready state, the ImageCache is created and can be used.

    NAME                     AGE     CACHEID                    PHASE   PROGRESS
    imagecache-sample-test   5m30s   imc-2ze2kbf9l1onf03z****   Ready   100%

Query an ImageCache

You can query details about an ImageCache or the ImageCache list in a cluster.

  • Query the ImageCache list in a cluster

    kubectl get imagecache
  • Query details about an ImageCache

    kubectl get imagecache <imagecache-name> -o yaml

Delete an ImageCache

An ImageCache is a snapshot. If you want to retain an ImageCache that you manually created, you must pay the snapshot cost. If you no longer use the ImageCache, we recommend that you manually delete it in a timely manner.

If you want to delete an ImageCache, run the following command:

kubectl delete imagecache <imagecache-name>