zl程序教程

您现在的位置是:首页 >  其他

当前栏目

企业实战!基于Harbor搭建企业镜像仓库

2023-02-25 18:20:55 时间

企业实战!基于Harbor搭建企业镜像仓库

虽然Docker官方提供了Docker Hub作为公共的Registry服务器,给到用户进行镜像的保存和管理工作。但对于企业而言,考虑到安全性和网络效率等原因,通常会搭建私有的Registry服务器,用于提供企业内部的镜像仓库服务。 本文将基于开源的Harbor项目来介绍关于私有仓库的搭建。 我的博客:https://blog.itwk.cc/post/harbor_install.html

1、Introduction to Harbor

Harbor是由VMware公司中国团队开发的一个企业级Registry项目,可用于搭建企业内部的容器镜像仓库。Harbor在Docker Registry的基础上增加了企业用户所需的权限控制、安全漏洞扫描、日志审核和远程复制等重要功能,还提供了图形管理界面及面向国内用户的中文支持,开源后便迅速业内流行开来,成为中国云原生用户的主流容器镜像仓库。 2018年7月,Harbor正式进入CNCF(谷歌创办的云原生基金会,旗下项目包括Kubernetes、Prometheus等世界级产品),并在2020年6月顺利毕业,成为了CNCF首个来自中国的开源项目。

<img src="https://static.itwk.cc:8443/LightPicture/2022/11/fe52eb027a468440.png" style="zoom: 67%;" />

Harbor的架构如下图所示,其中Core services为Harbor的核心模块,主要包括UI、token和webhook三个组件。UI提供图形化界面,辅助用户管理镜像;webhook 用于及时 获取Registry上镜像状态的变化情况,并传递给其他模块;token组件用于提供验证令牌。

另外,还有Job service用于多个Harbor间的镜像同步功能,Log collector用于日志收集和审核等功能

image.png

除了自身组件外,Harbor也需要使用到一些外部组件,如使用Nginx作为代理、Registry v2作为镜像存储、PostgreSQL作为数据库等等。

harbor的每个组件都是以Docker容器的形式进行部署,可以使用Docker Compose来进行统一管理。

2、Resource allocation requirements

hardware requirements

硬件

最低配置

推荐配置

CPU

2CPU

4CPU

内存

4GB

8GB

磁盘

40GB

160GB

Software Requirements

软件

版本

Docker

Docker 17.06.0-ce+版或更高版本

Docker Compose

1.18.0或更高

Openssl

首选latest版本

Install Deployment

Install Docker

Centos/RHEL版本

安装依赖

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

根据你的发行版下载repo文件

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

把软件仓库地址替换为 国内加速源(这边采用清华大学)

sudo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo

安装

sudo yum makecache fast
sudo yum install docker-ce

Debian/Ubuntu版本

安装依赖

sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

信任 Docker 的 GPG 公钥

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg	#Debian
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg	#Ubuntu

添加软件仓库

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装

sudo apt-get update
sudo apt-get install docker-ce

安装 Docker-compose

参考:https://www.928wang.cn/archives/1006.html

Install Harbor

下载并解压Harbor

cd /opt && wget https://github.com/goharbor/harbor/releases/download/v1.10.15/harbor-offline-installer-v1.10.15.tgz
tar xvf harbor-offline-installer-v1.10.15.tgz

Edit**/harbor/harbor.yml**

hostname: www.example.com #harbor域名或IP地址,使用域名的话需提前配置域名解析
http:
  port: 80		#默认HTTP端口
https:
  port: 443		#默认HTTPS端口
  certificate: /etc/ssl/server.crt  #证书
  private_key: /etc/ssl/server.key
harbor_admin_password: Harbor12345 #admin用户登录密码
database:
  password: Harbor12345    #harbor数据库ROOT用户链接的密码
  max_idle_conns: 50
  max_open_conns: 100
data_volume: /data    #数据目录

编辑完成之后推出运行安装脚本

sh install.sh

防火墙放行相应端口

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload

完成后操作

使用firewall-cmd --list-ports 命令查看是否已放行对应端口 (云服务器需要查看安全组是否放行对应端口)

安装完成后 运行docker ps -a 命令 确认所有容器正常运行

确认Harbor相关组件容器运行正常后,打开浏览器访问域名(IP),可看到登录界面,安装完成。