harbor镜像仓库自建

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: harbor镜像仓库自建

1.部署harbor服务端环境
环境准备:Ubuntu系统

1.1 软件包地址
wget https://github.com/goharbor/harbor/releases/download/v2.7.4/harbor-offline-installer-v2.7.4.tgz

1.2 安装docker和docker-compose环境
docker环境就使用一键安装脚本快速安装了【没有脚本就手动安装】

[root@harbor ~]# tar xf autoinstall-docker-docker-compose.tar.gz
[root@harbor ~]# ./install-docker.sh i
[root@harbor ~]# docker --version
Docker version 20.10.24, build 297e128
[root@harbor ~]#
[root@harbor ~]# docker-compose --version
Docker Compose version v2.23.0
[root@harbor ~]#
1.3 解压harbor软件包
[root@harbor ~]# tar xf harbor-offline-installer-v2.7.4.tgz -C /Project/softwares/

1.4 创建工作目录
[root@harbor ~]# mkdir -pv /Project/softwares/harbor/certs/{ca,harbor-server,docker-client}
mkdir: created directory '/Project/softwares/harbor/certs'
mkdir: created directory '/Project/softwares/harbor/certs/ca'
mkdir: created directory '/Project/softwares/harbor/certs/harbor-server'
mkdir: created directory '/Project/softwares/harbor/certs/docker-client'
[root@harbor ~]#
2.配置harbor相关证书
2.1 进入到harbor证书存放目录
[root@harbor ~]# cd /Project/softwares/harbor/certs/
[root@harbor certs]#
[root@harbor certs]# ll
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 3 root root 4096 Sep 10 17:16 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 harbor-server/
[root@harbor certs]#
2.2 创建CA的私钥
[root@harbor certs]# openssl genrsa -out ca/ca.key 4096
[root@harbor certs]# ll -R
.:
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 3 root root 4096 Sep 10 17:16 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 harbor-server/

./ca:
total 12
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw------- 1 root root 3272 Sep 10 17:16 ca.key

./docker-client:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../

./harbor-server:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
[root@harbor certs]#
2.3 基于自建的CA私钥创建CA证书(注意,证书签发的域名范围)
[root@harbor certs]# openssl req -x509 -new -nodes -sha512 -days 3650 \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=nolenlinux.cn" \
-key ca/ca.key \
-out ca/ca.crt

[root@harbor certs]# ll -R
.:
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 3 root root 4096 Sep 10 17:16 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 harbor-server/

./ca:
total 16
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2049 Sep 10 17:17 ca.crt
-rw------- 1 root root 3272 Sep 10 17:16 ca.key

./docker-client:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../

./harbor-server:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
[root@harbor certs]#
2.4 查看自建证书信息
[root@harbor certs]# openssl x509 -in ca/ca.crt -noout -text
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
58:5a:1e:23:84:70:d6:d8:5a:f9:6c:3b:d2:e6:9f:56:83:95:84:45
Signature Algorithm: sha512WithRSAEncryption
Issuer: C = CN, ST = Beijing, L = Beijing, O = example, OU = Personal, CN = nolenlinux.cn
Validity
Not Before: Sep 10 09:17:11 2024 GMT
Not After : Sep 8 09:17:11 2034 GMT
Subject: C = CN, ST = Beijing, L = Beijing, O = example, OU = Personal, CN = nolenlinux.cn
...
2.5 配置harbor证书
1) 生成harbor服务器的私钥
[root@harbor certs]# openssl genrsa -out harbor-server/harbor.nolenlinux.cn.key 4096
[root@harbor certs]#
[root@harbor certs]# ll -R
.:
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 3 root root 4096 Sep 10 17:16 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:18 harbor-server/

./ca:
total 16
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2049 Sep 10 17:17 ca.crt
-rw------- 1 root root 3272 Sep 10 17:16 ca.key

./docker-client:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../

./harbor-server:
total 12
drwxr-xr-x 2 root root 4096 Sep 10 17:18 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw------- 1 root root 3272 Sep 10 17:18 harbor.nolenlinux.cn.key
[root@harbor certs]#
2) harbor服务器基于私钥签发证书认证请求(csr文件),让自建CA认证
[root@harbor certs]# openssl req -sha512 -new \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.nolenlinux.cn" \
-key harbor-server/harbor.nolenlinux.cn.key \
-out harbor-server/harbor.nolenlinux.cn.csr

[root@harbor certs]# ll -R
.:
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 3 root root 4096 Sep 10 17:16 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:18 harbor-server/

./ca:
total 16
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2049 Sep 10 17:17 ca.crt
-rw------- 1 root root 3272 Sep 10 17:16 ca.key

./docker-client:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../

./harbor-server:
total 16
drwxr-xr-x 2 root root 4096 Sep 10 17:18 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 1716 Sep 10 17:18 harbor.nolenlinux.cn.csr
-rw------- 1 root root 3272 Sep 10 17:18 harbor.nolenlinux.cn.key
[root@harbor certs]#
3) 生成 x509 v3 的扩展文件用于认证
[root@harbor certs]# cat > harbor-server/v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=harbor.nolenlinux.cn
EOF

[root@harbor certs]# ll -R
.:
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 3 root root 4096 Sep 10 17:16 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:19 harbor-server/

./ca:
total 16
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2049 Sep 10 17:17 ca.crt
-rw------- 1 root root 3272 Sep 10 17:16 ca.key

./docker-client:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../

./harbor-server:
total 20
drwxr-xr-x 2 root root 4096 Sep 10 17:19 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 1716 Sep 10 17:18 harbor.nolenlinux.cn.csr
-rw------- 1 root root 3272 Sep 10 17:18 harbor.nolenlinux.cn.key
-rw-r--r-- 1 root root 239 Sep 10 17:19 v3.ext
[root@harbor certs]#
4) 基于 x509 v3 的扩展文件认证签发harbor server证书
[root@harbor certs]# openssl x509 -req -sha512 -days 3650 \
-extfile harbor-server/v3.ext \
-CA ca/ca.crt -CAkey ca/ca.key -CAcreateserial \
-in harbor-server/harbor.nolenlinux.cn.csr \
-out harbor-server/harbor.nolenlinux.cn.crt
...
Certificate request self-signature ok
subject=C = CN, ST = Beijing, L = Beijing, O = example, OU = Personal, CN = harbor.nolenlinux.cn
[root@harbor certs]#
[root@harbor certs]# ll -R
.:
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 3 root root 4096 Sep 10 17:16 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:16 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:19 harbor-server/

./ca:
total 16
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2049 Sep 10 17:17 ca.crt
-rw------- 1 root root 3272 Sep 10 17:16 ca.key

./docker-client:
total 8
drwxr-xr-x 2 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../

./harbor-server:
total 24
drwxr-xr-x 2 root root 4096 Sep 10 17:19 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2147 Sep 10 17:19 harbor.nolenlinux.cn.crt
-rw-r--r-- 1 root root 1716 Sep 10 17:18 harbor.nolenlinux.cn.csr
-rw------- 1 root root 3272 Sep 10 17:18 harbor.nolenlinux.cn.key
-rw-r--r-- 1 root root 239 Sep 10 17:19 v3.ext
[root@harbor certs]#
5) 修改harbor的配置文件使用自建证书
[root@harbor certs]# cp ../harbor.yml.tmpl ../harbor.yml
[root@harbor certs]# vim ../harbor.yml
...
hostname: harbor.nolenlinux.cn
https:
...
certificate: /Project/softwares/harbor/certs/harbor-server/harbor.nolenlinux.cn.crt
private_key: /Project/softwares/harbor/certs/harbor-server/harbor.nolenlinux.cn.key
...
harbor_admin_password: 1
...
data_volume: /Project/data/harbor
...
[root@harbor certs]#
6) 安装harbor
[root@harbor certs]# ../install.sh --with-chartmuseum

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

Note: docker version: 20.10.24

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

Note: docker-compose version: 2.23.0

[Step 2]: loading Harbor images ...
...

[Step 5]: starting Harbor ...

Chartmusuem will be deprecated as of Harbor v2.6.0 and start to be removed in v2.8.0 or later.
Please see discussion here for more details. https://github.com/goharbor/harbor/discussions/15057
[+] Building 0.0s (0/0) docker:default
[+] Running 12/12
✔ Network harbor_harbor-chartmuseum Created 0.2s
✔ Network harbor_harbor Created 0.1s
✔ Container harbor-log Started 0.1s
✔ Container registry Started 0.1s
✔ Container harbor-portal Started 0.2s
✔ Container chartmuseum Started 0.1s
✔ Container redis Started 0.1s
✔ Container harbor-db Started 0.1s
✔ Container registryctl Started 0.1s
✔ Container harbor-core Started 0.1s
✔ Container nginx Started 0.1s
✔ Container harbor-jobservice Started 0.1s
✔ ----Harbor has been installed and started successfully.----
[root@harbor certs]#
安装成功之后根据域名【或者ip】访问harborWebUI

3.配置docker客户端证书
如果不配置会出现docker客户端无法拉取镜像

3.1 生成docker客户端证书
[root@harbor certs]# openssl x509 -inform PEM -in harbor-server/harbor.nolenlinux.cn.crt -out docker-client/harbor.nolenlinux.cn.cert
[root@harbor certs]#
[root@harbor certs]#
[root@harbor certs]#
[root@harbor certs]# pwd
/Project/softwares/harbor/certs
[root@harbor certs]#
[root@harbor certs]# md5sum docker-client/harbor.nolenlinux.cn.cert harbor-server/harbor.nolenlinux.cn.crt
c7a004c0dc90c857f8fac59d73141515 docker-client/harbor.nolenlinux.cn.cert
c7a004c0dc90c857f8fac59d73141515 harbor-server/harbor.nolenlinux.cn.crt
[root@harbor certs]#
3.2 拷贝docker client证书文件
[root@harbor certs]# cp harbor-server/harbor.nolenlinux.cn.key docker-client/
[root@harbor certs]#
[root@harbor certs]# cp ca/ca.crt docker-client/
[root@harbor certs]#
[root@harbor certs]# ll -R
.:
total 20
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ./
drwxr-xr-x 4 root root 4096 Sep 10 17:30 ../
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ca/
drwxr-xr-x 2 root root 4096 Sep 10 17:37 docker-client/
drwxr-xr-x 2 root root 4096 Sep 10 17:19 harbor-server/

./ca:
total 16
drwxr-xr-x 2 root root 4096 Sep 10 17:17 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2049 Sep 10 17:17 ca.crt
-rw------- 1 root root 3272 Sep 10 17:16 ca.key

./docker-client:
total 20
drwxr-xr-x 2 root root 4096 Sep 10 17:37 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2049 Sep 10 17:37 ca.crt
-rw-r--r-- 1 root root 2147 Sep 10 17:36 harbor.nolenlinux.cn.cert
-rw------- 1 root root 3272 Sep 10 17:36 harbor.nolenlinux.cn.key

./harbor-server:
total 24
drwxr-xr-x 2 root root 4096 Sep 10 17:19 ./
drwxr-xr-x 5 root root 4096 Sep 10 17:16 ../
-rw-r--r-- 1 root root 2147 Sep 10 17:19 harbor.nolenlinux.cn.crt
-rw-r--r-- 1 root root 1716 Sep 10 17:18 harbor.nolenlinux.cn.csr
-rw------- 1 root root 3272 Sep 10 17:18 harbor.nolenlinux.cn.key
-rw-r--r-- 1 root root 239 Sep 10 17:19 v3.ext
[root@harbor certs]#
3.3 docker客户端创建自建证书的目录结构(注意域名的名称和目录要一致)
这里我选择了一个K8S集群以docker为运行时的节点作为docker客户端

[root@worker211 ~]# mkdir -pv /etc/docker/certs.d/harbor.nolenlinux.cn/
mkdir: created directory '/etc/docker/certs.d'
mkdir: created directory '/etc/docker/certs.d/harbor.nolenlinux.cn/'
[root@worker211 ~]#
3.4 将客户端证书文件进行拷贝
[kod.51shouqianla.com)
[kod.513ex.com)
[kod.534570.com)
[kod.5a5j.net)
[kod.52nanjing.com)
[kod.61yuer.com)
[kod.673619.com)
[kod.884095.com)
[kod.884027.com)
[root@worker211 ~]# scp harbor.nolenlinux.cn:/Project/softwares/harbor/certs/docker-client/* /etc/docker/certs.d/harbor.nolenlinux.cn/
...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'harbor.nolenlinux.cn' (ED25519) to the list of known hosts.
root@harbor.nolenlinux.cn’s password:
ca.crt 100% 2049 1.1MB/s 00:00
harbor.nolenlinux.cn.cert 100% 2147 801.7KB/s 00:00
harbor.nolenlinux.cn.key 100% 3272 1.4MB/s 00:00
[root@worker211 ~]#
3.5 docker客户端验证
[root@worker211 ~]# docker login -u admin -p 1 harbor.nolenlinux.cn
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
4.测试镜像推送
创建harbor的项目后进行镜像推送测试

4.1 打镜像
[root@worker211 ~]# docker tag wordpress:v6.2.2 harbor.nolenlinux.cn/wordpress/wordpress:v6.2.2

4.2 推送镜像
[root@worker211 ~]# docker push harbor.nolenlinux.cn/wordpress/wordpress
Using default tag: v6.2.2
The push refers to repository [harbor.nolenlinux.cn/wordpress/wordpress]
c7ae1aefcc69: Pushed
8f1b90d2be40: Pushed
98b68a843574: Pushed
f0287c40b0d6: Pushed
a4e2d002482e: Pushed
4793c0a2ad02: Pushed
0d79fe9ffe74: Pushed
d390f2d56bd8: Pushed
10dfb82106c4: Pushed
7446d340e7f8: Pushed
55d40777afe6: Pushed
56543a169be6: Pushed
b299cffd87cb: Pushed
23946094ff3f: Pushed
6c39776a30a0: Pushed
564928686313: Pushed
6e4300c6b758: Pushed
ee0ca96d307e: Pushed
0fdfbbf7aebd: Pushed
2a3138346faa: Pushed
2edcec3590a4: Pushed
latest: digest: sha256:fc71e6a3a9214ed4bd29a268ade5fbec77589a50e00a59fa3c82641f6b58b1a9 size: 4710
[root@worker211 ~]#
5 浏览器访问WebUI验证镜像是否传输成功
分类: K8S

相关实践学习
通过容器镜像仓库与容器服务快速部署spring-hello应用
本教程主要讲述如何将本地Java代码程序上传并在云端以容器化的构建、传输和运行。
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
12月前
|
存储 Docker 容器
企业实战(6)修改Harbor镜像仓库默认存储路径
企业实战(6)修改Harbor镜像仓库默认存储路径
270 0
|
12月前
|
安全 应用服务中间件 数据安全/隐私保护
企业实战(5) Docker私有镜像仓库Harbor的部署与使用详解
企业实战(5) Docker私有镜像仓库Harbor的部署与使用详解
460 0
|
Linux Docker 容器
centos 7.9 部署 harbor 镜像仓库实践
centos 7.9 部署 harbor 镜像仓库实践
centos 7.9 部署 harbor 镜像仓库实践
|
5月前
|
Kubernetes Cloud Native 应用服务中间件
云原生|kubernetes|kubernetes集群使用私有镜像仓库拉取镜像(harbor或者官方的registry私有镜像仓库)
云原生|kubernetes|kubernetes集群使用私有镜像仓库拉取镜像(harbor或者官方的registry私有镜像仓库)
887 0
|
3月前
|
数据可视化 Linux 数据安全/隐私保护
Harbor私有镜像仓库搭建
Harbor私有镜像仓库搭建
54 3
|
2月前
|
Docker 容器
docker: 搭建 harbor 镜像仓库
docker: 搭建 harbor 镜像仓库
|
5月前
|
存储 API 数据安全/隐私保护
企业级Docker镜像仓库Harbor部署与使用
企业级Docker镜像仓库Harbor部署与使用
|
5月前
|
存储 安全 数据库
搭建Harbor镜像仓库
搭建Harbor镜像仓库
298 2
|
5月前
|
JavaScript Devops Java
DevOps搭建(一)-安装Harbor镜像仓库详细步骤
DevOps搭建(一)-安装Harbor镜像仓库详细步骤
96 0
|
5月前
|
Shell Linux 开发工具
搭建自己的Docker Harbor镜像仓库(2)-- 使用篇
搭建自己的Docker Harbor镜像仓库(2)-- 使用篇
175 1