Harbor企业级镜像仓库安装部署全攻略

一、Harbor核心价值与适用场景

Harbor作为VMware开源的企业级Docker Registry项目,通过提供RBAC权限控制、镜像扫描、漏洞检测、日志审计等企业级功能,解决了原生Docker Registry在安全性、可管理性和扩展性方面的不足。尤其适用于以下场景:

  1. 跨团队协作开发时需要集中管理镜像
  2. 金融、医疗等对数据安全要求高的行业
  3. 需要与CI/CD流水线深度集成的DevOps环境
  4. 多数据中心需要同步镜像的分布式架构

相比Nexus和JFrog Artifactory,Harbor的优势在于:

  • 专为容器镜像优化设计
  • 开源免费且社区活跃
  • 与Kubernetes原生集成
  • 中国用户友好的文档支持

二、安装前环境准备

1. 硬件配置建议

组件 最小配置 推荐配置
CPU 2核 4核
内存 4GB 8GB
磁盘空间 40GB 100GB+
网络带宽 100Mbps 1Gbps

2. 软件依赖检查

  1. # 检查Docker版本(需17.06+)
  2. docker --version
  3. # 检查Docker Compose版本(需1.18.0+)
  4. docker-compose --version
  5. # 系统参数优化
  6. sudo sysctl -w vm.max_map_count=262144
  7. sudo sysctl -w fs.file-max=65536

3. 网络环境配置

  • 开放端口:80(HTTP)、443(HTTPS)、4443(控制台)
  • 防火墙规则示例:
    1. sudo ufw allow 80/tcp
    2. sudo ufw allow 443/tcp
    3. sudo ufw allow 4443/tcp

三、安装部署详细步骤

1. 离线安装包准备

  1. # 下载最新稳定版(示例为2.4.3)
  2. wget https://github.com/goharbor/harbor/releases/download/v2.4.3/harbor-offline-installer-v2.4.3.tgz
  3. # 解压安装包
  4. tar xvf harbor-offline-installer-v2.4.3.tgz
  5. cd harbor

2. 配置文件修改

编辑harbor.yml.tmpl(或重命名为harbor.yml):

  1. hostname: registry.example.com # 必须为FQDN
  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. fs_driver:
  16. rootdirectory: /var/lib/registry
  17. storage_path: /data

3. 证书配置指南

自签名证书生成:

  1. mkdir -p /data/cert
  2. openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  3. -keyout /data/cert/server.key \
  4. -out /data/cert/server.crt \
  5. -subj "/CN=registry.example.com"

证书权限设置:

  1. chown -R 10000:10000 /data/cert
  2. chmod 600 /data/cert/server.key
  3. chmod 644 /data/cert/server.crt

4. 安装执行过程

  1. # 执行安装(需root权限)
  2. sudo ./install.sh --with-trivy --with-chartmuseum
  3. # 预期输出:
  4. [Step 5]: starting Harbor ...
  5. Creating network "harbor_harbor" with the default driver
  6. Creating harbor-portal ... done
  7. Creating harbor-db ... done
  8. Creating registryctl ... done
  9. Creating registry ... done
  10. Creating harbor-jobservice ... done
  11. Creating harbor-core ... done
  12. Creating nginx ... done
  13. ----Harbor has been installed and started successfully.----

四、安装后验证与配置

1. 服务状态检查

  1. docker-compose ps
  2. # 正常状态应显示所有服务为"Up"
  3. # 日志查看命令
  4. docker-compose logs -f harbor-core

2. 客户端配置

Docker配置:

  1. # 编辑/etc/docker/daemon.json
  2. {
  3. "insecure-registries": ["registry.example.com"] # 如果是自签名证书
  4. }
  5. # 重启服务
  6. sudo systemctl restart docker

登录测试:

  1. docker login registry.example.com
  2. Username: admin
  3. Password: Harbor12345
  4. Login Succeeded

3. 基础功能验证

  1. # 镜像推送测试
  2. docker pull alpine:latest
  3. docker tag alpine:latest registry.example.com/library/alpine:latest
  4. docker push registry.example.com/library/alpine:latest
  5. # 镜像拉取测试
  6. docker pull registry.example.com/library/alpine:latest

五、高级配置与运维建议

1. 存储优化配置

  1. # 在harbor.yml中配置
  2. storage_driver:
  3. name: filesystem
  4. redirect:
  5. disable: true
  6. cache:
  7. blobdescriptor: inmemory
  8. maintenance:
  9. uploadpurging:
  10. enabled: true
  11. age: 168h # 保留7天的上传记录
  12. interval: 24h
  13. dryrun: false

2. 备份恢复策略

完整备份脚本:

  1. #!/bin/bash
  2. BACKUP_DIR="/backup/harbor_$(date +%Y%m%d)"
  3. mkdir -p $BACKUP_DIR
  4. # 数据库备份
  5. docker exec -it harbor-db \
  6. pg_dump -U postgres -h 127.0.0.1 registry > $BACKUP_DIR/registry.sql
  7. # 配置文件备份
  8. cp -r /etc/harbor $BACKUP_DIR/config
  9. # 镜像数据备份(硬链接方式节省空间)
  10. cp -al /data/registry $BACKUP_DIR/registry_data
  11. # 打包压缩
  12. tar -czvf $BACKUP_DIR/harbor_backup_$(date +%Y%m%d).tar.gz $BACKUP_DIR

3. 性能调优参数

数据库配置优化:

  1. # /var/lib/docker/volumes/harbor-db/_data/postgresql.conf
  2. max_connections = 200
  3. shared_buffers = 256MB
  4. effective_cache_size = 2GB
  5. work_mem = 4MB
  6. maintenance_work_mem = 128MB

核心服务配置:

  1. # harbor.yml中的core配置段
  2. core:
  3. url: https://registry.example.com
  4. secret: XVFBWEHUI23874JHBSDF
  5. session_timeout: 30 # 分钟
  6. token_expiration: 30 # 分钟
  7. project_creation_restriction: everyone

六、常见问题解决方案

1. 登录失败问题排查

  1. # 检查认证服务日志
  2. docker-compose logs -f harbor-core
  3. # 常见原因:
  4. # 1. 证书问题:检查/etc/docker/daemon.json配置
  5. # 2. 时钟不同步:ntpdate pool.ntp.org
  6. # 3. 密码错误:使用重置脚本
  7. # docker exec -it harbor-db psql -U postgres registry
  8. # ALTER USER admin WITH PASSWORD '新密码';

2. 镜像推送缓慢优化

  1. # 修改registry配置
  2. registry:
  3. storage:
  4. cache:
  5. blobdescriptor: redis
  6. maintenance:
  7. uploadpurging:
  8. enabled: true
  9. redis:
  10. host: redis
  11. port: 6379
  12. password:

3. 高可用架构设计

推荐方案:

  1. 主从复制:使用Harbor的复制功能实现

    1. # 在harbor.yml中配置
    2. replication:
    3. - name: master_to_slave
    4. disabled: false
    5. src_registry:
    6. url: https://master.example.com
    7. insecure: false
    8. dest_registry:
    9. url: https://slave.example.com
    10. insecure: false
    11. dest_namespace: library
    12. triggers:
    13. - type: event_based
  2. 负载均衡:使用Nginx或HAProxy

    1. upstream harbor {
    2. server harbor1.example.com:443;
    3. server harbor2.example.com:443;
    4. }
    5. server {
    6. listen 443 ssl;
    7. server_name registry.example.com;
    8. location / {
    9. proxy_pass https://harbor;
    10. proxy_set_header Host $host;
    11. }
    12. }

七、升级与维护指南

1. 版本升级流程

  1. # 1. 备份当前环境
  2. ./prepare.sh backup
  3. # 2. 下载新版本
  4. wget https://github.com/goharbor/harbor/releases/download/v2.5.0/harbor-offline-installer-v2.5.0.tgz
  5. # 3. 停止服务
  6. cd harbor
  7. docker-compose down
  8. # 4. 替换二进制文件
  9. rm -rf harbor
  10. tar xvf harbor-offline-installer-v2.5.0.tgz
  11. cp -r old_harbor/harbor.yml harbor/
  12. # 5. 执行升级
  13. cd harbor
  14. ./install.sh --with-clair --with-trivy

2. 定期维护任务

  1. # 每周日凌晨3点执行维护
  2. 0 3 * * 0 /usr/local/bin/harbor_maintenance.sh
  3. # 维护脚本内容
  4. #!/bin/bash
  5. # 清理未完成的上传
  6. docker exec -it harbor-core /harbor/bin/harbor_cleanup.sh
  7. # 优化数据库
  8. docker exec -it harbor-db psql -U postgres -c "VACUUM FULL;" registry
  9. # 清理日志
  10. find /var/log/harbor/ -name "*.log" -mtime +30 -exec rm {} \;

通过以上详细的安装部署指南,开发者可以构建一个稳定、安全、高效的企业级Docker镜像仓库。实际部署时,建议先在测试环境验证所有配置,再逐步推广到生产环境。对于大型企业,建议结合Prometheus和Grafana构建监控体系,实时掌握Harbor的运行状态。