测试环境的Harbor磁盘已经不堪重负,一天不知道多少个迭代测试包丢上面,清理已经刻不容缓。因公司使用Harbor版本比较老,无法在线清理,故需要两步清理:先删除不需要的tag,然后在使用garbage collection(GC)回收磁盘空间。

Harbor已经在1.7.0版本支持在线清理空间.

一、清理不需要的镜像tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
# 此脚本基于时间排序,仅保留最新的15个tag

import requests
requests.packages.urllib3.disable_warnings()


class CleanHarbor:
def __init__(self, user, password):
self.user = user
self.password = password
self.base_url = "https://registry.youdomain.com/"
self.login_url = self.base_url + 'login'
self.project_url = self.base_url + 'api/projects'
self.repo_url = self.base_url + 'api/repositories'
self.session = requests.Session()
self.session.verify = False
# 此处是个坑,harbor源码auth部分的username是principal,故params参数必须是principal,否则会401
self.session.post(self.login_url, params={"principal": self.user, "password": self.password})

def get_project(self):
project_info = []
harbor_info = self.session.get(self.project_url).json()
for project in harbor_info:
info = {}
info['project_id'] = project['project_id']
info['project_name'] = project['name']
project_info.append(info)
return project_info

def get_delete_repo(self):
project_info = self.get_project()
delete_reponame = []
for project_id in project_info:
repo_info = self.session.get(self.repo_url, params={"project_id": project_id['project_id']}).json()
for repo in repo_info:
if repo['tags_count'] > 15:
delete_reponame.append(repo['name'])
return delete_reponame

def delete_repo_tag(self):
delete_reponame = self.get_delete_repo()
for repo_name in delete_reponame:
tag_url = self.repo_url + "/" + repo_name + "/tags"
tags = self.session.get(tag_url).json()
tags_sort = sorted(tags, key=lambda tags: tags["created"])
del_tags = tags_sort[0:len(tags_sort) - 15]
for tag in del_tags:
del_repo_tag_url = tag_url + "/" + tag['name']
result = self.session.delete(del_repo_tag_url)
if result.status_code == 200:
print('{}:{} tag删除成功..'.format(repo_name, tag['name']))
print("所有tag删除完成...")


if __name__ == "__main__":
Harobr = CleanHarbor('admin', 'Harbor12345')
Harobr.delete_repo_tag()

二、使用garbage collection(GC)机制删除镜像的实际文件

1
2
3
4
5
6
7
8
9
10
11
# 先停止harbor服务
docker-compose down

# 使用gc回收磁盘空间(GC使用“标记-清理”法)
# 第一步,标记:registry扫描元数据,元数据能够索引到的blob标记为不能删除
# 第二步,清理:registry扫描所有blobs,如果该blob没有被标记,则删除它
# 注意此处的registry-photon,请保持和你的版本一致,如下图
docker run -it --name gc --rm --volumes-from registry vmware/registry-photon:v2.6.2-v1.5.2 garbage-collect /etc/registry/config.yml

# 启动Harbor
docker-compose start

参考: