2026-11-11 12:57:24 +08:00
<!DOCTYPE html>
< html lang = "zh-CN" data-theme = "light" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< title > 第 15 章 密钥与镜像供应链 · Kubernetes 专题知识库< / title >
< meta name = "description" content = "把可被覆盖的 tag 换成 digest, 用 trivy 扫漏洞、cosign 给镜像签名、Kyverno 做准入校验,并把明文 Secret 移出 Git; 含镜像拉取、策略锁死与 digest 失效三个供应链故障的排查路径。" >
< meta name = "keywords" content = "镜像供应链, digest, cosign, trivy, Kyverno, imagePullSecrets, ImageValidatingPolicy, 准入" >
< link rel = "stylesheet" href = "assets/doc.css" >
< / head >
< body >
< header class = "topbar" >
< div class = "tb-in" >
< a class = "brand" href = "index.html" > < span class = "logo" > K8S< / span > < div > Kubernetes 专题< small > 从第一个 Pod 到生产平台< / small > < / div > < / a >
< div class = "crumbs" >
< a href = "index.html" > 总目录< / a > < span class = "sep" > /< / span > < b > 第 15 章< / b >
< / div >
< div class = "spacer" > < / div >
< button class = "btn" id = "tocToggle" > ☰ 导航< / button >
< button class = "btn" id = "themeBtn" title = "切换到深色主题" > 深色< / button >
< / div >
< / header >
< div class = "layout" id = "layout" >
< aside class = "sidebar" >
< h2 > 章节目录< / h2 >
2026-11-11 22:20:47 +08:00
< nav id = "chapters" > < div class = "toc-stage" > < b > S0< / b > < span class = "h" > 起步:环境与心智< / span > < / div > < a class = "toc-a" href = "00-why.html" > < i class = "cn" > 0< / i > 从一次线上事故理解 K8s< / a > < a class = "toc-a" href = "01-env.html" > < i class = "cn" > 1< / i > 装第一个集群: Windows 11 + WSL2 与 Linux 双轨< / a > < a class = "toc-a" href = "02-kubectl.html" > < i class = "cn" > 2< / i > kubectl 与集群解剖:把黑盒变成可观测对象< / a > < div class = "toc-stage" > < b > S1< / b > < span class = "h" > 跑起来:单服务最小闭环< / span > < / div > < a class = "toc-a" href = "03-pod.html" > < i class = "cn" > 3< / i > Pod: 最小调度单元< / a > < a class = "toc-a" href = "04-deployment.html" > < i class = "cn" > 4< / i > Deployment: 副本与滚动更新< / a > < a class = "toc-a" href = "05-service.html" > < i class = "cn" > 5< / i > Service 与集群 DNS< / a > < a class = "toc-a" href = "06-config.html" > < i class = "cn" > 6< / i > 配置与密钥: ConfigMap 与 Secret< / a > < a class = "toc-a" href = "07-probe.html" > < i class = "cn" > 7< / i > 探针与资源:让它活得像生产< / a > < div class = "toc-stage" > < b > S2< / b > < span class = "h" > 撑得住:状态、入口与多服务< / span > < / div > < a class = "toc-a" href = "08-storage.html" > < i class = "cn" > 8< / i > 存储:从 emptyDir 到 StatefulSet< / a > < a class = "toc-a" href = "09-ingress.html" > < i class = "cn" > 9< / i > 入口与网关: Ingress 与 Gateway API< / a > < a class = "toc-a" href = "10-job.html" > < i class = "cn" > 10< / i > Job 与 CronJob: 迁移与定时任务< / a > < a class = "toc-a" href = "11-multisvc.html" > < i class = "cn" > 11< / i > 多服务与网络策略< / a > < div class = "toc-stage" > < b > S3< / b > < span class = "h" > 看得见:可观测、发布与排障< / span > < / div > < a class = "toc-a" href = "12-logging.html" > < i class = "cn" > 12< / i > 日志:从 kubectl logs 到可检索< / a > < a class = "toc-a" href = "13-metrics.html" > < i class = "cn" > 13< / i > 指标、扩缩与告警< / a > < a class = "toc-a" href = "14-release.html" > < i class = "cn" > 14< / i > 发布策略:滚动、蓝绿、金丝雀< / a > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 15< / i > 密钥与镜像供应链< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-15-1" > 15.1 本章在哪< / a > < a class = "toc-a lv3" href = "#sec-15-2" > 15.2 学习目标< / a > < a class = "toc-a lv3" href = "#sec-15-3" > 15.3 场景:安全团队的三封邮件< / a > < a class = "toc-a lv3" href = "#sec-15-4" > 15.4 心智模型: 标签是承诺, digest 是事实< / a > < a class = "toc-a lv3" href = "#sec-15-5" > 15.5 概念与字段速查< / a > < a class = "toc-a lv3" href = "#sec-15-6" > 15.6 动手:把镜像锁死,把口令拿出 Git< / a > < a class = "toc-a lv3" href = "#sec-15-7" > 15.7 验收清单< / a > < a class = "toc-a lv3" href = "#sec-15-8" > 15.8 故障实验室< / a > < a class = "toc-a lv3" href = "#sec-15-9" > 15.9 生产对照< / a > < a class = "toc-a lv3" href = "#sec-15-10" > 15.10 清理与残留检查< / a > < a class = "toc-a lv3" href = "#sec-15-11" > 15.11 小结与自测< / a > < a class = "toc-a lv3" href = "#sec-15-12" > 15.12 衔接< / a > < a class = "toc-a lv3" href = "#sec-15-13" > 15.13 参考与验证版本< / a > < / div > < a class = "toc-a" href = "16-debug.html" > < i class = "cn" > 16< / i > 故障诊断方法论:六类故障,一张诊断树< / a > < div class = "toc-stage" > < b > S4< / b > < span class = "h" > 上生产:集群、调度、安全、交付< / span > < / div > < a class = "toc-a" href = "17-kubeadm.html" > < i class = "cn" > 17< / i > 生产集群搭建: kubeadm HA< / a > < a class = "toc-a" href = "18-schedule.html" > < i class = "cn" > 18< / i > 节点、调度与容量< / a > < a class = "toc-a" href = "19-rbac.html" > < i class = "cn" > 19< / i > 安全基线: RBAC 与准入< / a > < a class = "toc-a" href = "20-gitops.html" > < i class = "cn" > 20< / i > GitOps: Helm/Kustomize + Argo CD< / a > < a class = "toc-a" href = "21-ci.html" > < i class = "cn" > 21< / i > CI/CD 流水线< / a > < a class = "toc-a" href = "22-backup.html" > < i class = "cn" > 22< / i > 备份、恢复与升级< / a > < div class = "toc-stage" > < b > S5< / b > < span class = "h" > 守得住:值班、成本与评审< / span > < / div > < a class = "toc-a" href = "23-oncall.html" > < i class = "cn" > 23< / i > 值班与事故响应< / a > < a class = "toc-a" href = "24-cost.html" > < i class = "cn" > 24< / i > 成本与容量< / a > < a class = "toc-a" href = "25-prr.html" > < i class = "cn" > 25< / i > 生产就绪评审与下一步< / a > < / nav >
2026-11-11 12:57:24 +08:00
< div class = "side-foot" >
< a class = "btn" href = "index.html" > ← 总目录< / a >
< / div >
< / aside >
< div class = "mask" id = "mask" > < / div >
< main class = "main-col" >
< article >
< p class = "chapnav" >
< a href = "index.html" > Kubernetes 专题< / a >
< span > ·< / span >
< span > S3 看得见:可观测、发布与排障< / span >
< span > ·< / span >
< span > 第 < b class = "pos" > 15< / b > / 26 章< / span >
< / p >
< h2 id = "ch15" > < span class = "n" > 15< / span > 密钥与镜像供应链< / h2 >
< div class = "lead" >
< b > 30 秒速览:< / b > 前十四章里,镜像一直是「本地 < code > kind load< / code > 进去、名字带个 tag」的东西, Secret 一直是
YAML 里的一段 < code > stringData< / code > 。演示没问题,审计过不了。本章把镜像搬进带认证的私有仓库,用 digest 钉死
「跑的是哪一份」,再用 trivy、cosign 与 Kyverno 补上三个问题。约 150 分钟。
< / div >
< h3 id = "sec-15-1" > 15.1 本章在哪< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 项< / th > < th > 内容< / th > < / tr > < / thead >
< tbody >
< tr > < td > 主线之前< / td > < td > < code > shop-api< / code > 的镜像写的是 < code > registry.local/aurora/shop-api:v1.2.3< / code > ,靠 < code > kind load< / code > 进节点;数据库口令写在 < code > deploy/base/shop-api.yaml< / code > 的 < code > stringData< / code > 里,跟着 Git 走< / td > < / tr >
< tr > < td > 主线之后< / td > < td > 镜像从带认证的私有仓库拉取,清单里写 < code > @sha256:< / code > ;仓库里同一 digest 有签名; Kyverno 拒绝任何未签名的 Pod; 口令由命令生成, Git 里只剩模板< / td > < / tr >
< tr > < td > 本章产物< / td > < td > < code > deploy/base/kyverno-require-signed.yaml< / code > 、< code > scripts/supply.sh< / code > 、< code > Makefile< / code > 里的 < code > verify-ch15< / code > < / td > < / tr >
< tr > < td > 适用环境< / td > < td > ENV-A( Win11 + WSL2 + kind) 必做; 需要 Docker 能起容器、能连外网拉 trivy 与 cosign 镜像< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-15-2" > 15.2 学习目标< / h3 >
< div class = "box note" >
< span class = "bt" > 学完你能< / span >
< ul >
< li > 把镜像引用从 tag 换成 digest, 判定标准: < code > kubectl get deploy shop-api -n shop -o jsonpath='{.spec.template.spec.containers[0].image}'< / code > 里出现 < code > @sha256:< / code > ,且只有换 digest 才触发滚动。< / li >
< li > 说清 < code > imagePullSecrets< / code > 与 Secret < code > type< / code > 的关系,判定标准:能从 < code > 401 Unauthorized< / code > 与 < code > no basic auth credentials< / code > 反推是哪一环断了。< / li >
< li > 用 cosign 对 digest 签名并在集群外验证,判定标准:< code > cosign verify< / code > 输出里能看到 < code > The signatures were verified against the specified public key< / code > 。< / li >
< li > 写一条只放行已签名镜像的准入策略,判定标准:未签名镜像创建的 ReplicaSet 报 < code > FailedCreate< / code > , Event 里有你写在策略里的 message。< / li >
< / ul >
< / div >
< h3 id = "sec-15-3" > 15.3 场景:安全团队的三封邮件< / h3 >
< p > 周一上午,安全团队发来三个问题,每个都只要一行答案:这个镜像谁构建的;里面有什么高危漏洞;线上跑的到底是不是审核过的那一份。
你打开 < code > deploy/base/shop-api.yaml< / code > ,看到 < code > image: registry.local/aurora/shop-api:v1.2.3< / code > ——一个< strong > 可覆盖的指针< / strong > :
任何人重新 push 同名 tag, 集群下次拉取拿到的就是另一份内容, 而清单一个字没变。同一份文件里, < code > stringData< / code > 的口令跟着
< code > git log< / code > 一起躺在历史里;第 06 章讲过它的挂载行为,没讲它该不该进 Git。< / p >
< div class = "box warn" >
< span class = "bt" > 现象 · 三个问题都答不上来< / span >
< p > < b > 现象< / b > : 你能证明「Pod 在跑」,但证明不了「跑的是哪一份代码」;漏洞扫描停在上周某次手动执行,
没有任何机制保证它和线上一致;口令在 Git 历史里,改一次要改所有副本的 YAML。< / p >
< p > < b > 原因< / b > : tag 是人为约定的字符串, digest 才是内容的哈希; Secret 是可以只存在于集群里的对象,
< code > stringData< / code > 是写在文件里的明文,天然进版本库。< / p >
< p > < b > 怎么验证< / b > : < code > docker inspect --format='{{index .RepoDigests 0}}' registry.local/aurora/shop-api:v1.2.3< / code >
能拿到 digest, 但清单里没有它; < code > git log -p -- deploy/base/shop-api.yaml | grep -c stringData< / code > 大于 0。< / p >
< / div >
< p > 本章就为这三个问题各配一个可执行的答案:< strong > digest 回答「哪一份」< / strong > ,
< strong > 签名与扫描回答「谁构建、有什么漏洞」< / strong > , < strong > 准入策略回答「没通过审核的不许进来」< / strong > 。< / p >
< h3 id = "sec-15-4" > 15.4 心智模型: 标签是承诺, digest 是事实< / h3 >
< figure class = "figure" >
< svg viewBox = "0 0 720 250" role = "img" aria-label = "同一个 tag 被两次推送指向不同 digest, 而 digest 永远指向同一份内容" >
< defs >
< marker id = "a15" markerWidth = "9" markerHeight = "9" refX = "8" refY = "4.5" orient = "auto" >
< path d = "M0,0 L9,4.5 L0,9 z" style = "fill:var(--muted)" > < / path >
< / marker >
< / defs >
< rect x = "16" y = "96" width = "150" height = "52" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "91" y = "118" text-anchor = "middle" font-size = "13" style = "fill:var(--ink)" > shop-api.yaml< / text >
< text x = "91" y = "136" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > image: ...:v1.2.3< / text >
< rect x = "214" y = "96" width = "128" height = "52" rx = "9" style = "fill:var(--warn-bg);stroke:var(--warn)" > < / rect >
< text x = "278" y = "118" text-anchor = "middle" font-size = "13" style = "fill:var(--warn)" > tag v1.2.3< / text >
< text x = "278" y = "136" text-anchor = "middle" font-size = "11.5" style = "fill:var(--warn)" > 可被覆盖的指针< / text >
< rect x = "416" y = "26" width = "280" height = "46" rx = "8" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "556" y = "46" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > 第一次 push: 内容 A< / text >
< text x = "556" y = "63" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > sha256:9c0f…( 已审核) < / text >
< rect x = "416" y = "122" width = "280" height = "46" rx = "8" style = "fill:var(--danger-bg);stroke:var(--danger)" > < / rect >
< text x = "556" y = "142" text-anchor = "middle" font-size = "12" style = "fill:var(--danger)" > 第二次 push: 内容 B< / text >
< text x = "556" y = "159" text-anchor = "middle" font-size = "11.5" style = "fill:var(--danger)" > sha256:4a71…( 没人审) < / text >
< rect x = "214" y = "196" width = "482" height = "40" rx = "8" style = "fill:var(--ok-bg);stroke:var(--ok)" > < / rect >
< text x = "455" y = "221" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ok)" > 写成 @sha256:9c0f… 之后, tag 怎么变都不影响线上< / text >
< path d = "M166,118 L210,118" style = "stroke:var(--muted);fill:none" marker-end = "url(#a15)" > < / path >
< path d = "M342,112 L412,60" style = "stroke:var(--muted);fill:none" marker-end = "url(#a15)" > < / path >
< path d = "M342,136 L412,142" style = "stroke:var(--danger);fill:none" stroke-dasharray = "5 4" marker-end = "url(#a15)" > < / path >
< / svg >
< div class = "figcap" > < b > 图 1 ·< / b > 同一个 tag 推送两次会指向两份内容,< code > @sha256:< / code > 永远指向同一份。< / div >
< / figure >
< p > 见图 1 的虚线: push 同名 tag 是合法的,仓库只把指针挪到新 manifest 上,所以「我们发的是 v1.2.3」在不同时间点指不同内容。
换成 < code > @sha256:9c0f…< / code > ,拉的就是「哈希等于 9c0f 的那一份」——找得到就拉,找不到就报错。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 300" role = "img" aria-label = "从构建到运行的五个环节,以及三个安全问题分别由哪个环节回答" >
< defs >
< marker id = "a15b" markerWidth = "9" markerHeight = "9" refX = "8" refY = "4.5" orient = "auto" >
< path d = "M0,0 L9,4.5 L0,9 z" style = "fill:var(--muted)" > < / path >
< / marker >
< / defs >
< rect x = "14" y = "86" width = "122" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "75" y = "110" text-anchor = "middle" font-size = "13" style = "fill:var(--ink)" > 构建< / text >
< text x = "75" y = "129" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > docker build< / text >
< rect x = "164" y = "86" width = "122" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "225" y = "110" text-anchor = "middle" font-size = "13" style = "fill:var(--ink)" > 扫描< / text >
< text x = "225" y = "129" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > trivy< / text >
< rect x = "314" y = "86" width = "122" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "375" y = "110" text-anchor = "middle" font-size = "13" style = "fill:var(--ink)" > 签名< / text >
< text x = "375" y = "129" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > cosign< / text >
< rect x = "464" y = "86" width = "122" height = "58" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "525" y = "110" text-anchor = "middle" font-size = "13" style = "fill:var(--brand)" > 仓库< / text >
< text x = "525" y = "129" text-anchor = "middle" font-size = "11" style = "fill:var(--brand)" > tag + digest< / text >
< rect x = "614" y = "86" width = "92" height = "58" rx = "9" style = "fill:var(--ok-bg);stroke:var(--ok)" > < / rect >
< text x = "660" y = "110" text-anchor = "middle" font-size = "13" style = "fill:var(--ok)" > 准入< / text >
< text x = "660" y = "129" text-anchor = "middle" font-size = "11" style = "fill:var(--ok)" > Kyverno< / text >
< path d = "M136,115 L160,115" style = "stroke:var(--muted);fill:none" marker-end = "url(#a15b)" > < / path >
< path d = "M286,115 L310,115" style = "stroke:var(--muted);fill:none" marker-end = "url(#a15b)" > < / path >
< path d = "M436,115 L460,115" style = "stroke:var(--muted);fill:none" marker-end = "url(#a15b)" > < / path >
< path d = "M586,115 L610,115" style = "stroke:var(--muted);fill:none" marker-end = "url(#a15b)" > < / path >
< text x = "75" y = "52" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 谁构建的< / text >
< text x = "225" y = "52" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 有什么漏洞< / text >
< text x = "375" y = "52" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 是谁签的< / text >
< text x = "660" y = "52" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ok)" > 是不是审核过的那份< / text >
< path d = "M75,58 L75,82" style = "stroke:var(--line);fill:none" > < / path >
< path d = "M225,58 L225,82" style = "stroke:var(--line);fill:none" > < / path >
< path d = "M375,58 L375,82" style = "stroke:var(--line);fill:none" > < / path >
< path d = "M660,58 L660,82" style = "stroke:var(--line);fill:none" > < / path >
< rect x = "14" y = "186" width = "692" height = "60" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "360" y = "211" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > 节点按 digest 拉取;拉不到就 ImagePullBackOff, 绝不会「凑合跑一份旧的」< / text >
< text x = "360" y = "232" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 签名与扫描的结论存在仓库里,跟着 digest 走,不跟着人走< / text >
< / svg >
< div class = "figcap" > < b > 图 2 ·< / b > 三个安全问题分别由哪一段回答;准入是最后一道闸门,不是唯一一道。< / div >
< / figure >
< p > 见图 2: 扫描与签名在镜像< strong > 进仓库之前< / strong > ,保护「进入集群的东西」;准入在< strong > 对象进集群时< / strong > ,
保护「有人绕过流水线手动 push」。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 常见误解< / th > < th > 事实< / th > < / tr > < / thead >
< tbody >
< tr > < td > 「用了私有仓库就安全了」< / td > < td > 私有仓库只解决「谁能推」, 不解决「推上去的是什么」; tag 仍可被覆盖< / td > < / tr >
< tr > < td > 「有扫描报告就等于扫过线上那份」< / td > < td > 报告要绑定 digest, 否则扫的是 A, 跑的是 B< / td > < / tr >
< tr > < td > 「签名是给人看的」< / td > < td > 签名是给准入控制器验的;没人验的签名只是仪式< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-15-5" > 15.5 概念与字段速查< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 对象 / 字段< / th > < th > 作用< / th > < th > 默认值< / th > < th > 生产建议< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > image: repo@sha256:…< / code > < / td > < td > 按内容哈希拉取,不受 tag 覆盖影响< / td > < td > 无(必须显式写)< / td > < td > 所有工作负载都应写成 digest< / td > < / tr >
< tr > < td > < code > imagePullPolicy< / code > < / td > < td > 何时重新拉镜像< / td > < td > 带 digest 或非 < code > :latest< / code > 的 tag 时为 < code > IfNotPresent< / code > , < code > :latest< / code > 为 < code > Always< / code > < / td > < td > 显式写 < code > IfNotPresent< / code > ,别依赖推断< / td > < / tr >
< tr > < td > < code > imagePullSecrets< / code > < / td > < td > kubelet 拉镜像时用的认证信息< / td > < td > 空(匿名拉取)< / td > < td > 挂到 ServiceAccount 上,比逐个 Deployment 写更稳< / td > < / tr >
< tr > < td > < code > Secret< / code > 的 < code > type< / code > < / td > < td > 决定 kubelet 怎么解析内容< / td > < td > < code > Opaque< / code > < / td > < td > 镜像认证必须用 < code > kubernetes.io/dockerconfigjson< / code > < / td > < / tr >
< tr > < td > < code > ImageValidatingPolicy< / code > < / td > < td > 准入阶段校验镜像签名与证明< / td > < td > 无< / td > < td > 先 < code > Audit< / code > 跑一天再切 < code > Deny< / code > < / td > < / tr >
< tr > < td > < code > spec.matchImageReferences< / code > < / td > < td > 策略作用于哪些镜像< / td > < td > 必填< / td > < td > 用 < code > glob< / code > 限定到自己的仓库前缀< / td > < / tr >
< tr > < td > < code > cosign.key.data< / code > < / td > < td > 策略里内联的公钥( PEM) < / td > < td > 无< / td > < td > 生产用 KMS 引用,别把公钥硬编码进多个策略< / td > < / tr >
< tr > < td > < code > validationActions< / code > < / td > < td > 违规时拒绝还是只记录< / td > < td > < code > [Deny]< / code > < / td > < td > 灰度期用 < code > [Audit]< / code > < / td > < / tr >
< tr > < td > < code > failurePolicy< / code > < / td > < td > 策略引擎不可用时放行还是拒绝< / td > < td > webhook 默认 < code > Fail< / code > < / td > < td > 新策略先 < code > Ignore< / code > ,稳定后再收紧< / td > < / tr >
< tr > < td > < code > mutateDigest< / code > < / td > < td > 是否把 Pod 里的 tag 改写成 digest< / td > < td > < code > true< / code > < / td > < td > 保持默认,等于免费获得 digest 固定< / td > < / tr >
< / tbody >
< / table >
< / div >
< details >
< summary > 可跳过 · < code > ClusterPolicy< / code > 与 < code > ImageValidatingPolicy< / code > 是什么关系< / summary >
< p > 老写法是 < code > ClusterPolicy< / code > ( < code > kyverno.io/v1< / code > )里的 < code > verifyImages< / code > 规则;
1.19 起标记废弃, 1.20 起不再被引擎执行,迁移目标是 < code > policies.kyverno.io/v1< / code > 的 CEL 策略族,
管镜像的就是 < code > ImageValidatingPolicy< / code > 。字段一一对应:< code > imageReferences< / code > 变成
< code > matchImageReferences< / code > , < code > attestors[].entries[].keys< / code > 变成 < code > attestors[].cosign.key< / code > ,
< code > mutateDigest< / code > 这类开关收进 < code > validationConfigurations< / code > 。旧文档里的 < code > verifyImages< / code > 就是它的前身。< / p >
< / details >
< p class = "take" > < b > 一句话:< / b > tag 是给人读的, digest 是给机器验的;能被覆盖的东西不能当证据。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 策略把 tag 改写成 digest, 你在 Pod 里看不到 tag 了< / span >
< p > < b > 现象< / b > :策略生效后 < code > kubectl get pod -o yaml< / code > 里的 < code > image< / code > 变成 < code > …@sha256:…< / code > ,
清单里却写的是 < code > :v1.2.3< / code > ,有人以为是自己写错了。< / p >
< p > < b > 原因< / b > : < code > mutateDigest< / code > 默认 < code > true< / code > ,准入阶段把可解析的 tag 解析成 digest 再写回 Pod spec,
运行中的 Pod 就不随 tag 漂移。< / p >
< p > < b > 怎么验证< / b > :临时删掉策略再建一个 Pod, < code > image< / code > 会保持 tag; 两者对比即可确认是策略改的。< / p >
< / div >
< h3 id = "sec-15-6" > 15.6 动手:把镜像锁死,把口令拿出 Git< / h3 >
< p > 七个步骤按顺序做。工作目录仍是 < code > ~/work/aurora-shop< / code > ,命令默认在 WSL 的 bash 里执行。
本章的仓库有两个名字,先记住谁用哪个:< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 谁在访问< / th > < th > 用哪个地址< / th > < th > 原因< / th > < / tr > < / thead >
< tbody >
< tr > < td > 你在宿主机 push、登录< / td > < td > < code > localhost:5001< / code > < / td > < td > 容器端口映射到宿主机< / td > < / tr >
< tr > < td > 你在宿主机跑 trivy、cosign< / td > < td > < code > localhost:5001< / code > < / td > < td > 同上,凭据在 < code > ~/.docker/config.json< / code > < / td > < / tr >
< tr > < td > 节点拉镜像、Pod 清单< / td > < td > < code > aurora-registry:5000< / code > < / td > < td > kind 网络内的容器名,节点能解析< / td > < / tr >
< tr > < td > 策略的 < code > matchImageReferences< / code > < / td > < td > < code > aurora-registry:5000/aurora/*< / code > < / td > < td > 准入控制器本身也是 Pod< / td > < / tr >
< / tbody >
< / table >
< / div >
< h4 > Step 1 · 起一个带认证的本地仓库< / h4 >
< p > < span class = "tag" > 意图< / span > 先让「推」和「拉」都需要账号,后面才谈得上 < code > imagePullSecrets< / code > 与 < code > 401< / code > 。< / p >
< pre data-lang = "bash" data-name = "起仓库并推第一份镜像" > # env: all
mkdir -p auth cosign
# 用 httpd 镜像里的 htpasswd 生成账号,免去装 apache2-utils
docker run --rm httpd:2.4-alpine htpasswd -nbB aurora aurora-local-dev > auth/htpasswd
docker run -d --name aurora-registry --network kind -p 5001:5000 \
-v "$PWD/auth:/auth" -v aurora-registry-data:/var/lib/registry \
-e REGISTRY_AUTH=htpasswd \
-e REGISTRY_AUTH_HTPASSWD_REALM=aurora \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
registry:2
docker login localhost:5001 -u aurora -p aurora-local-dev
docker tag registry.local/aurora/shop-api:v1.2.3 localhost:5001/aurora/shop-api:v1.2.3
docker push localhost:5001/aurora/shop-api:v1.2.3< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
Login Succeeded
The push refers to repository [localhost:5001/aurora/shop-api]
v1.2.3: digest: sha256:9c0f… size: 1573< / pre >
< p > < code > --network kind< / code > 是关键:仓库与三个节点在同一 Docker 网络里,节点才能用容器名找到它;
< code > -p 5001:5000< / code > 只是给宿主机的 < code > docker< / code > 客户端用的。< / p >
< div class = "grid2" >
< div class = "box" >
< h4 > Windows 11 + WSL2< / h4 >
< pre data-lang = "bash" data-name = "WSL 里的挂载路径" > # env: wsl
echo "$PWD/auth" # /home/you/work/aurora-shop/auth
docker run --rm -v "$PWD/auth:/auth" alpine ls /auth< / pre >
< p > WSL 的 bash 里 < code > $PWD< / code > 就是 Linux 路径, Docker Desktop 能直接挂载;只有回到 PowerShell 才要写
< code > ${PWD}< / code > ,且目录得在 < code > /mnt/d/…< / code > 下。< / p >
< / div >
< div class = "box" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< pre data-lang = "bash" data-name = "本机路径" > # env: linux
echo "$PWD/auth" # /home/you/work/aurora-shop/auth
docker run --rm -v "$PWD/auth:/auth" alpine ls /auth< / pre >
< p > 没有跨文件系统的问题,< code > $PWD< / code > 与 < code > ${PWD}< / code > 等价; htpasswd 文件不需要额外改权限。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > :挂载写法在 bash 里通用; 要分开记的是「PowerShell 写 < code > ${PWD}< / code > 」与「Windows 路径得在 Docker Desktop 共享目录里」。< / p >
< h4 > Step 2 · 让节点认这个名字,并真的拉一次< / h4 >
< p > < span class = "tag" > 意图< / span > 节点上的 containerd 默认按 HTTPS 访问仓库,而 < code > registry:2< / code > 是明文 HTTP, 不改配置永远拉不下来。< / p >
< pre data-lang = "bash" data-name = "给三个节点写 containerd 的仓库配置" > # env: all
for n in aurora-control-plane aurora-worker aurora-worker2; do
docker exec "$n" mkdir -p "/etc/containerd/certs.d/aurora-registry:5000"
printf 'server = "http://aurora-registry:5000"\n[host."http://aurora-registry:5000"]\n capabilities = ["pull", "resolve"]\n' \
| docker exec -i "$n" tee "/etc/containerd/certs.d/aurora-registry:5000/hosts.toml" >/dev/null
docker exec "$n" systemctl restart containerd
done
# 直接让一个节点拉一次,绕过 kubelet, 先确认通道通
docker exec aurora-worker crictl pull \
--creds aurora:aurora-local-dev \
aurora-registry:5000/aurora/shop-api:v1.2.3< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
Image is up to date for sha256:9c0f…< / pre >
< div class = "box warn" >
< span class = "bt" > 如果不一样 · 节点还是拉不到< / span >
< p > 先看 < code > crictl pull< / code > 的报错:< code > connection refused< / code > 多半是仓库容器没进 < code > kind< / code > 网络
( < code > docker network connect kind aurora-registry< / code > 补上);< code > x509: certificate signed by unknown authority< / code >
说明 < code > hosts.toml< / code > 没生效(确认路径里的冒号、并重启过 containerd) ; < code > 401< / code > 说明 < code > --creds< / code > 没带或密码错。< / p >
< / div >
< p > Pod 里的 < code > localhost< / code > 是 Pod 自己,准入控制器也是 Pod, 同样解析不到宿主机; 仓库是同一个, 只是两个网络看过去名字不同。< / p >
< p > 顺手把口令搬出 Git: 新建 < code > .env.local< / code > 写进 < code > .gitignore< / code > , Git 里只留 < code > .env.example< / code > ;
< code > kubectl create secret docker-registry< / code > 会自己拼出 < code > .dockerconfigjson< / code > ,比手写 base64 可靠。< / p >
< p > < b > 不一致怎么办< / b > : 5001 被占就换 5002, 并把后面所有地址一起改。< / p >
< h4 > Step 3 · 先扫描,再决定让不让它进仓库< / h4 >
< p > < span class = "tag" > 意图< / span > 把「有什么漏洞」变成一个能卡住流水线的退出码,而不是一份没人看的报告。< / p >
< pre data-lang = "bash" data-name = "用容器跑 trivy 扫私有仓库里的镜像" > # env: all
docker run --rm \
-v "$HOME/.docker/config.json:/root/.docker/config.json:ro" \
aquasec/trivy:0.76.2 image \
--scanners vuln --severity HIGH,CRITICAL --ignore-unfixed \
--exit-code 1 localhost:5001/aurora/shop-api:v1.2.3
echo "exit=$?"< / pre >
< pre data-lang = "text" data-name = "预期输出(条目随漏洞库变化)" > # 预期输出
shop-api:v1.2.3 (alpine 3.20.3)
Total: 3 (HIGH: 2, CRITICAL: 1)
exit=1< / pre >
< p > 退出码 1 表示「有未修复的高危或严重漏洞」。条目数随漏洞库变化,看 < code > Total< / code > 与退出码即可。
< code > --ignore-unfixed< / code > 很重要:没有修复版本的漏洞只能靠加例外绕过。< / p >
< p > < b > 不一致怎么办< / b > :扫不动多半是漏洞库没下载完,先加 < code > --skip-db-update< / code > 复用旧库。< / p >
< h4 > Step 4 · 对 digest 签名< / h4 >
< p > < span class = "tag" > 意图< / span > 签名要绑在 digest 上,绑在 tag 上的签名等于没签。< / p >
< pre data-lang = "bash" data-name = "生成密钥对并签名" > # env: all
# 用一个固定口令保护私钥;生产里这个口令来自密钥管理系统,不该出现在命令行
export COSIGN_PASSWORD=aurora-local-dev
docker run --rm -w /keys -v "$PWD/cosign:/keys" -e COSIGN_PASSWORD \
gcr.io/projectsigstore/cosign:v3.1.4 generate-key-pair
DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' \
localhost:5001/aurora/shop-api:v1.2.3 | cut -d@ -f2)
echo "$DIGEST"
docker run --rm -w /keys -v "$PWD/cosign:/keys" -e COSIGN_PASSWORD \
-v "$HOME/.docker/config.json:/root/.docker/config.json:ro" \
gcr.io/projectsigstore/cosign:v3.1.4 sign --allow-insecure-registry --yes \
--tlog-upload=false \
--key /keys/cosign.key "localhost:5001/aurora/shop-api@$DIGEST"
docker run --rm -v "$PWD/cosign:/keys" \
-v "$HOME/.docker/config.json:/root/.docker/config.json:ro" \
gcr.io/projectsigstore/cosign:v3.1.4 verify --allow-insecure-registry \
--key /keys/cosign.pub "localhost:5001/aurora/shop-api@$DIGEST"< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
sha256:9c0f…
Pushing signature to: localhost:5001/aurora/shop-api
Verified OK< / pre >
< p > 命令在 < code > cosign/< / code > 下生成 < code > cosign.key< / code > 与 < code > cosign.pub< / code > ,口令只是加密私钥:
< strong > 私钥与口令只能进密钥管理系统,公钥才进 Git< / strong > (策略里要内联它)。< / p >
< p > < b > 不一致怎么办< / b > : < code > 401< / code > 是容器里没挂 docker 配置; digest 为空就回 Step 1 重推一次。< / p >
< div class = "grid2" >
< div class = "box" >
< h4 > Windows 11 + WSL2< / h4 >
< p > WSL 的 bash 里用 < code > export COSIGN_PASSWORD=…< / code > , < code > -e COSIGN_PASSWORD< / code > 会把变量透传进容器。
在 PowerShell 里直接调 < code > docker< / code > 时改成 < code > $env:COSIGN_PASSWORD = "…"< / code > ,不能用 < code > export< / code > ;
< code > -v "${PWD}/cosign:/keys"< / code > 的双引号也不能省。< / p >
< / div >
< div class = "box" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< p > < code > export< / code > 与 < code > -e< / code > 的组合直接可用,< code > $PWD< / code > 不加引号也能工作。
私钥建议 < code > chmod 600 cosign/cosign.key< / code > ,别放进会被 < code > git add .< / code > 带走的目录。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > : 差别只在「变量怎么进进程」——bash 用 < code > export< / code > , PowerShell 用 < code > $env:< / code > ;容器里的 cosign 看到的是同一个环境变量。< / p >
< h4 > Step 5 · 把 Deployment 钉到 digest 上< / h4 >
< p > < span class = "tag" > 意图< / span > 这是本章唯一改变线上行为的一步,也是「线上是不是审核过的那份」的答案。< / p >
< pre data-lang = "bash" data-name = "挂拉取凭据并把 image 换成 digest" > # env: all
kubectl create secret docker-registry regcred -n shop \
--docker-server=aurora-registry:5000 \
--docker-username=aurora --docker-password=aurora-local-dev
# 同一份凭据在 kyverno 里也要有一份:验签时是 Kyverno 自己去拉 manifest
kubectl create secret docker-registry regcred -n kyverno \
--docker-server=aurora-registry:5000 \
--docker-username=aurora --docker-password=aurora-local-dev
kubectl patch deployment shop-api -n shop --type=merge \
-p '{"spec":{"template":{"spec":{"imagePullSecrets":[{"name":"regcred"}]}}}}'
DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' \
localhost:5001/aurora/shop-api:v1.2.3 | cut -d@ -f2)
kubectl set image deployment/shop-api -n shop \
shop-api="aurora-registry:5000/aurora/shop-api@${DIGEST}"
kubectl rollout status deployment/shop-api -n shop< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
secret/regcred created
secret/regcred created
deployment.apps/shop-api patched
deployment.apps/shop-api image updated
Waiting for deployment "shop-api" rollout to finish: 1 out of 3 new replicas have been updated...
deployment "shop-api" successfully rolled out< / pre >
< p > < b > 不一致怎么办< / b > :卡在 < code > ImagePullBackOff< / code > 时先看
< code > kubectl describe pod -n shop < pod> < / code > 最后一行——< code > 401< / code > 是凭据问题( 15.8 故障 1) ,
< code > not found< / code > 是 digest 写错,< code > connection refused< / code > 是 Step 2 没通。别急着重启节点。< / p >
< h4 > Step 6 · 装 Kyverno, 先只看不拦< / h4 >
< p > < span class = "tag" > 意图< / span > 准入策略上线最怕「一开就把集群锁死」,所以先在 < code > Audit< / code > 下跑一遍。< / p >
< pre data-lang = "bash" data-name = "用 Helm 安装 Kyverno" > # env: all
helm repo add kyverno https://kyverno.github.io/kyverno/
helm repo update
helm install kyverno kyverno/kyverno -n kyverno --create-namespace \
--set admissionController.container.extraArgs.allowInsecureRegistry=true
kubectl -n kyverno get pods< / pre >
< pre data-lang = "yaml" data-name = "deploy/base/kyverno-require-signed.yaml" > # file: deploy/base/kyverno-require-signed.yaml
apiVersion: policies.kyverno.io/v1
kind: ImageValidatingPolicy
metadata:
name: require-signed-images
spec:
validationActions: ["Audit"] # 先记录,观察一天后改成 ["Deny"]
webhookConfiguration:
timeoutSeconds: 15
failurePolicy: Ignore # 引擎自身不可用时放行,避免锁死集群
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
matchImageReferences:
- glob: "aurora-registry:5000/aurora/*"
credentials:
secrets:
- regcred # 必须存在于 kyverno 命名空间
attestors:
- name: cosign
cosign:
key:
data: |
-----BEGIN PUBLIC KEY-----
把 cosign/cosign.pub 的内容整段贴进来
-----END PUBLIC KEY-----
validations:
- expression: >-
images.containers.map(image,
verifyImageSignatures(image, [attestors.cosign])).all(e, e > 0)
message: image signature verification failed< / pre >
< p > < code > allowInsecureRegistry< / code > 是给 kind 的:本地仓库是明文 HTTP, Kyverno 默认按 HTTPS 访问,生产要去掉这个参数。
更关键的是 < code > failurePolicy: Ignore< / code > ——它决定 Kyverno 自己挂掉时集群放行还是全拒, 15.8 的故障 2 会演示。< / p >
< p > < b > 不一致怎么办< / b > : < code > helm< / code > 卡住先看 < code > kyverno< / code > 命名空间里的 Pod 事件。< / p >
< h4 > Step 7 · 负向验证:让一个未签名镜像去撞墙< / h4 >
< p > < span class = "tag" > 意图< / span > 策略不拒绝东西,就等于没上线。< / p >
< pre data-lang = "bash" data-name = "把策略切到 Deny 并用未签名镜像试" > # env: all
sed -i 's/\["Audit"\]/["Deny"]/' deploy/base/kyverno-require-signed.yaml
kubectl apply -f deploy/base/kyverno-require-signed.yaml
# 用同一个 tag( 没有签名) 建一个 Deployment, 故意让它撞墙
kubectl create deployment unsigned-demo -n shop \
--image=aurora-registry:5000/aurora/shop-api:v1.2.3 --replicas=1
sleep 5
kubectl describe rs -n shop -l app=unsigned-demo | tail -n 5< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
imagevalidatingpolicy.policies.kyverno.io/require-signed-images configured
deployment.apps/unsigned-demo created
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedCreate 2s replicaset-controller Error creating: admission webhook denied the request:
image signature verification failed< / pre >
< p > 被拒的原因是< strong > 仓库里没有这个 digest 的签名< / strong > ,不是镜像不存在——后者会报 < code > manifest unknown< / code > 。< / p >
< p > < b > 不一致怎么办< / b > :策略没拒就先确认已 apply, 再看 < code > validationActions< / code > 是不是 < code > Deny< / code > 。< / p >
< div class = "box tip" >
< span class = "bt" > 一个刻意的顺序< / span >
< p > 先 < code > Audit< / code > 再 < code > Deny< / code > 、先 < code > Ignore< / code > 再收紧,不是为了保守,而是唯一能避免「策略上线后全集群建不了 Pod」的顺序。
第 19 章会把它写进评审清单。< / p >
< / div >
< h3 id = "sec-15-7" > 15.7 验收清单< / h3 >
< p > 把下面五条固化成 < code > make verify-ch15< / code > ,以后每次改动镜像或策略都跑一遍。< / p >
< ol class = "steps" >
< li > 镜像引用是 digest 形式,且与仓库里的签名 digest 一致。< / li >
< li > Pod 模板带着 < code > imagePullSecrets< / code > ,且对应 Secret 的 < code > type< / code > 是 < code > kubernetes.io/dockerconfigjson< / code > 。< / li >
< li > 宿主机的 < code > cosign verify< / code > 用公钥能验过同一个 digest。< / li >
< li > 集群里存在 < code > ImageValidatingPolicy< / code > ,且 < code > validationActions< / code > 是 < code > Deny< / code > 。< / li >
< li > 未签名镜像创建的 ReplicaSet 留有 < code > FailedCreate< / code > 事件,且文本包含策略 message。< / li >
< / ol >
< pre data-lang = "bash" data-name = "跑本章的验收" > # env: all
make verify-ch15< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
[ok] shop-api image pinned to digest
[ok] imagePullSecrets regcred present on pod template
[ok] cosign verify passed for current digest
[ok] ImageValidatingPolicy require-signed-images exists (Deny)
[ok] unsigned image denied with expected message< / pre >
< h3 id = "sec-15-8" > 15.8 故障实验室< / h3 >
< div class = "box danger" >
< span class = "bt" > 故障 1 · ImagePullBackOff 与 401 Unauthorized< / span >
< p > < b > 注入< / b > : < code > kubectl patch deployment shop-api -n shop --type=json -p='[{"op":"remove","path":"/spec/template/spec/imagePullSecrets"}]'< / code > ,
或把 < code > regcred< / code > 的 < code > type< / code > 改成 < code > Opaque< / code > 。< / p >
< p > < b > 症状< / b > : Pod 停在 < code > ImagePullBackOff< / code > , < code > describe pod< / code > 的 Events 里依次出现
< code > ErrImagePull< / code > 与 < code > 401 Unauthorized< / code > ,有时伴随 < code > no basic auth credentials< / code > 。< / p >
< p > < b > 诊断< / b > :① < code > kubectl get pod -n shop < pod> -o jsonpath='{.spec.imagePullSecrets}'< / code > 看有没有挂;
② < code > kubectl get secret regcred -n shop -o jsonpath='{.type}'< / code > 看是不是 < code > kubernetes.io/dockerconfigjson< / code > ;
③ < code > kubectl get secret regcred -n shop -o jsonpath='{.data.\.dockerconfigjson}' | base64 -d< / code > 看 < code > auths< / code > 里的 server 是不是 < code > aurora-registry:5000< / code > 。< / p >
< p > < b > 根因< / b > : kubelet 只认 < code > .dockerconfigjson< / code > 这个键和对应的 type; 键名或 type 任一不对,它就当作「没有凭据」,
用匿名请求去问仓库,仓库回了 401。< / p >
< p > < b > 修复< / b > :用 < code > kubectl create secret docker-registry< / code > 重建( server 必须与镜像里的 host 完全一致,端口也算),
再 patch 回 Pod 模板。< / p >
< p > < b > 预防< / b > :把 < code > regcred< / code > 挂到 namespace 的默认 ServiceAccount 上,新工作负载自动继承;并用准入策略要求
「来自私有仓库的镜像必须带 pull secret」( 第 19 章)。< / p >
< / div >
< div class = "box danger" >
< span class = "bt" > 故障 2 · 策略把集群锁死< / span >
< p > < b > 注入< / b > :把策略的 < code > failurePolicy< / code > 改成 < code > Fail< / code > ,再把 < code > matchImageReferences< / code > 的 glob 写成
< code > aurora-registry:5000/*< / code > ,同时让 Kyverno 访问不到仓库(删掉 < code > allowInsecureRegistry< / code > 参数后重启准入控制器)。< / p >
< p > < b > 症状< / b > :任何 Pod 都建不出来,报错是 < code > failed calling webhook … context deadline exceeded< / code >
或 < code > failed to verify image< / code > ;已有 Pod 不受影响,但滚动更新全卡住,连调试用的 < code > kubectl run< / code > 都进不去。< / p >
< p > < b > 诊断< / b > :① < code > kubectl get validatingwebhookconfigurations | grep kyverno< / code > 看 webhook 在不在;
② < code > kubectl -n kyverno logs deploy/kyverno-admission-controller --tail=50< / code > 看是超时还是验签失败。< / p >
< p > < b > 根因< / b > :准入 webhook 是同步调用,< code > failurePolicy: Fail< / code > 就是「webhook 不通就拒绝请求」;
引擎不可用、证书过期、超时都会变成「拒绝一切」。< / p >
< p > < b > 修复< / b > :先 < code > kubectl delete ivpol require-signed-images< / code > 恢复集群——删策略比修引擎快;
然后用 < code > failurePolicy: Ignore< / code > 重建,把 < code > timeoutSeconds< / code > 调到 15 以上再逐步收紧。< / p >
< p > < b > 预防< / b > :新策略一律 < code > Audit< / code > + < code > Ignore< / code > 上线,观察 24 小时无异常再切 < code > Deny< / code > 与 < code > Fail< / code > ;
在 CI 里用 < code > kyverno apply --policy-report< / code > 先离线跑一遍清单。< / p >
< / div >
< div class = "box danger" >
< span class = "bt" > 故障 3 · 重新构建镜像,线上没变< / span >
< p > < b > 注入< / b > :改一行业务代码,重新 < code > docker build -t localhost:5001/aurora/shop-api:v1.2.3 .< / code > 并 push,
然后 < code > kubectl rollout restart deployment/shop-api -n shop< / code > 。< / p >
< p > < b > 症状< / b > : < code > rollout status< / code > 显示成功, Pod 也确实重建了,但新 Pod 跑的仍是旧代码;
< code > kubectl get pod -n shop -o jsonpath='{.items[0].status.containerStatuses[0].imageID}'< / code > 里的 digest 没变。< / p >
< p > < b > 诊断< / b > :① 用 < code > docker inspect --format='{{index .RepoDigests 0}}'< / code > 取新镜像的 digest;
② 与 < code > kubectl get deploy shop-api -n shop -o jsonpath='{.spec.template.spec.containers[0].image}'< / code > 里的 digest 对比;
③ 两者不同即确认是「清单没换」。< / p >
< p > < b > 根因< / b > : < code > image< / code > 里写的是 digest, digest 是不变的内容哈希;重建镜像产生了新 digest,
但 Deployment 的 spec 一个字都没改,< code > rollout restart< / code > 只是重启了 Pod, 没有换镜像。< / p >
< p > < b > 修复< / b > :用新 digest 再执行一次 < code > kubectl set image< / code > ;或把 push 与 set image 写进同一条流水线命令,避免人工漏步。< / p >
< p > < b > 预防< / b > :让流水线在 push 之后自动把 digest 写回清单并提交(第 21 章的 GitOps 做法),人只审 diff, 不手抄 digest。< / p >
< / div >
< h3 id = "sec-15-9" > 15.9 生产对照< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 维度< / th > < th > 学习环境( kind) < / th > < th > 生产环境< / th > < th > 为什么< / th > < th > 迁移动作< / th > < / tr > < / thead >
< tbody >
< tr > < td > 密钥来源< / td > < td > 命令行生成的 < code > Secret< / code > ,口令在本地 < code > .env.local< / code > < / td > < td > 外部密钥库 + External Secrets Operator 或 CSI 驱动直接挂载< / td > < td > Secret 只是 base64, 能读 etcd 的人就能读它< / td > < td > 用 ESO 把 Secret 同步成 K8s 对象,业务清单不动< / td > < / tr >
< tr > < td > 工作负载身份< / td > < td > 静态用户名口令< / td > < td > IRSA / Workload Identity 之类, Pod 拿短期令牌< / td > < td > 长期凭据一旦泄漏,有效期是「永远」< / td > < td > 给 ServiceAccount 绑定身份,去掉静态 < code > regcred< / code > < / td > < / tr >
< tr > < td > 仓库留存< / td > < td > 本地 < code > registry:2< / code > , 无留存策略, tag 可覆盖< / td > < td > 开启 immutable tag、按 digest 留存、跨区域复制< / td > < td > 可覆盖的 tag 是供应链攻击最常见的入口< / td > < td > 仓库侧开启 tag 不可变;只允许按 digest 部署< / td > < / tr >
< tr > < td > 扫描与 SBOM< / td > < td > 手动 < code > trivy image< / code > ,退出码当门禁< / td > < td > CI 里强制扫描 + 生成 SBOM 存证,按周期重扫已部署镜像< / td > < td > 镜像会「变旧」:今天干净不等于下周干净< / td > < td > 把扫描前移到流水线,用第 21 章的流水线改造承接< / td > < / tr >
< tr > < td > 准入策略< / td > < td > 一条 < code > ImageValidatingPolicy< / code > ,直接 < code > Deny< / code > < / td > < td > < code > Audit< / code > 灰度 → 例外清单 → < code > Deny< / code > ,配套 PolicyException 流程< / td > < td > 策略误杀的成本远高于晚一天上线< / td > < td > 先 < code > Audit< / code > 收集违规清单,再按命名空间分批收紧< / td > < / tr >
< / tbody >
< / table >
< / div >
< p class = "take" > < b > 一句话:< / b > 生产与学习的差别不在工具,而在「谁有权改 tag、谁有权改策略、改了谁审批」。< / p >
< h3 id = "sec-15-10" > 15.10 清理与残留检查< / h3 >
< pre data-lang = "bash" data-name = "第 16 章读完之后再执行" > # env: all
# 本章结束时不要跑这一段:第 16 章要在「镜像按 digest 拉、策略已生效」的状态下排错
kubectl delete deployment unsigned-demo -n shop --ignore-not-found
kubectl delete -f deploy/base/kyverno-require-signed.yaml --ignore-not-found
helm uninstall kyverno -n kyverno
kubectl delete namespace kyverno --ignore-not-found
docker rm -f aurora-registry< / pre >
< p > 本章结束只做一件事:< code > kubectl delete deployment unsigned-demo -n shop< / code > 。仓库容器、Kyverno 与策略都留着,
第 16 章还要用;要确认残留,看 < code > kubectl get ivpol< / code > 与 < code > docker ps --filter name=aurora-registry< / code > 。
要退回 tag 形式:把 image 改成 < code > aurora-registry:5000/aurora/shop-api:v1.2.3< / code > ,并把 < code > imagePullPolicy< / code > 写死为 < code > IfNotPresent< / code > 。< / p >
< h3 id = "sec-15-11" > 15.11 小结与自测< / h3 >
< p class = "take" > < b > 一句话:< / b > 能回答「跑的是哪一份」的只有 digest, tag 只是一个便利贴。< / p >
< p class = "take" > < b > 一句话:< / b > 签名、扫描、准入都绑 digest, 绑在同一个东西上才叫供应链。< / p >
< p class = "take" > < b > 一句话:< / b > 准入策略的第一版永远应该是 < code > Audit< / code > + < code > Ignore< / code > 。< / p >
< details >
< summary > 自测 3 题(点击展开答案)< / summary >
< p > < b > 1( 概念) < / b > 同一个 tag 被覆盖后,已经在跑的 Pod 会受影响吗?< / p >
< p > 不会。Pod 创建时镜像已解析成 digest, kubelet 不会因 tag 指针变化而重拉;受影响的是下一次调度或重建。
这恰好说明为什么必须写 digest: 写 tag 的集群会在某个不确定的时刻换掉内容。< / p >
< p > < b > 2( 排错) < / b > < code > ImagePullBackOff< / code > , Events 里是 < code > 401 Unauthorized< / code > ,
Pod spec 里也确实有 < code > imagePullSecrets< / code > ,下一步看什么?< / p >
< p > 看 Secret 的 < code > type< / code > 与 < code > .dockerconfigjson< / code > 里的 server 地址。type 不对时 kubelet 直接忽略它;
server 写成 < code > localhost:5001< / code > 而镜像写 < code > aurora-registry:5000< / code > 时,凭据匹配不上也等于没挂。< / p >
< p > < b > 3( 决策) < / b > 团队说「我们的镜像都在私有仓库,不需要签名」。你怎么反驳,用一句话?< / p >
< p > 私有仓库管的是谁有推送权限,管不了有权限的人推了什么;签名管的是推上去的这一份是不是流水线产出的。< / p >
< / details >
< div class = "box tip" >
< span class = "bt" > 延伸挑战( ≤40 分钟)< / span >
< p > 给 < code > shop-web< / code > 也换成 digest 形式的镜像,并把 < code > regcred< / code > 挂到 < code > shop< / code > 命名空间的
default ServiceAccount 上,再删掉 Deployment 里的 < code > imagePullSecrets< / code > ——验证新 Pod 仍能拉到镜像。
这是第 19 章「默认安全」的入口。< / p >
< / div >
< h3 id = "sec-15-12" > 15.12 衔接< / h3 >
< p > < b > 回收< / b > :第 06 章讲过 < code > stringData< / code > 会转成 < code > data< / code > 、< code > immutable< / code > 的 Secret 改不动;
本章补上那半句——Secret 不该进 Git, 能进 Git 的只有「怎么创建它」的命令。< / p >
< p > < b > 钩子< / b > :镜像有了出处、签名与闸门,但 Pod 起不来时,< code > ImagePullBackOff< / code > 、< code > FailedCreate< / code > 、
< code > CrashLoopBackOff< / code > 、探针失败、调度失败各是各的现象,排查顺序常被搞反。< a href = "16-debug.html" > 第 16 章< / a > 给出一条从上到下的定位路径。< / p >
< h3 id = "sec-15-13" > 15.13 参考与验证版本< / h3 >
< ul >
< li > < a href = "https://kubernetes.io/docs/concepts/containers/images/" > Images( digest、imagePullPolicy、imagePullSecrets) < / a > < / li >
< li > < a href = "https://kyverno.io/docs/policy-types/image-validating-policy/" > Kyverno · ImageValidatingPolicy< / a > < / li >
< li > < a href = "https://kyverno.io/docs/guides/migration-to-cel/" > Kyverno · Migrating to CEL Policies< / a > < / li >
< li > < a href = "https://docs.sigstore.dev/cosign/signing/signing_with_containers/" > Sigstore · Signing containers with cosign< / a > < / li >
< li > < a href = "https://trivy.dev/latest/docs/" > Trivy 文档< / a > < / li >
< li > < a href = "https://distribution.github.io/distribution/" > Distribution( registry:2) 文档< / a > < / li >
< / ul >
< p > 验证版本: k8s v1.36.1 / kubectl v1.36.1 / Kyverno 1.20 线( chart 3.10.x) / trivy v0.76.2 / cosign v3.1.4 / registry:2 · 最后验证: 2026-11-11< / p >
< / article >
< nav class = "pager" >
< a class = "prev" href = "14-release.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 14 章 · 发布策略:滚动、蓝绿、金丝雀< / span > < / a >
< a class = "next" href = "16-debug.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 16 章 · 故障诊断方法论:六类故障,一张诊断树< / span > < / a >
< / nav >
< p class = "foot" >
Kubernetes 专题 · S3 第 15 章 密钥与镜像供应链< br >
< a href = "index.html" > 总目录< / a > · < a href = "../../index.html" > 知识库首页< / a > · < a href = "../../SRE工程师/sre-learning-path/index.html" > SRE 学习路径< / a >
< / p >
< / main >
< / div >
< button id = "toTop" title = "回到顶部" > ↑< / button >
< script src = "assets/doc.js" > < / script >
< script src = "../../assets/back.js" data-portal-back = "knowledge/kubernetes" data-portal-label = "知识库 › Kubernetes 专题知识库" > < / script >
< / body >
< / html >