使用 Docker 搭建企业级私有仓库HARBOR

2024-09-02 01:44

本文主要是介绍使用 Docker 搭建企业级私有仓库HARBOR,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1 HARBOR 的获取

1.1 下载软件包地址

1.2 HARBOR 的介绍

2 部署harbor

2.1 仓库端操作

2.1.1 修改harbor配置文件

2.1.2 生成服务端的证书与秘钥

2.1.3 管理HARBOR

2.1.4 查看是否运行

2.2 客户端操作

2.2.1 证书拷贝给客户端

 2.2.2 环境配置

2.2.3 批量读取本地镜像

2.2.4 批量打标签

2.2.5 批量上传打过标签的镜像

2.2.6 删除打过标签的镜像

3 编写自动化上传脚本


1 HARBOR 的获取

1.1 下载软件包地址

Releases · goharbor/harbor (github.com)icon-default.png?t=N7T8https://github.com/goharbor/harbor/releases

1.2 HARBOR 的介绍


Harbor 是由vmware公司开源的企业级 Docker Registry 项目。

它提供了以下主要功能和特点:

1. 基于角色的访问控制(RBAC):可以为不同的用户和用户组分配不同的权限,增强了安全性和管理的灵活性。

2. 镜像复制:支持在不同的 Harbor 实例之间复制镜像,方便在多个数据中心或环境中分发镜像。

3. 图形化用户界面(UI):提供了直观的 Web 界面,便于管理镜像仓库、项目、用户等。

4. 审计日志:记录了对镜像仓库的各种操作,有助于追踪和审查活动。

5. 垃圾回收:可以清理不再使用的镜像,节省存储空间。

2 部署harbor

2.1 仓库端操作

2.1.1 修改harbor配置文件

[root@complete ~]# tar xzf harbor-offline-installer-v2.11.1.tgz 
[root@complete ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg        certs   harbor-offline-installer-v2.11.1.tgz
模板  图片  下载  桌面  busybox-latest.tar.gz  harbor  script[root@complete ~]# cd harbor/
[root@complete harbor]# cp harbor.yml.tmpl harbor.yml# 打开yml管理文件修改以下参数
[root@complete harbor]# vim harbor.ymlharbor_admin_password: 123456        # 设置管理员密码
hostname: reg.shuyan.com             # 设置本机主机名certificate: /data/certs/shuyan.crt    # 证书地址private_key: /data/certs/shuyan.key    # 私钥地址

2.1.2 生成服务端的证书与秘钥

[root@complete harbor]# mkdir /data/certs -p
[root@complete ~]# openssl req -newkey rsa:4096 \
-nodes -sha256 -keyout /data/certs/shuyan.key \
-addext "subjectAltName = DNS:reg.shuyan.com" \
-x509 -days 365 -out /data/certs/shuyan.crt
.......+.........+.........+..+.+.....+....+.....+.........+.......+..+...+....+.....+....+.....+....+......+..+...+.+...+..+.+..+......+......+...+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+.....+.......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+..+.......+........+......+..........+..+.........+..........+..................+......+..+...+.........+......+.......+..+.......+......+.....+....+.....+.+............+......+..............+......+............+.......+.....+.......+.....+..........+...........+.........................+............+..+...+...+.............+....................+.+..............+....+........+...+.+...+........+...+...................+.....+.......+..................+..+...............+.+.........+.....+......+...+..........+............+.....+.........+...+...............+.+.........+.......................+...+.......+.........+......+.....+...+....+........+.........+...+......................+......+.......................+.........+......+.......+.....+.......+..+.+..................+.................+...+.+.....................+..+.+.....+......+...+............+.+...+...........+.+..+.+...............+.....+..........+......+...+......+.....+.+...........+..........+..+...+...+....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.......+...+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...+++++++++++++++++++++++++++++++++++++++++++...+......+...............+.+...............+..............+.+.....+......+...............+.............+.........+........+.....+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
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) []:guangxi
Locality Name (eg, city) [Default City]:wu'zhou
Organization Name (eg, company) [Default Company Ltd]:docker
Organizational Unit Name (eg, section) []:shuyan
Common Name (eg, your name or your server's hostname) []:reg.shuyan.com
Email Address []:admin@shuayn.com

2.1.3 管理HARBOR

管理harbor的容器:

[root@rockynode-1 harbor]# docker compose stop

[root@rockynode-1 harbor]# docker compose up -d

2.1.4 查看是否运行

[root@complete harbor]# docker compose -f /root/harbor/docker-compose.yml ps 
WARN[0000] /root/harbor/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion 
NAME                IMAGE                                 COMMAND                   SERVICE       CREATED       STATUS                 PORTS
harbor-core         goharbor/harbor-core:v2.11.1          "/harbor/entrypoint.…"   core          4 hours ago   Up 4 hours (healthy)   
harbor-db           goharbor/harbor-db:v2.11.1            "/docker-entrypoint.…"   postgresql    4 hours ago   Up 4 hours (healthy)   
harbor-jobservice   goharbor/harbor-jobservice:v2.11.1    "/harbor/entrypoint.…"   jobservice    4 hours ago   Up 4 hours (healthy)   
harbor-log          goharbor/harbor-log:v2.11.1           "/bin/sh -c /usr/loc…"   log           4 hours ago   Up 4 hours (healthy)   127.0.0.1:1514->10514/tcp
harbor-portal       goharbor/harbor-portal:v2.11.1        "nginx -g 'daemon of…"   portal        4 hours ago   Up 4 hours (healthy)   
nginx               goharbor/nginx-photon:v2.11.1         "nginx -g 'daemon of…"   proxy         4 hours ago   Up 4 hours (healthy)   0.0.0.0:80->8080/tcp, :::80->8080/tcp, 0.0.0.0:443->8443/tcp, :::443->8443/tcp
redis               goharbor/redis-photon:v2.11.1         "redis-server /etc/r…"   redis         4 hours ago   Up 4 hours (healthy)   
registry            goharbor/registry-photon:v2.11.1      "/home/harbor/entryp…"   registry      4 hours ago   Up 4 hours (healthy)   
registryctl         goharbor/harbor-registryctl:v2.11.1   "/home/harbor/start.…"   registryctl   4 hours ago   Up 4 hours (healthy)   

 

其中library为默认的仓库,在里面的镜像不需要指定仓库名拉取镜像

2.2 客户端操作

2.2.1 证书拷贝给客户端

[root@complete harbor]# scp /data/certs/shuyan.crt root@192.168.239.10:~

 2.2.2 环境配置

# 删除原有的镜像
[root@rockynode-1 ~]# docker rmi `docker images | awk 'BEGIN{FS=" "} {if(NR>1)print $3}'`[root@rockynode-1 ~]# mkdir -p /etc/docker/certs.d/reg.shuyan.com/
[root@rockynode-1 ~]# cp shuyan.crt /etc/docker/certs.d/reg.shuyan.com/[root@rockynode-1 ~]# systemctl restart docker# 增加客户端的域名解析
[root@rockynode-1 ~]# vim /etc/hosts 
192.168.239.20  reg.shuyan.com

2.2.3 批量读取本地镜像

# 批量读取镜像[root@rockynode-1 docker-page]# ls
busybox-latest.tar.gz  game2048.tar.gz                      mario.tar.gz       nginx-latest.tar.gz       ubuntu-latest.tar.gz
centos-7.tar.gz        haproxy-2.3.tar.gz                   mysql-5.7.tar.gz   phpmyadmin-latest.tar.gz
debian11.tar.gz        harbor-offline-installer-v2.5.4.tgz  nginx-1.23.tar.gz  registry.tag.gz[root@rockynode-1 docker-page]# for i in `ls`;do docker load -i $i;done[root@rockynode-1 docker-page]# docker images 
REPOSITORY                        TAG       IMAGE ID       CREATED         SIZE
nginx                             latest    5ef79149e0ec   2 weeks ago     188MB
phpmyadmin                        latest    2b39e17532a1   2 weeks ago     562MB
ubuntu                            latest    edbfe74c41f8   4 weeks ago     78.1MB
mysql                             5.7       5107333e08a8   8 months ago    501MB
registry                          latest    cfb4d9904335   11 months ago   25.4MB
nginx                             1.23      a7be6198544f   15 months ago   142MB
busybox                           latest    65ad0d468eb1   15 months ago   4.26MB
haproxy                           2.3       7ecd3fda00f4   2 years ago     99.4MB
centos                            7         eeb6ee3f44bd   2 years ago     204MB
timinglee/game2048                latest    19299002fdbe   7 years ago     55.5MB
timinglee/mario                   latest    9a35a9e43e8c   9 years ago     198MB
gcr.io/distroless/base-debian11   latest    2a6de77407bf   N/A             20.6MB

2.2.4 批量打标签

[root@rockynode-1 ~]# docker images | awk '{if(NR>1) system("docker tag "$1":"$2" reg.shuyan.com/library/"$1":"$2"")}'# 查看所有镜像
[root@rockynode-1 ~]# docker images 
REPOSITORY                                               TAG       IMAGE ID       CREATED         SIZE
nginx                                                    latest    5ef79149e0ec   2 weeks ago     188MB
reg.shuyan.com/library/nginx                             latest    5ef79149e0ec   2 weeks ago     188MB
phpmyadmin                                               latest    2b39e17532a1   2 weeks ago     562MB
reg.shuyan.com/library/phpmyadmin                        latest    2b39e17532a1   2 weeks ago     562MB
ubuntu                                                   latest    edbfe74c41f8   4 weeks ago     78.1MB
reg.shuyan.com/library/ubuntu                            latest    edbfe74c41f8   4 weeks ago     78.1MB
mysql                                                    5.7       5107333e08a8   8 months ago    501MB
reg.shuyan.com/library/mysql                             5.7       5107333e08a8   8 months ago    501MB
registry                                                 latest    cfb4d9904335   11 months ago   25.4MB
reg.shuyan.com/library/registry                          latest    cfb4d9904335   11 months ago   25.4MB
nginx                                                    1.23      a7be6198544f   15 months ago   142MB
reg.shuyan.com/library/nginx                             1.23      a7be6198544f   15 months ago   142MB
busybox                                                  latest    65ad0d468eb1   15 months ago   4.26MB
reg.shuyan.com/library/busybox                           latest    65ad0d468eb1   15 months ago   4.26MB
haproxy                                                  2.3       7ecd3fda00f4   2 years ago     99.4MB
reg.shuyan.com/library/haproxy                           2.3       7ecd3fda00f4   2 years ago     99.4MB
centos                                                   7         eeb6ee3f44bd   2 years ago     204MB
reg.shuyan.com/library/centos                            7         eeb6ee3f44bd   2 years ago     204MB
timinglee/game2048                                       latest    19299002fdbe   7 years ago     55.5MB
reg.shuyan.com/library/timinglee/game2048                latest    19299002fdbe   7 years ago     55.5MB
timinglee/mario                                          latest    9a35a9e43e8c   9 years ago     198MB
reg.shuyan.com/library/timinglee/mario                   latest    9a35a9e43e8c   9 years ago     198MB
gcr.io/distroless/base-debian11                          latest    2a6de77407bf   N/A             20.6MB
reg.shuyan.com/library/gcr.io/distroless/base-debian11   latest    2a6de77407bf   N/A             20.6MB# 查看打过标签的镜像
[root@rockynode-1 ~]# docker images | awk '/reg.shuyan.com/{print $1":"$2}'
reg.shuyan.com/library/nginx:latest
reg.shuyan.com/library/phpmyadmin:latest
reg.shuyan.com/library/ubuntu:latest
reg.shuyan.com/library/mysql:5.7
reg.shuyan.com/library/registry:latest
reg.shuyan.com/library/nginx:1.23
reg.shuyan.com/library/busybox:latest
reg.shuyan.com/library/haproxy:2.3
reg.shuyan.com/library/centos:7
reg.shuyan.com/library/timinglee/game2048:latest
reg.shuyan.com/library/timinglee/mario:latest
reg.shuyan.com/library/gcr.io/distroless/base-debian11:latest

2.2.5 批量上传打过标签的镜像

# 将标签赋值给变量~]# reg_shuyan=$(docker images | awk  -v store="$store" \
> -v ip="$hub_ip" '$1 ~ ip "/"store"/" {print $1":"$2}')# 遍历变量批量上传~]# for i in $reg_shuyan
> do
>     docker push $i
> done

2.2.6 删除打过标签的镜像

~]# docker images | awk '$1 ~ /reg\.shuyan\.com/ {print $1":"$2}'

3 编写自动化上传脚本

# 演示
[root@rockynode-1 ~]# reg_shuyan=$(docker images | awk '/reg.shuyan.com/{print $1":"$2}')
[root@rockynode-1 ~]# echo -e "$reg_shuyan"
reg.shuyan.com/library/nginx:latest
reg.shuyan.com/library/phpmyadmin:latest
reg.shuyan.com/library/ubuntu:latest
reg.shuyan.com/library/mysql:5.7
reg.shuyan.com/library/registry:latest
reg.shuyan.com/library/nginx:1.23
reg.shuyan.com/library/busybox:latest
reg.shuyan.com/library/haproxy:2.3
reg.shuyan.com/library/centos:7
reg.shuyan.com/library/timinglee/game2048:latest
reg.shuyan.com/library/timinglee/mario:latest
reg.shuyan.com/library/gcr.io/distroless/base-debian11:latest[root@rockynode-1 script]# vim hub.sh#!/bin/bashhub_ip=reg.shuyan.com
store=shuyan
docker images | awk -v ip="$hub_ip" -v store="$store" 'NR > 1 {system("docker tag " $1 ":" $2 " " ip "/"store"/" $1 ":" $2)
}'reg_shuyan=$(docker images | awk  -v store="$store" -v ip="$hub_ip" '$1 ~ ip "/"store"/" {print $1":"$2}')for i in $reg_shuyan; dodocker push $i
donedocker rmi $(docker images | awk -v ip="$hub_ip" -v store="$store" '$1 ~ ip "/"store"/" {print $1":"$2}')

这篇关于使用 Docker 搭建企业级私有仓库HARBOR的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1128650

相关文章

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Windows Docker端口占用错误及解决方案总结

《WindowsDocker端口占用错误及解决方案总结》在Windows环境下使用Docker容器时,端口占用错误是开发和运维中常见且棘手的问题,本文将深入剖析该问题的成因,介绍如何通过查看端口分配... 目录引言Windows docker 端口占用错误及解决方案汇总端口冲突形成原因解析诊断当前端口情况解

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器