🚀 Harbor 私有镜像仓库部署与权限配置指南

企业级 CI/CD 自动化研发系统核心组件

📅 版本:v1.0 🕐 日期:2026-03-15 🎯 场景:端到端研发自动化 🔒 安全:RBAC 权限控制

📖 Harbor 概述

Harbor 是 VMware 公司开源的企业级 Docker Registry 项目,旨在帮助用户迅速搭建一个企业级的 Docker Registry 服务。

核心功能特点

在 CI/CD 流水线中的角色

GitLab
代码仓库
Jenkins
CI/CD 引擎
Harbor
镜像仓库
K8S
容器编排

🏗️ 系统架构设计

部署架构图

┌─────────────────────────────────────────────────────┐ │ Harbor 集群架构 │ └─────────────────────────────────────────────────────┘ │ ┌───────────────────┼───────────────────┐ │ │ │ ▼ ▼ ▼ ┌───────────┐ ┌───────────┐ ┌───────────┐ │ Nginx │ │ Core │ │ Database │ │ Proxy │ │ Service │ │PostgreSQL │ └───────────┘ └───────────┘ └───────────┘ │ │ │ ▼ ▼ ▼ ┌───────────┐ ┌───────────┐ ┌───────────┐ │ Registry │ │ Redis │ │ Job │ │ Storage │ │ Cache │ │ Service │ └───────────┘ └───────────┘ └───────────┘

服务器规划

服务器角色 IP 地址 配置 用途
Harbor Server 192.168.1.103 4C8G, 100G 磁盘 Harbor 主服务
Jenkins Server 192.168.1.102 2C4G, 50G 磁盘 CI/CD 引擎
K8S Master 192.168.1.104 4C8G, 50G 磁盘 K8S 控制节点
K8S Worker 192.168.1.105 4C8G, 100G 磁盘 K8S 工作节点

⚙️ Harbor 安装部署

系统要求

  • 操作系统: Ubuntu 20.04+ / CentOS 7+
  • Docker: 20.10.0+
  • Docker Compose: 2.0.0+
  • CPU: 最少 2 核,推荐 4 核
  • 内存: 最少 4GB,推荐 8GB
  • 磁盘: 最少 50GB,推荐 100GB+

安装步骤

  1. 安装 Docker 和 Docker Compose
  2. 配置 Docker 镜像加速器
  3. 下载 Harbor 安装包
  4. 修改 harbor.yml 配置文件
  5. 执行安装脚本
  6. 启动并验证服务

快速部署命令

# 安装 Docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
systemctl enable docker && systemctl start docker

# 安装 Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# 下载 Harbor
wget https://github.com/goharbor/harbor/releases/download/v2.12.1/harbor-offline-installer-v2.12.1.tgz
tar -zxvf harbor-offline-installer-v2.12.1.tgz -C /opt/

# 配置并安装
cd /opt/harbor
cp harbor.yml.tmpl harbor.yml
# 编辑 harbor.yml 修改 hostname 和密码
sudo ./install.sh
✅ 验证安装:访问 http://192.168.1.103:18081,使用 admin/Harbor@12345 登录

🔐 RBAC 权限配置

Harbor 角色体系

角色 角色 ID 权限说明
超级管理员 1 系统最高权限,管理所有项目、用户、系统设置
项目管理员 2 管理项目内的成员、仓库、标签、复制规则等
开发人员 3 推送/拉取镜像、管理标签、查看日志
访客 4 仅拉取镜像、查看项目信息
受限访客 5 仅拉取镜像(不能查看其他信息)

自动化配置脚本

# 使用提供的脚本快速配置
chmod +x harbor_rbac_setup.sh

# 配置演示环境
./harbor_rbac_setup.sh demo

# 配置 CI/CD 环境
./harbor_rbac_setup.sh cicd

# 创建项目
./harbor_rbac_setup.sh create-project myproject false

# 创建用户
./harbor_rbac_setup.sh create-user john john@example.com "John" "Pass123"

# 分配角色
./harbor_rbac_setup.sh assign-role 1 john 3
⚠️ 安全提示:生产环境请修改默认密码,启用 HTTPS,定期轮换凭据

🔗 与 Jenkins/K8S 集成

Jenkins Pipeline 示例

pipeline {
    agent any
    
    environment {
        HARBOR_URL = '192.168.1.103:18081'
        HARBOR_PROJECT = 'backend-services'
        APP_NAME = 'myapp-backend'
    }
    
    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', 
                    url: 'git@192.168.1.101:project/myapp.git',
                    credentialsId: 'gitlab-ssh-credentials'
            }
        }
        
        stage('Build & Test') {
            steps {
                sh 'mvn clean test'
            }
        }
        
        stage('Build Docker Image') {
            steps {
                script {
                    docker.build("${HARBOR_URL}/${HARBOR_PROJECT}/${APP_NAME}:${BUILD_NUMBER}")
                }
            }
        }
        
        stage('Push to Harbor') {
            steps {
                withCredentials([usernamePassword(
                    credentialsId: 'harbor-admin-credentials',
                    usernameVariable: 'HARBOR_USER',
                    passwordVariable: 'HARBOR_PASS'
                )]) {
                    sh '''
                        echo ${HARBOR_PASS} | docker login ${HARBOR_URL} -u ${HARBOR_USER} --password-stdin
                        docker push ${HARBOR_URL}/${HARBOR_PROJECT}/${APP_NAME}:${BUILD_NUMBER}
                    '''
                }
            }
        }
        
        stage('Deploy to K8S') {
            steps {
                sh '''
                    kubectl set image deployment/${APP_NAME} \
                        ${APP_NAME}=${HARBOR_URL}/${HARBOR_PROJECT}/${APP_NAME}:${BUILD_NUMBER} \
                        -n production
                '''
            }
        }
    }
}

K8S Deployment 配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-backend
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp-backend
  template:
    spec:
      containers:
      - name: myapp-backend
        image: 192.168.1.103:18081/backend-services/myapp-backend:v1.0
        ports:
        - containerPort: 8080
      imagePullSecrets:
      - name: harbor-secret
💡 提示:完整集成指南请参考 cicd_integration_guide.md 文档

🔧 故障排查

常见问题及解决方案

问题 1: Docker 无法登录 Harbor

错误:http: server gave HTTP response to HTTPS client
解决:在 /etc/docker/daemon.json 中添加 insecure-registries
"insecure-registries": ["192.168.1.103:18081"]

问题 2: Harbor 服务无法启动

# 检查端口占用
netstat -tlnp | grep 18081

# 检查容器日志
docker-compose logs core

# 检查磁盘空间
df -h

问题 3: K8S 无法拉取镜像

# 检查 Secret 是否正确
kubectl get secret harbor-secret -n production

# 查看 Pod 事件
kubectl describe pod <pod-name> -n production

# 检查网络连接
kubectl run test --rm -it --image=busybox -- wget http://192.168.1.103:18081

运维命令速查

# 查看服务状态
docker-compose ps

# 查看日志
docker-compose logs -f

# 重启服务
docker-compose restart

# 垃圾回收
docker-compose run --rm registry garbage-collect --delete-untagged /etc/registry/config.yml

# 备份数据
tar -czf harbor_backup_$(date +%Y%m%d).tar.gz /data/harbor

📚 相关资源