Files
ljwx-k8s/CACHE-IMAGE-K8S-SUPPORT.md
2026-02-20 22:50:45 +08:00

7.3 KiB
Raw Permalink Blame History

cache-image.sh 添加 registry.k8s.io 支持

日期: 2025-12-31 问题: 无法缓存 registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0 状态: ✅ 已修复


问题分析

失败原因

原始错误:

./cache-image.sh registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0

❌ 两个源都拉取失败(超时或网络问题)

根本原因:

  1. ✅ EU Harbor 已配置 k8s-proxy 项目用于代理 registry.k8s.io
  2. ❌ cache-image.sh 脚本只尝试 library 和 dockerhub-proxy,没有尝试 k8s-proxy

EU Harbor 代理项目

Registry 来源 EU Harbor 代理项目 用途
docker.io dockerhub-proxy Docker Hub 镜像 ✅
registry.k8s.io k8s-proxy Kubernetes 官方镜像 ✅(已配置但脚本未使用)
gcr.io 待确认 Google Container Registry
quay.io 待确认 Red Hat Quay Registry

修复内容

文件变更

文件: cache-image.sh 修改位置: 第166-201行(cache_single_image 函数)

修复逻辑

修复前(仅支持 Docker Hub):

# 尝试 library
docker pull harbor.eu.lingjingwanxiang.cn/library/${normalized_image}
    ↓ 失败
# 尝试 dockerhub-proxy
docker pull harbor.eu.lingjingwanxiang.cn/dockerhub-proxy/${docker_image}
    ↓ 失败
❌ 报错退出

修复后(支持多个 Registry):

# 检查是否是 registry.k8s.io 镜像
if [[ "$input_image" =~ ^registry\.k8s\.io/ ]]; then
    # 使用 k8s-proxy
    docker pull harbor.eu.lingjingwanxiang.cn/k8s-proxy/${normalized_image}
    ✅ 成功
else
    # 原有逻辑:先尝试 library,再尝试 dockerhub-proxy
    ...
fi

代码对比

关键改动:

# 新增: 检测 registry.k8s.io 并使用 k8s-proxy
if [[ "$input_image" =~ ^registry\.k8s\.io/ ]]; then
    log_info "检测到 registry.k8s.io 镜像,使用 k8s-proxy..."
    if timeout 300 docker pull "${EU_HARBOR}/k8s-proxy/${normalized_image}" &> /dev/null; then
        src_image="${EU_HARBOR}/k8s-proxy/${normalized_image}"
        log_success "从 EU Harbor k8s-proxy 拉取成功"
    else
        log_error "k8s-proxy 拉取失败: $input_image"
        return 1
    fi
elif ...  # 原有的 library 和 dockerhub-proxy 逻辑

验证测试

测试镜像

./cache-image.sh registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0

执行流程

[→] 登录 Harbor 仓库...
[✓] Harbor 登录成功

[INFO] === 缓存镜像: registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0 ===
[INFO]     本地存储为: prometheus-adapter-prometheus-adapter:v0.12.0

[→] [1/5] 从 EU Harbor 拉取镜像(300秒超时)...
[INFO] 检测到 registry.k8s.io 镜像,使用 k8s-proxy...  ✅ 新功能
[✓] 从 EU Harbor k8s-proxy 拉取成功

[→] [2/5] 标记镜像...
[✓] 标记成功

[→] [3/5] 推送到本地 Harbor...
[✓] 推送成功

[→] [4/5] 验证从本地 Harbor 拉取...
[✓] 验证成功

[→] [5/5] 检查镜像架构...
[✓] 架构: arm64

✅ 镜像缓存完成!

  原始镜像: registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0
  本地路径: harbor.omniverseai.net/library/prometheus-adapter-prometheus-adapter:v0.12.0
  架构: arm64

测试结果

测试项 结果
识别 registry.k8s.io 前缀 ✅ 成功
从 k8s-proxy 拉取镜像 ✅ 成功
推送到本地 Harbor ✅ 成功
路径扁平化 ✅ 成功(prometheus-adapter-prometheus-adapter:v0.12.0)
镜像架构 ✅ arm64

更新的功能特性

支持的镜像 Registry

Registry 示例镜像 EU Harbor 代理 支持状态
Docker Hub nginx:alpine dockerhub-proxy ✅ 已支持
Docker Hub mysql/mysql-server:8.0 dockerhub-proxy ✅ 已支持
registry.k8s.io registry.k8s.io/metrics-server/metrics-server:v0.7.2 k8s-proxy ✅ 新增
registry.k8s.io registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0 k8s-proxy ✅ 新增
gcr.io gcr.io/kaniko-project/executor:latest 待配置 ⚠️ 需要配置
quay.io quay.io/prometheus/node-exporter:v1.8.2 待配置 ⚠️ 需要配置

镜像格式转换

原始镜像 本地 Harbor 路径
registry.k8s.io/metrics-server/metrics-server:v0.7.2 harbor.omniverseai.net/library/metrics-server-metrics-server:v0.7.2
registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0 harbor.omniverseai.net/library/prometheus-adapter-prometheus-adapter:v0.12.0
nginx:alpine harbor.omniverseai.net/library/nginx:alpine

使用示例

缓存 Kubernetes 官方镜像

# Metrics Server
./cache-image.sh registry.k8s.io/metrics-server/metrics-server:v0.7.2

# Prometheus Adapter
./cache-image.sh registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0

# Pause Container
./cache-image.sh registry.k8s.io/pause:3.9

# CoreDNS
./cache-image.sh registry.k8s.io/coredns/coredns:v1.11.1

批量缓存

cat > k8s-images.txt << 'EOF'
registry.k8s.io/metrics-server/metrics-server:v0.7.2
registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0
registry.k8s.io/pause:3.9
registry.k8s.io/coredns/coredns:v1.11.1
EOF

./cache-image.sh -f k8s-images.txt

自动修复 Deployment

# 假设 Deployment 使用 registry.k8s.io 的镜像
./cache-image.sh --update-deployment monitoring/prometheus-adapter

后续优化建议

1. 支持更多 Registry(可选)

建议在 EU Harbor 配置以下代理项目:

Registry 代理项目名 用途
gcr.io gcr-proxy Google Container Registry
ghcr.io ghcr-proxy GitHub Container Registry
quay.io quay-proxy Red Hat Quay Registry

脚本修改(示例):

# 添加更多 registry 检测
if [[ "$input_image" =~ ^registry\.k8s\.io/ ]]; then
    proxy_project="k8s-proxy"
elif [[ "$input_image" =~ ^gcr\.io/ ]]; then
    proxy_project="gcr-proxy"
elif [[ "$input_image" =~ ^ghcr\.io/ ]]; then
    proxy_project="ghcr-proxy"
elif [[ "$input_image" =~ ^quay\.io/ ]]; then
    proxy_project="quay-proxy"
else
    # 默认使用 library 和 dockerhub-proxy
    ...
fi

2. 自动降级策略(可选)

如果 k8s-proxy 拉取失败,可以尝试其他源:

# k8s-proxy 失败时尝试直接拉取(需要配置代理)
if ! timeout 300 docker pull "${EU_HARBOR}/k8s-proxy/${normalized_image}"; then
    log_warning "k8s-proxy 失败,尝试直接拉取..."
    timeout 300 docker pull "registry.k8s.io/${normalized_image}"
fi

总结

项目 状态
问题识别 ✅ 脚本缺少 k8s-proxy 支持
修复实施 ✅ 添加 registry.k8s.io 检测和 k8s-proxy 拉取
功能验证 ✅ 成功缓存 prometheus-adapter:v0.12.0
文档更新 ✅ 更新使用说明
向后兼容 ✅ 保持原有 Docker Hub 支持

修复状态: ✅ 完成 测试状态: ✅ 通过 可用性: ✅ 生产可用


相关文档

  • CACHE-IMAGE-BUGFIX.md - update_deployment 路径不一致 BUG 修复
  • CACHE-IMAGE-FIX.md - 镜像缓存工具使用说明
  • QUICK-FIX-IMAGEPULL.md - ImagePull 问题快速修复指南