首页 > 技术文章 > Harbor仓库部署

diqiyao 2021-03-09 14:20 原文

Harbor简介

Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器。

作为一个企业级私有 Registry 服务器,Harbor 提供了更好的性能和安全。提升用户使用 Registry 构建和运行环境传输镜像的效率。Harbor 支持安装在多个 Registry 节点的镜像资源复制,镜像全部保存在私有 Registry 中, 确保数据和知识产权在公司内部网络中管控。另外,Harbor 也提供了高级的安全特性,诸如用户管理,访问控制和活动审计等。

  • 基于角色的访问控制 - 用户与 Docker 镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。
  • 镜像复制 - 镜像可以在多个 Registry 实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。
  • 图形化用户界面 - 用户可以通过浏览器来浏览,检索当前 Docker 镜像仓库,管理项目和命名空间。
  • AD/LDAP 支持 - Harbor 可以集成企业内部已有的 AD/LDAP,用于鉴权认证管理。
  • 审计管理 - 所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。
  • 国际化 - 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。
  • RESTful API - RESTful API 提供给管理员对于 Harbor 更多的操控, 使得与其它管理软件集成变得更容易。

 

Docker Compose

Harbor在物理机上部署是非常难的,而为了简化Harbor的应用,Harbor官方直接把Harbor做成了在容器中运行的应用,而且这个容器在Harbor中依赖类似redis、mysql、pgsql等很多存储系统,所以它需要编排很多容器协同起来工作,因此VMWare Harbor在部署和使用时,需要借助于Docker的单机编排工具(Docker compose)来实现。

 Docker Compose官方文档: https://docs.docker.com/compose/

Harbor官方文档 :https://github.com/goharbor/harbor

 

安装Harbor

前提:安装docker 和docker compose

 

安装docker compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

#给予可执行权限
sudo chmod +x /usr/local/bin/docker-compose

 

这里使用harbor版本:harbor-offline-installer-v2.2.0.tgz

#下载harbor安装包
wget https://github.com/goharbor/harbor/releases/download/v2.2.0/harbor-offline-installer-v2.2.0.tgz

#解压缩
[root@test ~]# tar -xf ~/harbor-offline-installer-v2.2.0.tgz  -C /usr/local/
[root@test ~]# cd /usr/local/harbor/
[root@test harbor]# ls
common.sh  harbor.v2.2.0.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare

#配置harbor.yml文件
[root@test harbor]# cp harbor.yml.tmpl  harbor.yml

 

修改harbor.yml文件,这没有使用https,注释掉

[root@test harbor]# vim  harbor.yml
# Configuration file of Harbor
  
# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: 192.168.248.129    //修改此处

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 80

# 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            //注释掉
  #pri vate_key: /your/private/key/path       //注释掉
................................................
................................................
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: Harbor12345     //初始账号密码:admin/Harbor12345

 

 重新生成配置文件,安装

[root@test harbor]# ./prepare
[root@test harbor]# ./install.sh

 

 设置开机自启

[root@test harbor]# echo "/usr/local/bin/docker-compose  -f  /usr/local/harbor/docker-compose.yml up -d" >> /etc/rc.d/rc.local 

 

启动后在web页面对仓库进行访问,账号密码:admin/Harbor12345

 

 用户创建

在harbor页面创建一些用户组。便于这些用户可以获取不同的访问权限

 

填写完整信息完成用户注册

 

可以把该账户设为管理员

 

 镜像仓库创建

添加项目名称,就是我们要把镜像上传到的位置。比如创建一个test_project

 

 

添加项目成员

 

可以在服务器上验证是否已经成功创建了用户

[root@test harbor]# docker login 192.168.248.129 
Username: xiao 
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

 

镜像上传和拉取

 登录到仓库

[root@test harbor]# docker login  192.168.248.129
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

 

选取一个镜像打上标签,上传到仓库

 

#打上标签
[root@test harbor]# docker tag  apache:0.3   192.168.248.129/test_project/apache:v0.1
[root@test harbor]# docker images
REPOSITORY                            TAG       IMAGE ID       CREATED        SIZE
192.168.248.129/test_project/apache   v0.1      12b00adf81b2   24 hours ago   110MB
apache                                0.3       12b00adf81b2   24 hours ago   110MB

#上传到仓库
[root@test harbor]# docker push   192.168.248.129/test_project/apache:v0.1
The push refers to repository [192.168.248.129/test_project/apache]
666592f01555: Pushed 
c08625005cc4: Pushed 
c04d1437198b: Pushed 
v0.1: digest: sha256:3028bec094b7cf0d92da1878cefdb60d1069c78a37a5c2ee571a7a3325dcb241 size: 952

 

在web上访问仓库,就可以看到刚刚上传的镜像

 

拉取镜像,将本地镜像删除然后拉取

[root@test harbor]# docker rmi   192.168.248.129/test_project/apache:v0.1
Untagged: 192.168.248.129/test_project/apache:v0.1
Untagged: 192.168.248.129/test_project/apache@sha256:3028bec094b7cf0d92da1878cefdb60d1069c78a37a5c2ee571a7a3325dcb241


[root@test harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED        SIZE
apache                          0.3       12b00adf81b2   24 hours ago   110MB

#拉取镜像
[root@test harbor]# docker pull 192.168.248.129/test_project/apache:v0.1
v0.1: Pulling from test_project/apache
Digest: sha256:3028bec094b7cf0d92da1878cefdb60d1069c78a37a5c2ee571a7a3325dcb241
Status: Downloaded newer image for 192.168.248.129/test_project/apache:v0.1
192.168.248.129/test_project/apache:v0.1

[root@test harbor]# docker images
REPOSITORY                            TAG       IMAGE ID       CREATED        SIZE
192.168.248.129/test_project/apache   v0.1      12b00adf81b2   24 hours ago   110MB
apache                                0.3       12b00adf81b2   24 hours ago   110MB

 

推荐阅读