管理ImageCache
阿里云以ImageCache CRD的方式将ECI的镜像缓存功能提供给Kubernetes用户,以便Kubernetes用户也可以使用该功能来加速创建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到最新版本,以便更好地使用新功能。
关于ack-virtual-node组件的版本信息,请参见ack-virtual-node。
关于如何升级组件,请参见管理组件。
如果返回imagecaches.eci.alibabacloud.com的信息,则表示您的kubernetes集群已经支持ImageCache,返回示例如下:
NAME CREATED AT imagecaches.eci.alibabacloud.com 2019-09-27T01:15:07Z
如果您的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列表。
|
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
创建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: - centos:latest - busybox:latest imagePullSecrets: - default:secret1 - default:secret2 - kube-system:secret3 imageCacheSize: 25 retentionDays: 7
查看ImageCache状态。
kubectl get imagecache imagecache-sample-test
返回结果示例如下:
NAME AGE CACHEID PHASE PROGRESS imagecache-sample-test 20h imc-2zeditzeoemfhqor**** Ready 100%
查询ImageCache
创建ImageCache后,您可以根据需要查询集群下所有ImageCache列表或者查看某个ImageCache的详细信息:
查询集群下所有ImageCache列表
kubectl get imagecache
查看某个ImageCache的详细信息
kubectl get imagecache/imagecache-sample-test -o yaml
删除ImageCache
ImageCache对应一份快照。对于手动创建的ImageCache,保留需要支付快照费用,如果不再使用,建议您及时手动删除。
如果想要删除某个ImageCache,可执行如下命令:
kubectl delete imagecache/imagecache-sample-test