从零搭建Harbor私有镜像仓库并实现镜像拉取指南

从零搭建Harbor私有镜像仓库并实现镜像拉取指南

一、Harbor镜像仓库的核心价值

在容器化部署场景中,私有镜像仓库是保障软件交付安全性和效率的关键基础设施。Harbor作为CNCF(云原生计算基金会)毕业项目,相较于基础Docker Registry具有以下核心优势:

  1. 企业级安全机制:支持RBAC权限控制、镜像签名、漏洞扫描(集成Clair)
  2. 高可用架构:支持多节点部署、数据持久化存储、自动备份恢复
  3. 项目管理能力:支持多项目隔离、镜像复制策略、Webhook通知
  4. 审计追踪:完整记录用户操作日志,满足合规性要求

典型应用场景包括:

  • 金融行业对镜像安全的高要求环境
  • 跨国企业需要镜像全球同步分发
  • 开发团队需要细粒度权限控制
  • 离线环境需要本地镜像缓存

二、部署环境准备

硬件配置建议

组件 最低配置 推荐配置
服务器 2核4G 4核8G+
磁盘空间 40GB 200GB+(SSD优先)
网络带宽 10Mbps 100Mbps+

软件依赖清单

  1. 操作系统:CentOS 7/8 或 Ubuntu 20.04 LTS
  2. Docker引擎:20.10+版本(支持BuildKit)
  3. Docker Compose:1.29+版本
  4. 依赖包
    1. # CentOS示例
    2. sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    3. sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

网络规划要点

  1. 端口分配

    • 80/443:Web服务
    • 2376:Docker守护进程TLS端口
    • 2377:集群管理端口
    • 7946:节点通信端口
    • 4789:Overlay网络端口
  2. 域名配置

    1. # 示例Nginx配置片段
    2. server {
    3. listen 443 ssl;
    4. server_name harbor.example.com;
    5. ssl_certificate /path/to/cert.pem;
    6. ssl_certificate_key /path/to/key.pem;
    7. location / {
    8. proxy_pass http://harbor-core:8080;
    9. }
    10. }

三、Harbor安装与配置

1. 离线安装包准备

  1. # 下载指定版本(以2.5.3为例)
  2. wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-offline-installer-v2.5.3.tgz
  3. tar xvf harbor-offline-installer-v2.5.3.tgz
  4. cd harbor

2. 配置文件详解

修改harbor.yml.tmpl核心参数:

  1. hostname: harbor.example.com
  2. http:
  3. port: 80
  4. https:
  5. port: 443
  6. certificate: /data/cert/server.crt
  7. private_key: /data/cert/server.key
  8. harbor_admin_password: Harbor12345
  9. database:
  10. password: root123
  11. max_idle_conns: 50
  12. max_open_conns: 100
  13. storage_driver:
  14. name: filesystem
  15. # S3配置示例
  16. # name: s3
  17. # s3:
  18. # accesskey: xxx
  19. # secretkey: xxx
  20. # region: us-west-1
  21. # bucket: harbor-bucket

3. 安装执行流程

  1. # 生成配置文件
  2. cp harbor.yml.tmpl harbor.yml
  3. # 执行安装(需提前安装docker-compose)
  4. sudo ./install.sh --with-clair --with-trivy

安装日志关键节点:

  1. [Step 5]: starting Harbor ...
  2. Creating network "harbor_harbor" with the default driver
  3. Creating harbor-portal ... done
  4. Creating harbor-log ... done
  5. Creating registryctl ... done
  6. Creating registry ... done
  7. Creating harbor-db ... done
  8. Creating redis ... done
  9. Creating harbor-core ... done
  10. Creating harbor-jobservice ... done
  11. Creating nginx ... done
  12. ----Harbor has been installed and started successfully.----

四、镜像推送与拉取实战

1. 客户端配置

  1. # 登录Harbor仓库
  2. docker login harbor.example.com
  3. # 输入用户名/密码(admin/Harbor12345)
  4. # 配置镜像命名空间(推荐)
  5. export REGISTRY=harbor.example.com/project-name

2. 镜像推送流程

  1. # 标记本地镜像
  2. docker tag nginx:latest $REGISTRY/nginx:v1
  3. # 推送镜像
  4. docker push $REGISTRY/nginx:v1
  5. # 推送日志示例
  6. The push refers to repository [harbor.example.com/project-name/nginx]
  7. 5f70bf18a086: Pushed
  8. a3ed95caeb02: Pushed
  9. v1: digest: sha256:4a5573... size: 1362

3. 镜像拉取操作

  1. # 从Harbor拉取镜像
  2. docker pull harbor.example.com/project-name/nginx:v1
  3. # 常见问题处理
  4. # 错误1:证书验证失败
  5. # 解决方案:配置insecure-registries或添加CA证书
  6. # /etc/docker/daemon.json添加:
  7. {
  8. "insecure-registries": ["harbor.example.com"]
  9. }
  10. # 或将CA证书放入/etc/docker/certs.d/harbor.example.com/
  11. # 错误2:权限拒绝
  12. # 解决方案:检查项目成员权限
  13. # Web界面:项目设置→成员管理→添加用户并分配角色

五、高级运维技巧

1. 镜像复制策略配置

  1. # 在Harbor Web界面配置复制规则
  2. # 源项目:library
  3. # 目标项目:backup-library
  4. # 触发模式:定时同步(每天2点)
  5. # 过滤器:标签匹配regex ^v.*

2. 漏洞扫描集成

  1. # 手动触发扫描
  2. curl -X POST -u admin:Harbor12345 \
  3. "http://harbor.example.com/api/v2.0/projects/1/repositories/library%2Fnginx/artifacts/sha256:4a5573/scan"
  4. # 扫描结果解读
  5. {
  6. "severity": "High",
  7. "vulnerabilities": [
  8. {
  9. "id": "CVE-2021-41745",
  10. "package": "curl",
  11. "version": "7.74.0",
  12. "fix_version": "7.76.0",
  13. "severity": "High",
  14. "description": "..."
  15. }
  16. ]
  17. }

3. 高可用部署方案

  1. 数据库集群

    1. # 使用PostgreSQL集群
    2. database:
    3. type: external
    4. postgresql:
    5. hostname: pg-primary.example.com
    6. port: 5432
    7. username: harbor
    8. password: securepass
    9. database: registry
    10. sslmode: require
  2. Redis集群配置

    1. redis:
    2. type: external
    3. redis_url: redis://redis-cluster.example.com:6379/0
    4. password: redispass
  3. 存储冗余设计

    • 使用Ceph/GlusterFS提供分布式存储
    • 配置存储类为ReadWriteMany

六、性能优化建议

1. 镜像存储优化

  1. 层合并策略

    1. # 优化前(产生多层)
    2. RUN apt update
    3. RUN apt install -y nginx
    4. # 优化后(合并层)
    5. RUN apt update && apt install -y nginx
  2. 镜像清理策略

    1. # 删除未标记的镜像
    2. docker system prune -af
    3. # 清理Harbor中的未引用镜像
    4. curl -X DELETE -u admin:Harbor12345 \
    5. "http://harbor.example.com/api/v2.0/system/gc"

2. 网络性能调优

  1. Nginx配置优化

    1. # 增大客户端请求体大小
    2. client_max_body_size 2000m;
    3. # 启用gzip压缩
    4. gzip on;
    5. gzip_types application/json text/css application/javascript;
  2. Docker守护进程调优

    1. # /etc/docker/daemon.json
    2. {
    3. "max-concurrent-uploads": 10,
    4. "max-download-attempts": 10,
    5. "shutdown-timeout": 15
    6. }

七、安全加固指南

1. 传输层安全

  1. 证书配置

    1. # 生成自签名证书(生产环境应使用CA证书)
    2. openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    3. -keyout /data/cert/server.key -out /data/cert/server.crt \
    4. -subj "/CN=harbor.example.com"
  2. TLS版本控制

    1. # Nginx配置禁用旧版本TLS
    2. ssl_protocols TLSv1.2 TLSv1.3;
    3. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';

2. 访问控制策略

  1. 机器人账号管理

    1. # 创建机器人账号
    2. curl -X POST -u admin:Harbor12345 \
    3. -H "Content-Type: application/json" \
    4. -d '{"project_id": 1, "name": "ci-robot", "expires_in": 86400}' \
    5. "http://harbor.example.com/api/v2.0/robots"
  2. IP白名单

    1. # 在harbor.yml中配置
    2. authentication:
    3. jwt_token_expire_time: 30
    4. session_timeout: 1800
    5. ip_whitelist:
    6. - 192.168.1.0/24
    7. - 10.0.0.0/16

八、监控与运维

1. 指标采集配置

  1. Prometheus配置

    1. # scraping配置示例
    2. scrape_configs:
    3. - job_name: 'harbor'
    4. metrics_path: '/api/v2.0/metrics'
    5. static_configs:
    6. - targets: ['harbor.example.com:9090']
    7. basic_auth:
    8. username: 'prometheus'
    9. password: 'securepass'
  2. 关键指标说明

    • registry_storage_action_total:存储操作计数
    • harbor_project_count:项目数量
    • harbor_artifact_count:镜像数量
    • harbor_jobservice_queue_length:任务队列长度

2. 日志分析方案

  1. EFK栈集成

    1. # Filebeat配置示例
    2. filebeat.inputs:
    3. - type: log
    4. paths:
    5. - /var/log/harbor/*.log
    6. fields:
    7. app: harbor
    8. output.elasticsearch:
    9. hosts: ["elasticsearch:9200"]
  2. 关键日志模式

    • 认证失败:Authentication failed
    • 存储错误:Failed to upload layer
    • 扫描异常:Scan job failed

九、常见问题解决方案

1. 安装失败处理

问题现象Jobservice is not ready

解决方案

  1. 检查数据库连接:

    1. psql -h harbor-db -U harbor -d registry
  2. 查看Jobservice日志:

    1. docker logs harbor-jobservice
  3. 重启相关服务:

    1. docker-compose restart harbor-jobservice

2. 性能瓶颈诊断

问题现象:镜像推送速度慢

诊断步骤

  1. 网络带宽测试:

    1. iperf3 -c harbor.example.com
  2. 存储IOPS监控:

    1. iostat -x 1
  3. Docker守护进程诊断:

    1. docker system info | grep -i storage

十、升级与迁移指南

1. 版本升级流程

  1. # 1. 备份数据
  2. docker-compose exec harbor-db pg_dump -U harbor -d registry > backup.sql
  3. # 2. 下载新版本
  4. wget https://github.com/goharbor/harbor/releases/download/v2.6.0/harbor-offline-installer-v2.6.0.tgz
  5. # 3. 修改配置文件
  6. vi harbor.yml # 更新版本号和配置
  7. # 4. 执行升级
  8. sudo ./install.sh --with-clair

2. 数据迁移方案

场景:从单机迁移到集群

  1. 数据库迁移

    1. # 主库导出
    2. pg_dump -U harbor -d registry -Fc > full_backup.dump
    3. # 从库导入
    4. pg_restore -U harbor -d registry -c full_backup.dump
  2. 存储迁移

    1. # 使用rsync同步存储
    2. rsync -avz --progress /data/registry/ harbor-new:/data/

通过以上系统化的部署和运维指南,开发者可以构建满足企业级需求的私有镜像仓库。实际部署中建议先在测试环境验证配置,再逐步推广到生产环境。定期进行安全审计和性能调优是保障仓库稳定运行的关键。