MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > 精选文章 / 正文

CKA备考实验 | 使用harbor搭建私有仓库

2024-12-16 13:23 huorong 精选文章 4 ℃ 0 评论

书籍来源:《CKA/CKAD应试指南:从Docker到Kubernetes完全攻略》

一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:CKA备考实验 | 汇总_热爱编程的通信人的博客-CSDN博客


前面讲的用registry搭建私有仓库的方法虽然简单,但是都以命令的方式来管理的。有另外一种更好用的工具可以搭建私有仓库,就是harbor。

harbor是一个通过web界面管理仓库里的镜像,使用起来非常方便且功能强大。安装harbor需要compose,compose是一种容器编排工具,所以需要先把docker-compose安装好。

安装compose

本实验里,准备在vms101上搭建harbor,所以下面的操作在vms101上进行。

步骤1:使用yum安装docker-compose。

步骤2:查看compose版本。

##########实操验证##########
[root@vms101 ~]# docker-compose -v
docker-compose version 1.18.0, build 8dd22a9
[root@vms101 ~]# 

安装harbor

前面用registry搭建过仓库,因为现在改用另外的软件搭建私有仓库,为了能让docker通过http访问,必须要修改相关配置。

步骤1:在vms101上修改/etc/docker/daemon.json,并重启docker。

##########实操验证##########
[root@vms101 ~]# cat /etc/docker/daemon.json 
{
  "insecure-registries": ["192.168.26.101"]
}
[root@vms101 ~]# 

注意:之前修改的/usr/lib/systemd/system/docker.service,需要删除之前配置的内容,修改后如下:
ExecStart=/usr/bin/dockerd --insecure-registry=192.168.26.101:5000 -H fd:// --containerd=/run/containerd/containerd.sock

和之前相比,就是把5000端口删除,重启docker。

##########实操验证##########
[root@vms101 ~]# systemctl daemon-reload ; systemctl restart docker
[root@vms101 ~]# 

步骤2:到https://github.com/goharbor/harbor/releases下载最新版harbor离线包并解压,解压之后进入目录harbor。

##########实操验证##########
到https://github.com/goharbor/harbor/releases/tag/v2.0.6下载离线包
[root@vms101 ~]# tar zxvf harbor-offline-installer-v2.0.6.tgz 
harbor/harbor.v2.0.6.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl
[root@vms101 ~]# cd harbor/
[root@vms101 harbor]# ls
common.sh  harbor.v2.0.6.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare
[root@vms101 harbor]# 

步骤3:导入harbor所需要的镜像。

##########实操验证##########
[root@vms101 harbor]# docker load -i harbor.v2.0.6.tar.gz 
16c66899afe2: Loading layer [==================================================>]  34.51MB/34.51MB
140ffb3df060: Loading layer [==================================================>]  9.639MB/9.639MB
4b1e11724eb9: Loading layer [==================================================>]  6.318MB/6.318MB
6cbca54d9f4b: Loading layer [==================================================>]  16.04MB/16.04MB
dd58d81ad95a: Loading layer [==================================================>]  28.25MB/28.25MB
afd228b63447: Loading layer [==================================================>]  22.02kB/22.02kB
900592c06d79: Loading layer [==================================================>]  16.04MB/16.04MB
Loaded image: goharbor/notary-server-photon:v2.0.6
7242ca94a1c5: Loading layer [==================================================>]  116.5MB/116.5MB
70b2ef02091f: Loading layer [==================================================>]  12.63MB/12.63MB
56220c5e8642: Loading layer [==================================================>]  3.072kB/3.072kB
9c10fdc202a0: Loading layer [==================================================>]  49.15kB/49.15kB
2e6ba845f20b: Loading layer [==================================================>]  4.096kB/4.096kB
e10c55c004d9: Loading layer [==================================================>]   13.5MB/13.5MB
Loaded image: goharbor/clair-photon:v2.0.6
c048de2e27be: Loading layer [==================================================>]  9.645MB/9.645MB
facd900963ec: Loading layer [==================================================>]  4.096kB/4.096kB
b79026c5bd86: Loading layer [==================================================>]  3.072kB/3.072kB
84857f59f602: Loading layer [==================================================>]  13.28MB/13.28MB
6c8ac72362d1: Loading layer [==================================================>]   14.1MB/14.1MB
Loaded image: goharbor/clair-adapter-photon:v2.0.6
aa31f773c326: Loading layer [==================================================>]  11.49MB/11.49MB
cf10c2945c50: Loading layer [==================================================>]  8.494MB/8.494MB
93908b71a428: Loading layer [==================================================>]  178.7kB/178.7kB
0d73bea5837b: Loading layer [==================================================>]  157.2kB/157.2kB
74a6d0b95d3b: Loading layer [==================================================>]  33.28kB/33.28kB
ff79adc23c6a: Loading layer [==================================================>]  17.41kB/17.41kB
b317e6a84b08: Loading layer [==================================================>]  15.36kB/15.36kB
0de754b914e6: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: goharbor/harbor-portal:v2.0.6
28eecfa451f6: Loading layer [==================================================>]  13.42MB/13.42MB
8470ab3f2651: Loading layer [==================================================>]  3.584kB/3.584kB
b35e9592dfee: Loading layer [==================================================>]   2.56kB/2.56kB
f1b6316833f6: Loading layer [==================================================>]  49.99MB/49.99MB
1905745a13cc: Loading layer [==================================================>]  5.632kB/5.632kB
f161b6a740c5: Loading layer [==================================================>]  54.27kB/54.27kB
1e4a60ef27ff: Loading layer [==================================================>]  50.81MB/50.81MB
a9908fbc2016: Loading layer [==================================================>]   2.56kB/2.56kB
Loaded image: goharbor/harbor-core:v2.0.6
6b0aa29bcac4: Loading layer [==================================================>]  63.62MB/63.62MB
3ce97e069d3c: Loading layer [==================================================>]  79.28MB/79.28MB
eefe2b287e13: Loading layer [==================================================>]  6.144kB/6.144kB
8e713632a10e: Loading layer [==================================================>]   2.56kB/2.56kB
a67aae4afd9c: Loading layer [==================================================>]   2.56kB/2.56kB
7af48c77c810: Loading layer [==================================================>]   2.56kB/2.56kB
1286105fa250: Loading layer [==================================================>]   2.56kB/2.56kB
7bdaad95aa83: Loading layer [==================================================>]  11.26kB/11.26kB
Loaded image: goharbor/harbor-db:v2.0.6
542990a9cd95: Loading layer [==================================================>]  13.42MB/13.42MB
48c661eefcb7: Loading layer [==================================================>]  3.584kB/3.584kB
d03c0da177e7: Loading layer [==================================================>]   2.56kB/2.56kB
710f9a50dfa6: Loading layer [==================================================>]  60.51MB/60.51MB
3f2921344f65: Loading layer [==================================================>]  61.33MB/61.33MB
Loaded image: goharbor/harbor-jobservice:v2.0.6
ac824f681ecb: Loading layer [==================================================>]  40.66MB/40.66MB
6dd89aa571f5: Loading layer [==================================================>]  3.072kB/3.072kB
1ab88978b7d6: Loading layer [==================================================>]   59.9kB/59.9kB
4d65adbf3d7c: Loading layer [==================================================>]  61.95kB/61.95kB
Loaded image: goharbor/redis-photon:v2.0.6
6380379a4dc8: Loading layer [==================================================>]  14.61MB/14.61MB
518f1820e075: Loading layer [==================================================>]  28.25MB/28.25MB
a9d0e3ba0b82: Loading layer [==================================================>]  22.02kB/22.02kB
202e7f272f44: Loading layer [==================================================>]  14.61MB/14.61MB
Loaded image: goharbor/notary-signer-photon:v2.0.6
12234460b034: Loading layer [==================================================>]  75.92MB/75.92MB
84e6e04bc0e0: Loading layer [==================================================>]  3.584kB/3.584kB
5693a3880a52: Loading layer [==================================================>]  3.072kB/3.072kB
518d38be3f6d: Loading layer [==================================================>]   2.56kB/2.56kB
ecec493b1270: Loading layer [==================================================>]  3.072kB/3.072kB
e63cd8780cbc: Loading layer [==================================================>]  3.584kB/3.584kB
3ab835f59a3f: Loading layer [==================================================>]  12.29kB/12.29kB
5b30848e51e0: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: goharbor/harbor-log:v2.0.6
e1fc753918d0: Loading layer [==================================================>]  9.645MB/9.645MB
716efc76027e: Loading layer [==================================================>]  4.096kB/4.096kB
c21113e21386: Loading layer [==================================================>]  20.94MB/20.94MB
080e7f017989: Loading layer [==================================================>]  3.072kB/3.072kB
55dea557d4ef: Loading layer [==================================================>]  8.726MB/8.726MB
a64446c5d548: Loading layer [==================================================>]  30.49MB/30.49MB
Loaded image: goharbor/harbor-registryctl:v2.0.6
4a179e84af6f: Loading layer [==================================================>]  10.95MB/10.95MB
0b7414e7b16f: Loading layer [==================================================>]  4.096kB/4.096kB
ad62ef65d04f: Loading layer [==================================================>]  3.072kB/3.072kB
467190d5b504: Loading layer [==================================================>]  23.44MB/23.44MB
66d855ab3844: Loading layer [==================================================>]  13.25MB/13.25MB
3fefdaf8c5bf: Loading layer [==================================================>]  37.51MB/37.51MB
Loaded image: goharbor/trivy-adapter-photon:v2.0.6
a58d08c989ed: Loading layer [==================================================>]  9.644MB/9.644MB
68151298ffb2: Loading layer [==================================================>]  67.49MB/67.49MB
27df8635f227: Loading layer [==================================================>]  3.072kB/3.072kB
f47ddcf87e35: Loading layer [==================================================>]  4.096kB/4.096kB
e6ba620f60b8: Loading layer [==================================================>]  68.31MB/68.31MB
Loaded image: goharbor/chartmuseum-photon:v2.0.6
527eaa1c3534: Loading layer [==================================================>]  77.32MB/77.32MB
631d903c980c: Loading layer [==================================================>]  51.58MB/51.58MB
77ad9a89f0f9: Loading layer [==================================================>]   2.56kB/2.56kB
ab797dc18e3d: Loading layer [==================================================>]  1.536kB/1.536kB
047e09499da9: Loading layer [==================================================>]  18.43kB/18.43kB
1e239f0076e3: Loading layer [==================================================>]  4.055MB/4.055MB
c941cbaba1ae: Loading layer [==================================================>]  233.5kB/233.5kB
Loaded image: goharbor/prepare:v2.0.6
32157259d7b9: Loading layer [==================================================>]  11.49MB/11.49MB
Loaded image: goharbor/nginx-photon:v2.0.6
57d114d5785d: Loading layer [==================================================>]  9.645MB/9.645MB
690299ab67a4: Loading layer [==================================================>]  4.096kB/4.096kB
cc159c85e0e1: Loading layer [==================================================>]  3.072kB/3.072kB
47d60237e55f: Loading layer [==================================================>]  20.94MB/20.94MB
5902fa0ab0c1: Loading layer [==================================================>]  21.77MB/21.77MB
Loaded image: goharbor/registry-photon:v2.0.6
[root@vms101 harbor]# 

步骤4:生成harbor.yml文件。

##########实操验证##########
[root@vms101 harbor]# cp harbor.yml.tmpl harbor.yml
[root@vms101 harbor]# 

编辑harbor.yml文件,修改hostname的值为本主机名。

##########实操验证##########
hostname: vms101 #之前通过hostname vms101修改

把以下几行注释掉。可以在代码前加上“#”号,此时加“#”号的代码就会被注释掉,不再生效。

##########实操验证##########
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

注意,前面的数字是所在行号。

harbor_admin_password是登录harbor的密码,大概在34行,这里默认为Harbor12345,可以在此处修改管理员密码。

##########实操验证##########
不修改密码
harbor_admin_password: Harbor12345

步骤5:运行脚本./prepare执行一些准备工作。 #原书籍这里执行顺序不对,要先编辑harbor.yml后再./prepare

##########实操验证##########
[root@vms101 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/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@vms101 harbor]# 

步骤6:运行./install.sh。

##########实操验证##########
[root@vms101 harbor]# ./install.sh 

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

Note: docker version: 23.0.5

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

Note: docker-compose version: 1.18.0

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


[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/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/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/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
Creating harbor-log ... done
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir


Creating harbor-db ... done
Creating harbor-core ... done
Creating network "harbor_harbor" with the default driver
Creating nginx ... done
Creating registryctl ... 
Creating harbor-portal ... 
Creating registry ... 
Creating harbor-db ... 
Creating redis ... 
Creating harbor-core ... 
Creating nginx ... 
Creating harbor-jobservice ... 
? ----Harbor has been installed and started successfully.----
[root@vms101 harbor]# 

安装完毕,下面开始访问harbor。

步骤7:在浏览器里输入192.168.26.101,如图2-4所示。

用户名输入admin,密码输入Harbor12345,单击登录,如图2-5所示。

注意:单击左下角的“浅色主题”,整个面板的颜色以浅色显示。

步骤8:单击项目→新建项目,如图2-6所示。

项目名称输入cka,访问级别选择公开,单击“确定”按钮。

步骤9:选择系统管理→用户管理→创建用户,如图2-7所示。

设置新创建用户的信息,单击“确定”按钮。

步骤10:为项目添加用户。

单击项目→cka,如图2-8所示。

单击“+用户”,如图2-9和图2-10所示。

名称里输入tom,角色选择项目管理员,单击“确定”按钮。

点击镜像仓库,可以看到没有任何镜像,如图2-11所示。

下面在客户端vms100上操作。

步骤11:因为更换了私有仓库,所以要修改/etc/docker/daemon.json,如下所示。

##########实操验证##########
[root@vms100 ~]# cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://frz7i079.mirror.aliyuncs.com"],
  "insecure-registries": ["192.168.26.101"]
}
[root@vms100 ~]# 

步骤12:重启docker。

##########实操验证##########
[root@vms100 ~]# systemctl restart docker
[root@vms100 ~]#

步骤13:登录私有仓库。

##########实操验证##########
#创建到vms101的域名解析
[root@vms100 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.26.101 vms101
[root@vms100 ~]# 
[root@vms100 ~]# docker login 192.168.26.101
Username: tom
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@vms100 ~]# 

按提示输入刚创建的tom用户和密码,登录成功后会在当前目录下生成一个隐藏文件夹.docker,里面记录了登录信息。

##########实操验证##########
[root@vms100 ~]# ls .docker/
buildx  config.json
[root@vms100 ~]# 

步骤14:测试推送镜像。

##########实操验证##########
#重新构建新的centos:v1镜像
[root@vms100 ~]# docker tag centos:v1 192.168.26.101/cka/centos:v1
Error response from daemon: No such image: centos:v1
[root@vms100 ~]# docker build -t centos:v1 .
[+] Building 24.0s (6/6) FINISHED                                                                                                                                                                 
 => [internal] load build definition from Dockerfile                                                                                                                                         0.0s
 => => transferring dockerfile: 193B                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                                              0.0s
 => [internal] load metadata for hub.c.163.com/library/centos:latest                                                                                                                         0.9s
 => [1/2] FROM hub.c.163.com/library/centos@sha256:ab7e9c357fa8e5c822dd22615d3f704090780df1e089ac4ff8c6098f26a71fef                                                                          7.9s
 => => resolve hub.c.163.com/library/centos@sha256:ab7e9c357fa8e5c822dd22615d3f704090780df1e089ac4ff8c6098f26a71fef                                                                          0.0s
 => => sha256:2409c3878ba187544bb24e61591e6888e81e01aad3ac711b852813275a9958b0 70.48MB / 70.48MB                                                                                             0.0s
 => [2/2] RUN yum install net-tools -y                                                                                                                                                      14.1s
 => exporting to image                                                                                                                                                                       1.0s
 => => exporting layers                                                                                                                                                                      1.0s
 => => writing image sha256:672e02cfad58a916497ccc289dd9fde418ceb7d25c720dff219625f97bb747bf                                                                                                 0.0s 
 => => naming to docker.io/library/centos:v1                                                                                                                                                 0.0s 
[root@vms100 ~]# docker images                                                                                                                                                                    
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE                                                                                                                                        
centos       v1        672e02cfad58   4 seconds ago   392MB
[root@vms100 ~]# 
[root@vms100 ~]# docker tag centos:v1 192.168.26.101/cka/centos:v1
[root@vms100 ~]# 
[root@vms100 ~]# docker push 192.168.26.101/cka/centos:v1
The push refers to repository [192.168.26.101/cka/centos]
ae67567546a9: Pushed 
b362758f4793: Pushed 
v1: digest: sha256:5724aa2f0fc818c693c544151fbc45c140c4d3320cd82501da46de9fb067005b size: 741
[root@vms100 ~]# 

步骤15:打开harbor管理页面,如图2-12所示。

这里已经可以看到刚刚推送过来的镜像了。

Tags:harbor安装

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言