harbor鏡像倉庫-02-https訪問配置
harbor搭建部署參考上一章節 harbor鏡像倉庫-01-搭建部署
Harbor默認使用http,給harbor配置上https,需要用到證書,這里我們使用自簽證書,來進行配置
1. 證書的生成
在測試或開發環境中,您可以選擇使用自簽名證書,而不是來自受信任的第三方CA的證書。
以下內容將向您展示如何創建自己的CA,並使用您的CA簽署服務器證書和客戶端證書。
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.csr
[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)重啟harbor
為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導入瀏覽器以解決警告。