All Products
Search
Document Center

Manage ImageCache

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 the ImageCache resource and how to create, query, and delete ImageCache.

Preparations

Make sure that your Kubernetes cluster supports ImageCache. To check whether a Kubernetes cluster supports ImageCache, log on to the cluster and run the following command:

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

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

    If your cluster does not support ImageCache, update Virtual Kubelet to a version that supports ImageCache.

    Note

    We recommend that you update Virtual Kubelet to the latest version to use new features. For more information about Virtual Kubelet versions, see ack-virtual-node.

  • If imagecaches.eci.alibabacloud.com is returned, the cluster supports ImageCache. Sample output:

    NAME                                                CREATED AT
    imagecaches.eci.alibabacloud.com                    2019-09-27T01:15:07Z

    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

    Sample imagecache-crd-sample.yaml file:

    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 ImageCache

After you create the ImageCache CRD, you can use it to manage ImageCache. For more information, see API operations of Kubernetes ImageCache.

Sample YAML configuration file of ImageCache:

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

The following table describes the parameters.

Parameter

Type

Required

Description

spec.images

String[]

Yes

The container images that are used to create the ImageCache.

spec.imagePullSecrets

String[]

No

The secrets of private image repositories.

  • If an image in spec.images is specified from 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: GB. 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 image cache expires and 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 image cache feature. For example, in the preceding YAML file, the k8s.aliyun.com/imc-enable-reuse: "true" annotation is added to enable image cache reuse. For more information, see Annotations supported by ImageCache.

Create an ImageCache

  1. Create an ImageCache.

    kubectl create -f imagecache-secrets-test.yaml

    Sample imagecache-secrets-test.yaml file:

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

    kubectl get imagecache imagecache-sample-test

    Sample output:

    NAME                              AGE   CACHEID                               PHASE   PROGRESS
    imagecache-sample-test            20h   imc-2zeditzeoemfhqor****              Ready    100%

Query ImageCache

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

  • Query details about the ImageCache list in a cluster

    kubectl get imagecache
  • Query details about an ImageCache

    kubectl get imagecache/imagecache-sample-test -o yaml

Delete an ImageCache

Alibaba Cloud automatically creates a snapshot for each ImageCache. You are charged for the snapshot for each retained ImageCache. We recommend that you manually delete the ImageCache that is no longer needed in a timely manner if you did not configure the retention period when you created the ImageCache.

To delete an ImageCache, run the following command:

kubectl delete imagecache/imagecache-sample-test