Automate image cache creation with image-cache-operator

Updated at:

Use the image-cache-operator add-on to automatically create image caches in an ACS cluster.

Prerequisites

  1. Create an ACS cluster.

  2. Obtain the cluster kubeconfig and connect to the cluster by using kubectl.

  3. On the cluster’s Add-ons page, install the image-cache-operator add-on.

    (Recommended) Set rbac.secret to true. The add-on then automatically reads the imagePullSecrets referenced by Pods and uses them for authentication when pulling images

Limits

  • An image cache that is automatically created from a Pod is not updated to the new version after you push a new image with the same tag to the container registry.

  • The status of an ImageCache CR in a cluster does not necessarily reflect the actual status of the corresponding image cache. For example, after you delete an image cache in the console, the status of the ImageCache CR remains unchanged and the CR is not deleted.

  • When you create an image cache by creating an ImageCache CR, you can specify the image.alibabacloud.com/cascade-delete-image-cache=true Annotation to cascade delete the remote image cache when the CR is deleted. In this case, deleting the CR forcibly deletes the remote image cache, which causes Pods that are using the image cache to fail to pull images.

  • If the number of ImageCache CRs in the cluster exceeds the configured limit, the operator periodically cleans up the CRs but does not delete the remote image caches. For more information, see Operator configuration.

  • After you modify the ConfigMap of the operator configuration, you must restart the operator for the changes to take effect.

  • Because this add-on must obtain imagePullSecrets, it requires RBAC permissions to read Secrets cluster-wide.

  • When image caches are created automatically, each creation job occupies an IP address in the same vSwitch as the Pod. If IP addresses are insufficient, the creation of new Pods or image caches may fail.

  • ImageCache CRs in the Failed state are retried. Clean up these CRs and troubleshoot the failures promptly. Otherwise, the CRs consume build quotas and slow down the overall image cache creation.

Usage

Method 1: Create automatically from a Pod

Add the image.alibabacloud.com/auto-image-cache: controller Annotation to automatically create image caches for all containers of a Pod when the Pod is created.

Step 1: Create sample resources

  1. (Optional) Create the imagePullSecret required to pull the image

    kubectl create secret docker-registry pull-secret --docker-server=<acrurl> --docker-username=<username> --docker-password=<password>

    Replace the placeholders in the command with your actual values.

    1. <acrurl>: The URL of the container registry, such as ***-***-registry.cn-hangzhou.cr.aliyuncs.com.

    2. <username>: The username of the container registry.

    3. <password>: The password of the container registry.

  2. Save the following content as imc-pod.yaml, and then run kubectl apply -f imc-pod.yaml to create the sample Pod.

    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        alibabacloud.com/compute-class: performance
        alibabacloud.com/compute-qos: default
        alibabacloud.com/acs: 'true'
      annotations:
        image.alibabacloud.com/auto-image-cache: controller # Specifies that automatic creation is enabled.
      name: image-cache-01
      namespace: default
    spec:
      imagePullSecrets:
      - name: pull-secret
      containers:
      - image: registry.openanolis.cn/openanolis/nginx:1.14.1-8.6
        name: main
        resources:
          limits:
            cpu: "4"
            memory: 8Gi
          requests:
            cpu: "4"
            memory: 8Gi

Step 2: Verify the results

  1. Check the ImageCache status.

    kubectl get imagecaches.image.alibabacloud.com

    In the expected output, if Phase is Ready, the image cache is created. If it is Failed, run the kubectl describe imagecaches.image.alibabacloud.com <image-cache-name> command to view the detailed error message in the Status field.

    NAME                           PHASE   IMAGECACHEID               SIZE   AGE
    image-cache-4612197a7706****   Ready   imc-bp11oilzhe191fve****   1      119m
    You can also view the automatically created image caches in the Image Cache console.

Method 2: Create from an ImageCache CRD

An ImageCache CR is a cluster-scoped resource. If you submit an image with the same tag by using the CRD, the image cache is recreated.

Step 1: Submit the ImageCache CRD

  1. (Optional) Create the imagePullSecret required to pull the image

    kubectl create secret docker-registry pull-secret --docker-server=<acrurl> --docker-username=<username> --docker-password=<password>

    Replace the placeholders in the command with your actual values.

    1. <acrurl>: The URL of the container registry, such as ***-***-registry.cn-hangzhou.cr.aliyuncs.com.

    2. <username>: The username of the container registry.

    3. <password>: The password of the container registry.

  2. Save the following content as imc-crd.yaml, and then run kubectl apply -f imc-crd.yaml to create the ImageCache resource object.

    apiVersion: image.alibabacloud.com/v1
    kind: ImageCache
    metadata:
      name: image-cache-01
      annotations: 
        image.alibabacloud.com/cascade-delete-image-cache: "false" # Specifies whether to delete the image cache when this CR is deleted.
    spec:
      imagePullCredentials:
      - secretName: pull-secret
        secretNamespace: default
      images:
      - registry.cn-hangzhou.aliyuncs.com/eci_open/nginx:latest
      networkConfig:
        securityGroupId: sg-bp1hfv5fwpyx4ry0****
        vSwitchIds:
        - vsw-bp169dodwplgfpc86****

    Key parameters:

    Parameter

    Description

    spec.imagePullCredentials

    The Secret that is required to create the image cache. It is used to pull the image.

    spec.images

    The image that is associated with the image cache. Only a single image is supported.

    spec.networkConfig

    • securityGroupId: The security group that is required to pull the image cache.

    • vSwitchIds: The vSwitches that are required to pull the image cache. You can specify multiple vSwitches.

    By default, the vSwitch and security group that are associated with the cluster are used.

    image.alibabacloud.com/cascade-delete-image-cache Annotation

    Specifies whether to delete the image cache after the ImageCache CR is deleted.

    • true: The image cache is deleted when the CR is deleted.

    • false: The image cache is not deleted when the CR is deleted. You must delete it later in the Image Cache console or by calling the OpenAPI.

Step 2: Verify the results

  1. Check the status of the ImageCache.

    kubectl get imagecaches.image.alibabacloud.com image-cache-01

    In the expected output, if Phase is Ready, the image cache is created. If it is Failed, run the kubectl describe imagecaches.image.alibabacloud.com image-cache-01 command to view the detailed error message in the Status field.

    NAME             PHASE   IMAGECACHEID               SIZE   AGE
    image-cache-01   Ready   imc-bp1anzjc0v90h8jm****   1      6m39s
  2. Go to the Image Cache console to view the recreated image cache with the same tag.

Operator configuration

If the number of ImageCache CRs in the cluster exceeds the configured limit, image-cache-operator periodically cleans up the automatically created CRs. For details about the configuration, see ImageCacheGCConfig in the ConfigMap.

apiVersion: v1
kind: ConfigMap
metadata:
  name: image-cache-config
  namespace: kube-system
data:
  operator-config: |
    {
      "NetworkConfig": {
        "vSwitchIds": ["vSwitch1", "vSwitch2"],
        "securityGroupId": "XXX"
      },
      "IgnoreAutoCacheImages": ["image-1", "image-2"],
      "ImageCacheGCConfig": {
        "threshold": 1000,
        "period": 1
      }
    }

Key parameters:

Parameter

Description

NetworkConfig

If a Pod is not scheduled for a long time, the network configuration used to create the image cache is obtained from NetworkConfig.

IgnoreAutoCacheImages

Image caches are not automatically created for the specified images.

ImageCacheGCConfig.threshold

If the number of automatically created ImageCache CRs exceeds the threshold, the excess CRs that are in the Ready state are cleaned up.

ImageCacheGCConfig.period

The garbage collection (GC) interval. Unit: minutes.