官網(wǎng)文檔:https://goharbor.io/docs/2.3.0/
OSS: https://github.com/docker/docker.github.io/blob/master/registry/storage-drivers/oss.md
前置環(huán)境
1、安裝docker
$ curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 在/etc/apt/source.list新增以下行
$ vim /etc/apt/source.list
deb [arch=amd64] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu focal stable
$ apt-get update
$ apt-get -y install docker-ce=5:19.03.14~3-0~ubuntu-focal docker-ce-cli=5:19.03.14~3-0~ubuntu-focal containerd.io
2、安裝docker-compose
# 下載 docker compose
$ curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
部署harbor
Harbor的安裝包分為在線版和離線版,離線版包含安裝所需的所有鏡像
Harbor無法登錄問題: https://blog.csdn.net/weixin_43952432/article/details/100577560
下載harbor離線安裝包
$ wget https://github.com/goharbor/harbor/releases/download/v2.1.0/harbor-offline-installer-v2.1.0.tgz
# 上傳到服務器后,解壓壓縮包
$ tar -xf harbor-offline-installer-v2.1.0.tgz
修改harbor配置
進到harbor-offline-installer-v2.1.0.tgz 解壓后的目錄, 從harbor.yml.tmpl復制一份harbor.yml
$ cp harbor.yml.tmpl harbor.yml
$ vim harbor.yml
# Configuration file of Harbor
# 此處定義為harbor的域名或者IP,注意不能定義為 localhost、127.0.0.1
hostname: harbor.domin.com
# http related config
http:
# port for http, default is 80\. If https enabled, this port will redirect to https port
port: 80
# 這里把https注釋掉(根據(jù)自己實際需求配置)
# https:
# https port for harbor, default is 443
# port: 443
# The path of cert and key files for nginx
# certificate: /your/certificate/path
# private_key: /your/private/key/path
# 如果要啟用外部代理,請取消去掉external_url的注釋,當它啟用時,hostname將不再使用
# 因為我們還會給harbor接入反向代理,所以此處去掉external_url的注釋
external_url: https://harbor.domin.com
?
# harbor admin賬戶的初始密碼(建議修改)
harbor_admin_password: Harbor12345
?
# 因為我們會使用外部postgres數(shù)據(jù)庫,所以此處此database的配置可以忽略
database:
password: root123
max_idle_conns: 50
max_open_conns: 1000
?
# 默認harbor數(shù)據(jù)存儲目錄
# 注意:雖然本次部署會使用oss作為registry的外部存儲,但是data_volume字段不要注釋掉
data_volume: /data/harbor-storage
?
# 更改鏡像存儲位置,本地的話不用配置直接用,本次部署中使用了阿里云的oss作為后端存儲
# 去掉storage注釋,配置oss 可參考:https://docs.docker.com/registry/configuration/#storage
?
storage:
cache:
layerinfo: redis
oss:
accesskeyid: 填寫你的具有阿里云oss權限賬戶的RAM的AccessKey ID
accesskeysecret: 填寫你的具有阿里云oss權限賬戶的RAM的AccessKey Secret
region: 地域節(jié)點 # EndPoint, 如cn-shenzhen
endpoint: Bucket 域名 #[bucket].[region].aliyuncs.com 或者 當 internal=true, [bucket].[region]-internal.aliyuncs.com
internal:
secure: true # 指定是否通過ssl傳輸數(shù)據(jù)到bucket
bucket: Bucket 名稱
rootdirectory: 指定oss下面某路徑作為存儲目錄
# 保持默認配置
trivy:
ignore_unfixed: false
skip_update: false
insecure: false
# 保持默認配置
jobservice:
max_job_workers: 10
# 保持默認配置
notification:
# Maximum retry count for webhook job
webhook_job_max_retry: 10
# 保持默認配置
chart:
absolute_url: disabled
# 日志配置,根據(jù)自己需求進行修改
log:
level: info
local:
rotate_count: 50
rotate_size: 200M
location: /var/log/harbor
_version: 2.2.0
?
# 使用外部數(shù)據(jù)庫,需要把external_database的注釋取消,進行配置, 注意需要在postgres數(shù)據(jù)庫中將registry、clair的數(shù)據(jù)庫預先創(chuàng)建
external_database:
harbor:
host: harbor_db_host
port: harbor_db_port
db_name: harbor_db_name
username: harbor_db_username
password: harbor_db_password
ssl_mode: disable
max_idle_conns: 2
max_open_conns: 0
clair:
host: clair_server_db_host
port: clair_server_db_port
db_name: clair_server_db_name
username: clair_server_db_username
password: clair_server_db_password
ssl_mode: disable
# notary_signer:
# host: notary_signer_db_host
# port: notary_signer_db_port
# db_name: notary_signer_db_name
# username: notary_signer_db_username
# password: notary_signer_db_password
# ssl_mode: disable
# notary_server:
# host: notary_server_db_host
# port: notary_server_db_port
# db_name: notary_server_db_name
# username: notary_server_db_username
# password: notary_server_db_password
# ssl_mode: disable
?
# 如果使用外部redis的情況下,需要將external_redis的注釋去掉
external_redis:
host: redis:6379
password:
registry_db_index: 1
jobservice_db_index: 2
chartmuseum_db_index: 3
trivy_db_index: 5
idle_timeout_seconds: 30
安裝harbor
- 使用prepare初始化harbor配置文件
./prepare --with-clair --with-trivy --with-chartmuseum
#--with-clair 鏡像安全掃描插件
#--with_notary 內(nèi)容信任插件
#--with-trivy 鏡像漏洞檢測插件
#--with-chartmuseum Chart倉庫服務
- 注釋掉docker-compose.yml 中registry、registryctl、chartmuseum 的本地存儲路徑,使用oss進行存儲
提前需確認初始化后的配置文件: common/config/registry/config.yaml 、common/config/chartserver/env配置的是否是OSS的路徑
# 如:以registry為例
registry:
image: goharbor/registry-photon:v2.0.0
container_name: registry
restart: always
cap_drop:
- ALL
cap_add:
- CHOWN
- SETGID
- SETUID
volumes:
# - /data/registry:/storage:z
- 如果harbor 前面還會加一層proxy代理,且代理中有配置“X-Forwarded-Proto”,則需要將harbor的proxy組件中的配置文件’
X-Forwarded-Proto‘注釋或刪掉
sed -i '/X-Forwarded-Proto/d' common/config/nginx/nginx.conf
- 啟動Harbor
# 將harbor相關鏡像文件導入
docker load -i harbor.v2.1.0.tar.gz
# 啟動harbor
docker-compose up -d
驗證harbor
增加harbor的域名到docker-daemon
$ vim /etc/docker/daemon.json
{
"insecure-registries": ["harbor.mydomian.com"]
}
# 重載docker服務
$ systemctl reload docker
綁定hosts
$ vim /etc/hosts
[ip] harbor.mydomian.com
harbor常見問題
1) harbor push出現(xiàn)unknow blob
解決:一般出現(xiàn)這個問題是由:X-Forwarded-Proto 這個導致的,如果harbor應用前有多個nginx 代理,要確保只保留一個
# 刪除harbor自己nginx配置中的X-Forwarded-Proto
sed -i '/X-Forwarded-Proto/d' common/config/nginx/nginx.conf
2) 阿里云harbor實例前的slb健康檢測不通過
查看slb的健康檢查日志提示以下問題:
[x.x.x.x]:80 to x.x.x.x:80 abnormal; cause: check protocol http error
排查
slb 健康檢查異常報錯:https://www.sites-help.com/aliyun/1575843803.html check protocol http error 表示響應中HTTP status code不是用戶指定的(默認為 2xx/3xx)
1、在harbor應用服務器上使用NC檢測,發(fā)現(xiàn)結果返回的狀態(tài)碼確實為400
echo -e 'HEAD /harbor/sign-in HTTP/1.0\r\n' | nc -t harbor_ip 80
2、增加Host后,結果返回的狀態(tài)碼為200
echo -e ‘HEAD /harbor/sign-in HTTP/1.0\r\nHost: harbor.mydomian.com\r\n\r\n’ | nc -t harbor_ip 80
解決 將阿里云slb的健康檢查中增加”健康檢查域名:harbor.mydomian.com“
3) harbor 登錄狀態(tài)時好時壞
排查:harbor后端有兩個實例,連接同一個pgsql和redis,最后查到由于harbor安裝時兩個實例上分別執(zhí)行了install.sh腳本,自動生成的保存在本地harbor-data下面的secret/下面的密鑰文件不一致導致這個問題出現(xiàn)
解決:將其中一個實例下的secret目錄覆蓋到另一個實例下,重啟這個實例的harbor
3) harbor登錄失敗
報錯
$ docker login -u admin -p Harbor12345 reg.mydomain.com
Error response from daemon: Get https://reg.mydomain.com/v2/: unauthorized: authentication required
解決
在harbor2.x當中如果需要使用nginx做代理,官方提供了external_url字段解決了這個問題,只需要在harbor.yml文件當中配置這個字段值即可。
eg:
hostname: reg.mydomain.com
external_url: https://reg.mydomain.com
修改后 docker-compose 重啟即可