harbor镜像仓库-02-https访问配置

harbor镜像仓库-02-https访问配置

harbor搭建部署参考上一章节 harbor镜像仓库-01-搭建部署html

Harbor默认使用http,给harbor配置上https,须要用到证书,这里咱们使用自签证书,来进行配置node

1. 证书的生成

在测试或开发环境中,您能够选择使用自签名证书,而不是来自受信任的第三方CA的证书。
如下内容将向您展现如何建立本身的CA,并使用您的CA签署服务器证书和客户端证书。git

1.1 生成ca证书
[root@docker ~]# mkdir /docker
[root@docker ~]# cd /docker/
[root@docker docker]# ll
总用量 0
[root@docker docker]# openssl genrsa -out ca.key 4096
Generating RSA private key, 4096 bit long modulus
................................................................................................................++
......................................................................................................................................................................................................................................................................++
e is 65537 (0x10001)

### 这里CN填写你的https设置的域名,即你harbor配置文件中hostanme填写的内容

[root@docker docker]# openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/CN=192.168.1.109" \
 -key ca.key \
 -out ca.crt

[root@docker docker]# ll
总用量 8
-rw-r--r-- 1 root root 1797 4月  18 11:16 ca.crt
-rw-r--r-- 1 root root 3247 4月  18 11:16 ca.key
1.2 得到服务器证书

1)建立本身的私钥:
openssl genrsa -out server.key 4096
2)生成证书签名请求:
openssl req -sha512 -new
-subj "/CN=192.168.1.109"
-key server.key
-out server.csrdocker

[root@docker docker]# ll
总用量 16
-rw-r--r-- 1 root root 1797 4月  18 11:16 ca.crt
-rw-r--r-- 1 root root 3247 4月  18 11:16 ca.key
-rw-r--r-- 1 root root 1590 4月  18 11:22 server.csr
-rw-r--r-- 1 root root 3247 4月  18 11:21 server.key
1.3 生成harbor仓库主机的证书
1.3.1 修改v3.ext文件

不管您是使用相似yourdomain.com的 FQDN 仍是IP来链接注册表主机,请运行此命令以生成符合主题备用名称(SAN)和x509 v3扩展要求的注册表主机证书:v3.ext浏览器

cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth 
subjectAltName = @alt_names

[alt_names]
DNS.1=192.168.1.109
EOF
1.3.2 生成harbor仓库主机的证书
[root@docker docker]# openssl x509 -req -sha512 -days 3650 \
-extfile v3.ext \
-CA ca.crt -CAkey ca.key -CAcreateserial \
-in server.csr \
-out server.crt
Signature ok
subject=/CN=192.168.1.109
Getting CA Private Key
此时证书建立成功
[root@docker docker]# ll
总用量 28
-rw-r--r-- 1 root root 1797 4月  18 11:16 ca.crt
-rw-r--r-- 1 root root 3247 4月  18 11:16 ca.key
-rw-r--r-- 1 root root 1834 4月  18 11:31 server.crt
-rw-r--r-- 1 root root 1590 4月  18 11:22 server.csr
-rw-r--r-- 1 root root 3247 4月  18 11:21 server.key
-rw-r--r-- 1 root root  233 4月  18 11:30 v3.ext
2 配置和安装

1)配置服务器证书和港口密钥
获取server.crt和server.key文件后,您能够将它们拷贝到目录/data/cert/:安全

2)修改harbor配置文件
编辑文件harbor.cfg,更新主机名和协议,并更新属性ssl_cert和ssl_cert_key:服务器

hostname = yourdomain.com:port
ui_url_protocol = https
ssl_cert = /data/cert/server.crt
ssl_cert_key = /data/cert/server.key

3)重启harbordom

为Harbor生成配置文件:
 ./prepare
若是Harbor已在运行,请中止并删除现有实例。您的图像数据保留在文件系统中
  docker-compose down -v
最后,重启harbor:
  docker-compose up -d

4)为客户端配置harbor认证
全部须要push或者pull镜像的机器都须要拷贝证书到docker.测试

a. 建立存放域名证书的目录
[root@Arcgisdocker ~]# mkdir -p /etc/docker/certs.d/192.168.1.109
b. 移动证书到docker目录下
cp ca.crt  /etc/docker/certs.d/192.168.1.109/ca.crt
c. 测试推送拉取
[root@DockerRegistry harbor]# docker tag busybox:latest 192.168.1.109/software/busybox:002  ##打上仓库地址标签
[root@DockerRegistry harbor]# docker push 192.168.1.109/software/busybox:002                ##推送测试,
The push refers to repository [192.168.1.109/software/busybox]                              ##这里已经推送过了,提示存在
0b97b1c81a32: Layer already exists
002: digest: sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4 size: 527
[root@DockerRegistry harbor]# docker rmi -f af2f74c517aa                                    ##删除镜像
Untagged: 192.168.1.109/software/busybox:002
Untagged: 192.168.1.109/software/busybox@sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4
Untagged: busybox:latest
Untagged: busybox@sha256:954e1f01e80ce09d0887ff6ea10b13a812cb01932a0781d6b0cc23f743a874fd
Deleted: sha256:af2f74c517aac1d26793a6ed05ff45b299a037e1a9eefeae5eacda133e70a825
Deleted: sha256:0b97b1c81a3200e9eeb87f17a5d25a50791a16fa08fc41eb94ad15f26516ccea
[root@DockerRegistry harbor]# docker pull 192.168.1.109/software/busybox:002                ###拉取镜像测试
002: Pulling from software/busybox
fc1a6b909f82: Pull complete
Digest: sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4
Status: Downloaded newer image for 192.168.1.109/software/busybox:002

为Harbor设置HTTPS后,您能够经过如下步骤进行验证:
打开浏览器并输入地址:https://yourdomain.com。它应该显示Harbor的用户界面。
请注意,即便咱们经过自签名CA签署证书并将CA部署到上述位置,某些浏览器仍可能出于安全缘由显示有关证书颁发机构(CA)未知的警告。这是由于自签名CA本质上不是受信任的第三方CA. 您能够本身将CA导入浏览器以解决警告。ui

原文出处:https://www.cnblogs.com/wzstudy/p/10761636.html

相关文章
相关标签/搜索