欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Harbor镜像清理(包含清理无tag的镜像)

程序员文章站 2024-03-12 10:30:56
...

Harbor镜像清理(包含清理无tag的镜像)

背景:harbor镜像仓库中推送过很多相同tag的镜像,无法通过harbor的UI界面执行删除

一、进入harbor安装目录,停止harbor服务

[email protected]:/home/harbor/make# ls
common                          docker-compose.clair.yml   harbor.cfg            NOTICE
config.yml                      docker-compose.notary.yml  harbor.v1.6.1.tar.gz  open_source_license
dle.sh                          docker-compose.yml         install.sh            prepare
docker-compose.chartmuseum.yml  ha                         LICENSE
[email protected]:/home/harbor/make# docker-compose stop
Stopping nginx              ... done
Stopping harbor-jobservice  ... done
Stopping harbor-ui          ... done
Stopping harbor-adminserver ... done
Stopping registry           ... done
Stopping registryctl        ... done
Stopping harbor-db          ... done
Stopping redis              ... done
Stopping harbor-log         ... done
[email protected]:/home/harbor/make# 

二、 编辑 common/config/registry/config.yml文件

此文件在harbor安装目录下,关闭的目的是为了禁止身份验证

[email protected]:/home/harbor/make# vim common/config/registry/config.yml 
version: 0.1
log:
  level: info
  fields:
    service: registry
storage:
  cache:
    layerinfo: redis
  filesystem:
    rootdirectory: /storage
  maintenance:
    uploadpurging:
      enabled: false
  delete:
    enabled: true
redis:
  addr: redis:6379
  db: 1
http:
  addr: :5000
  secret: placeholder
  debug:
    addr: localhost:5001
    # 注释掉以下6行文件即可,其余无需改动
    #auth:
    #  token:
    #    issuer: harbor-token-issuer
    #    realm: http://192.168.11.225/service/token
    #    rootcertbundle: /etc/registry/root.crt
    #    service: harbor-registry

三、编辑 docker-compose.yml文件,

[email protected]:/home/harbor/make# vim docker-compose.yml 

version: '2'
services:
  log:
    image: vmware/harbor-log:dev
    container_name: harbor-log
    restart: always
    volumes:
      - /var/log/harbor/:/var/log/docker/:z
      - ./common/config/log/:/etc/logrotate.d/:z
    ports:
      - 127.0.0.1:1514:10514
    networks:
      - harbor
  registry:
    # 添加以下两行配置
    ports:
      - 127.0.0.1:5000:5000
    image: vmware/registry-photon:v2.6.2-dev

四、启动harbor

[email protected]:/home/harbor/make# docker-compose up -d
Starting harbor-log ... done
Starting registryctl ... 
Starting harbor-adminserver ... 
Starting harbor-db ... 
Recreating registry ... 
Recreating registry ... done
Recreating harbor-ui ... done
Recreating harbor-jobservice ... 
Recreating harbor-jobservice ... done

五、将被覆盖过的镜像标记为可删除

[email protected]:/home/harbor/make# docker run --network="host" -it -v /data/registry:/registry -e REGISTRY_URL=http://127.0.0.1:5000 mortensrasmussen/docker-registry-manifest-cleanup
Running against local storage
Caught error trying to read manifest, ignoring.
Caught error trying to read manifest, ignoring.
Caught error trying to read manifest, ignoring.
Caught error trying to read manifest, ignoring.
Caught error trying to read manifest, ignoring.
Caught error trying to read manifest, ignoring.
Found 6094 manifests without tags. Deleting
Cleaning 1 of 6094
Cleaning 2 of 6094
Cleaning 3 of 6094
Cleaning 4 of 6094
Cleaning 5 of 6094
Cleaning 6 of 6094
...
Cleaning 6093 of 6094
Cleaning 6094 of 6094
Job done, Cleaned 6095 of 6094 manifests.

Please run a garbage-collect on the registry now to free up disk space.

六、执行删除已经被标记可删除的镜像

[email protected]:/home/harbor/make# docker run -it --name gc --rm --volumes-from registry vmware/registry-photon:v2.6.2-dev garbage-collect  /etc/registry/config.yml
vse/postgres
vse/postgres: marking manifest sha256:28ab96b419e754b05e93109eb4e54fab7f19264e103743f77a6a33921a3a19a4 
vse/postgres: marking blob sha256:f9b577fb1ed6ad42987f06b79889a4f140cb356aad67a61557c1683ea8009f13
vse/postgres: marking blob sha256:a5a6f2f73cd8abbdc55d0df0d8834f7262713e87d6c8800ea3851f103025e0f0
vse/postgres: marking blob sha256:e50fbea8af5a0d3f282443e820c3ea2a5223ca089ab906faea318922da594f2a

...
5014 blobs marked, 32370 blobs eligible for deletion
blob eligible for deletion: sha256:79288098314f078995df8a9b98168464bc72490eceadbea8870de277b00d9407
INFO[0004] Deleting blob: /docker/registry/v2/blobs/sha256/79/79288098314f078995df8a9b98168464bc72490eceadbea8870de277b00d9407  go.version=go1.7.3 instance.id=8cf9a0b5-7713-4b9f-929a-d3da354be01f service=registry
blob eligible for deletion: sha256:97583c8caf43066c64934b336c9c64622b03d9a492da024a4c13293beb656613
INFO[0004] Deleting blob: /docker/registry/v2/blobs/sha256/97/97583c8caf43066c64934b336c9c64622b03d9a492da024a4c13293beb656613  go.version=go1.7.3 instance.id=8cf9a0b5-7713-4b9f-929a-d3da354be01f service=registry

七、还原步骤二、步骤三修改的配置文件,重启harbor服务

[email protected]:/home/harbor/make# docker-compose stop
Stopping nginx              ... done
Stopping harbor-jobservice  ... done
Stopping harbor-ui          ... done
Stopping harbor-adminserver ... done
Stopping registry           ... done
Stopping registryctl        ... done
Stopping harbor-db          ... done
Stopping redis              ... done
Stopping harbor-log         ... done
[email protected]:/home/harbor/make# docker-compose up -d
Starting harbor-log ... done
Starting registryctl ... 
Starting harbor-adminserver ... 
Starting harbor-db ... 
Recreating registry ... 
Recreating registry ... done
Recreating harbor-ui ... done
Recreating harbor-jobservice ... 
Recreating harbor-jobservice ... done

相关标签: harbor