一、harbor介绍
### Harbor 简介
**Harbor** 是一个开源的企业级 Docker 镜像仓库,由 VMware 开发并贡献给 CNCF(云原生计算基金会)。它提供了镜像的存储、分发、安全管理和用户权限控制等功能,适用于企业级容器化应用的镜像管理。
---
### Harbor 的核心功能
1. **镜像管理**
- 支持 Docker 镜像的存储、拉取和推送。
- 提供镜像的版本管理,支持多版本镜像的存储和检索。
2. **用户权限管理**
- 支持多用户和用户组管理。
- 提供基于角色的访问控制(RBAC),可以细粒度地控制用户对镜像仓库的访问权限。
3. **镜像安全**
- **漏洞扫描**:集成 Clair 等工具,支持对镜像进行漏洞扫描,确保镜像的安全性。
- **内容信任**:支持 Docker Content Trust(DCT),确保镜像的来源可信。
- **镜像签名**:支持对镜像进行签名,防止镜像被篡改。
4. **高可用性和扩展性**
- 支持多节点部署,提供高可用性。
- 支持与外部存储系统(如 S3、NFS)集成,方便扩展存储容量。
5. **日志和审计**
- 记录所有操作日志,便于审计和故障排查。
- 提供操作日志的查询和分析功能。
6. **多租户支持**
- 支持多租户模式,不同团队或项目可以独立管理自己的镜像仓库。
7. **镜像复制**
- 支持跨多个 Harbor 实例的镜像复制,适用于多数据中心或混合云场景。
8. **Web 管理界面**
- 提供直观的 Web 界面,方便用户管理镜像、用户、项目等。
9. **API 支持**
- 提供完整的 RESTful API,方便与其他系统集成。
---
### Harbor 的架构
Harbor 的架构主要包括以下组件:
1. **Core Service(核心服务)**
- 负责处理用户请求、权限验证、镜像管理等核心逻辑。
2. **Registry**
- 基于 Docker Distribution,负责镜像的存储和分发。
3. **Database**
- 存储 Harbor 的元数据(如用户信息、项目信息、权限配置等)。
- 支持 PostgreSQL 或 MySQL。
4. **Job Service**
- 负责异步任务的处理,如镜像复制、漏洞扫描等。
5. **Log Collector**
- 收集和存储日志数据,支持日志的查询和分析。
6. **UI**
- 提供 Web 管理界面,方便用户操作。
7. **Notary**
- 提供镜像签名和内容信任功能。
8. **Clair**
- 集成漏洞扫描工具,用于检测镜像中的安全漏洞。
---
### Harbor 的部署方式
Harbor 支持多种部署方式:
1. **单机部署**
- 适用于小型环境或测试环境。
- 使用 Docker Compose 快速部署。
2. **高可用部署**
- 适用于生产环境,支持多节点部署。
- 需要配置外部数据库(如 PostgreSQL)和外部存储(如 S3、NFS)。
3. **Kubernetes 部署**
- 使用 Helm Chart 在 Kubernetes 集群中部署 Harbor。
---
### Harbor 的使用场景
1. **企业内部的私有镜像仓库**
- 企业可以使用 Harbor 搭建私有镜像仓库,管理内部开发的容器镜像。
2. **多团队协作**
- Harbor 支持多租户模式,适合多个团队或项目共享同一个镜像仓库。
3. **混合云环境**
- 支持镜像的跨云复制,适用于混合云或多数据中心的场景。
4. **安全敏感场景**
- 通过漏洞扫描、镜像签名和内容信任功能,确保镜像的安全性。
---
### Harbor 的优势
1. **开源免费**
- Harbor 是开源项目,可以免费使用和定制。
2. **功能丰富**
- 提供了企业级镜像仓库所需的所有功能。
3. **易于集成**
- 支持与 Kubernetes、Jenkins 等工具集成。
4. **社区活跃**
- 作为 CNCF 项目,Harbor 拥有活跃的社区和持续的更新。
---
### Harbor 的安装与使用
#### 1. 安装 Harbor
- 下载 Harbor 安装包:
```bash
wget https://github.com/goharbor/harbor/releases/download/v2.5.0/harbor-offline-installer-v2.5.0.tgz
```
- 解压并配置:
```bash
tar -xzf harbor-offline-installer-v2.5.0.tgz
cd harbor
cp harbor.yml.tmpl harbor.yml
```
- 修改 `harbor.yml` 配置文件,设置主机名、端口、数据库等信息。
- 安装 Harbor:
```bash
./install.sh
```
#### 2. 使用 Harbor
- 访问 Web 界面:
- 打开浏览器,访问 `http://<Harbor主机名>`。
- 登录:
- 默认管理员用户为 `admin`,密码在配置文件中设置。
- 创建项目:
- 在 Web 界面中创建项目,用于管理镜像。
- 推送镜像:
- 登录 Harbor:
```bash
docker login <Harbor主机名>
```
- 打标签并推送镜像:
```bash
docker tag <镜像名> <Harbor主机名>/<项目名>/<镜像名>:<标签>
docker push <Harbor主机名>/<项目名>/<镜像名>:<标签>
总结
Harbor 是一个功能强大且易于使用的企业级 Docker 镜像仓库,适用于需要私有镜像仓库、多团队协作、镜像安全管理的场景。通过 Harbor,企业可以更好地管理容器化应用的镜像,提升开发和运维效率。
二、实战部署
[root@cjr ~]# mkdir -p /data/ssl
[root@cjr ~]# cd /data/ssl/
[root@cjr ssl]# openssl genrsa -out ca.key 3096
Generating RSA private key, 3096 bit long modulus
..................................................................................................................................................................................................++
........................................++
e is 65537 (0x10001)
[root@cjr ssl]# openssl req -new -x509 -days 3650 -key ca.key -out ca.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:SZ
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:cjr
Organizational Unit Name (eg, section) []:k8s
Common Name (eg, your name or your server's hostname) []:cjr
Email Address []:123456@qq.com
[root@cjr ssl]# openssl genrsa -out harbor.key 3072
Generating RSA private key, 3072 bit long modulus
..............................................++
..................................................................................................................++
e is 65537 (0x10001)
[root@cjr ssl]# openssl req -new -key harbor.key -out harbor.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:SZ^H^Hs^C
[root@cjr ssl]# openssl req -new -key harbor.key -out harbor.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:SZ
State or Province Name (full name) []:SZ
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:cjr
Organizational Unit Name (eg, section) []:cjr
Common Name (eg, your name or your server's hostname) []:cjr
Email Address []:123456@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@cjr ssl]# openssl x509 -req -in harbor.csr -CAkey ca.key -CAkey ca.key -CA ca.pem -CAcreateserial -out harbor.pem -days 3650
Signature ok
subject=/C=SZ/ST=SZ/L=SZ/O=cjr/OU=cjr/CN=cjr/emailAddress=123456@qq.com
Getting CA Private Key