管理访问和清理

使用本页面执行 Alauda Container Platform Registry 管理员和命名空间管理员任务, 包括命名空间镜像访问、使用情况报告、镜像清理、Registry 垃圾回收以及 镜像签名验证。

任务索引

任务使用场景
授予命名空间权限用户或服务账户需要在镜像命名空间中访问拉取、推送、删除或镜像目录。
查看使用情况管理员需要镜像或 ImageStream 使用情况信息。
验证镜像签名管理员需要检查或保存受信任的镜像签名条件。
清理镜像管理员在查看试运行结果后,需要删除未使用的镜像元数据。
运行 Registry 垃圾回收管理员在清理元数据后,需要从 Registry 存储中回收未被引用的 blob。

先决条件

  • Registry 已安装并可用。
  • 你拥有对目标集群的 kubectl 和 ac 访问权限。
  • 你拥有在镜像命名空间中创建 RoleBinding 的权限。
  • 你拥有执行使用情况报告、镜像清理、Registry 垃圾回收和签名验证的管理员权限。

在使用 ac 管理命令之前,选择 ACP 4.4 Registry 后端:

ac config set-registry-mode modern

预期结果:

  • 后续的 ac Image API、清理、垃圾回收和签名命令会使用 ACP 4.4 Registry。

授予命名空间权限

向用户授予拉取权限:

kubectl create rolebinding image-puller-user \
  --clusterrole=system:image-puller \
  --user=<username> \
  -n <image-namespace>

向用户授予推送权限:

kubectl create rolebinding image-pusher-user \
  --clusterrole=system:image-pusher \
  --user=<username> \
  -n <image-namespace>

向另一个命名空间中的服务账户授予拉取权限:

kubectl create rolebinding image-puller-sa \
  --clusterrole=system:image-puller \
  --serviceaccount=<workload-namespace>:<serviceaccount-name> \
  -n <image-namespace>
占位符描述
<username> 或需要访问目标命名空间中镜像的 Kubernetes 用户名。
<image-namespace>拥有镜像仓库的命名空间,例如 team-a。
<workload-namespace>工作负载服务账户运行所在的命名空间。
<serviceaccount-name>拉取镜像的工作负载所使用的 ServiceAccount 名称。

验证 RoleBinding 和有效访问权限:

kubectl -n <image-namespace> get rolebinding

kubectl auth can-i get imagestreams/layers.image.alauda.io \
  -n <image-namespace> \
  --as=system:serviceaccount:<workload-namespace>:<serviceaccount-name>

预期结果:

  • RoleBinding 存在于镜像命名空间中。
  • 对于应拉取镜像的服务账户,kubectl auth can-i 命令返回 yes。

Registry 使用 ImageStream 层授权:

操作典型角色Image API 权限
拉取system:image-pullerimage.alauda.io imagestreams/layers get
推送system:image-pusherimage.alauda.io imagestreams/layers update
删除system:image-deleter针对目标镜像元数据的 Image API 删除权限
写入签名system:image-signature-writerimage.alauda.io imagesignatures create、delete
读取镜像目录system:image-registry-catalog-readerimage.alauda.io imagestreams list
读取 Registry 指标system:image-registry-metrics-readerimage.alauda.io registry/metrics get

system:image-pruner ClusterRole 专供 Operator 管理的 pruner ServiceAccount 使用。 它不是命名空间访问角色。手动清理和垃圾回收需要相应的管理员权限。

对于 Pull 和 Push 以外的角色,请根据你的授权模型授予相应的平台角色或 ClusterRole,然后验证有效权限。指标权限的作用域为集群;其他检查在适用时限定于镜像命名空间:

kubectl auth can-i delete images.image.alauda.io \
  --as=<username>
kubectl auth can-i create imagesignatures.image.alauda.io \
  --as=<username>
kubectl auth can-i list imagestreams.image.alauda.io \
  -n <image-namespace> \
  --as=<username>
kubectl auth can-i get registry/metrics.image.alauda.io \
  --as=<username>

预期结果:

  • 对于具有相应有效权限的主体,每个命令均返回 yes。该权限也可以由其他角色授予。

查看使用情况

显示 Images 的存储和使用情况统计信息:

ac adm top images

显示 ImageStreams 的存储和使用情况统计信息:

ac adm top imagestreams

预期结果:

  • 命令会输出当前管理员可见的 Image 或 ImageStream 资源的使用情况行。

验证镜像签名

验证 Image 对象中记录的镜像签名身份:

ac adm verify-image-signature sha256:<digest> \
  --expected-identity=registry.example.com/team-a/demo:latest
占位符描述
sha256:<digest>要验证的 Image 对象摘要。使用 ac get images 或 ac get imagestreamtags <name>:<tag> -n <namespace> -o wide 查找摘要。
registry.example.com/team-a/demo:latest预期的已签名镜像身份。使用签名策略要求的镜像引用。

将受信任条件保存回 Image 对象:

ac adm verify-image-signature sha256:<digest> \
  --expected-identity=registry.example.com/team-a/demo:latest \
  --save

验证已保存的条件:

ac get images.image.alauda.io sha256:<digest> -o yaml

预期结果:

  • Image 对象包含验证命令保存的受信任签名条件。

保存信任条件会更改 Image API 元数据。要回滚已保存的条件,请编辑 Image 对象并移除已保存的条件,或从已知良好的备份中恢复 Image 对象。

仅当不再需要签名记录时,才从 Image 对象中移除所有签名记录:

ac adm verify-image-signature sha256:<digest> --remove-all

警告:--remove-all 会移除 Image 对象中的每条签名记录。此操作 无法通过 Registry 撤销。命令执行后请验证 Image 对象;如果误删了记录,请从已知良好的备份中恢复 元数据。

ac get images.image.alauda.io sha256:<digest> -o yaml

裁剪 Images

确认裁剪可能会永久移除未使用的镜像元数据。如果同时运行 Registry 垃圾回收, 未引用的 Blob 可能会从存储中永久回收。请在维护窗口期间运行这些命令,保留所需的备份, 并在添加 --confirm 前查看试运行输出。

预览镜像裁剪:

ac adm prune images

预期结果:

  • 试运行会列出待裁剪对象,不会删除镜像元数据。

查看试运行输出后运行裁剪:

ac adm prune images \
  --keep-tag-revisions=5 \
  --keep-younger-than=72h \
  --confirm

验证裁剪:

ac adm prune images \
  --keep-tag-revisions=5 \
  --keep-younger-than=72h

预期结果:

  • 后续试运行不再列出已由确认命令裁剪的元数据。

使用 --whitelist 通过允许列表模式排除仓库:

# Preview the effect of the allow-list pattern
ac adm prune images \
  --whitelist='^cpaas-system/.*'

# Confirm pruning only after reviewing the preview
ac adm prune images \
  --whitelist='^cpaas-system/.*' \
  --confirm

有关计划裁剪,请参阅设置和配置 Registry。

确认的裁剪不会自动还原。需要恢复时,请从备份中恢复已裁剪的元数据,从受信任的镜像源重新创建 元数据,或重新导入镜像。

运行 Registry 垃圾回收

Registry 垃圾回收会从 Registry 存储中回收未被引用的 blob。确认后,无法从 Registry 中还原 blob 回收操作。

元数据清理后,预览 Registry 垃圾回收:

ac adm registry gc

预期结果:

  • 该命令会预览符合条件的未引用 blob,但不会删除它们。

仅在查看预览后运行 Registry 垃圾回收:

ac adm registry gc --confirm

预期结果:

  • 该命令会从 Registry 存储中删除符合条件的未引用 blob。

也可以在裁剪过程中触发 Registry 垃圾回收:

ac adm prune images --confirm --prune-registry

ac adm prune images 和 ac adm registry gc 默认都以试运行方式执行。在添加 --confirm 之前,请查看预览。确认的 blob 回收无法从 Registry 中还原;如果所需 blob 已被回收,请从存储备份中恢复数据,或从受信任的源再次复制镜像。

确认垃圾回收后,通过拉取或检查关键镜像来验证它们:

mkdir -p /tmp/registry-auth
ac registry login \
  --registry registry.example.com \
  --to /tmp/registry-auth/config.json

ac image info registry.example.com/team-a/demo:latest \
  --registry-config=/tmp/registry-auth/config.json