VMware Harbor 开源的Docker Registry管理项目

本文详细介绍了如何搭建企业级Docker Registry Harbor,包括关闭防火墙和SELinux,安装Docker和Docker-compose,配置Harbor,启动服务,以及通过docker-compose管理服务状态。Harbor提供了基于角色的访问控制、镜像复制、图形化用户界面、AD/LDAP集成、镜像删除和审计管理等功能,适用于负载均衡、高可用和多云场景。
摘要由CSDN通过智能技术生成

一、简介

Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它主要提供 Dcoker Registry 管理界面UI,可基于角色访问控制,镜像复制, AD/LDAP 集成,日志审核等功能,完全的支持中文

 二、功能

  • 基于角色的访问控制

用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。

  • 基于镜像的复制策略

镜像可以在多个Registry实例中复制(可以将仓库中的镜像同步到远程的Harbor,类似于MySQL主从同步功能),尤其适合于负载均衡,高可用,混合云和多云的场景。

  • 图形化用户界面

用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。

  • 支持 AD/LDAP

Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理。

  • 镜像删除和垃圾回收

Harbor支持在Web删除镜像,回收无用的镜像,释放磁盘空间。image可以被删除并且回收image占用的空间。

  • 审计管理

所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。

  • RESTful API

RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。

  • 部署简单

提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备。

 三、架构

 

1、Proxy:反向代理工具
2、Registry:负责存储docker镜像,处理上传/下载命令。对用户进行访问控制,它指向一个token服务,强制用户的每次docker pull/push请求都要携带一个合法的token,registry会通过公钥对token进行解密验证。
3、Core service:Harbor的核心功能:
UI:图形界面
Webhook:及时获取registry上image状态变化情况,在registry上配置 webhook,把状态变化传递给UI模块。
Token服务:复杂根据用户权限给每个docker push/p/ull命令签发token。Docker客户端向registry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向registry进行请求。
4、Database:提供数据库服务,存储用户权限,审计日志,docker image分组信息等数据
5、Log collector:为了帮助监控harbor运行,复责收集其他组件的log,供日后进行分析

四、部署

1、关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
 
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
setenforce 0  # 临时

2、安装并启动Docker

第一步:安装工具包

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2        #安装工具包,缺少这些依赖将无法完成

第二步:设置docker源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
或者
wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

第三步:安装

yum install docker-ce -y

第四步:启动

systemctl daemon-reload && systemctl start docker      #启动docker
chkconfig docker on         #加入开机启动

3、安装Docker-compose

我这里安装1.29.2,可以通过下面的连接找版本 

Docker-compose网址:Releases · docker/compose · GitHub

[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose     #下载
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose   #赋可执行权限
[root@localhost ~]# docker-compose --version            #测试下docker-compose是否安装成功
docker-compose version 1.29.2, build 5becea4c
[root@localhost ~]# 

4、Harbor服务搭建及启动

GitHub上https://github.com/goharbor/harbor/releases下载指定版本的安装包

 离线包安装:

[root@localhost ~]# yum install wget -y
[root@localhost harbor]# wget 
 https://github.com/goharbor/harbor/releases/download/v2.2.3/harbor-offline-installer-v2.2.3.tgz
[root@localhost harbor]# tar -zxvf harbor-offline-installer-v2.2.3.tgz 
[root@localhost harbor]# cd harbor
[root@localhost harbor]# ls
common.sh             harbor.yml.tmpl  LICENSE
harbor.v2.2.3.tar.gz  install.sh       prepare
[root@localhost harbor]# cp harbor.yml.tmpl  harbor.yml
[root@localhost harbor]# vi harbor.yml

1、 配置hostname  //需要写IP地址或者域名,需要看你的访问方式

2、注释https所有的配置

3、harbor_admin_password: Harbor12345   # //admin密码

# 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: 10.70.36.220    //需要写IP地址或者域名

# 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
  #private_key: /your/private/key/path
#这里不启用https
# # Uncomment following will enable tls communication between all harbor components
# internal_tls:
#   # set enabled to true means internal tls is enabled
#   enabled: true
#   # put your cert and key files on dir
#   dir: /etc/harbor/tls/internal

# Uncomment external_url if you want to enable external proxy
# And when it enabled the hostname will no longer used
# external_url: https://reg.mydomain.com:8433

# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: Harbor12345   # //admin密码

# Harbor DB configuration
database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: root123

# Harbor DB configuration
database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: root123
  # The maximum number of connections in the idle connection pool. If it <=0, no idle connections are retained.
  max_idle_conns: 50
  # The maximum number of open connections to the database. If it <= 0, then there is no limit on the number of open connections.
  # Note: the default number of connections is 1024 for postgres of harbor.
  max_open_conns: 1000

# The default data volume
data_volume: /data

 如果持久化数据目录 可以修改data_volume: /data  

[root@localhost harbor]# ./prepare 
prepare base dir is set to /root/harbor
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
[root@localhost harbor]# 
[root@localhost harbor]# ./install.sh 

[Step 0]: checking if docker is installed ...

Note: docker version: 20.10.7

[Step 1]: checking docker-compose is installed ...

Note: docker-compose version: 1.29.2

[Step 2]: loading Harbor images ...
Loaded image: goharbor/registry-photon:v2.2.3
Loaded image: goharbor/notary-server-photon:v2.2.3
Loaded image: goharbor/trivy-adapter-photon:v2.2.3
Loaded image: goharbor/harbor-jobservice:v2.2.3
Loaded image: goharbor/harbor-registryctl:v2.2.3
Loaded image: goharbor/redis-photon:v2.2.3
Loaded image: goharbor/chartmuseum-photon:v2.2.3
Loaded image: goharbor/prepare:v2.2.3
Loaded image: goharbor/harbor-db:v2.2.3
Loaded image: goharbor/notary-signer-photon:v2.2.3
Loaded image: goharbor/harbor-portal:v2.2.3
Loaded image: goharbor/harbor-log:v2.2.3
Loaded image: goharbor/nginx-photon:v2.2.3
Loaded image: goharbor/harbor-core:v2.2.3
Loaded image: goharbor/harbor-exporter:v2.2.3


[Step 3]: preparing environment ...

[Step 4]: preparing harbor configs ...
prepare base dir is set to /root/harbor
WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
Clearing the configuration file: /config/portal/nginx.conf
Clearing the configuration file: /config/log/logrotate.conf
Clearing the configuration file: /config/log/rsyslog_docker.conf
Clearing the configuration file: /config/nginx/nginx.conf
Clearing the configuration file: /config/core/env
Clearing the configuration file: /config/core/app.conf
Clearing the configuration file: /config/registry/passwd
Clearing the configuration file: /config/registry/config.yml
Clearing the configuration file: /config/registry/root.crt
Clearing the configuration file: /config/registryctl/env
Clearing the configuration file: /config/registryctl/config.yml
Clearing the configuration file: /config/db/env
Clearing the configuration file: /config/jobservice/env
Clearing the configuration file: /config/jobservice/config.yml
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
loaded secret from file: /data/secret/keys/secretkey
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir


Note: stopping existing Harbor instance ...
Stopping harbor-jobservice ... done
Stopping harbor-core       ... done
Stopping registry          ... done
Stopping registryctl       ... done
Stopping redis             ... done
Stopping harbor-portal     ... done
Stopping harbor-db         ... done
Stopping harbor-log        ... done
Removing harbor-jobservice ... done
Removing nginx             ... done
Removing harbor-core       ... done
Removing registry          ... done
Removing registryctl       ... done
Removing redis             ... done
Removing harbor-portal     ... done
Removing harbor-db         ... done
Removing harbor-log        ... done
Removing network harbor_harbor


[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating registry      ... done
Creating redis         ... done
Creating harbor-portal ... done
Creating harbor-db     ... done
Creating registryctl   ... done
Creating harbor-core   ... done
Creating harbor-jobservice ... done
Creating nginx             ... done
✔ ----Harbor has been installed and started successfully.----
[root@localhost harbor]# 

 通过docker-compose ps 查看服务状态

[root@localhost harbor]# docker-compose ps
      Name                     Command                  State                      Ports                
--------------------------------------------------------------------------------------------------------
harbor-core         /harbor/entrypoint.sh            Up (healthy)                                       
harbor-db           /docker-entrypoint.sh            Up (healthy)                                       
harbor-jobservice   /harbor/entrypoint.sh            Up (healthy)                                       
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)   127.0.0.1:1514->10514/tcp           
harbor-portal       nginx -g daemon off;             Up (healthy)                                       
nginx               nginx -g daemon off;             Up (healthy)   0.0.0.0:80->8080/tcp,:::80->8080/tcp
redis               redis-server /etc/redis.conf     Up (healthy)                                       
registry            /home/harbor/entrypoint.sh       Up (healthy)                                       
registryctl         /home/harbor/start.sh            Up (healthy)      
[root@localhost ~]# docker-compose ps
ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml
        
[root@localhost ~]# 

 这个报错解决方法

进入到harbor的安装目录,安装目录里有docker-compose.yml文件,才能执行docker-compose ps

[root@localhost ~]# ls
anaconda-ks.cfg  Dockerfile  harbor  harbor-offline-installer-v2.2.3.tgz  htdocs  test.tar  vmtools  vmtools-2.5.0.156.tar.bz2
[root@localhost ~]# cd harbor
[root@localhost harbor]# ls
common  common.sh  docker-compose.yml  harbor.v2.2.3.tar.gz  harbor.yml  harbor.yml.tmpl  install.sh  LICENSE  prepare
[root@localhost harbor]# docker-compose ps
      Name                     Command                  State                      Ports                
--------------------------------------------------------------------------------------------------------
harbor-core         /harbor/entrypoint.sh            Up (healthy)                                       
harbor-db           /docker-entrypoint.sh            Up (healthy)                                       
harbor-jobservice   /harbor/entrypoint.sh            Up (healthy)                                       
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)   127.0.0.1:1514->10514/tcp           
harbor-portal       nginx -g daemon off;             Up (healthy)                                       
nginx               nginx -g daemon off;             Up (healthy)   0.0.0.0:80->8080/tcp,:::80->8080/tcp
redis               redis-server /etc/redis.conf     Up (healthy)                                       
registry            /home/harbor/entrypoint.sh       Up (healthy)                                       
registryctl         /home/harbor/start.sh            Up (healthy)           

 打开浏览器输入harbor的IP地址登录UI界面。用账户密码进行登录 默认80端口

 5、Harbor的配置和使用(harbor主机)

1、配置http镜像仓库可信任

编辑 /etc/docker/daemon.json 

添加"insecure-registries":["10.70.36.220"]         #这里的ip是harbor的ip

[root@localhost harbor]# vi /etc/docker/daemon.json 

{
  "registry-mirrors": ["https://5twf62k1.mirror.aliyuncs.com"],
  "insecure-registries": ["http://10.70.36.220"]
}

或者也可以修改 /usr/lib/systemd/system/docker.service(二选一)

[root@server3 ~]# vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 10.70.36.220 --containerd=/run/containerd/containerd.sock    #添加服务器的ip

重启docker

[root@localhost harbor]# systemctl restart docker

 docker info一下找到 Insecure Registries:

表示ok

docker-compose ps  发现服务down了

启动harbor的服务   docker-compose up -d

[root@localhost harbor]# docker-compose ps
      Name                     Command                       State                     Ports          
------------------------------------------------------------------------------------------------------
harbor-core         /harbor/entrypoint.sh            Exit 137                                         
harbor-db           /docker-entrypoint.sh            Exit 137                                         
harbor-jobservice   /harbor/entrypoint.sh            Exit 137                                         
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (health: starting)   127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Exit 128                                         
nginx               nginx -g daemon off;             Restarting                                       
redis               redis-server /etc/redis.conf     Exit 128                                         
registry            /home/harbor/entrypoint.sh       Exit 137                                         
registryctl         /home/harbor/start.sh            Exit 137                                         
[root@localhost harbor]# docker-compose up -d
harbor-log is up-to-date
Starting harbor-db     ... done
Starting registryctl   ... done
Starting redis         ... done
Starting harbor-portal ... done
Starting registry      ... done
Starting harbor-core   ... done
Starting harbor-jobservice ... 
Starting harbor-jobservice ... done

2、推送镜像

这里的示例是admin用户和默认的项目(可以自建账户和自建项目)

①登录

[root@localhost harbor]# docker login 10.70.36.220
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.jso
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost harbor]# 

②给镜像打标签(模拟httpd)

 可以参考推送命令

[root@localhost harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED        SIZE
httpd                           latest    bd29370f84ea   3 days ago     138MB
[root@localhost harbor]# docker tag  httpd  10.70.36.220/library/httpd:v1
[root@localhost harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED        SIZE
10.70.36.220/library/httpd      v1        bd29370f84ea   3 days ago     138MB
[root@localhost harbor]# 

 ③上传镜像(上传之前一定要登录①)

[root@localhost harbor]# docker push 10.70.36.220/library/httpd:v1
The push refers to repository [10.70.36.220/library/httpd]
239871c4cac5: Pushed 
9262f7dd1498: Pushed 
61172cb5065c: Pushed 
9fbbeddcc4e4: Pushed 
764055ebc9a7: Pushed 
v1: digest: sha256:51dabee6ad1cca2ae3a80e974224f30e7121dab1da9edf62e11179f8c652b09e size: 1366
[root@localhost harbor]# 

 如图上传上去了

 ④下载镜像

 上面复制的拉取命令(但是有点乱特别是带sha256...)

[root@localhost harbor]# docker pull 10.70.36.220/library/httpd@sha256:51dabee6ad1cca2ae3a80e974224f30e7121dab1da9edf62e11179f8c652b09e
10.70.36.220/library/httpd@sha256:51dabee6ad1cca2ae3a80e974224f30e7121dab1da9edf62e11179f8c652b09e: Pulling from library/httpd
Digest: sha256:51dabee6ad1cca2ae3a80e974224f30e7121dab1da9edf62e11179f8c652b09e
Status: Downloaded newer image for 10.70.36.220/library/httpd@sha256:51dabee6ad1cca2ae3a80e974224f30e7121dab1da9edf62e11179f8c652b09e
10.70.36.220/library/httpd@sha256:51dabee6ad1cca2ae3a80e974224f30e7121dab1da9edf62e11179f8c652b09e
[root@localhost harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED        SIZE
10.70.36.220/library/httpd      <none>    bd29370f84ea   3 days ago     138MB
[root@localhost harbor]# 

找到有tag标签的,还可以编辑tag标签和属性的页面,还可以下载带tag标签的命令的页面

 继续点击下面的

[root@localhost harbor]# docker pull 10.70.36.220/library/httpd:v1
v1: Pulling from library/httpd
Digest: sha256:51dabee6ad1cca2ae3a80e974224f30e7121dab1da9edf62e11179f8c652b09e
Status: Downloaded newer image for 10.70.36.220/library/httpd:v1
10.70.36.220/library/httpd:v1
[root@localhost harbor]# 

 也能看到属性及构造历史

6、其他服务器登录Harbor仓库

 ①、配置http镜像仓库可信任

编辑 /etc/docker/daemon.json 

添加"insecure-registries":["10.70.36.220"]         #这里的ip是harbor的ip

[root@localhost harbor]# vi /etc/docker/daemon.json 

{
  "registry-mirrors": ["https://5twf62k1.mirror.aliyuncs.com"],
  "insecure-registries": ["http://10.70.36.220"]
}

或者也可以修改 /usr/lib/systemd/system/docker.service(二选一)

[root@server3 ~]# vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 10.70.36.220 --containerd=/run/containerd/containerd.sock    #添加服务器的ip

报错下面这个问题就是因为上面的因素没有配置insecure-registry

root@server3 ~]# docker login 10.70.36.220
Error response from daemon: Get https://20.0.0.10/v2/: dial tcp 20.0.0.10:443: connect: connection refused

重启docker

[root@localhost harbor]# systemctl restart docker

 docker info一下找到 Insecure Registries:

表示ok

剩下的操作不变

①登录

②给镜像打标签(模拟httpd)    

③上传镜像(上传之前一定要登录①)

④下载镜像

功能挺多的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值