现许多团队使用 Nexus 来管理 Docker 镜像,产品不断迭代,镜像仓库占用的磁盘空间也越来越大。由于 Nexus 的控制台并未提供批量操作镜像功能,清理镜像十分不便。本文分享一个清理 Nexus 中 Docker 镜像的小工具 — nexus-cli。
下载脚本并授权
wget https://s3.eu-west-2.amazonaws.com/nexus-cli/1.0.0-beta/linux/nexus-cli
chmod +x nexus-cli
配置待清理的库
$ nexus-cli configure
Enter Nexus Host: http://127.0.0.1:8081
Enter Nexus Repository Name: kubeoperator
Enter Nexus Username: admin
Enter Nexus Password: xxxxxx
http://127.0.0.1:8081
是 Nexus 服务的地址,为了安全,可以直接在 Nexus 所在的机器处理。
注意:demo是要清理的那个仓库名,在这里应该使用kubeoperator
列出所有镜像
$ nexus-cli image ls
jdd-k8s/apph5
jdd-k8s/cobrandedcard-app
jdd-k8s/cobrandedcard_new_ui
jdd-k8s/gateway
jdd-k8s/icbc-park
jdd-k8s/icbc-park-sfm
jdd-k8s/icbc-server-app
jdd-k8s/jdd-boot-pc-system
jdd-k8s/jdd-invoice-cloud
jdd-k8s/jdd-merchant-entry
jdd-k8s/jdd-monitor
jdd-k8s/jdd-park-bigscreen
jdd-k8s/jdd-park-wx
jdd-k8s/jdd-parking-cloud
jdd-k8s/jdd-parking-cloud-admin
jdd-k8s/jdd-parking-cloud-ui
jdd-k8s/jdd-parking-fee
jdd-k8s/jdd-parking-report
jdd-k8s/jdd-third-activity-message
jdd-k8s/jdd-third-activity-order
jdd-k8s/jdd-thirdparty
jdd-k8s/jdd-yangergo-scenic
jdd-k8s/mapedit
jdd-k8s/mapmain
jdd-k8s/mqtt
jdd-k8s/nginx_test
jdd-k8s/park-open
jdd-k8s/promote-hotel
jdd-k8s/promote-icbc
jdd-k8s/promote-travel
jdd-k8s/sfmwxparkingh5new
清理镜像
# 查看镜像显示的是啥,这个name就要写那个
./nexus-cli image delete -name IMAGE_NAME -keep X,-keep X 表示保留几个tag
# 清理单个镜像的tag,这里仅保留最新的两个tag
$ ./nexus-cli image delete -name jdd-k8s/jdd-thirdparty -keep 2
清理所有镜像的tag,结合 notepad++
的批量处理功能,十分方便。
清理磁盘空间
最后,创建两个Nexus Task 来清理物理空间。先运行 Purge unused docker manifests and images,再运行 Compact blob store。