harbor資源管理垃圾回收


1 . Harbor常用api接口應用

[root@confluence_jira ~]# curl -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "172.24.101.37:8885/api/projects?project_name=library"
[
  {
    "project_id": 2,
    "owner_id": 1,
    "name": "public_opinion",
    "creation_time": "2019-01-16T06:37:40Z",
    "update_time": "2019-01-16T06:37:40Z",
    "deleted": false,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "chart_count": 0,
    "metadata": {
      "public": "false"
    }
  },
  {
    "project_id": 1,
    "owner_id": 1,
    "name": "library",
    "creation_time": "2019-01-16T06:33:52.831689Z",
    "update_time": "2019-01-16T06:33:52.831689Z",
    "deleted": false,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "chart_count": 0,
    "metadata": {
      "public": "true"
    }
  }
[root@harbor ~]# curl -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/projects/1"
{
  "project_id": 1,
  "owner_id": 1,
  "name": "library",
  "creation_time": "2019-01-25T12:04:20.829437Z",
  "update_time": "2019-01-25T12:04:20.829437Z",
  "deleted": false,
  "owner_name": "",
  "togglable": true,
  "current_user_role_id": 1,
  "repo_count": 1,
  "chart_count": 0,
  "metadata": {
    "public": "true"
  }
[root@harbor ~]# curl  -u "admin:Harbor12345"  -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/search?q=centos"
{
  "project": [],
  "repository": [
    {
      "project_id": 1,
      "project_name": "library",
      "project_public": true,
      "pull_count": 0,
      "repository_name": "library/centos",
      "tags_count": 1
    }
  ],
  "Chart": null
}

[root@harbor ~]# curl -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/projects/"
[
  {
    "project_id": 2,
    "owner_id": 1,
    "name": "public_opinion",
    "creation_time": "2019-01-25T14:17:14Z",
    "update_time": "2019-01-25T14:17:14Z",
    "deleted": false,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "chart_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 1,
    "owner_id": 1,
    "name": "library",
    "creation_time": "2019-01-25T12:04:20.829437Z",
    "update_time": "2019-01-25T12:04:20.829437Z",
    "deleted": false,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "chart_count": 0,
    "metadata": {
      "public": "true"
    }
  }
[root@harbor ~]# curl -u "admin:Harbor12345" -X POST -H "Content-Type: application/json" "http://192.168.138.130/api/projects" -d @createproject.json
[root@harbor ~]# cat createproject.json 
{

  "project_name": "testrpo",

  "public": 0

}
curl -u "admin:Harbor12345" -X POST -H "Content-Type: application/json" "http://192.168.138.130/api/projects/{project_id}/logs/filter" -d @log.json
[root@harbor ~]# cat log.json 
{

  "username": "admin"

}
curl -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/users" 獲取用戶信息
[root@harbor ~]# curl -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/users/current"
{
  "user_id": 1,
  "username": "admin",
  "email": "admin@example.com",
  "password": "",
  "realname": "system admin",
  "comment": "admin user",
  "deleted": false,
  "role_name": "",
  "role_id": 0,
  "has_admin_role": true,
  "reset_uuid": "",
  "creation_time": "2019-01-25T12:04:20.829437Z",
  "update_time": "2019-01-25T12:04:21.466614Z"
[root@harbor ~]# curl -u "admin:Harbor12345" -X POST -H "Content-Type: application/json" "http://192.168.138.130/api/users" -d @user.json
[root@harbor ~]# cat user.json 
{

  "user_id": 5,

  "username": "xinju",

  "email": "xinju@gmail.com",

  "password": "Xinju12345",

  "realname": "xinju",

  "role_id": 2

}

[root@harbor ~]# curl -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/users"
[
  {
    "user_id": 3,
    "username": "xinju",
    "email": "xinju@gmail.com",
    "password": "8c9bd99bc87dbfc5168e316c770c032d",
    "realname": "xinju",
    "comment": "",
    "deleted": false,
    "role_name": "",
    "role_id": 0,
    "has_admin_role": false,
    "reset_uuid": "",
    "creation_time": "2019-01-25T14:30:06Z",
    "update_time": "2019-01-25T14:30:06Z"
  }

[root@harbor ~]# curl -u "admin:Harbor12345" -X DELETE  -H "Content-Type: application/json" "http://192.168.138.130/api/users/3"
[root@harbor ~]# curl-u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/repositories?project_id=1&q=centos"
[
  {
    "id": 1,
    "name": "library/centos",
    "project_id": 1,
    "description": "",
    "pull_count": 0,
    "star_count": 0,
    "tags_count": 1,
    "labels": [],
    "creation_time": "2019-01-25T14:12:31.421446Z",
    "update_time": "2019-01-25T14:12:31.421446Z"
  }
[root@harbor ~]# curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "http://192.168.138.130/api/repositories/library%2Fcentos/tags/"
[
  {
    "digest": "sha256:20f32279b5e4801424c21f56c2f645fc2109d81bdc3cc6855e7e982b5d3d334b",
    "name": "7.2.1511",
    "size": 71563149,
    "architecture": "amd64",
    "os": "linux",
    "docker_version": "17.06.2-ce",
    "author": "The CentOS Project \u003ccloud-ops@centos.org\u003e",
    "created": "2018-10-09T18:21:43.342796088Z",
    "config": {
      "labels": {
        "license": "GPLv2",
        "name": "CentOS Base Image",
        "vendor": "CentOS"
      }
    },
    "signature": null,
    "labels": []
  }

2 .harbor 刪除鏡像

[root@harbor ~]# curl -u "admin:Harbor12345" -X DELETE -H "Content-Type: application/json" http://192.168.138.130/api/repositories/library%2Fcentos/tags/7.2.1511

#解釋 library是項目名,centos是鏡像名稱,7.2.1511是鏡像標簽

3 harbor垃圾回收

   a、首先,刪除Harbor的UI中的存儲庫。這是軟刪除。您可以刪除整個存儲庫或僅刪除它的標簽。軟刪除后,Harbour中不再管理存儲庫,但是存儲庫的文件仍然保留在Harbour的存儲中。 

[root@harbor ~]# curl -u "admin:Harbor12345" -X DELETE -H "Content-Type: application/json" http://192.168.138.130/api/repositories/library%2Fcentos/tags/7.2.1511

   b、接下來,使用注冊表的垃圾回收(GC)刪除存儲庫的實際文件。在執行GC之前,確保沒有人推送圖像或Harbour根本沒有運行。如果有人在GC運行時推送鏡像,則存在鏡像層被錯誤刪除的風險,從而導致鏡像損壞。所以在運行GC之前,首選的方法是先停止Harbour。

[root@harbor harbor]# docker-compose stop
Stopping nginx              ... done
Stopping harbor-jobservice  ... done
Stopping harbor-portal      ... done
Stopping harbor-core        ... done
Stopping harbor-adminserver ... done
Stopping registryctl        ... done
Stopping redis              ... done
Stopping harbor-db          ... done
Stopping registry           ... done
Stopping harbor-log         ... done

  c、在部署Harbour的主機上運行以下命令以預覽會影響哪些文件/鏡像 

       注:上述選項”–dry-run”將打印進度而不刪除任何數據。

[root@harbor ~]# docker run -it --name gc --rm --volumes-from registry vmware/registry-photon:v2.6.2-v1.5.0 garbage-collect --dry-run /etc/registry/config.yml
Unable to find image 'vmware/registry-photon:v2.6.2-v1.5.0' locally
v2.6.2-v1.5.0: Pulling from vmware/registry-photon
97b915ad4ee1: Pull complete 
7403d4fcc953: Pull complete 
d79f0623a023: Pull complete 
03fe7d8c1fcc: Pull complete 
692f0a17470a: Pull complete 
7c85caeabf8f: Pull complete 
ef3310fc291c: Pull complete 
Digest: sha256:3ee1c992200a069d8d943aa21f555950bd7d1764122a936ba2a3cb115885bf86
Status: Downloaded newer image for vmware/registry-photon:v2.6.2-v1.5.0
library/centos

0 blobs marked, 3 blobs eligible for deletion
blob eligible for deletion: sha256:20f32279b5e4801424c21f56c2f645fc2109d81bdc3cc6855e7e982b5d3d334b
blob eligible for deletion: sha256:4cbf48630b46d315687eb42e32ff93da0197f6a6e07c010c2eb180528da80e91
blob eligible for deletion: sha256:f2d1d709a1daed8f529e5f561d7e102c08cab4856f1ca44c36509c552d14e3fe
[root@harbor harbor]# du -sh /data/
128M    /data/

  d、以下命令執行垃圾回收

[root@harbor harbor]# docker run -it --name gc --rm --volumes-from registry vmware/registry-photon:v2.6.2-v1.5.0 garbage-collect /etc/registry/config.yml
library/centos

0 blobs marked, 3 blobs eligible for deletion
blob eligible for deletion: sha256:f2d1d709a1daed8f529e5f561d7e102c08cab4856f1ca44c36509c552d14e3fe
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/f2/f2d1d709a1daed8f529e5f561d7e102c08cab4856f1ca44c36509c552d14e3fe  go.version=go1.7.3 instance.id=b3425a25-da41-4049-922b-1bb3151d7ee9 service=registry
blob eligible for deletion: sha256:20f32279b5e4801424c21f56c2f645fc2109d81bdc3cc6855e7e982b5d3d334b
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/20/20f32279b5e4801424c21f56c2f645fc2109d81bdc3cc6855e7e982b5d3d334b  go.version=go1.7.3 instance.id=b3425a25-da41-4049-922b-1bb3151d7ee9 service=registry
blob eligible for deletion: sha256:4cbf48630b46d315687eb42e32ff93da0197f6a6e07c010c2eb180528da80e91
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/4c/4cbf48630b46d315687eb42e32ff93da0197f6a6e07c010c2eb180528da80e91  go.version=go1.7.3 instance.id=b3425a25-da41-4049-922b-1bb3151d7ee9 service=registry

  e、重啟harbor各組件鏡像

[root@harbor harbor]# docker-compose start
Starting log         ... done
Starting postgresql  ... done
Starting redis       ... done
Starting adminserver ... done
Starting registry    ... done
Starting core        ... done
Starting jobservice  ... done
Starting portal      ... done
Starting proxy       ... done
Starting registryctl ... done
[root@harbor harbor]# du -sh /data/
59M    /data/

 

參考:https://www.cnblogs.com/xiyangxixi/p/10126106.html 通過api接口刪除鏡像

https://blog.csdn.net/kong2030/article/details/81331142 回收harbor存儲資源


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM