[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]

看了很多大佬的搭建环境,自己也尝试着搭建一个,记录一下笔记,以后时间长忘记了能够快速找得到

一、安装docker-compose

1. 打开docker官方网站: Install Docker Compose | Docker Documentation,找到Dock-Compose-->Install Docker Compose

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第1张图片

2. 执行以下命令来下载当前稳定版本的Docker Compose,当前练习版本为1.29.2

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

3. 添加可执行权限到docker-compose文件

sudo chmod +x /usr/local/bin/docker-compose

4. 如果安装过程中无明显错误,则检查安装结果

docker-compose --version
docker-compose version 1.29.2, build 5becea4c

5. 升级Docker Compose(适用于1.2或更早版本)

docker-compose migrate-to-labels

6.卸载Docker Compose(curl下载)

sudo rm /usr/local/bin/docker-compose

二、安装Harbor私有仓库

2.1下载Offline包

打开官网:https://github.com/goharbor/harbor/tags, 下载当前最新的离线版本,由于官网下载速度非常慢,如果网速快的还是建议官网下载,我这边选择了另外一个网址https://storage.googleapis.com/harbor-releases/refs/tags/v2.2.1/harbor-offline-installer-latest.tgz下载,但是此版本不是最新,有可能会有兼容性问题,比如有可能某些步骤错误的情况下配置私有仓库的时候,无法Login,故请参考方法即可。

将离线文件拷贝到虚拟机内,我这边Ubuntu安装在虚拟机内

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第2张图片

 切换到指定目录然后解压harbor离线包

sudo tar -xvf harbor-offline-installer-v2.2.1.tgz

加压完成后检查harbor文件

默认解压完没有harbor.yml文件,使用cp命令cp harbor.yml.tmpl harbor.yml文件

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第3张图片

 2.2 下载Online包

1. 打开官网: https://github.com/goharbor/harbor/tags/v2.4.1,这次以v2.4.1为例,然后下载onlie的包

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第4张图片

 2. 和离线方式一样将online包拷贝到虚拟机内/usr/local/harbor,此处省略

3.  解压online包,并复制一份harbor.yml文件

tar xvf harbor-online-installer-v2.4.1.tgz

2.3 安装部署Harbor

1. 此处offline和online都适用,切换到目录/usr/local/harbor打开并修改harbor.yml文件

sudo nano 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: xxx.xxx.xxx.xxx

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

# 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

# # 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

# 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: /root/docker/harbor/data

host_name为你要配置的私有仓库服务器地址, 可自定义
port: 为服务器端口,可自定义
harbor_admin_password: 默认登陆harbor的管理员密码Harbor12345
.........

2. 安装Harbor(需先切换到Harbor目录)

./prepare
# 安装过程如下:

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第5张图片

./install.sh
# 安装过程如下:

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第6张图片

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第7张图片[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第8张图片

online模式和offline模式唯一的差别是镜像会在线重新下载一份,其余进程都一直,这里就不再具体更新流程了  

2.4 查看Harbor网站并登陆私有仓库

地址为刚刚的hostname+port,这里需要注意的是防火墙需要打开该端口,这里就不具体演示如何打开防火墙端口了

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第9张图片

 1. 增加仓库地址

文件目录: /etc/docker/daemon.json, 没有此文件则使用命令创建

sudo -i
touch /etc/docker/daemon.json
chmod +x /etc/docker/daemon.json
echo '{ "insecure-registries":["$hostname:$port"] }' > /etc/docker/daemon.json

添加完成后加载daemon并重启docker服务

systemctl daemon-relaod
systemctl restart docker

2. 重启完成后先检查下docker状态

docker info

.......
.......
.......
 Insecure Registries:
  xxx.xxx.xxx.xxx:xx
  127.0.0.0/8
 Live Restore Enabled: false

如果Insecure Registries下方出现刚刚设置的hostname:port即成功

3. 检查docker容器运行状态

docker ps -a

# 因为在重新启动docker时,所有的容器都已经关闭了,可以使用以下两种方法重启

#方法一
docker-compose start

#方法二
docker start $(docker container ls -a | grep goharbor | awk '{print $1}')

方法一会重启所有的容器,方法二只会启动指定容器

这里还有一个其他的方法就是创建一个服务来自动重启,我没有尝试过,有需求的可以借鉴这位大哥的博客

配置Harbor Service

将 harbor配成systemd的service,添加配置文件 /etc/systemd/system/harbor.service

[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor
[Service]
Type=simple
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/bin/docker-compose -f /opt/harbor/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /opt/harbor/docker-compose.yml down
 
[Install]
WantedBy=multi-user.target

4. 确认该私有仓库能成功登陆

docker login -u admin -p Harbor12345 xxx.xxx.xxx.xxx:xx
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

三、上传镜像

1. 在Harbor上创建一个项目(私有仓库)

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第10张图片

2. 先拉取一个镜像,这里拉取nginx的镜像为例

docker pull nginx:latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
f3409a9a9e73: Pull complete 
9919a6cbae9c: Pull complete 
fc1ce43285d7: Pull complete 
1f01ab499216: Pull complete 
13cfaf79ff6d: Pull complete 
Digest: sha256:366e9f1ddebdb844044c2fafd13b75271a9f620819370f8971220c2b330a9254
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest 
#等待拉取完成即可

docker images
REPOSITORY           TAG       IMAGE ID       CREATED        SIZE
nginx                latest    f6987c8d6ed5   3 days ago     141MB

 3. 重新给镜像打赏私有仓库的TAG

docker tag nginx:latest xxx.xxx.xxx.xxx:xx/test/nginx:v1

REPOSITORY                            TAG       IMAGE ID       CREATED        SIZE
xxx.xxx.xxx.xxx:xx/test/nginx         v1        f6987c8d6ed5   3 days ago     141MB
nginx                                 latest    f6987c8d6ed5   3 days ago     141MB

注意:这里的test不能少,是私有仓库的项目名称,修改完成之后会发现有两个同样镜像ID的TAG文件 

4. push镜像到私有仓库

docker push xxx.xxx.xxx.xxx:xx/test/nginx:v1

51a4ac025eb4: Pushed 
4ded77d16e76: Pushed 
32359d2cd6cd: Pushed 
4270b63061e5: Pushed 
5f5f780b24de: Pushed 
2edcec3590a4: Pushed 
v1: digest: sha256:2e87d9ff130deb0c2d63600390c3f2370e71e71841573990d54579bc35046203 size: 1570

如果这个时候上传失败,确认下能否登陆Harbor

5. 检查上传镜像到test项目

[Ubuntu 20.04搭建Harbor 2.2.1私有仓库笔记记录]_第11张图片

 四、 下载镜像

1. 准备另外一台虚拟机, 然后安装好相关的docker环境,这里就不赘述了,尤其第二章的2.4.1-2设置私有仓库地址,设置完成后检查docker状态

docker info

.......
.......
.......
 Insecure Registries:
  xxx.xxx.xxx.xxx:xx
  127.0.0.0/8
 Live Restore Enabled: false

2. 设置完成之后登陆Harbor私服

docker login -u admin -p Harbor12345 xxx.xxx.xxx.xxx:xx
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

3. 下载镜像 

docker pull xxx.xxx.xxx.xxx:xx/test/nginx:v1
v1: Pulling from test/nginx
a2abf6c4d29d: Pull complete 
f3409a9a9e73: Pull complete 
9919a6cbae9c: Pull complete 
fc1ce43285d7: Pull complete 
1f01ab499216: Pull complete 
13cfaf79ff6d: Pull complete 
Digest: sha256:2e87d9ff130deb0c2d63600390c3f2370e71e71841573990d54579bc35046203
Status: Downloaded newer image for xxx.xxx.xxx.xxx:xx/test/nginx:v1
xxx.xxx.xxx.xxx:xx/test/nginx:v1

4.查看docker镜像

docker images
REPOSITORY                      TAG       IMAGE ID       CREATED      SIZE
xxx.xxx.xxx.xxx:xx/test/nginx   v1        f6987c8d6ed5   3 days ago   141MB

至此简单的搭建Harbor私服成功了

你可能感兴趣的:(docker)