Automate image cache creation with image-cache-operator
Use the image-cache-operator add-on to automatically create image caches in an ACS cluster.
Prerequisites
Obtain the cluster kubeconfig and connect to the cluster by using kubectl.
On the cluster’s Add-ons page, install the
image-cache-operatoradd-on.(Recommended) Set
rbac.secrettotrue. The add-on then automatically reads theimagePullSecretsreferenced 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=trueAnnotation 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
(Optional) Create the
imagePullSecretrequired to pull the imagekubectl 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.
<acrurl>: The URL of the container registry, such as***-***-registry.cn-hangzhou.cr.aliyuncs.com.<username>: The username of the container registry.<password>: The password of the container registry.
Save the following content as
imc-pod.yaml, and then runkubectl apply -f imc-pod.yamlto 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
Check the ImageCache status.
kubectl get imagecaches.image.alibabacloud.comIn the expected output, if
PhaseisReady, the image cache is created. If it isFailed, run thekubectl describe imagecaches.image.alibabacloud.com <image-cache-name>command to view the detailed error message in theStatusfield.NAME PHASE IMAGECACHEID SIZE AGE image-cache-4612197a7706**** Ready imc-bp11oilzhe191fve**** 1 119mYou 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
(Optional) Create the
imagePullSecretrequired to pull the imagekubectl 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.
<acrurl>: The URL of the container registry, such as***-***-registry.cn-hangzhou.cr.aliyuncs.com.<username>: The username of the container registry.<password>: The password of the container registry.
Save the following content as
imc-crd.yaml, and then runkubectl apply -f imc-crd.yamlto create theImageCacheresource 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.imagePullCredentialsThe Secret that is required to create the image cache. It is used to pull the image.
spec.imagesThe image that is associated with the image cache. Only a single image is supported.
spec.networkConfigsecurityGroupId: 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-cacheAnnotationSpecifies 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
Check the status of the
ImageCache.kubectl get imagecaches.image.alibabacloud.com image-cache-01In the expected output, if
PhaseisReady, the image cache is created. If it isFailed, run thekubectl describe imagecaches.image.alibabacloud.com image-cache-01command to view the detailed error message in theStatusfield.NAME PHASE IMAGECACHEID SIZE AGE image-cache-01 Ready imc-bp1anzjc0v90h8jm**** 1 6m39sGo 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 |
| If a Pod is not scheduled for a long time, the network configuration used to create the image cache is obtained from NetworkConfig. |
| Image caches are not automatically created for the specified images. |
| If the number of automatically created ImageCache CRs exceeds the |
| The garbage collection (GC) interval. Unit: minutes. |