打造獨立的Image Registry: Harbor

2023/10/22閱讀時間約 14 分鐘

今天來跟大家分享如何建置容器世界的一個非常重要的元件:Image Registry。

通常在容器環境下,所有服務都是基於容器映像(container image)來建構並提供。為了更方便地管理整個環境的映像,並提供訪問。一般來說會建立一個專門用來存放容器映像的位置,也就是本文所使用的Harbor來與大家分享。本文使用常見的Harbor來與大家分享。

raw-image

本文將會分成以下幾點進行說明:

  1. Harbor基本說明
  2. Harbor建置
  3. 結論

1. Harbor基本說明

Harbor是一個從CNCF畢業的專案。透過Policies與RBAC來確保使用的安全性。也可以與弱掃工具整合來保證映像的安全性,更可以對映像進行簽章確保信任來實現合規、效能、操作性,有效地協助容器環境的管理工作順利進行。

Harbor有以下幾個特色:

raw-image
  • 安全性:包含使用者身份驗證、RBAC、內容簽名和加密,確保傳輸和儲存是安全的。例如整合Cosign/Notation來進行內容簽名(Content Trust)。
  • 映像複制:可以透過image-syncer在不同的Harbor之間進行映像同步,可以實現映像的備份與故障回復,或是做DR(災難復原)Site使用。
  • Project / Namespace : 透過將映像分組可以更好的控制存取與權限。


  • Audit Log: 可以根據Project來查詢所有操作的記錄,用來確保合規性。
  • API : 使用REST API與其他系統來進行整合,實現自動化與自定義的操作。
  • UI界面:透過Web UI,使用者可以更方便的進行管理。
  • 漏洞掃描:可以在安裝的時候整合Trivy scanner,或是在安裝完畢後再連接到其他的scanner都可以。
  • 儲存管理:一般會直接連接Object storage較為方便(Ex. MinIO),但不限於Object Storage

2. Harbor建置

#-------------------------------------------------------
# S2-1. 安裝Docker環境
#-------------------------------------------------------
[harbor]# yum install yum-utils
[harbor]# yum config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[harbor]# yum clean all ; yum repolist

#### RHEL8先移除podman
[harbor]# yum remove podman buildah
[harbor]# yum install containerd.io-1.6.15-3.1.el8.x86_64.rpm \
docker-ce-20.10.8-3.el8.x86_64.rpm \
docker-ce-cli-20.10.22-3.el8.x86_64.rpm \

[harbor]# systemctl enable --now docker
raw-image
#-------------------------------------------------
# S2-2. 安裝docker-compose
#-------------------------------------------------
[harbor]# curl -SL https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
[harobr]# mv docker-compose /usr/local/bin/
[harbor]# chmod +x docker-compose
[harbor]# docker-compose version
#-------------------------------------------------
# S2-3. 建立憑證
#-------------------------------------------------
[harbor]# openssl genrsa -out ca.key 4096
[harbor]# openssl req -x509 -new -nodes -sha512 -days 3650 -key ca.key -out ca.crt
...
Common Name (eg, your name or your servers hostname) []:harbor.test.example.poc.
...

[harbor]# openssl genrsa -out harbor.test.example.poc.key 4096
[harbor# openssl req -sha512 -new -key harbor.test.example.poc.key -out harbor.test.example.poc.csr
...
Common Name (eg, your name or your servers hostname) []:harbor.test.example.poc
...

[harbor]# vim v3.ext
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=habor.test.example.poc
DNS.2=harbor.test.example.poc
DNS.3=harbor.test.example.poc

[harbor]# openssl x509 -req -sha512 -days 3650 \
-extfile v3.ext \
-CA ca.crt -CAkey ca.key -CAcreateserial \
-in harbor.test.example.poc.csr \
-out harbor.test.example.poc.crt
#--------------------------------------------
# s2-4. 套用憑證,啟動docker
#--------------------------------------------
[harbor]# mkdir -p /data/cert/
[harbor]# cp -rp harbor.test.example.poc.crt harbor.test.example.poc.key /data/cert/
[harbor]# ls -al /data/cert/
harbor.test.example.poc.crt
harbor.test.example.poc.key

### convert crt to cert for docker login
[harbor]# cd /root/docker/certs/
[harbor]# openssl x509 -inform PEM -in harbor.test.example.poc.crt -out harbor.test.example.poc.cert

### copy to docker certificate folder
[harbor]# mkdir -p /etc/docker/certs.d/harbor.test.example.poc
[harbor]# cp harbor.test.example.poc.cert /etc/docker/certs.d/harbor.test.example.poc
[harbor]# cp harbor.test.example.poc.key /etc/docker/certs.d/harbor.test.example.poc
[harbor]# cp ca.crt /etc/docker/certs.d/harbor.test.example.poc
[harbor]# systemctl restart docker
#------------------------------------------
# S2-5. Download & Config
#------------------------------------------
[harbor]# wget https://github.com/goharbor/harbor/releases/download/v2.8.2/harbor-offline-installer-v2.8.2.tgz
[harbor]# tar zxvf harbor-offline-installer-v2.8.2.tgz
[harbor]# cd /data/harbor/
[harbor]# cp harbor.yml.tmpl harbor.yml
[harbor]# vim harbor.yml
hostname: harbor.test.example.poc #指定要部署harbor的位置
external_url: https://harbor.test.example.poc
http: #Production環境不要用
port: 80
https:
port: 443
certificate: /etc/docker/certs.d/harbor.test.example.poc:8443/test.example.poc.cert #SSL certificate
private_key: /etc/docker/certs.d/harbor.test.example.poc:8443/test.example.poc.key #SSL key
harbor_admin_password: Harbor12345 #設定第一次進入harbor的密碼(預設admin/Harbor12345)
#------------------------------------
# S2-6. Install
#------------------------------------
[harbor]# ./install.sh --with-notary --with-trivy
=> 確認都是Healty狀態

[harbor]# docker-compose ps

[harbor]# docker login harbor.test.example.poc
Username: admin
Password: Harbor12345
確認全部的狀態

確認全部的狀態

#---------------------------------------
# S2-7. 讓其他主機也可以正常登入使用
#---------------------------------------
[Client]# scp -rp /data/harbor/cert/ca.crt root@192.168.100.11:/root/
[Cleint]# cp /root/ca.crt /etc/pki/ca-trust/source/anchors/
[Client]# update-ca-trust

[Client]# docker login harbor.test.example.poc:8443
Username: admin
Password:
Login Succeeded!
[Client]# docker images
[Client]# podman pull harbor.test.example.poc/test/redis-photon:v2.4.3

※ browser
https://harbor.test.example.poc:8443
WebUI

WebUI

上傳Image成功

上傳Image成功

### 確認節點CA憑證
### Podman檢查CA的路徑與docker不同 (/etc/containers/certs.d/)

[worker01]# mkdir /etc/containers/certs.d/harbor.test.example.poc/
[harbor]# scp -rp ca.crt root@worker01:/etc/containers/certs.d/harbor.test.example.poc/
[worker01]# podman pull harbor.test.example.poc/example/nginx:latest


raw-image

3. 結論

以上說明Harbor的基本安裝流程,除了獨立安裝之外,也可以用容器的型式存在於K8S內運作。安裝時所整合的Trivy也是另一個專門的安全掃描專案,不只是針對容器映像,也支援檔案系統(filesystem)、VM映像(VM image)、遠端的Git、K8S、AWS等標的。並且還可以發現像是OS軟體套件與相依性、軟體授權、敏感資料等資訊。

基本上現在建置Harbor時,同時也會整合Trivy,如此一來,安裝完的Harbor便具有基礎的合規掃描工具,接下來如同上述,可以另外再整合其他Scanner亦可。

未來有機會的話,再繼續分享Harbor的進階應用,例如升級、離線更新、簽名等功能。


※ References:

  • https://github.com/aquasecurity/trivy
  • https://goharbor.io/docs/main/
10會員
40內容數
記錄IT社畜的自我學習筆記,如同專題名稱,主要是怕自已忘記自已做過什麼、學到什麼。索性就分享我自已在學習Kubernetes這條路上的各種測試、學習心得。
留言0
查看全部
發表第一個留言支持創作者!