镜像仓库的垃圾回收(Garbage Collection, GC)是保障存储空间健康、降低企业成本的关键运维操作。对承载大量 Java 微服务镜像的仓库而言,频繁的构建与推送会产生大量不再被引用的层(Blob),若不及时清理,存储将迅速膨胀。面试中,你需要展现对 GC 原理、风险控制和自动化策略的深入理解。
一、为什么需要镜像仓库 GC
Docker 镜像由清单(Manifest)和一系列层(Blob)构成。当镜像标签被删除或覆盖时,其清单不再被任何标签引用 ,但这些清单引用的 Blob 可能仍被其他镜像共享。GC 的目标就是识别并物理清除那些已无任何清单引用且不被任何策略保护的 Blob,释放存储空间。
#mermaid-svg-sRZYuVaGt6vBu5aB{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sRZYuVaGt6vBu5aB .error-icon{fill:#552222;}#mermaid-svg-sRZYuVaGt6vBu5aB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sRZYuVaGt6vBu5aB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sRZYuVaGt6vBu5aB .marker.cross{stroke:#333333;}#mermaid-svg-sRZYuVaGt6vBu5aB svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sRZYuVaGt6vBu5aB p{margin:0;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge{stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .section--1 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section--1 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section--1 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section--1 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section--1 text{fill:#ffffff;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth--1{stroke-width:17;}#mermaid-svg-sRZYuVaGt6vBu5aB .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-0 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-0 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-0 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-0 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-0 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-0{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-0{stroke-width:14;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-1 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-1 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-1 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-1 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-1 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-1{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-1{stroke-width:11;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 text{fill:#ffffff;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-2{stroke-width:8;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-3 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-3 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-3 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-3 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-3 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-3{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-3{stroke-width:5;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-4 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-4 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-4 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-4 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-4 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-4{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-4{stroke-width:2;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-5 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-5 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-5 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-5 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-5 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-5{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-5{stroke-width:-1;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-6 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-6 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-6 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-6 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-6 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-6{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-6{stroke-width:-4;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-7 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-7 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-7 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-7 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-7 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-7{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-7{stroke-width:-7;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-8 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-8 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-8 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-8 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-8 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-8{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-8{stroke-width:-10;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-9 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-9 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-9 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-9 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-9 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-9{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-9{stroke-width:-13;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-10 rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-10 path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-10 circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-10 polygon,#mermaid-svg-sRZYuVaGt6vBu5aB .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-10 text{fill:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .node-icon-10{font-size:40px;color:black;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .edge-depth-10{stroke-width:-16;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled circle,#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:lightgray;}#mermaid-svg-sRZYuVaGt6vBu5aB .disabled text{fill:#efefef;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-root rect,#mermaid-svg-sRZYuVaGt6vBu5aB .section-root path,#mermaid-svg-sRZYuVaGt6vBu5aB .section-root circle,#mermaid-svg-sRZYuVaGt6vBu5aB .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-sRZYuVaGt6vBu5aB .section-root text{fill:#ffffff;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-root span{color:#ffffff;}#mermaid-svg-sRZYuVaGt6vBu5aB .section-2 span{color:#ffffff;}#mermaid-svg-sRZYuVaGt6vBu5aB .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-sRZYuVaGt6vBu5aB .edge{fill:none;}#mermaid-svg-sRZYuVaGt6vBu5aB .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-sRZYuVaGt6vBu5aB :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 镜像仓库 GC 的必要性
产生垃圾的原因
删除镜像标签
覆盖标签(如latest)
失败或中断的推送
过期或悬空的清单
GC 要解决的问题
释放磁盘空间
避免存储成本线性增长
维持仓库性能
符合数据保留合规
不执行 GC 的后果
存储被耗尽,拒绝推送
备份时间与成本剧增
性能因过多垃圾文件下降
二、可达性分析:GC 的核心原理
镜像仓库的 GC 基于可达性分析 ,以标签作为根对象(GC Root),凡是从任何标签出发可以追溯到的 Manifest 和 Blob 均为"存活",其余即为"垃圾"。
#mermaid-svg-sHyEPTKRrjGlKOpu{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sHyEPTKRrjGlKOpu .error-icon{fill:#552222;}#mermaid-svg-sHyEPTKRrjGlKOpu .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sHyEPTKRrjGlKOpu .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sHyEPTKRrjGlKOpu .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sHyEPTKRrjGlKOpu .marker.cross{stroke:#333333;}#mermaid-svg-sHyEPTKRrjGlKOpu svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sHyEPTKRrjGlKOpu p{margin:0;}#mermaid-svg-sHyEPTKRrjGlKOpu .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-sHyEPTKRrjGlKOpu .cluster-label text{fill:#333;}#mermaid-svg-sHyEPTKRrjGlKOpu .cluster-label span{color:#333;}#mermaid-svg-sHyEPTKRrjGlKOpu .cluster-label span p{background-color:transparent;}#mermaid-svg-sHyEPTKRrjGlKOpu .label text,#mermaid-svg-sHyEPTKRrjGlKOpu span{fill:#333;color:#333;}#mermaid-svg-sHyEPTKRrjGlKOpu .node rect,#mermaid-svg-sHyEPTKRrjGlKOpu .node circle,#mermaid-svg-sHyEPTKRrjGlKOpu .node ellipse,#mermaid-svg-sHyEPTKRrjGlKOpu .node polygon,#mermaid-svg-sHyEPTKRrjGlKOpu .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-sHyEPTKRrjGlKOpu .rough-node .label text,#mermaid-svg-sHyEPTKRrjGlKOpu .node .label text,#mermaid-svg-sHyEPTKRrjGlKOpu .image-shape .label,#mermaid-svg-sHyEPTKRrjGlKOpu .icon-shape .label{text-anchor:middle;}#mermaid-svg-sHyEPTKRrjGlKOpu .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-sHyEPTKRrjGlKOpu .rough-node .label,#mermaid-svg-sHyEPTKRrjGlKOpu .node .label,#mermaid-svg-sHyEPTKRrjGlKOpu .image-shape .label,#mermaid-svg-sHyEPTKRrjGlKOpu .icon-shape .label{text-align:center;}#mermaid-svg-sHyEPTKRrjGlKOpu .node.clickable{cursor:pointer;}#mermaid-svg-sHyEPTKRrjGlKOpu .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-sHyEPTKRrjGlKOpu .arrowheadPath{fill:#333333;}#mermaid-svg-sHyEPTKRrjGlKOpu .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-sHyEPTKRrjGlKOpu .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-sHyEPTKRrjGlKOpu .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sHyEPTKRrjGlKOpu .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-sHyEPTKRrjGlKOpu .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sHyEPTKRrjGlKOpu .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-sHyEPTKRrjGlKOpu .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-sHyEPTKRrjGlKOpu .cluster text{fill:#333;}#mermaid-svg-sHyEPTKRrjGlKOpu .cluster span{color:#333;}#mermaid-svg-sHyEPTKRrjGlKOpu div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-sHyEPTKRrjGlKOpu .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-sHyEPTKRrjGlKOpu rect.text{fill:none;stroke-width:0;}#mermaid-svg-sHyEPTKRrjGlKOpu .icon-shape,#mermaid-svg-sHyEPTKRrjGlKOpu .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sHyEPTKRrjGlKOpu .icon-shape p,#mermaid-svg-sHyEPTKRrjGlKOpu .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-sHyEPTKRrjGlKOpu .icon-shape .label rect,#mermaid-svg-sHyEPTKRrjGlKOpu .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sHyEPTKRrjGlKOpu .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-sHyEPTKRrjGlKOpu .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-sHyEPTKRrjGlKOpu :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 垃圾对象
存活对象
根对象 GC Roots
标签: v1.0
标签: v1.1
清单 A
Blob: 层1
Blob: 层2
清单 B
Blob: 层3
清单 C
Blob: 层4
Blob: 层5
孤立清单 D
存活
垃圾
- Blob 共享:B1 被清单 A 和 B 共同引用,只要有任意一个标签可达,B1 就保留。
- 孤立清单:若某清单既不关联标签,也不作为其他清单的子清单被引用,则成为垃圾。
三、不同仓库的 GC 机制对比
| 仓库类型 | GC 方式 | 原理与特点 | 自动能力 | 适用场景 |
|---|---|---|---|---|
| Docker Registry v2 (官方) | bin/registry garbage-collect |
无守护进程,需手动以只读模式运行或停止写入后执行。扫描所有 Blob 并删除无引用的对象。 | 无内置定时,需外部 cron 调度 | 简单自建场景,维护窗口期执行 |
| Harbor | 后台 Job Service 执行 | 基于 Registry 的 GC,但通过 Web UI 或 API 触发,支持定时任务。GC 过程中 Registry 自动进入只读模式。 | 支持定时策略,可设置每日/每周等 | 企业级环境,需自动化运维 |
| 云原生 Registry (如 ECR, ACR) | 托管服务自动管理 | 用户通常不可见,可配置生命周期策略自动清理过期镜像或未标记镜像。 | 全自动,策略驱动 | 公有云用户,免运维 |
四、Harbor 垃圾回收的完整执行流程
Harbor 的 GC 由 Job Service 编排,确保在安全模式下执行,避免数据损坏。
存储后端 Registry 实例 Job Service Harbor UI/API 管理员 存储后端 Registry 实例 Job Service Harbor UI/API 管理员 #mermaid-svg-dUP2lpRcBMrYotb9{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dUP2lpRcBMrYotb9 .error-icon{fill:#552222;}#mermaid-svg-dUP2lpRcBMrYotb9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dUP2lpRcBMrYotb9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dUP2lpRcBMrYotb9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dUP2lpRcBMrYotb9 .marker.cross{stroke:#333333;}#mermaid-svg-dUP2lpRcBMrYotb9 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dUP2lpRcBMrYotb9 p{margin:0;}#mermaid-svg-dUP2lpRcBMrYotb9 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-dUP2lpRcBMrYotb9 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-dUP2lpRcBMrYotb9 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-dUP2lpRcBMrYotb9 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-dUP2lpRcBMrYotb9 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-dUP2lpRcBMrYotb9 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-dUP2lpRcBMrYotb9 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-dUP2lpRcBMrYotb9 .sequenceNumber{fill:white;}#mermaid-svg-dUP2lpRcBMrYotb9 #sequencenumber{fill:#333;}#mermaid-svg-dUP2lpRcBMrYotb9 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-dUP2lpRcBMrYotb9 .messageText{fill:#333;stroke:none;}#mermaid-svg-dUP2lpRcBMrYotb9 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-dUP2lpRcBMrYotb9 .labelText,#mermaid-svg-dUP2lpRcBMrYotb9 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-dUP2lpRcBMrYotb9 .loopText,#mermaid-svg-dUP2lpRcBMrYotb9 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-dUP2lpRcBMrYotb9 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-dUP2lpRcBMrYotb9 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-dUP2lpRcBMrYotb9 .noteText,#mermaid-svg-dUP2lpRcBMrYotb9 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-dUP2lpRcBMrYotb9 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-dUP2lpRcBMrYotb9 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-dUP2lpRcBMrYotb9 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-dUP2lpRcBMrYotb9 .actorPopupMenu{position:absolute;}#mermaid-svg-dUP2lpRcBMrYotb9 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-dUP2lpRcBMrYotb9 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-dUP2lpRcBMrYotb9 .actor-man circle,#mermaid-svg-dUP2lpRcBMrYotb9 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-dUP2lpRcBMrYotb9 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 暂停所有推送和删除操作拉取不受影响 手动触发或定时任务启动 GC提交 GC 作业发送准备 GC 信号将所有 Registry 实例切换为只读模式执行 GC 扫描命令遍历所有 Manifest 建立可达 Blob 集合标记所有不可达 Blob 为待删除返回待删除列表确认执行物理删除删除垃圾 Blob 文件删除完成,释放空间命令退出只读模式恢复正常读写GC 作业完成,报告释放空间
关键细节:
- 只读模式:防止在建立引用快照后又有新推送导致误删。多实例部署时,所有 Registry 实例必须同步进入只读状态。
- 两阶段:标记阶段(分析引用)与清除阶段(物理删除)分离,保证安全。
- 审计记录:Harbor 会记录每次 GC 的执行时间、释放空间、是否成功,便于追溯。
五、配置与策略
1. 手动触发
通过 Harbor Web UI "垃圾回收" 页面或 API 立即执行,适用于维护窗口或紧急清理。
2. 定时触发(推荐)
在 Harbor 配置中设置 gc_schedule,例如每日凌晨 2 点执行。应选择业务低峰期,减少只读模式对 CI/CD 的影响。
3. 与镜像保留策略联动
Harbor 的标签保留策略可自动清理过期的或无用的标签,这些标签被删除后,下一次 GC 即可回收相应的 Blob。保留策略是 GC 的前置步骤,两者组合形成自动化清理流水线:
#mermaid-svg-lcrp41jZYvMBRI3o{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-lcrp41jZYvMBRI3o .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-lcrp41jZYvMBRI3o .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-lcrp41jZYvMBRI3o .error-icon{fill:#552222;}#mermaid-svg-lcrp41jZYvMBRI3o .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-lcrp41jZYvMBRI3o .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-lcrp41jZYvMBRI3o .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-lcrp41jZYvMBRI3o .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-lcrp41jZYvMBRI3o .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-lcrp41jZYvMBRI3o .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-lcrp41jZYvMBRI3o .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-lcrp41jZYvMBRI3o .marker{fill:#333333;stroke:#333333;}#mermaid-svg-lcrp41jZYvMBRI3o .marker.cross{stroke:#333333;}#mermaid-svg-lcrp41jZYvMBRI3o svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-lcrp41jZYvMBRI3o p{margin:0;}#mermaid-svg-lcrp41jZYvMBRI3o .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-lcrp41jZYvMBRI3o .cluster-label text{fill:#333;}#mermaid-svg-lcrp41jZYvMBRI3o .cluster-label span{color:#333;}#mermaid-svg-lcrp41jZYvMBRI3o .cluster-label span p{background-color:transparent;}#mermaid-svg-lcrp41jZYvMBRI3o .label text,#mermaid-svg-lcrp41jZYvMBRI3o span{fill:#333;color:#333;}#mermaid-svg-lcrp41jZYvMBRI3o .node rect,#mermaid-svg-lcrp41jZYvMBRI3o .node circle,#mermaid-svg-lcrp41jZYvMBRI3o .node ellipse,#mermaid-svg-lcrp41jZYvMBRI3o .node polygon,#mermaid-svg-lcrp41jZYvMBRI3o .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-lcrp41jZYvMBRI3o .rough-node .label text,#mermaid-svg-lcrp41jZYvMBRI3o .node .label text,#mermaid-svg-lcrp41jZYvMBRI3o .image-shape .label,#mermaid-svg-lcrp41jZYvMBRI3o .icon-shape .label{text-anchor:middle;}#mermaid-svg-lcrp41jZYvMBRI3o .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-lcrp41jZYvMBRI3o .rough-node .label,#mermaid-svg-lcrp41jZYvMBRI3o .node .label,#mermaid-svg-lcrp41jZYvMBRI3o .image-shape .label,#mermaid-svg-lcrp41jZYvMBRI3o .icon-shape .label{text-align:center;}#mermaid-svg-lcrp41jZYvMBRI3o .node.clickable{cursor:pointer;}#mermaid-svg-lcrp41jZYvMBRI3o .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-lcrp41jZYvMBRI3o .arrowheadPath{fill:#333333;}#mermaid-svg-lcrp41jZYvMBRI3o .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-lcrp41jZYvMBRI3o .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-lcrp41jZYvMBRI3o .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-lcrp41jZYvMBRI3o .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-lcrp41jZYvMBRI3o .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-lcrp41jZYvMBRI3o .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-lcrp41jZYvMBRI3o .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-lcrp41jZYvMBRI3o .cluster text{fill:#333;}#mermaid-svg-lcrp41jZYvMBRI3o .cluster span{color:#333;}#mermaid-svg-lcrp41jZYvMBRI3o div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-lcrp41jZYvMBRI3o .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-lcrp41jZYvMBRI3o rect.text{fill:none;stroke-width:0;}#mermaid-svg-lcrp41jZYvMBRI3o .icon-shape,#mermaid-svg-lcrp41jZYvMBRI3o .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-lcrp41jZYvMBRI3o .icon-shape p,#mermaid-svg-lcrp41jZYvMBRI3o .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-lcrp41jZYvMBRI3o .icon-shape .label rect,#mermaid-svg-lcrp41jZYvMBRI3o .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-lcrp41jZYvMBRI3o .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-lcrp41jZYvMBRI3o .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-lcrp41jZYvMBRI3o :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 保留策略运行
删除过期标签/清单
产生孤儿 Blob
定时 GC 触发
标记无引用 Blob
物理删除并释放空间
4. 仅清理未标记镜像(untagged)
部分仓库支持专门清理无任何标签的镜像清单,此类镜像通常是推送失败或中间状态,清理它们风险极低,可更频繁执行。
六、风险、注意事项与最佳实践
| 风险 / 挑战 | 说明 | 最佳实践 |
|---|---|---|
| 只读窗口影响推送 | GC 期间所有推送被拒绝,可能导致 CI/CD 失败。 | 计划在业务低谷执行,监控只读时长;对 CI 设置重试机制。 |
| 误删共享 Blob | 如果 GC 在并发删除标签时运行,理论上有极小概率误删。 | Harbor 通过只读模式和事务性快照避免;确保 GC 期间无其他管理操作。 |
| 性能开销 | 大规模仓库(TB 级)GC 可能耗时数小时,消耗 IOPS。 | 首次 GC 可能时间很长,后续日常 GC 增量清理会快很多;使用 SSD 后端。 |
| 不可变性冲突 | 若配置了镜像不可变性(immutable),GC 不能删除被保护的标签。 | 设计合理的保留和不可变策略,避免冲突。 |
| 多实例同步只读 | 高可用部署时,必须所有实例同时只读。 | 使用共享数据库的锁机制或 Harbor 集群协调能力。 |
七、Java 面试追问与回答思路
1. 为什么删除镜像后磁盘空间没有立即释放?
答:删除标签仅移除 Manifest 引用,使 Blob 变为"不可达"状态,但文件仍存留在磁盘。必须等到下一次 GC 执行后,这些孤儿 Blob 才被物理清除并释放空间。这体现了"标记-清除"的基本原理。
2. 如果 GC 过程中有人推送镜像怎么办?
答:企业级仓库会进入只读模式,推送请求会被拒绝并返回错误(如 503)。CI/CD 流水线应具备重试机制,并选择低峰期执行 GC。也可将 GC 安排在保留策略之后,并配置较短的只读窗口。
3. 在 Kubernetes 环境中,Harbor GC 有何特殊影响?
答:Kubernetes 节点通常缓存了镜像层,GC 仅影响仓库端存储,不影响已拉取到节点的镜像。但若 GC 正巧在 Pod 重启需要重新拉取镜像时发生,拉取请求会失败。可通过合理调度 GC 时间,或在凌晨业务低谷期执行来缓解。
4. 如何验证 GC 是否真正释放了空间?
答:比较 GC 前后存储后端的磁盘使用量,Harbor 的 GC 日志会显示释放的字节数。同时,可监控存储桶的 CloudWatch 等指标,形成持续验证。
八、思维导图总结
#mermaid-svg-vik3TEqJwHs6ZM0M{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vik3TEqJwHs6ZM0M .error-icon{fill:#552222;}#mermaid-svg-vik3TEqJwHs6ZM0M .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vik3TEqJwHs6ZM0M .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vik3TEqJwHs6ZM0M .marker.cross{stroke:#333333;}#mermaid-svg-vik3TEqJwHs6ZM0M svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vik3TEqJwHs6ZM0M p{margin:0;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge{stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .section--1 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section--1 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section--1 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section--1 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section--1 text{fill:#ffffff;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth--1{stroke-width:17;}#mermaid-svg-vik3TEqJwHs6ZM0M .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-0 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-0 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-0 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-0 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-0 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-0{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-0{stroke-width:14;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-1 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-1 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-1 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-1 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-1 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-1{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-1{stroke-width:11;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 text{fill:#ffffff;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-2{stroke-width:8;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-3 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-3 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-3 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-3 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-3 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-3{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-3{stroke-width:5;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-4 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-4 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-4 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-4 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-4 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-4{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-4{stroke-width:2;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-5 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-5 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-5 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-5 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-5 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-5{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-5{stroke-width:-1;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-6 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-6 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-6 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-6 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-6 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-6{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-6{stroke-width:-4;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-7 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-7 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-7 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-7 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-7 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-7{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-7{stroke-width:-7;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-8 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-8 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-8 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-8 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-8 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-8{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-8{stroke-width:-10;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-9 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-9 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-9 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-9 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-9 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-9{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-9{stroke-width:-13;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-10 rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-10 path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-10 circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-10 polygon,#mermaid-svg-vik3TEqJwHs6ZM0M .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-10 text{fill:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .node-icon-10{font-size:40px;color:black;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .edge-depth-10{stroke-width:-16;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled circle,#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:lightgray;}#mermaid-svg-vik3TEqJwHs6ZM0M .disabled text{fill:#efefef;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-root rect,#mermaid-svg-vik3TEqJwHs6ZM0M .section-root path,#mermaid-svg-vik3TEqJwHs6ZM0M .section-root circle,#mermaid-svg-vik3TEqJwHs6ZM0M .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-vik3TEqJwHs6ZM0M .section-root text{fill:#ffffff;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-root span{color:#ffffff;}#mermaid-svg-vik3TEqJwHs6ZM0M .section-2 span{color:#ffffff;}#mermaid-svg-vik3TEqJwHs6ZM0M .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-vik3TEqJwHs6ZM0M .edge{fill:none;}#mermaid-svg-vik3TEqJwHs6ZM0M .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-vik3TEqJwHs6ZM0M :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 镜像仓库垃圾回收
原理
可达性分析
标签为 GC Root
Manifest 与 Blob 引用链
实现机制
两阶段:标记 -> 清除
只读模式保障安全
多实例协调
配置方式
手动触发
定时策略
配合保留策略
未标记镜像清理
风险控制
业务低峰执行
监控只读时长
CI/CD 重试
不可变性策略
存储优化
使用 S3/MinIO 等对象存储
评估 IOPS 影响
定期监控空间增长
总结:镜像仓库 GC 并非简单的删除操作,而是一项需要精心设计的存储空间管理策略。理解其可达性分析原理、安全执行流程和与保留策略的联动,能帮助 Java 团队持续控制镜像存储成本、保障 CI/CD 畅通。在面试中,将 GC 置于整个镜像生命周期管理的框架下阐述,正是高级工程师应具备的运营思维。