Docker搭建私有仓库Harbor

Harbor

无论是使用Docker-distribution去自建仓库,还是通过官方镜像跑容器的方式去自建仓库,通过前面的演示我们可以发现其是非常的简陋的,还不如直接使用官方的Docker Hub去管理镜像来得方便,至少官方的Docker Hub能够通过web界面来管理镜像,还能在web界面执行搜索,还能基于Dockerfile利用Webhooks和Automated Builds实现自动构建镜像的功能,用户不需要在本地执行docker build,而是把所有build上下文的文件作为一个仓库推送到github上,让Docker Hub可以从github上去pull这些文件来完成自动构建。

但无论官方的Docker Hub有多强大,它毕竟是在国外,所以速度是最大的瓶颈,我们很多时候是不可能去考虑使用官方的仓库的,但是上面说的两种自建仓库方式又十分简陋,不便管理,所以后来就出现了一个被 CNCF 组织青睐的项目,其名为Harbor。

Harbor简介

Harbor是由VMWare在Docker Registry的基础之上进行了二次封装,加进去了很多额外程序,而且提供了一个非常漂亮的web界面。

  • Harbor是一个开源可信的云原生的仓库项目,用于存储、用户管理和查找镜像。
  • Harbor通过添加用户通常需要的功能,如安全、身份和管理,扩展了开源Docker分发版。
  • Harbor支持高级特性,如用户管理、访问控制、活动监视和实例之间的复制。

Harbor的功能

  • 多租户内容签名和验证

  • 安全性和漏洞分析

  • 审计日志记录

  • 身份集成和基于角色的访问控制

  • 实例之间的映像复制

  • 可扩展API和图形UI

  • 国际化(目前为中英文化)

Docker compose

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

Compose是一个用于定义和运行多容器Docker应用程序的工具。使用Compose,您可以使用YAML文件来配置应用程序的服务。然后,通过一个命令,您可以创建并启动配置中的所有服务。

Docker Compose官方文档

Harbor安装

Harbor官方文档
harbor-offline-installer-v2.3.5.tgz

//修改主机名
[root@localhost ~]# hostname harbor.example.com
[root@localhost ~]# bash


//关闭防火墙
[root@harbor ~]# systemctl disable --now firewalld.service 
[root@harbor ~]# sed -i s/SELINUX=enforing/SELINUX=disabled/g /etc/selinux/config 
[root@harbor ~]# setenforce 0
[root@harbor ~]# reboot


//安装docker-compose(centos8)
[root@localhost ~]#  curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   664  100   664    0     0   1449      0 --:--:-- --:--:-- --:--:--  1446
100 12.1M  100 12.1M    0     0   419k      0  0:00:29  0:00:29 --:--:-- 2869k

[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# ll /usr/local/bin/
总用量 3824
-rwxr-xr-x. 1 root root 12737304 1215 22:40 docker-compose
[root@localhost ~]# which docker-compose
/usr/local/bin/docker-compose


//下载安装harbor并解压
[root@harbor ~]# wget -c https://github.com/goharbor/harbor/releases/download/v2.3.5/harbor-offline-installer-v2.3.5.tgz
[root@harbor ~]# ls
anaconda-ks.cfg  harbor-offline-installer-v2.3.5.tgz
[root@harbor ~]# tar xf harbor-offline-installer-v2.3.5.tgz -C /usr/local/
[root@harbor ~]# ls /usr/local/
bin  etc  games  harbor  include  lib  lib64  libexec  sbin  share  src


//编写yaml文件
[root@harbor ~]# cd /usr/local/harbor/
[root@harbor harbor]# ls
common.sh  harbor.v2.3.5.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare
[root@harbor harbor]# cp harbor.yml.tmpl harbor.yml


//添加解析地址
[root@harbor ~]# cat /etc/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.129.205 harbor.example.com

//修改配置文件
[root@harbor ~]# cat /usr/local/harbor/harbor.yml
hostname: harbor.example.com     #主机名修改为域名(也可以是ip)

#注释掉以下没注释的
# https related config
# https:
  # https port for harbor, default is 443     #因为未做https,所以将https全部注释
  # port: 443
  # The path of cert and key files for nginx
  # certificate: /your/certificate/path
  # private_key: /your/private/key/path

harbor_admin_password: harbor123               #web界面登录密码

//执行脚本
[root@harbor harbor]# ./install.sh 
[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating redis         ... done
Creating harbor-portal ... done
Creating registry      ... done
Creating harbor-db     ... done
Creating registryctl   ... done
Creating harbor-core   ... done
Creating nginx             ... done
Creating harbor-jobservice ... done
✔ ----Harbor has been installed and started successfully.----		//安装成功


//查看镜像
[root@harbor harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED      SIZE
goharbor/harbor-exporter        v2.3.5    1730c6f650e2   5 days ago   81.9MB
goharbor/chartmuseum-photon     v2.3.5    47004f032938   5 days ago   179MB
goharbor/redis-photon           v2.3.5    3d0cedc89a0d   5 days ago   156MB
goharbor/trivy-adapter-photon   v2.3.5    5c0212e98070   5 days ago   133MB
goharbor/notary-server-photon   v2.3.5    f20a76c65359   5 days ago   111MB
goharbor/notary-signer-photon   v2.3.5    b9fa38eef4d7   5 days ago   108MB
goharbor/harbor-registryctl     v2.3.5    7a52567a76ca   5 days ago   133MB
goharbor/registry-photon        v2.3.5    cf22d3e386b8   5 days ago   82.6MB
goharbor/nginx-photon           v2.3.5    5e3b6d9ce11a   5 days ago   45.7MB
goharbor/harbor-log             v2.3.5    a03e4bc963d6   5 days ago   160MB
goharbor/harbor-jobservice      v2.3.5    2ac32df5a2e0   5 days ago   211MB
goharbor/harbor-core            v2.3.5    23baee01156f   5 days ago   193MB
goharbor/harbor-portal          v2.3.5    bb545cdedf5a   5 days ago   58.9MB
goharbor/harbor-db              v2.3.5    9826c57a5749   5 days ago   221MB
goharbor/prepare                v2.3.5    a1ceaabe47b2   5 days ago   255MB

[root@harbor harbor]# docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED          STATUS                             PORTS                                   NAMES
b4411ec6082c   goharbor/nginx-photon:v2.3.5         "nginx -g 'daemon of…"   13 seconds ago   Up 11 seconds (health: starting)   0.0.0.0:80->8080/tcp, :::80->8080/tcp   nginx
a4f0b16e201c   goharbor/harbor-jobservice:v2.3.5    "/harbor/entrypoint.…"   13 seconds ago   Up 11 seconds (health: starting)                                           harbor-jobservice
aba7b1c304eb   goharbor/harbor-core:v2.3.5          "/harbor/entrypoint.…"   13 seconds ago   Up 12 seconds (health: starting)                                           harbor-core
626609d9a68c   goharbor/registry-photon:v2.3.5      "/home/harbor/entryp…"   15 seconds ago   Up 13 seconds (health: starting)                                           registry
f2949670619c   goharbor/harbor-registryctl:v2.3.5   "/home/harbor/start.…"   15 seconds ago   Up 13 seconds (health: starting)                                           registryctl
cbbdda98e908   goharbor/harbor-db:v2.3.5            "/docker-entrypoint.…"   15 seconds ago   Up 13 seconds (health: starting)                                           harbor-db
a1c9f1572801   goharbor/redis-photon:v2.3.5         "redis-server /etc/r…"   15 seconds ago   Up 13 seconds (health: starting)                                           redis
2200dddc9b26   goharbor/harbor-portal:v2.3.5        "nginx -g 'daemon of…"   15 seconds ago   Up 13 seconds (health: starting)                                           harbor-portal
f15bd3dd9b5c   goharbor/harbor-log:v2.3.5           "/bin/sh -c /usr/loc…"   16 seconds ago   Up 14 seconds (health: starting)   127.0.0.1:1514->10514/tcp 

[root@harbor harbor]# ss -antl
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                127.0.0.1:1514              0.0.0.0:*                  
LISTEN    0         128                  0.0.0.0:80                0.0.0.0:*                  
LISTEN    0         128                  0.0.0.0:22                0.0.0.0:*                  
LISTEN    0         128                     [::]:80                   [::]:*                  
LISTEN    0         128                     [::]:22                   [::]:*   

使用IP登录管理Harbor

账户:admin
密码:harbor23
在这里插入图片描述
在这里插入图片描述

登录

[root@dharbor ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.129.205 harbor.example.com

[root@harbor harbor]# vim /etc/docker/daemon.json
[root@harbor harbor]# cat /etc/docker/daemon.json
{
        "insecure-registries": ["harbor.example.com"]
}


//重启服务
[root@harbor harbor]# systemctl restart docker	//重启服务以后需要再跑一遍脚本,80端口才会启动


//登录
[root@harbor harbor]# docker login harbor.example.com
Authenticating with existing credentials...
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@harbor harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED       SIZE
alpine                          latest    c059bfaa849c   2 weeks ago   5.59MB

[root@harbor harbor]# docker tag alpine:latest harbor.example.com/alpine-test/alpine:v0.1
[root@harbor harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED       SIZE
alpine                                     latest    c059bfaa849c   2 weeks ago   5.59MB
harbor.example.com/alpine-test/alpine   v0.1      c059bfaa849c   2 weeks ago   5.59MB

[root@harbor harbor]# docker push harbor.example.com/alpine-test/alpine:v0.1 
The push refers to repository [harbor.example.com/alpine-test/alpine]
8d3ac3489996: Pushed 
v0.1: digest: sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3 size: 528

效果

在这里插入图片描述
在这里插入图片描述
删除镜像后尝试拉取

[root@harbor harbor]# docker images
REPOSITORY                                 TAG       IMAGE ID       CREATED       SIZE
alpine                                     latest    c059bfaa849c   2 weeks ago   5.59MB
harbor.example.com/alpine-test/alpine      v0.1      c059bfaa849c   2 weeks ago   5.59MB

[root@harbor harbor]# docker rmi -f c059bfaa849c
[root@harbor harbor]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED      SIZE

[root@harbor harbor]# docker pull harbor.example.com/alpine-test/alpine:v0.1
v0.1: Pulling from alpine-test/alpine
59bf1c3509f3: Pull complete 
Digest: sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3
Status: Downloaded newer image for harbor.example.com/alpine-test/alpine:v0.1
harbor.example.com/alpine-test/alpine:v0.1

[root@harbor harbor]# docker images
REPOSITORY                                 TAG       IMAGE ID       CREATED       SIZE
harbor.example.com/alpine-test/alpine   v0.1      c059bfaa849c   2 weeks ago   5.59MB

使用Harbor的注意事项:

  • 在客户端上传镜像时一定要记得执行docker login进行用户认证,否则无法直接push
  • 在客户端使用的时候如果不是用的https则必须要在客户端的/etc/docker/daemon.json配置文件中配置insecure-registries参数
  • 数据存放路径应在配置文件中配置到一个容量比较充足的共享存储中
  • Harbor是使用docker-compose命令来管理的,如果需要停止Harbor也应用docker-compose stop来停止,其他参数请–help

Harbor命令管理

可以使用命令 docker restart containerID逐个重启,也可以使用docker-compose进行重新启动:
(此命令需要在安装目录下执行)

[root@harbor harbor]# docker-compose stop
Stopping nginx             ... done
Stopping harbor-jobservice ... done
Stopping harbor-core       ... done
Stopping registry          ... done
Stopping registryctl       ... done
Stopping harbor-db         ... done
Stopping redis             ... done
Stopping harbor-portal     ... done
Stopping harbor-log        ... done
[root@harbor harbor]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@harbor harbor]# docker-compose start
Starting harbor-log ... done
Starting harbor-db     ... done
Starting registryctl   ... done
Starting registry      ... done
Starting harbor-portal ... done
Starting redis         ... done
Starting harbor-core   ... done
Starting harbor-jobservice ... done
Starting nginx             ... done

[root@harbor harbor]# docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED             STATUS                             PORTS                                   NAMES
b4411ec6082c   goharbor/nginx-photon:v2.3.5         "nginx -g 'daemon of…"   About an hour ago   Up 8 seconds (health: starting)    0.0.0.0:80->8080/tcp, :::80->8080/tcp   nginx
a4f0b16e201c   goharbor/harbor-jobservice:v2.3.5    "/harbor/entrypoint.…"   About an hour ago   Up 8 seconds (health: starting)                                            harbor-jobservice
aba7b1c304eb   goharbor/harbor-core:v2.3.5          "/harbor/entrypoint.…"   About an hour ago   Up 9 seconds (health: starting)                                            harbor-core
626609d9a68c   goharbor/registry-photon:v2.3.5      "/home/harbor/entryp…"   About an hour ago   Up 9 seconds (health: starting)                                            registry
f2949670619c   goharbor/harbor-registryctl:v2.3.5   "/home/harbor/start.…"   About an hour ago   Up 9 seconds (health: starting)                                            registryctl
cbbdda98e908   goharbor/harbor-db:v2.3.5            "/docker-entrypoint.…"   About an hour ago   Up 9 seconds (health: starting)                                            harbor-db
a1c9f1572801   goharbor/redis-photon:v2.3.5         "redis-server /etc/r…"   About an hour ago   Up 9 seconds (health: starting)                                            redis
2200dddc9b26   goharbor/harbor-portal:v2.3.5        "nginx -g 'daemon of…"   About an hour ago   Up 9 seconds (health: starting)                                            harbor-portal
f15bd3dd9b5c   goharbor/harbor-log:v2.3.5           "/bin/sh -c /usr/loc…"   About an hour ago   Up 10 seconds (health: starting)   127.0.0.1:1514->10514/tcp               harbor-log

Harbor的开机自启

[root@harbor ~]# cd /usr/local/harbor/
[root@harbor harbor]# vim harbor-start.sh 
[root@harbor harbor]# cat harbor-start.sh 
#!/bin/bash

cd /usr/local/harbor/ && docker-compose up -d &> /dev/null

[root@harbor harbor]# chmod +x harbor-start.sh


[root@harbor ~]# vim /etc/rc.d/rc.local
[root@harbor harbor]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/harbor/harbor-start.sh

[root@harbor ~]# chmod +x /etc/rc.d/rc.local


//测试
[root@harbor harbor]# docker-compose stop
Stopping nginx             ... done
Stopping harbor-jobservice ... done
Stopping harbor-core       ... done
Stopping registry          ... done
Stopping registryctl       ... done
Stopping harbor-db         ... done
Stopping redis             ... done
Stopping harbor-portal     ... done
Stopping harbor-log        ... done
[root@harbor harbor]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@harbor harbor]# reboot

连接断开
连接成功
Last login: Thu Dec 16 18:06:36 2021 from 192.168.129.1
[root@harbor harbor]# docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED             STATUS                             PORTS                                   NAMES
b4411ec6082c   goharbor/nginx-photon:v2.3.5         "nginx -g 'daemon of…"   About an hour ago   Up 8 seconds (health: starting)    0.0.0.0:80->8080/tcp, :::80->8080/tcp   nginx
a4f0b16e201c   goharbor/harbor-jobservice:v2.3.5    "/harbor/entrypoint.…"   About an hour ago   Up 8 seconds (health: starting)                                            harbor-jobservice
aba7b1c304eb   goharbor/harbor-core:v2.3.5          "/harbor/entrypoint.…"   About an hour ago   Up 9 seconds (health: starting)                                            harbor-core
626609d9a68c   goharbor/registry-photon:v2.3.5      "/home/harbor/entryp…"   About an hour ago   Up 9 seconds (health: starting)                                            registry
f2949670619c   goharbor/harbor-registryctl:v2.3.5   "/home/harbor/start.…"   About an hour ago   Up 9 seconds (health: starting)                                            registryctl
cbbdda98e908   goharbor/harbor-db:v2.3.5            "/docker-entrypoint.…"   About an hour ago   Up 9 seconds (health: starting)                                            harbor-db
a1c9f1572801   goharbor/redis-photon:v2.3.5         "redis-server /etc/r…"   About an hour ago   Up 9 seconds (health: starting)                                            redis
2200dddc9b26   goharbor/harbor-portal:v2.3.5        "nginx -g 'daemon of…"   About an hour ago   Up 9 seconds (health: starting)                                            harbor-portal
f15bd3dd9b5c   goharbor/harbor-log:v2.3.5           "/bin/sh -c /usr/loc…"   About an hour ago   Up 10 seconds (health: starting)   127.0.0.1:1514->10514/tcp               harbor-log
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以通过以下步骤来搭建Harbor私有仓库: 1. 安装DockerDocker-compose 2. 下载Harbor镜像并启动容器 3. 访问Harbor Web界面并进行初始化设置 4. 创建用户帐户并登录 5. 推送和下载镜像 请注意,Harbor的安装和配置可能需要一些技术知识,并且应根据您的系统环境进行适当的调整。有关更详细的信息,请参阅Harbor官方文档。 ### 回答2: Docker是一项非常受欢迎的容器化技术,它使得应用开发和部署非常方便,而Harbor是一款优秀的开源Docker私有仓库软件,它提供了安全、可靠、灵活的私有容器仓库解决方案,是非常适合企业内部使用的仓库软件。 搭建Harbor私有仓库的过程可以分为以下几个步骤: 1. 安装Docker:在安装Harbor之前,你需要先安装好Docker,确保Docker已经正确安装、运行,并且网络已经配置好。 2. 下载并解压Harbor安装包:从Harbor的官方网站(https://goharbor.io/)下载Harbor的安装包,然后解压到指定目录中。 3. 配置Harbor:在解压后的目录中,找到harbor.cfg文件,按照需求修改该文件中的配置选项,例如端口、数据存储目录、认证方式、管理员密码等。 4. 启动Harbor:运行docker-compose up -d命令即可启动Harbor私有仓库,启动后可以使用docker ps命令查看状态。 5. 配置Docker客户端:在启动Harbor后,你需要配置Docker客户端的认证信息,才能访问Harbor仓库。在Docker客户端中使用docker login命令,输入用户名、密码和仓库地址即可。 6. 使用Harbor:在配置完成后,你可以使用docker命令或者Harbor的Web UI界面来管理和使用你的私有仓库了。 总之,在使用Harbor搭建私有仓库时,需要先安装好Docker,并在运行Harbor之前进行配置,配置好认证信息,才能正常访问私有仓库Harbor还提供了丰富的权限管理、镜像管理、日志管理等功能,非常适合用于企业内部应用的私有容器仓库解决方案。 ### 回答3: Docker是一种轻便的容器技术,Harbor是一种用于Docker注册表和管理系统的私有仓库Harbor私有仓库可以极大地方便企业级应用程序的构建、部署和管理,特别适用于A DevOps和微服务架构。要搭建Harbor私有仓库,步骤如下: 第一步,安装DockerDocker Compose。在安装完Docker后,可以使用以下命令来安装Docker Compose: curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose 第二步,下载并安装Harbor私有仓库。从Harbor的官方网站上下载和安装最新版本的Harbor软件包: wget https://api.github.com/repos/goharbor/harbor/releases/latest -O harbor.json RELEASE_VERSION=$(cat harbor.json|jq ".name"|sed 's/"//g') echo "Harbor Version: ${RELEASE_VERSION}" wget https://github.com/goharbor/harbor/releases/download/${RELEASE_VERSION}/harbor-online-installer-${RELEASE_VERSION}.tgz tar zxvf harbor-online-installer-${RELEASE_VERSION}.tgz -C /opt 第三步,创建并编辑Harbor配置文件。在Harbor的安装目录下,创建一个名为harbor.cfg的配置文件: cd /opt/harbor cp harbor.cfg.tmpl harbor.cfg vi harbor.cfg 在配置文件中,指定Harbor的主机名、端口和数据存储路径等细节。你可以调整这些设置来适应你的特定需求。 第四步,启动Harbor容器。使用Docker Compose和Harbor配置文件来创建Harbor的运行环境: docker-compose up -d 第五步,使用浏览器访问Harbor私有仓库。在浏览器中访问http://localhost:8888,你会看到Harbor的登录页面。输入管理员账户和密码,即可进入Harbor管理界面。 第六步,使用Harbor私有仓库。使用Docker Registry API或Docker客户端命令行工具,与你的Harbor私有仓库进行交互。将部署在Harbor私有仓库中的镜像下载并使用在你的应用程序中。 总之,搭建Harbor私有仓库非常不错,它提供了一个安全、集中和可扩展的Docker仓库,并支持自动构建和镜像扫描等高级功能。随着DevOps和微服务的兴起,Harbor私有仓库将成为企业应用的关键组成部分,也值得我们深入探索和研究。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值