一、Harbor核心价值与适用场景
Harbor作为VMware开源的企业级Docker Registry项目,通过提供RBAC权限控制、镜像扫描、漏洞检测、日志审计等企业级功能,解决了原生Docker Registry在安全性、可管理性和扩展性方面的不足。尤其适用于以下场景:
- 跨团队协作开发时需要集中管理镜像
- 金融、医疗等对数据安全要求高的行业
- 需要与CI/CD流水线深度集成的DevOps环境
- 多数据中心需要同步镜像的分布式架构
相比Nexus和JFrog Artifactory,Harbor的优势在于:
- 专为容器镜像优化设计
- 开源免费且社区活跃
- 与Kubernetes原生集成
- 中国用户友好的文档支持
二、安装前环境准备
1. 硬件配置建议
| 组件 | 最小配置 | 推荐配置 |
|---|---|---|
| CPU | 2核 | 4核 |
| 内存 | 4GB | 8GB |
| 磁盘空间 | 40GB | 100GB+ |
| 网络带宽 | 100Mbps | 1Gbps |
2. 软件依赖检查
# 检查Docker版本(需17.06+)docker --version# 检查Docker Compose版本(需1.18.0+)docker-compose --version# 系统参数优化sudo sysctl -w vm.max_map_count=262144sudo sysctl -w fs.file-max=65536
3. 网络环境配置
- 开放端口:80(HTTP)、443(HTTPS)、4443(控制台)
- 防火墙规则示例:
sudo ufw allow 80/tcpsudo ufw allow 443/tcpsudo ufw allow 4443/tcp
三、安装部署详细步骤
1. 离线安装包准备
# 下载最新稳定版(示例为2.4.3)wget https://github.com/goharbor/harbor/releases/download/v2.4.3/harbor-offline-installer-v2.4.3.tgz# 解压安装包tar xvf harbor-offline-installer-v2.4.3.tgzcd harbor
2. 配置文件修改
编辑harbor.yml.tmpl(或重命名为harbor.yml):
hostname: registry.example.com # 必须为FQDNhttp:port: 80https:port: 443certificate: /data/cert/server.crtprivate_key: /data/cert/server.keyharbor_admin_password: Harbor12345 # 默认管理员密码database:password: root123max_idle_conns: 50max_open_conns: 100storage_driver:name: filesystemfs_driver:rootdirectory: /var/lib/registrystorage_path: /data
3. 证书配置指南
自签名证书生成:
mkdir -p /data/certopenssl req -x509 -nodes -days 365 -newkey rsa:2048 \-keyout /data/cert/server.key \-out /data/cert/server.crt \-subj "/CN=registry.example.com"
证书权限设置:
chown -R 10000:10000 /data/certchmod 600 /data/cert/server.keychmod 644 /data/cert/server.crt
4. 安装执行过程
# 执行安装(需root权限)sudo ./install.sh --with-trivy --with-chartmuseum# 预期输出:[Step 5]: starting Harbor ...Creating network "harbor_harbor" with the default driverCreating harbor-portal ... doneCreating harbor-db ... doneCreating registryctl ... doneCreating registry ... doneCreating harbor-jobservice ... doneCreating harbor-core ... doneCreating nginx ... done✔ ----Harbor has been installed and started successfully.----
四、安装后验证与配置
1. 服务状态检查
docker-compose ps# 正常状态应显示所有服务为"Up"# 日志查看命令docker-compose logs -f harbor-core
2. 客户端配置
Docker配置:
# 编辑/etc/docker/daemon.json{"insecure-registries": ["registry.example.com"] # 如果是自签名证书}# 重启服务sudo systemctl restart docker
登录测试:
docker login registry.example.comUsername: adminPassword: Harbor12345Login Succeeded
3. 基础功能验证
# 镜像推送测试docker pull alpine:latestdocker tag alpine:latest registry.example.com/library/alpine:latestdocker push registry.example.com/library/alpine:latest# 镜像拉取测试docker pull registry.example.com/library/alpine:latest
五、高级配置与运维建议
1. 存储优化配置
# 在harbor.yml中配置storage_driver:name: filesystemredirect:disable: truecache:blobdescriptor: inmemorymaintenance:uploadpurging:enabled: trueage: 168h # 保留7天的上传记录interval: 24hdryrun: false
2. 备份恢复策略
完整备份脚本:
#!/bin/bashBACKUP_DIR="/backup/harbor_$(date +%Y%m%d)"mkdir -p $BACKUP_DIR# 数据库备份docker exec -it harbor-db \pg_dump -U postgres -h 127.0.0.1 registry > $BACKUP_DIR/registry.sql# 配置文件备份cp -r /etc/harbor $BACKUP_DIR/config# 镜像数据备份(硬链接方式节省空间)cp -al /data/registry $BACKUP_DIR/registry_data# 打包压缩tar -czvf $BACKUP_DIR/harbor_backup_$(date +%Y%m%d).tar.gz $BACKUP_DIR
3. 性能调优参数
数据库配置优化:
# /var/lib/docker/volumes/harbor-db/_data/postgresql.confmax_connections = 200shared_buffers = 256MBeffective_cache_size = 2GBwork_mem = 4MBmaintenance_work_mem = 128MB
核心服务配置:
# harbor.yml中的core配置段core:url: https://registry.example.comsecret: XVFBWEHUI23874JHBSDFsession_timeout: 30 # 分钟token_expiration: 30 # 分钟project_creation_restriction: everyone
六、常见问题解决方案
1. 登录失败问题排查
# 检查认证服务日志docker-compose logs -f harbor-core# 常见原因:# 1. 证书问题:检查/etc/docker/daemon.json配置# 2. 时钟不同步:ntpdate pool.ntp.org# 3. 密码错误:使用重置脚本# docker exec -it harbor-db psql -U postgres registry# ALTER USER admin WITH PASSWORD '新密码';
2. 镜像推送缓慢优化
# 修改registry配置registry:storage:cache:blobdescriptor: redismaintenance:uploadpurging:enabled: trueredis:host: redisport: 6379password:
3. 高可用架构设计
推荐方案:
-
主从复制:使用Harbor的复制功能实现
# 在harbor.yml中配置replication:- name: master_to_slavedisabled: falsesrc_registry:url: https://master.example.cominsecure: falsedest_registry:url: https://slave.example.cominsecure: falsedest_namespace: librarytriggers:- type: event_based
-
负载均衡:使用Nginx或HAProxy
upstream harbor {server harbor1.example.com:443;server harbor2.example.com:443;}server {listen 443 ssl;server_name registry.example.com;location / {proxy_pass https://harbor;proxy_set_header Host $host;}}
七、升级与维护指南
1. 版本升级流程
# 1. 备份当前环境./prepare.sh backup# 2. 下载新版本wget https://github.com/goharbor/harbor/releases/download/v2.5.0/harbor-offline-installer-v2.5.0.tgz# 3. 停止服务cd harbordocker-compose down# 4. 替换二进制文件rm -rf harbortar xvf harbor-offline-installer-v2.5.0.tgzcp -r old_harbor/harbor.yml harbor/# 5. 执行升级cd harbor./install.sh --with-clair --with-trivy
2. 定期维护任务
# 每周日凌晨3点执行维护0 3 * * 0 /usr/local/bin/harbor_maintenance.sh# 维护脚本内容#!/bin/bash# 清理未完成的上传docker exec -it harbor-core /harbor/bin/harbor_cleanup.sh# 优化数据库docker exec -it harbor-db psql -U postgres -c "VACUUM FULL;" registry# 清理日志find /var/log/harbor/ -name "*.log" -mtime +30 -exec rm {} \;
通过以上详细的安装部署指南,开发者可以构建一个稳定、安全、高效的企业级Docker镜像仓库。实际部署时,建议先在测试环境验证所有配置,再逐步推广到生产环境。对于大型企业,建议结合Prometheus和Grafana构建监控体系,实时掌握Harbor的运行状态。