搭建 Harbor 企业镜像仓库

适用场景

需要私有镜像仓库、镜像扫描、签名、复制和基于项目的权限控制。Harbor 适合替代简单的 registry,尤其是多团队、多集群和需要审计的生产环境。

前置条件

步骤

  1. 添加 Helm 仓库并准备 values:
helm repo add harbor https://helm.goharbor.io
helm repo update
helm show values harbor/harbor > harbor-values.yaml
  1. 配置外部数据库、Redis、对象存储和 TLS:
helm install harbor harbor/harbor \
  --namespace harbor --create-namespace \
  -f harbor-values.yaml

关键配置包括 expose.type=ingress、expose.tls.enabled=true、database.type=external、redis.type=external、persistence.imageChartStorage.type=s3。

  1. 等待组件就绪:
kubectl -n harbor get pods
kubectl -n harbor get ingress
curl -I https://harbor.example.com
  1. 创建项目并分配角色:
# 在 UI 或 API 中创建项目 prod
curl -u admin:"$HARBOR_PASSWORD" -X POST \
  -H 'Content-Type: application/json' \
  https://harbor.example.com/api/v2.0/projects \
  -d '{"project_name":"prod","public":false}'
  1. 创建机器人账号用于 CI 推送,只授予目标项目的 push/pull 权限。

  2. 配置镜像扫描和保留策略:

  1. 多集群复制:
# 在 UI 中创建目标 registry 端点和复制规则
# 也可以用 API 创建 replication policy
  1. 配置日志和监控,接入 Prometheus 与外部日志系统。

验证

回滚

常见坑

相关命令