docker/docker-harbor.md
2025-04-04 17:35:25 +08:00

253 lines
11 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h2><center>Docker-harbor</center></h2>
------
## 一harbor
### 1. harbor 概念
Harbor 是一个企业级的云原生容器镜像仓库,由`VMware`主导开发并贡献给`Cloud Native Computing Foundation(CNCF)`。它通过为`Docker`镜像提供安全、高效的管理能力,帮助企业简化容器应用程序的交付流程。相比于传统的`Docker Registry``Harbor`提供了更多的企业级特性,如容器镜像仓库之间的镜像复制、用户管理、访问控制、漏洞扫描和镜像签名等功能。
### 2. 特性
`Harbor`的主要作用是为容器化应用程序提供集中式的镜像存储管理。它允许企业通过集中的仓库存储、分发和保护容器镜像,确保开发、测试和生产环境中使用的镜像都符合安全和合规要求。
`Harbor`有以下特性:
- **基于云原生场景:**`Harbor`支持容器镜像和`Helm Chart`,可用作容器`Runtime`和编排平台等云原生环境的镜像仓库。
- **镜像管理:**`Harbor`作为一个企业级的镜像仓库,支持`Docker``OCI`格式镜像的存储和管理。
- **细粒度的访问控制:**通过基于角色的访问控制`(RBAC)``Harbor`能够确保不同用户在仓库中的操作权限得到精确控制。
- **镜像复制:**支持跨多个`Harbor`实例进行镜像复制,帮助实现多数据中心或混合云环境下的高效镜像分发。
- **漏洞扫描**: 集成了`Clair``Trivy`等安全工具,`Harbor`可以自动扫描镜像中的安全漏洞,确保部署的镜像安全。
- **镜像签名和内容信任:**通过`Notary`集成,`Harbor`支持镜像的签名和验证,确保镜像的完整性和可信度。
- **日志与审计:**提供详细的操作日志和审计功能,帮助企业了解镜像的使用和管理情况。
- **多租户支持:**`Harbor`支持项目隔离,帮助企业实现多租户环境下的镜像管理。
- **LDAP/AD 支持:**`Harbor`与现有的企业`LDAP/AD`集成以进行用户身份验证和管理,并支持将`LDAP`组导入`Harbor`,然后可以授予特定项目的权限。
- **镜像删除和垃圾收集:**系统管理员可以运行垃圾回收作业,以便可以删除**镜像**(悬挂的`manifests`和未引用的`blobs`),并且可以定期释放这些空间。
- **审核:**对存储库的所有操作都通过日志进行跟踪。
- **RESTful API**提供`RESTful API`以方便管理操作,并且易于使用以与外部系统集成。嵌入式`Swagger UI`可用于探索和测试`API`
### 3. harbor 的架构
`Harbor`的架构设计遵循微服务原则,由多个松耦合的组件组成,每个组件负责不同的功能模块。在`V2.0`版本,已经完全符合`OCI`标准。
下图是`Harbor`的整体架构。
![](http://182.92.143.66:40072/directlink/img/docker/image-202504040003.png)
### 4. harbor 的组件介绍
如上图所示Harbor 由放置在 3 层中的以下组件组成:
#### 1. 数据访问层Data Access Layer
数据访问层主要负责存储和管理容器镜像以及相关的元数据,是整个 Harbor 系统的基础。它包括以下几个核心组件:
- **k-v 存储:** 由 Redis 组成,提供数据缓存功能,并支持临时持久化 Job 服务的 Job 元数据。
- **Registry镜像仓库**: 基于 Docker Registry 扩展的核心组件负责存储容器镜像及其标签tags。它支持 Docker 和 OCI 格式的镜像,并允许通过 HTTP API 进行镜像的上传、下载和管理。
- **Database数据库**: Harbor 使用 PostgreSQL 数据库存储元数据,包括用户信息、项目、镜像标签、访问控制策略等。数据库是 Harbor 中各个功能模块元数据管理的核心。
- **Object Storage**: Harbor 也支持将镜像存储在对象存储中支持的存储后端包括文件系统、S3、Ceph 等。对象存储用于存储实际的镜像层layers和其他大文件数据。
#### 2. 基础服务Fundamental Services
基础服务,即是核心层,负责处理业务逻辑,是系统的心脏。这个层次负责管理用户、权限控制、镜像的生命周期、任务调度等操作。
- **Proxy** 由 Nginx Server 组成的反向代理,提供 API 路由能力。Harbor 的组件如核心、注册中心、Web 门户和 Token 服务等,都位于这个反向代理的后面。代理将来自浏览器和 Docker 客户端的请求转发到各种后端服务。
**核心:** Harbor 的核心服务,主要提供以下功能:
- **API Service**该组件是`Harbor`的核心服务,处理所有`API`请求。它负责用户身份验证、项目和镜像的管理,以及与其他微服务的交互。它还管理系统中的访问控制`RBAC`以及任务调度。
- **Job Service**负责异步任务的处理,比如镜像的复制、漏洞扫描等。它通过消息队列来调度任务,并监控任务的执行状态。
- **Notary**该组件用于提供镜像的签名与验证服务,确保镜像的来源可信。`Notary`通过实现`Docker Content Trust (DCT)`来保护镜像的完整性。
- **Clair/Trivy**这是`Harbor`的漏洞扫描服务,负责对上传的镜像进行安全漏洞扫描。通过定期扫描镜像中的已知漏洞`CVE`,帮助管理员发现并修复潜在的安全问题。
- **Replication Controller****复制控制器**):管理复制策略和第三方容器仓库的适配器,触发和监控并发复制进程。实现了许多第三方容器仓库适配器,包括不限于`docker registry``Docker Hub``Huawei SWR`、阿里 `ACR`等。
- **k (通知管理器)**:在`Harbor`中配置的一种机制,以便可以将`Harbor`中的工件状态更改事件触发`Harbor`中配置的`Webhook`端点。而第三方应用可以通过侦听相关的`webhook`事件来触发一些后续操作。现在,支持两种方式:
1. `HTTP Post`请求
2. `Slack channel`
- **Log collector日志收集器** 日志收集器,负责将其他模块的日志收集到一个地方。
#### 3. 消费者Consumers
- **外部客户端**:作为标准的云原生容器仓库,自然会支持相关客户端,如`docker CLI``notary`客户端、`OCI`兼容客户端`(如 Oras``Helm`。除了这些客户端。
- **Web 门户:** 一个图形用户界面,可帮助用户管理`Registry`上的映像。
## 二harbor 部署
环境:安装`docker``docker-compose`
### 1. 下载
安装包下载地址:[Tags · goharbor/harbor · GitHub](https://github.com/goharbor/harbor/tags)
```bash
[root@harbor ~]# wget https://github.com/goharbor/harbor/releases/download/v2.12.2/harbor-offline-installer-v2.12.2.tgz
解压
[root@harbor ~]# tar xf harbor-offline-installer-v2.12.2.tgz -C /usr/local/
```
### 2. 配置
修改配置文件
```bash
[root@harbor ~]# cd /usr/local/harbor/
[root@harbor harbor]# mv harbor.yml.tmpl harbor.yml
[root@harbor harbor]# vim harbor.yml
修改主机IP
hostname: 192.168.159.132
注释https
# https related config
# https:
# https port for harbor, default is 443
# port: 443
# The path of cert and key files for nginx
# certificate: /your/certificate/path
# private_key: /your/private/key/path
修改密码
harbor_admin_password: Harbor12345
```
### 3. 导入镜像
```bash
[root@harbor harbor]# docker load -i harbor.v2.12.2.tar.gz
# 查看镜像
[root@harbor harbor]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
goharbor/harbor-exporter v2.8.2 63341a78f287 22 months ago 98.1MB
goharbor/redis-photon v2.8.2 6f4498a430ca 22 months ago 121MB
goharbor/trivy-adapter-photon v2.8.2 06de9f1c733d 22 months ago 460MB
goharbor/notary-server-photon v2.8.2 ef7c8ea2dc49 22 months ago 114MB
goharbor/notary-signer-photon v2.8.2 4e5b1746a124 22 months ago 111MB
goharbor/harbor-registryctl v2.8.2 fa61a236a6d6 22 months ago 142MB
goharbor/registry-photon v2.8.2 f80e71363231 22 months ago 79.3MB
goharbor/nginx-photon v2.8.2 3d009028f260 22 months ago 120MB
goharbor/harbor-log v2.8.2 2914d282d9bf 22 months ago 127MB
goharbor/harbor-jobservice v2.8.2 40118f1568a8 22 months ago 141MB
goharbor/harbor-core v2.8.2 0bbbd1f379fc 22 months ago 165MB
goharbor/harbor-portal v2.8.2 3e74e0758aa4 22 months ago 127MB
goharbor/harbor-db v2.8.2 5126635ae9f0 22 months ago 174MB
goharbor/prepare v2.8.2 eb3cf3cdd17a 22 months ago 163MB
```
### 4. 初始化与安装
```bash
# 初始化
[root@harbor harbor]# ./prepare
# 安装
[root@harbor harbor]# ./install.sh
```
### 5. 查看 harbor 状态
```bash
[root@harbor harbor]# 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:80->8080/tcp, :::80->8080/tcp
redis "redis-server /etc/r…" redis running (healthy)
registry "/home/harbor/entryp…" registry running (healthy)
registryctl "/home/harbor/start.…" registryctl running (healthy)
```
### 6. 操作
关闭`harbor`
```bash
[root@harbor harbor]# docker-compose stop
```
启动`harbor`
```bash
[root@harbor harbor]# docker-compose start
```
创建`harbor`
```bash
[root@harbor harbor]# docker-compose up -d
```
删除`harbor`
```bash
[root@harbor harbor]# docker-compose down
```
### 7. 推送镜像到 harbor
登录页面http://ip:80
![](http://182.92.143.66:40072/directlink/img/docker/image-202504040004.png)
密码:配置文件中
docker 添加 harbor 仓库
```bash
[root@docker ~]# vim /etc/docker/daemon.json
{"insecure-registries":["http://192.168.159.132"]}
```
重启 docker
```bash
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker
```
查看dcoker是否配置成功
```bash
[root@docker ~]# docker info
Insecure Registries:
192.168.159.132
127.0.0.0/8
Live Restore Enabled: false
```
docker 客户端登录 harbor
```bash
[root@docker ~]# docker login 192.168.159.132
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
```
修改镜像 tag
```bash
[root@docker ~]# docker tag centos:7 192.168.159.132/library/centos:7
```
推送镜像
```bash
[root@docker ~]# docker push 192.168.159.132/library/centos:7
The push refers to repository [192.168.159.132/library/centos]
174f56854903: Pushed
7: digest: sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f size: 529
```
查看页面
![](http://182.92.143.66:40072/directlink/img/docker/image-202504040005.png)