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

相關文章
相關標籤/搜索