logo

Docker私有镜像仓库实战:Harbor命令行部署指南

作者:起个名字好难2025.10.10 18:33浏览量:1

简介:本文详细介绍如何使用Docker在命令行模式下快速搭建Harbor私有镜像仓库,涵盖环境准备、安装部署、配置优化及使用实践,助力开发者高效管理容器镜像。

Docker搭建Harbor私有镜像仓库(命令行模式)指南

一、为什么需要Harbor私有镜像仓库?

在容器化部署场景中,Docker Hub等公有仓库存在网络依赖、安全风险、镜像版本混乱等问题。Harbor作为企业级私有镜像仓库,提供以下核心价值:

  1. 安全控制:支持RBAC权限管理、镜像签名、漏洞扫描
  2. 性能优化:通过P2P镜像分发加速内网传输
  3. 管理便捷:提供Web界面与REST API双模式操作
  4. 扩展性强:支持与LDAP/AD集成、审计日志等企业功能

二、环境准备与前置条件

2.1 硬件要求

  • 服务器配置:4核CPU/8GB内存/40GB磁盘(生产环境建议翻倍)
  • 操作系统:CentOS 7/8或Ubuntu 20.04 LTS
  • 网络要求:开放443(HTTPS)、80(HTTP)、22(SSH)端口

2.2 软件依赖

  1. # 安装Docker CE(以Ubuntu为例)
  2. sudo apt-get update
  3. sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
  4. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  5. sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  6. sudo apt-get update
  7. sudo apt-get install -y docker-ce docker-ce-cli containerd.io
  8. # 安装Docker Compose
  9. sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  10. sudo chmod +x /usr/local/bin/docker-compose

三、命令行部署Harbor全流程

3.1 下载安装包

  1. wget https://github.com/goharbor/harbor/releases/download/v2.6.2/harbor-offline-installer-v2.6.2.tgz
  2. tar xzf harbor-offline-installer-v2.6.2.tgz
  3. cd harbor

3.2 配置修改要点

编辑harbor.yml文件,重点配置项:

  1. hostname: registry.example.com # 修改为实际域名或IP
  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. storage_driver:
  12. name: filesystem
  13. fs_driver:
  14. rootdirectory: /var/lib/registry

3.3 安装执行命令

  1. # 生成自签名证书(测试环境使用)
  2. sudo mkdir -p /data/cert
  3. sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  4. -keyout /data/cert/server.key -out /data/cert/server.crt \
  5. -subj "/CN=registry.example.com"
  6. # 执行安装(需提前配置好harbor.yml)
  7. sudo ./install.sh --with-trivy # --with-trivy启用漏洞扫描

四、核心功能配置详解

4.1 用户与项目管理

  1. # 通过API创建项目(示例)
  2. curl -u "admin:Harbor12345" -X POST -H "Content-Type: application/json" \
  3. -d '{"project_name": "devops", "public": false}' \
  4. "https://registry.example.com/api/v2.0/projects"
  5. # 添加用户(需先在Web界面创建)
  6. # 通过LDAP集成可自动化用户管理

4.2 镜像复制策略

配置跨区域镜像同步:

  1. # 在harbor.yml中添加复制适配器
  2. replication:
  3. - name: "aliyun-mirror"
  4. disabled: false
  5. src_registry:
  6. url: "https://registry.example.com"
  7. username: "admin"
  8. password: "Harbor12345"
  9. dest_registries:
  10. - name: "aliyun"
  11. url: "https://registry.cn-hangzhou.aliyuncs.com"
  12. username: "your_aliyun_id"
  13. password: "your_aliyun_pwd"
  14. triggers:
  15. - type: "immediate"
  16. rules:
  17. - resources:
  18. - artifact:
  19. repository: "**"
  20. destination_filter: "destination_project"

4.3 漏洞扫描配置

启用Trivy扫描器:

  1. # 检查扫描状态
  2. curl -u "admin:Harbor12345" \
  3. "https://registry.example.com/api/v2.0/systeminfo/scanAll"
  4. # 手动触发扫描
  5. curl -X POST -u "admin:Harbor12345" \
  6. "https://registry.example.com/api/v2.0/projects/devops/artifacts/library%2Fnginx%3Alatest/scan"

五、生产环境优化建议

5.1 高可用架构

  • 部署多节点Harbor集群
  • 使用NFS/Ceph作为共享存储
  • 配置Nginx负载均衡
    ```nginx
    upstream harbor {
    server harbor1.example.com:443;
    server harbor2.example.com:443;
    }

server {
listen 443 ssl;
server_name registry.example.com;
ssl_certificate /etc/nginx/certs/server.crt;
ssl_certificate_key /etc/nginx/certs/server.key;

  1. location / {
  2. proxy_pass https://harbor;
  3. proxy_set_header Host $host;
  4. proxy_set_header X-Real-IP $remote_addr;
  5. }

}

  1. ### 5.2 监控告警方案
  2. - Prometheus监控指标端点:`/api/v2.0/metrics`
  3. - 配置Grafana看板监控:
  4. - 存储使用率
  5. - 镜像拉取速率
  6. - 扫描任务队列
  7. ## 六、常见问题解决方案
  8. ### 6.1 证书问题处理
  9. ```bash
  10. # 证书不受信任的解决方法
  11. sudo cp /data/cert/server.crt /usr/local/share/ca-certificates/
  12. sudo update-ca-certificates
  13. # Docker客户端配置
  14. sudo mkdir -p /etc/docker/certs.d/registry.example.com
  15. sudo cp /data/cert/server.crt /etc/docker/certs.d/registry.example.com/ca.crt

6.2 性能调优参数

docker-compose.yml中添加:

  1. registry:
  2. environment:
  3. - REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/data/registry
  4. - REGISTRY_HTTP_SECRET=your_secret_key
  5. - REGISTRY_STORAGE_DELETE_ENABLED=true
  6. ulimits:
  7. nproc: 65535
  8. nofile:
  9. soft: 65535
  10. hard: 65535

七、升级与维护流程

7.1 版本升级步骤

  1. # 1. 备份数据
  2. sudo ./prepare.sh --conf harbor.yml.bak
  3. # 2. 下载新版本
  4. wget https://github.com/goharbor/harbor/releases/download/v2.7.0/harbor-offline-installer-v2.7.0.tgz
  5. # 3. 执行升级
  6. cd harbor
  7. sudo docker-compose down
  8. sudo tar xzf ../harbor-offline-installer-v2.7.0.tgz -C ../ --strip-components=1
  9. cd ../harbor
  10. sudo ./install.sh --with-chartmuseum --with-trivy

7.2 日常维护命令

  1. # 查看服务状态
  2. sudo docker-compose ps
  3. # 清理无用镜像
  4. sudo docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock alpine/harbor-db-cleaner:v2.6.2
  5. # 日志轮转配置
  6. 在/etc/logrotate.d/中添加:
  7. /var/log/harbor/*.log {
  8. daily
  9. missingok
  10. rotate 14
  11. compress
  12. delaycompress
  13. notifempty
  14. copytruncate
  15. }

通过以上步骤,您可以在30分钟内完成Harbor私有仓库的命令行部署。实际生产环境中,建议结合CI/CD流水线实现镜像的自动构建、扫描和推送,构建完整的DevOps容器管理平台。

相关文章推荐

发表评论

活动