一、概述
k8sharbor是一個企業級的容器鏡像倉庫,是CNCF(Cloud Native Computing Foundation)的孵化項目。它支持存儲、簽名和掃描鏡像。Harbor提供了CLI和API來管理鏡像倉庫,同時也可以與pre-commit、RBAC等集成。
Harbor是一個開源項目,旨在解決企業在使用容器時可能面臨的安全和管理問題,提供了多租戶、LDAP/AD、API、可擴展性等功能。它支持Docker和Kubernetes,並允許管理員設置存儲、調度和安全策略等等。
二、安裝與配置
1、安裝前的準備工作:
# 新建harbor用戶和用戶組
sudo useradd -r -s /bin/false harbor
# 新建配置文件目錄
sudo mkdir -p /etc/harbor/
# 新建數據存儲目錄
sudo mkdir -p /data/harbor
# 將harbor用戶添加到docker組中
sudo usermod -aG docker harbor
2、下載並解壓Harbor離線安裝包:
wget https://storage.googleapis.com/harbor-releases/release-2.1.2/harbor-offline-installer-v2.1.2.tgz
tar -zxvf harbor-offline-installer-v2.1.2.tgz
cd harbor
3、修改harbor.cfg:
# 設置Harbor的URI
hostname = hub.example.com
# 管理員密碼
harbor_admin_password = Harbor12345
# 資料庫密碼
database_password = root123
# 海外用戶訪問速度可選配置
https:
port: 443
certificate: /your/certificate/path
private_key: /your/private/key/path
# 使用Docker Compose進行安裝
docker-compose up -d
三、鏡像管理
1、鏡像庫的管理:
# 新增項目
curl -X POST -H "Content-Type:application/json" -H "Authorization: Basic YWRtaW46SGFyYm9yMTIzNDU=" http://hub.example.com/api/v2.0/projects -d '{"name": "test"}'
# 刪除項目
curl -X DELETE -H "Authorization: Basic YWRtaW46SGFyYm9yMTIzNDU=" http://hub.example.com/api/v2.0/projects/1
2、鏡像的上傳和下載:
# 登錄
docker login -u admin -p Harbor12345 hub.example.com
# 推送
docker tag nginx:latest hub.example.com/test/nginx:latest
docker push hub.example.com/test/nginx:latest
# 拉取
docker pull hub.example.com/test/nginx:latest
3、鏡像掃描與漏洞修復:
# 安裝和配置Trivy掃描器
wget https://github.com/aquasecurity/trivy/releases/download/v0.15.0/trivy_0.15.0_Linux-64bit.tar.gz
tar zxvf trivy_0.15.0_Linux-64bit.tar.gz
sudo mv trivy /usr/local/bin
# 掃描並列出漏洞
trivy -no-progress alpine:3.10
# 使用Harbor來掃描你的Docker鏡像
curl -X POST http://hub.example.com/api/security/scan -H 'Content-Type: application/json' -H 'Authorization: Basic YWRtaW46SGFyYm9yMTIzNDU=' -d '{"registry":"https://index.docker.io","repo":"library/python","tag":"2.7"}'
四、RBAC許可權管理
1、基本角色:
# 只讀用戶
docker login -u read-only-user -p password hub.example.com
# 項目管理員
docker login -u project-admin -p password hub.example.com
# 系統管理員
docker login -u admin -p Harbor12345 hub.example.com
2、鏡像倉庫的許可權管理:
# 新增用戶
curl -u admin:Harbor12345 -H "Content-Type: application/json" -X POST "http://hub.example.com/api/v2.0/users" -d '{"username": "testuser","password":"Testpassword1%","email":"test@example.com","realname":"testuser","comment":""}'
# 修改用戶密碼
curl -u admin:Harbor12345 -H "Content-Type: application/json" -X PUT "http://hub.example.com/api/users/password?username=testuser" -d '{"new_password": "Testpassword2%"}'
# 新增角色
curl -u admin:Harbor12345 -H "Content-Type: application/json" -X POST "http://hub.example.com/api/v2.0/projects/1/members" -d '{"member_user":{"username":"testuser"},"role_id":2}'
五、自動賦予許可權的pre-commit Hook
1、自動構建:
# 以Nginx為例,新建一個Dockerfile
FROM nginx:latest
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 創建pre-commit hook
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
# 寫入shell腳本
#!/bin/bash
if docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/work uumpa/harbor-precommit
then
exit 0
else
exit 1
fi
# 提交代碼
git add .
git commit -m "feat: add Nginx server"
2、自動賦予項目管理員許可權:
# 新建push事件的一個處理頁面(push.php)
array(
'user_id' => $user_id,
'username' => $user
),
'role_id' => 2
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$auth_token));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$server_output = curl_exec($ch);
curl_close($ch);
?>
六、總結
本文介紹了k8sharbor——一個企業級容器鏡像倉庫的安裝與配置、鏡像管理、RBAC許可權管理和使用pre-commit Hook實現自動賦予許可權等方面。它可以幫助企業在使用容器時提高安全性和管理效率,值得一試。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/245538.html