azure Aks acr purge命令挂起在“将删除此存储库的清单”上

krcsximq  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(103)

我们在Azure中有一个ACR容器注册表。在其中一个存储库中,我们有大约4,000张图像。我正在尝试使用ACR清除功能删除此文件,请使用以下命令:

PURGE_CMD="acr purge --filter '<service-name>:.*' --dry-run --ago 250d --untagged"
az acr run --cmd "$PURGE_CMD" --registry <registry-name> --timeout 3600 /dev/null

我已经设置了--ago设置,这样它就不会找到任何实际要删除的图像。然而,当它运行时,它可以很好地确定应该删除哪些标签(没有),但它只是挂在确定哪些清单将被删除。最终,命令在超时周期结束时超时。
输出如下。

Queued a run with ID: crc
Waiting for an agent...
2023/10/19 00:18:04 Alias support enabled for version >= 1.1.0, please see https://aka.ms/acr/tasks/task-aliases for more information.
2023/10/19 00:18:04 Creating Docker network: acb_default_network, driver: 'bridge'
2023/10/19 00:18:04 Successfully set up Docker network: acb_default_network
2023/10/19 00:18:04 Setting up Docker configuration...
2023/10/19 00:18:05 Successfully set up Docker configuration
2023/10/19 00:18:05 Logging in to registry: <registry>.azurecr.io
2023/10/19 00:18:09 Successfully logged into <registry>.azurecr.io
2023/10/19 00:18:09 Executing step ID: acb_step_0. Timeout(sec): 3600, Working directory: '', Network: 'acb_default_network'
2023/10/19 00:18:09 Launching container with name: acb_step_0
DRY RUN: The following output shows what WOULD be deleted if the purge command was executed. Nothing is deleted.
Tags for this repository would be deleted: <service-name>
Manifests for this repository would be deleted: <service-name>

我怎样才能避免这个挂呢?我猜这是因为存储库的大小而发生的。是否有其他方法可以从这种大小的存储库中删除图像?

raogr8fs

raogr8fs1#

与其尝试一次处理所有4,000张图像,不如考虑以较小的批量清除它们。例如,使用更具体的过滤器模式或日期范围来限制单次运行中处理的图像数量
或者直接使用CLI:
您可以直接使用Azure CLI列出和删除映像,而不是依赖acr清除功能。这将使您给予更多的控制权。举例来说:

# List images
az acr repository show-manifests --name <registry-name> --repository <service-name> | jq -r '.[] | select(.timestamp < "your-date-cutoff").digest'

# Delete images
az acr repository delete --name <registry-name> --image <service-name>@<digest>

使用Azure CLI方法时,您还可以并行执行删除过程。通过将要删除的图像列表分成较小的批次并同时处理它们(例如,使用脚本),您可以加快该过程。
例如:
此脚本:
1.列出早于指定日期的所有清单(映像清单)。
1.将这些清单拆分为批。
1.平行检测每批。

#!/bin/bash

# Variables
REGISTRY_NAME="<registry-name>"
REPOSITORY_NAME="<service-name>"
DATE_CUTOFF="your-date-cutoff"  # format: YYYY-MM-DDTHH:mm:ssZ
BATCH_SIZE=100

# List manifests older than the specified date
manifests=$(az acr repository show-manifests --name "$REGISTRY_NAME" --repository "$REPOSITORY_NAME" | jq -r ".[] | select(.timestamp < \"$DATE_CUTOFF\").digest")

# Split the manifests into batches and delete in parallel
batch_count=0
batch=()

for manifest in $manifests; do
    batch+=("$manifest")
    ((batch_count++))

    if [ $batch_count -eq $BATCH_SIZE ]; then
        (
            for m in "${batch[@]}"; do
                az acr repository delete --name "$REGISTRY_NAME" --image "$REPOSITORY_NAME"@"$m" --yes
            done
        ) &
        batch_count=0
        batch=()
    fi
done

# Handle the last batch (if it's not empty)
if [ $batch_count -ne 0 ]; then
    (
        for m in "${batch[@]}"; do
            az acr repository delete --name "$REGISTRY_NAME" --image "$REPOSITORY_NAME"@"$m" --yes
        done
    ) &
fi

# Wait for all parallel jobs to complete
wait

确保已安装jq以处理Azure CLI的JSON输出。

相关问题