扫描在 Kubernetes 命名空间中运行的 pod 使用的容器镜像

发布: (2026年2月10日 GMT+8 14:17)
1 分钟阅读
原文: Dev.to

Source: Dev.to

脚本

#!/bin/bash

namespace="kubernetes-dashboard"

# Create a directory for the scan results
mkdir -p "$namespace"

# Get a list of all the images used by Pods in the Namespace
images=($(kubectl get pods -n "$namespace" -o jsonpath='{.items[*].spec.containers[*].image}' | sort | uniq))

# Loop through the images and scan each one
for image in "${images[@]}"; do
    echo "Scanning image: $image"
    # Scan the image with --scanners vuln to skip scanning for secrets (faster)
    trivy image --severity HIGH,CRITICAL "$image" \
        --scanners vuln --quiet --format json \
        --output "$namespace/$(basename "$image").json"
done

用法

bash scan_images.sh

脚本会扫描 kubernetes-dashboard 命名空间中的所有镜像,并将 Trivy 的扫描结果以 JSON 文件的形式保存在以该命名空间命名的目录下。可以修改 namespace 变量,或将其移除,以扫描整个集群的镜像。

0 浏览
Back to Blog

相关文章

阅读更多 »