共计 7157 个字符,预计需要花费 18 分钟才能阅读完成。
背景
Registry 是由 Docker 官方发行的开源镜像仓库。
Harbor 是由 VMware 发行的开源镜像仓库,它也使用了 Registry 管理镜像。
Harbor 对比直接部署 Registry,提供了更丰富的能力:
- 现代化GUI
- 企业级用户管理和认证
- 多租户支持
- 漏洞扫描
- 细粒度访问控制
简单环境下直接使用 Registry 也是个好选择,但是在企业级生产环境中缺乏多租户和访问控制是难以忍受的。
部署
参考链接:goharbor.io/docs/2.12.0/install-config/download-installer/
官方文档介绍了两种安装方式:安装程序安装 & helm 安装。下文选择前者。
从 github.com/goharbor/harbor/releases 下载最新发行版并解压缩。
mkdir /usr/local/harbor && cd /usr/local/harbor
wget https://github.com/goharbor/harbor/releases/download/v2.12.0/harbor-offline-installer-v2.12.0.tgz
tar xf harbor-offline-installer-v2.12.0.tgz
mv harbor harbor-v2.12.0
cd harbor-v2.12.0 && cp harbor.yml.tmpl harbor.yml
文件目录结构如下:
14:34:16 # ll
total 658428
drwxr-xr-x 3 root root 20 Nov 13 14:28 common
-rw-r--r-- 1 root root 3646 Nov 5 15:17 common.sh
-rw-r--r-- 1 root root 6140 Nov 13 14:28 docker-compose.yml
-rw-r--r-- 1 root root 674160832 Nov 5 15:17 harbor.v2.12.0.tar.gz
-rw-r--r-- 1 root root 14325 Nov 13 14:27 harbor.yml
-rw-r--r-- 1 root root 14288 Nov 5 15:17 harbor.yml.tmpl
-rwxr-xr-x 1 root root 1975 Nov 5 15:17 install.sh
-rw-r--r-- 1 root root 11347 Nov 5 15:17 LICENSE
-rwxr-xr-x 1 root root 2211 Nov 5 15:17 prepare
其中 harbor.yml
为配置文件模版,修改如下:
# cat harbor.yml
hostname: 192.168.2.10
http:
port: 8083
harbor_admin_password: testpasswd123
database:
password: testpasswd123
max_idle_conns: 100
max_open_conns: 900
conn_max_lifetime: 5m
conn_max_idle_time: 0
data_volume: /disk-st4000vx015-4t-no1/harbor-data
trivy:
ignore_unfixed: false
skip_update: false
skip_java_db_update: false
offline_scan: false
security_check: vuln
insecure: false
timeout: 5m0s
jobservice:
max_job_workers: 10
job_loggers:
- STD_OUTPUT
- FILE
logger_sweeper_duration: 1 #days
notification:
webhook_job_max_retry: 3
webhook_job_http_client_timeout: 3 #seconds
log:
level: info
local:
rotate_count: 50
rotate_size: 200M
location: /var/log/harbor
_version: 2.12.0
proxy:
http_proxy:
https_proxy:
no_proxy:
components:
- core
- jobservice
- trivy
upload_purging:
enabled: true
age: 168h
interval: 24h
dryrun: false
cache:
enabled: false
expire_hours: 24
修改项:
- 修改http端口
- 禁用https(我选择在外部nginx代理中配置https而不是harbor本身)
- 修改默认密码
- 修改数据卷存储路径
执行安装:
./install.sh
14:27:53 # ./install.sh
[Step 0]: checking if docker is installed ...
Note: docker version: 23.0.6
[Step 1]: checking docker-compose is installed ...
Note: docker-compose version: 2.12.2
[Step 2]: loading Harbor images ...
910db7c25623: Loading layer [==================================================>] 40.51MB/40.51MB
...
[Step 3]: preparing environment ...
[Step 4]: preparing harbor configs ...
prepare base dir is set to /usr/local/harbor/harbor-v2.12.0
WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
Generated configuration file: /config/portal/nginx.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
Generated and saved secret to file: /data/secret/keys/secretkey
Successfully called func: create_root_cert
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir
Note: stopping existing Harbor instance ...
[Step 5]: starting Harbor ...
[+] Running 10/10
⠿ Network harbor-v2120_harbor Created 0.0s
⠿ Container harbor-log Started 0.3s
⠿ Container registryctl Started 0.6s
⠿ Container redis Started 0.9s
⠿ Container registry Started 1.0s
⠿ Container harbor-portal Started 0.8s
⠿ Container harbor-db Started 0.7s
⠿ Container harbor-core Started 1.1s
⠿ Container harbor-jobservice Started 1.4s
⠿ Container nginx Started 1.4s
✔ ----Harbor has been installed and started successfully.----
从最后的输出可以看到启动了多个容器。
再次查看文件目录结构:
15:30:37 # ll
total 658428
drwxr-xr-x 3 root root 20 Nov 13 14:28 common
-rw-r--r-- 1 root root 3646 Nov 5 15:17 common.sh
-rw-r--r-- 1 root root 6140 Nov 13 14:28 docker-compose.yml
-rw-r--r-- 1 root root 674160832 Nov 5 15:17 harbor.v2.12.0.tar.gz
-rw-r--r-- 1 root root 14325 Nov 13 14:27 harbor.yml
-rw-r--r-- 1 root root 14288 Nov 5 15:17 harbor.yml.tmpl
-rwxr-xr-x 1 root root 1975 Nov 5 15:17 install.sh
-rw-r--r-- 1 root root 11347 Nov 5 15:17 LICENSE
-rwxr-xr-x 1 root root 2211 Nov 5 15:17 prepare
可以看到生成了 docker-compose.yml,说明 harbor 是由 docker-sompose 启动和管理的。
[root@ centos-ops /usr/local/harbor/harbor-v2.12.0]
15:31:32 # docker-compose ps
NAME COMMAND SERVICE STATUS PORTS
harbor-core "/harbor/entrypoint.…" core running (healthy)
harbor-db "/docker-entrypoint.…" postgresql running (healthy)
harbor-jobservice "/harbor/entrypoint.…" jobservice running (healthy)
harbor-log "/bin/sh -c /usr/loc…" log running (healthy) 127.0.0.1:1514->10514/tcp
harbor-portal "nginx -g 'daemon of…" portal running (healthy)
nginx "nginx -g 'daemon of…" proxy running (healthy) 0.0.0.0:8083->8080/tcp
redis "redis-server /etc/r…" redis running (healthy)
registry "/home/harbor/entryp…" registry running (healthy)
registryctl "/home/harbor/start.…" registryctl running (healthy)
# 关闭
docker-compose stop
# 启动
docker-compose up -d
外部 nginx 配置范例:
15:01:51 # cat ***.conf
upstream harbor {
server 192.168.2.10:8083;
}
server {
listen 80;
server_name ***.cn;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name ***.cn;
ssl_certificate /root/.acme.sh/*.opshub.cn/fullchain.cer;
ssl_certificate_key /root/.acme.sh/*.opshub.cn/*.opshub.cn.key;
ssl_session_timeout 5m;
ssl_protocols TLSV1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect http:// https://;
proxy_pass http://harbor;
}
}
可通过网页方式访问 Harbor 管理页面:
使用
Harbor的组件如下:
- harbor-core(核心组件)
- Harbor的核心组件,提供主要API服务
- 负责系统的配置管理、权限控制、认证授权
- 协调其他组件之间的通信和交互
- 处理项目、用户、角色等核心功能
- harbor-db(PostgreSQL数据库)
- Harbor的主数据库
- 存储用户、项目、角色、权限等系统数据
- 保存镜像元数据和其他配置信息
- 为其他组件提供数据持久化服务
- harbor-jobservice(任务服务)
- 负责处理Harbor中的后台任务
- 管理镜像复制任务
- 处理镜像扫描任务
- 执行垃圾收集等维护任务
- harbor-log(日志服务)
- 集中管理Harbor所有组件的日志
- 提供日志收集和存储服务
- 监听在127.0.0.1:1514端口
- harbor-portal(Web界面)
- 提供Web用户界面
- 允许用户通过浏览器访问和管理Harbor
- 提供友好的图形化操作界面
- nginx(代理服务器)
- 作为反向代理服务器
- 处理外部访问请求
- 负责请求的负载均衡
- 监听在8083端口(在您的配置中)
- redis(缓存服务)
- 提供缓存服务
- 存储会话信息
- 提高系统访问性能
- 用于组件间的消息发布/订阅
- registry(镜像仓库)
- Docker镜像的存储和分发服务
- 处理镜像的上传和下载
- 管理镜像的元数据
- 提供镜像层的存储服务
- registryctl(注册中心控制器)
- 管理registry实例
- 处理registry的垃圾收集
- 提供registry的维护功能
- 控制registry的生命周期
Harbor 重点在于通过 访问控制
、权限管理
等手段,确保产物的安全存储与分发。
在大规模企业环境下,可能需要利用 多后端存储
、镜像复制
等能力,增强在多区域网络环境下的分发与管理。
这部分内容本文不展开讲,后续单独发文补充。
下面以不涉及复杂权限控制的一个简单场景,说说 Harbor 怎么用。
使用命令行推送镜像到 Harbor:
- 创建一个测试项目
-
使用 dockerfile 构建一个 busybox 基础镜像
# vim dockerfile FROM busybox:latest # docker build -t harbor.opshub.cn/test/busybox-20241113 . DEPRECATED: The legacy builder is deprecated and will be removed in a future release. Install the buildx component to build images with BuildKit: https://docs.docker.com/go/buildx/ Sending build context to Docker daemon 11.78kB Step 1/1 : FROM busybox:latest ---> 27a71e19c956 Successfully built 27a71e19c956 Successfully tagged harbor.opshub.cn/test/busybox-20241113:latest
- 命令行登录镜像仓库
docker login ***.cn
- 上传镜像
# docker push ***.cn/test/busybox-20241113 Using default tag: latest The push refers to repository [harbor.opshub.cn/test/busybox-20241113] 58f32e9504c8: Pushed latest: digest: sha256:ff0b2bbabd0147f23a4b4b499175a2aadf4b775285ea4cfdeb7b30fa3af4bdb8 size: 527
- 查看镜像
总结
Harbor 部署并不复杂,核心在于结合 ldap 、角色管理等打通企业化 CICD 产物交付流程。