文档

管理ImageCache

更新时间:

阿里云以ImageCache CRD的方式将ECI的镜像缓存功能提供给Kubernetes用户,以便Kubernetes用户也可以使用该功能来加速创建ECI Pod。本文介绍ImageCache资源,以及如何创建、查询和删除ImageCache。

准备工作

登录Kubernetes集群,执行以下命令,验证您的Kubernetes集群是否已经支持ImageCache。

kubectl get crd/imagecaches.eci.alibabacloud.com
  • 如果返回Error信息,则表示您的Kubernetes集群不支持ImageCache,返回示例如下:

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

    如果您的Kubernetes集群不支持ImageCache,您需要升级VK(ack-virtual-node组件)到相应支持ImageCache的版本。

    说明

    建议您升级VK到最新版本,以便更好地使用新功能。

  • 如果返回imagecaches.eci.alibabacloud.com的信息,则表示您的Kubernetes集群已经支持ImageCache,返回示例如下:

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

    如果您的Kubernetes集群的ImageCache CRD不是最新的,建议您执行kubectl apply命令重新部署。

    kubectl apply -f imagecache-crd-sample.yaml

    展开查看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

ImageCache配置说明

ImageCache的YAML配置文件如下,更多信息,请参见kubernetes ImageCache API

apiVersion: eci.alibabacloud.com/v1
kind: ImageCache
metadata:
  name: imagecache-sample
  annotations:
    k8s.aliyun.com/imc-enable-reuse: "true" #开启镜像缓存复用
spec:
  images:
  - centos:latest
  - busybox:latest
  imagePullSecrets:
  - default:secret1
  - default:secret2
  - kube-system:secret3
  imageCacheSize:
   25
  retentionDays:
   7

相关参数说明如下:

名称

类型

是否必选

描述

spec.images

String[]

用于创建镜像缓存的容器镜像列表。

spec.imagePullSecrets

String[]

镜像仓库对应的Secret列表。

  • 如果镜像列表中包含私有仓库的镜像,则需要为私有镜像仓库创建Secret,然后按照namespace:secretName的格式设置该参数。

  • 如果镜像列表中的镜像均为公有镜像,则无需设置该参数。

spec.imageCacheSize

int

镜像缓存大小。默认为20 GiB。取值范围为20~32768 GiB。

spec.retentionDays

int

镜像缓存保留时长,过期会被清理。单位为天。默认永不过期。

说明

ImageCache支持添加Annotation来充分使用镜像缓存功能,例如在上述YAML示例中,添加了k8s.aliyun.com/imc-enable-reuse: "true" 的Annotation来开启镜像缓存复用。更多信息,请参见ImageCache Annotation

创建ImageCache

  1. 创建ImageCache。

    kubectl create -f imagecache-test.yaml

    imagecache-test.yaml的内容示例如下:

    apiVersion: eci.alibabacloud.com/v1
    kind: ImageCache
    metadata:
      name: imagecache-sample-test
      annotations:
        k8s.aliyun.com/imc-enable-reuse: "true" #开启镜像缓存复用
    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. 查看ImageCache状态。

    kubectl get imagecache imagecache-sample-test

    返回结果示例如下,当状态为Ready时,表示ImageCache创建完成,可以使用该ImageCache。

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

查询ImageCache

创建ImageCache后,您可以根据需要查询集群下所有ImageCache列表或者查看某个ImageCache的详细信息:

  • 查询集群下所有ImageCache列表

    kubectl get imagecache
  • 查看某个ImageCache的详细信息

    kubectl get imagecache <imagecache-name> -o yaml

删除ImageCache

ImageCache对应一份快照。对于手动创建的ImageCache,保留需要支付快照费用,如果不再使用,建议您及时手动删除。

如果想要删除某个ImageCache,可执行如下命令:

kubectl delete imagecache <imagecache-name>

  • 本页导读 (1)
文档反馈