2026-11-08 15:27:37 +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 > 第 6 章 配置与密钥: ConfigMap 与 Secret · Kubernetes 专题知识库< / title >
< meta name = "description" content = "把写死在镜像里的配置搬出来:用 ConfigMap 与 Secret 注入环境变量、挂载成文件,观察热更新与 subPath 的取舍,并分清 base64 与加密。" >
< meta name = "keywords" content = "Kubernetes,ConfigMap,Secret,envFrom,subPath,immutable,stringData,热更新,kubectl,配置管理" >
< link rel = "stylesheet" href = "assets/doc.css" >
< / head >
< body >
< header class = "topbar" >
< a class = "brand" href = "index.html" > Kubernetes 专题知识库< / a >
< div class = "crumbs" > < a href = "../../index.html" > 知识库< / a > < span > /< / span > < a href = "index.html" > Kubernetes< / a > < span > /< / span > < b > 第 6 章< / b > < / div >
< button id = "tocToggle" class = "icon-btn" aria-label = "章节目录" > 目录< / button >
< button id = "themeBtn" class = "icon-btn" aria-label = "切换主题" > 主题< / button >
< / header >
< div class = "layout" id = "layout" >
< aside class = "sidebar" >
< h2 > 章节目录< / h2 >
2026-11-11 21:57:56 +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 > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 6< / i > 配置与密钥: ConfigMap 与 Secret< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-6-1" > 6.1 本章在哪:主线之前与之后< / a > < a class = "toc-a lv3" href = "#sec-6-2" > 6.2 学习目标< / a > < a class = "toc-a lv3" href = "#sec-6-3" > 6.3 场景:一个字符串要发一次版< / a > < a class = "toc-a lv3" href = "#sec-6-4" > 6.4 心智模型:复制还是投影< / a > < a class = "toc-a lv3" href = "#sec-6-5" > 6.5 概念与字段速查< / a > < a class = "toc-a lv3" href = "#sec-6-6" > 6.6 动手:从 env 到文件挂载< / a > < a class = "toc-a lv3" href = "#sec-6-7" > 6.7 验收清单< / a > < a class = "toc-a lv3" href = "#sec-6-8" > 6.8 故障实验室< / a > < a class = "toc-a lv3" href = "#sec-6-9" > 6.9 生产对照< / a > < a class = "toc-a lv3" href = "#sec-6-10" > 6.10 清理与残留检查< / a > < a class = "toc-a lv3" href = "#sec-6-11" > 6.11 小结与自测< / a > < a class = "toc-a lv3" href = "#sec-6-12" > 6.12 衔接:第 7 章要用的东西< / a > < a class = "toc-a lv3" href = "#sec-6-13" > 6.13 参考与验证版本< / a > < / div > < 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 > < a class = "toc-a" href = "15-supply.html" > < i class = "cn" > 15< / i > 密钥与镜像供应链< / a > < 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 > < span class = "toc-a todo" title = "待写" > < i class = "cn" > 23< / i > 值班与事故响应< / span > < span class = "toc-a todo" title = "待写" > < i class = "cn" > 24< / i > 成本与容量< / span > < span class = "toc-a todo" title = "待写" > < i class = "cn" > 25< / i > 生产就绪评审与下一步< / span > < / nav >
2026-11-08 15:27:37 +08:00
< div class = "side-foot" >
< p > 第 6 章 · S1< / p >
< p > < a href = "index.html" > 返回目录< / a > < / p >
< / 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 > S1 跑起来:单服务最小闭环< / span >
< span > ·< / span >
< span > 第 < b class = "pos" > 6< / b > / 26 章< / span >
< / p >
< h2 id = "ch6" > < span class = "n" > 6< / span > 配置与密钥: ConfigMap 与 Secret< / h2 >
< div class = "lead" >
< b > 30 秒速览:< / b > 上一章的三个副本跑的是同一份写死配置。本章把值搬出镜像:建一个
< code > ConfigMap< / code > 和一个 < code > Secret< / code > ,先用 < code > env< / code > 逐条注入,再换 < code > envFrom< / code > ,
最后挂成文件,亲手看一遍哪个会自己更新、哪个不会。约 120 分钟。
< / div >
< h3 id = "sec-6-1" > 6.1 本章在哪:主线之前与之后< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 项< / th > < th > 内容< / th > < / tr > < / thead >
< tbody >
< tr > < td > 主线之前< / td > < td > < code > shop-api< / code > 的 < code > APP_MODE< / code > 、< code > THEME< / code > 写在 Pod 模板的 < code > env.value< / code > 里,值随清单一起提交、一起回滚< / td > < / tr >
< tr > < td > 主线之后< / td > < td > < code > shop-api< / code > 从 < code > shop-config< / code > 与 < code > shop-secret< / code > 取值;改一个值只动配置对象,镜像不动、版本号不动< / td > < / tr >
< tr > < td > 本章产物< / td > < td > < code > deploy/base/shop-config.yaml< / code > 、< code > deploy/base/shop-secret.yaml< / code > 、改过的 < code > deploy/base/shop-api.yaml< / code > 、< code > Makefile< / code > 里的 < code > verify-ch06< / code > < / td > < / tr >
< tr > < td > 适用环境< / td > < td > ENV-A( Windows 11 + WSL2 + kind) 与 ENV-C( Ubuntu 24.04)都能跑通;差异只有两处,在 6.6 的双栏里< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 这一章只做一件事:把值从镜像里剥出来,变成能单独改、单独授权的对象。< / p >
< h3 id = "sec-6-2" > 6.2 学习目标< / h3 >
< div class = "box note" >
< span class = "bt" > 学完你能< / span >
< ul >
< li > 建出 < code > ConfigMap< / code > 与 < code > Secret< / code > 并让 Pod 用上, 判定标准: Pod 的 < code > env< / code > 里 < code > THEME< / code > 是 < code > dark< / code > 。< / li >
< li > 说清 < code > env< / code > 、< code > envFrom< / code > 、挂载文件的更新差别,判定标准:改一次能预测哪个值变。< / li >
< li > 解释 < code > subPath< / code > 为什么不热更新,判定标准:能说出 kubelet 换的是整个卷目录。< / li >
< li > 用 < code > stringData< / code > 与 < code > immutable< / code > 控制写法,判定标准:能读懂 base64 与明文互换。< / li >
< / ul >
< / div >
< h3 id = "sec-6-3" > 6.3 场景:一个字符串要发一次版< / h3 >
< p > 周三上午,运营要把活动页主题切成深色,顺手把运行模式从 < code > dev< / code > 改成 < code > prod< / code > 。两个值写死在 Dockerfile 里:< code > ENV THEME=light< / code > 。改一行的流程是改代码、打镜像、滚动,四十分钟里三十五分钟在等构建。< / p >
< p > 下午的事故更贵。数据库连不上,值班的人把镜像拉到本地 < code > docker inspect< / code > ,想看进程拿到了哪个连接串。连接串看到了,末尾连口令一起,那串口令还在 registry 里躺了三个月。< / p >
< div class = "box danger" >
< span class = "bt" > 现象< / span >
< p > 值在镜像里,镜像在 registry 里。同一个镜像没法和 prod 用两套值,除非打两个 tag; 拿到镜像的人能读出全部配置, 包括凭据。这不是权限没配好, 是打包方式的问题。< / p >
< / div >
< p > 本章把这些值搬进集群里的独立对象。< / p >
< h3 id = "sec-6-4" > 6.4 心智模型:复制还是投影< / h3 >
< figure class = "figure" >
< svg viewBox = "0 0 720 360" role = "img" aria-label = "ConfigMap 与 Secret 被 Deployment 引用, Pod 创建时把值复制进容器, env 与文件两条路径的更新行为不同" >
< defs >
< marker id = "a6" 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 = "24" y = "18" width = "190" height = "64" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "119" y = "40" text-anchor = "middle" font-size = "13" style = "fill:var(--brand-ink)" > ConfigMap< / text >
< text x = "119" y = "58" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > shop-config< / text >
< text x = "119" y = "74" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > 4 个键< / text >
< rect x = "506" y = "18" width = "190" height = "64" rx = "9" style = "fill:var(--warn-bg);stroke:var(--warn)" > < / rect >
< text x = "601" y = "40" text-anchor = "middle" font-size = "13" style = "fill:var(--warn)" > Secret< / text >
< text x = "601" y = "58" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > shop-secret< / text >
< text x = "601" y = "74" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > 1 个凭据键< / text >
< rect x = "250" y = "112" width = "220" height = "92" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "360" y = "138" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > Deployment shop-api< / text >
< text x = "360" y = "158" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > Pod 模板里只写引用< / text >
< text x = "360" y = "178" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > replicas: 3< / text >
< path d = "M119,82 L119,98 L300,98 L300,112" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6)" > < / path >
< path d = "M601,82 L601,98 L420,98 L420,112" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6)" > < / path >
< path d = "M300,204 L300,222 L114,222 L114,248" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6)" > < / path >
< path d = "M360,204 L360,248" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6)" > < / path >
< path d = "M420,204 L420,222 L606,222 L606,248" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6)" > < / path >
< text x = "196" y = "242" font-size = "10.5" style = "fill:var(--muted)" > 创建 Pod 时复制一份< / text >
< rect x = "16" y = "250" width = "196" height = "84" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "114" y = "276" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > Pod shop-api-7d9f8c< / text >
< text x = "114" y = "298" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > env: 启动时复制< / text >
< text x = "114" y = "318" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > 文件:卷挂载< / text >
< rect x = "262" y = "250" width = "196" height = "84" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "360" y = "276" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > Pod shop-api-7d9f8c< / text >
< text x = "360" y = "298" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > env: 启动时复制< / text >
< text x = "360" y = "318" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > 文件:卷挂载< / text >
< rect x = "508" y = "250" width = "196" height = "84" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "606" y = "276" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > Pod shop-api-7d9f8c< / text >
< text x = "606" y = "298" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > env: 启动时复制< / text >
< text x = "606" y = "318" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > 文件:卷挂载< / text >
< / svg >
< div class = "figcap" > < b > 图 1 ·< / b > 引用写在 Pod 模板里,值在 Pod 创建时进入容器;两条路径的更新行为不同。< / div >
< / figure >
< p > 见图 1。< code > ConfigMap< / code > 与 < code > Secret< / code > 是集群里的独立对象, Pod 模板只保存引用。kubelet 创建 Pod 时把值交给容器运行时,容器起来后这条通道就断了。所以改配置有两件事:改对象的值,让跑着的容器重新拿一次值——第二件不会自动发生。< / p >
< p > 三条箭头是三条注入路径:< code > env< / code > 精确到一个键,< code > envFrom< / code > 整份注入, 卷挂载把键变成文件。前两条是复制, 第三条是投影, kubelet 用符号链接指向最新内容。像贴在门口的配置单:抄到笔记本上的那份不会跟着改。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 常见说法< / th > < th > 实际< / th > < th > 正确做法< / th > < / tr > < / thead >
< tbody >
< tr > < td > 改了 ConfigMap, Pod 里的环境变量会跟着变< / td > < td > 不会。值在容器启动那一刻就固定了,进程读到的是一份拷贝< / td > < td > 改完 < code > kubectl rollout restart deployment/shop-api -n shop< / code > ,或让应用自己监听文件< / td > < / tr >
< tr > < td > Secret 是加密的,可以直接进 Git< / td > < td > base64 是编码不是加密,< code > kubectl get secret -o yaml< / code > 解一下就是明文< / td > < td > 仓库里用 Sealed Secrets 或外部密钥管理,见第 15 章< / td > < / tr >
< tr > < td > 挂载了 ConfigMap, 镜像里原目录的文件还在< / td > < td > 挂载点会把镜像里同路径的整个目录盖掉,原来那几个文件看不见了< / td > < td > 挂到空目录,或用 < code > subPath< / code > 只挂一个文件< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-6-5" > 6.5 概念与字段速查< / h3 >
< p > 这张表当字典用,字段名精确到 < code > spec< / code > 层级;默认一列决定你不写时会发生什么。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 字段 / 概念< / th > < th > 作用< / th > < th > 默认< / th > < th > 坑< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > data< / code > < / td > < td > 键值对,值必须是字符串< / td > < td > 必填< / td > < td > < code > true< / code > 不加引号会被 YAML 解析成布尔值,< code > apply< / code > 直接报错< / td > < / tr >
< tr > < td > < code > stringData< / code > < / td > < td > 只在 Secret 上有, 写明文, API server 收到后合并进 < code > data< / code > < / td > < td > 无< / td > < td > 同一个键两处都写,< code > stringData< / code > 赢< / td > < / tr >
< tr > < td > < code > binaryData< / code > < / td > < td > 放二进制内容,键对应文件名< / td > < td > 无< / td > < td > 会被 base64 编码后存储,挂载时还原< / td > < / tr >
< tr > < td > < code > type< / code > < / td > < td > Secret 的分类标记,决定校验哪些键< / td > < td > < code > Opaque< / code > < / td > < td > 它不加密任何东西,写错只是校验不通过< / td > < / tr >
< tr > < td > < code > immutable< / code > < / td > < td > 置 true 后对象内容不可再改,只能删掉重建< / td > < td > < code > false< / code > < / td > < td > 打开之后 < code > kubectl apply< / code > 也会被拒< / td > < / tr >
< tr > < td > < code > env[].valueFrom.configMapKeyRef< / code > < / td > < td > 把一个键注入成一个环境变量< / td > < td > 无< / td > < td > 键不存在时 Pod 卡在 < code > CreateContainerConfigError< / code > < / td > < / tr >
< tr > < td > < code > envFrom[].configMapRef< / code > < / td > < td > 整份注入,键名直接成为变量名< / td > < td > 无< / td > < td > 键名不是合法变量名时被静默跳过< / td > < / tr >
< tr > < td > < code > volumes[].configMap< / code > < / td > < td > 把对象挂成目录,每个键一个文件< / td > < td > 无< / td > < td > 不写 < code > items< / code > 就全量投影,< code > APP_MODE< / code > 也会变成一个文件< / td > < / tr >
< tr > < td > < code > volumeMounts[].subPath< / code > < / td > < td > 只挂卷里的某一个文件或子目录到指定路径< / td > < td > 无< / td > < td > 这条路径从此不再跟随更新< / td > < / tr >
< tr > < td > < code > optional< / code > < / td > < td > 允许引用的对象或键不存在< / td > < td > < code > false< / code > < / td > < td > 置 true 后引用错了不报错,只会静默少一个值< / td > < / tr >
< / tbody >
< / table >
< / div >
< details >
< summary > 可跳过 · Secret 的四种 type 各校验什么< / summary >
< p > < code > type< / code > 只决定 API server 检查哪些键,不改变存储方式。< code > Opaque< / code > 什么都不校验;< code > kubernetes.io/tls< / code > 要求 < code > tls.crt< / code > 与 < code > tls.key< / code > 同时存在;< code > kubernetes.io/dockerconfigjson< / code > 要求值能解析成合法 JSON。< / p >
< p > 写错类型不会更安全,只会让 < code > apply< / code > 多一步校验失败。< / p >
< / details >
< h3 id = "sec-6-6" > 6.6 动手:从 env 到文件挂载< / h3 >
< p > 八步:确认现场、建两个对象、三种注入方式、一次热更新观测、一个负向演练,最后提交。< / p >
< h4 > Step 1 · 校验上一章状态< / h4 >
< p > < span class = "tag" > 意图< / span > 确认 3 副本、2 副本、两个 Service 都在。< / p >
< pre data-lang = "bash" data-name = "校验第 5 章终态" >
# env: all
cd ~/work/aurora-shop
make verify-ch05
kubectl get deploy,svc -n shop< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
[ok] service shop-api clusterip 8080
[ok] endpointslice shop-api has 3 ready addresses
[ok] service shop-api-headless has no vip
[ok] headless returns 3 A records
[ok] deployment shop-web ready 2/2
[ok] curl shop-api:8080/healthz from shop-web is ok
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/shop-api 3/3 3 3 2d4h
deployment.apps/shop-web 2/2 2 2 2d4h
NAME TYPE CLUSTER-IP PORT(S) AGE
service/shop-api ClusterIP 10.96.45.12 8080/TCP 2d4h
service/shop-api-headless ClusterIP None 8080/TCP 2d4h< / pre >
< p > < b > 如果不一样< / b > :先回第 5 章把 < code > verify-ch05< / code > 跑到全绿,半截现场会让每个现象都被误读。< / p >
< h4 > Step 2 · 建 ConfigMap 与 Secret< / h4 >
< p > < span class = "tag" > 意图< / span > 把四个值搬进两个独立对象,让镜像一无所知。< / p >
< p > 两个对象结构几乎一样,都是 < code > data< / code > 底下一堆字符串。区别只有 < code > Secret< / code > 要先 base64, 以及默认被当敏感对象。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/shop-config.yaml" >
# file: deploy/base/shop-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: shop-config
namespace: shop
labels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
app.kubernetes.io/part-of: aurora-shop
data:
APP_MODE: "prod"
THEME: "dark"
FEATURE_NEW_CHECKOUT: "true"
config.yaml: |
theme: dark
log_level: info
feature_new_checkout: true< / pre >
< p > < code > config.yaml< / code > 这个键留给文件挂载,键名里的点可以直接当文件名。< code > true< / code > 不加引号会被 YAML 当布尔值。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/shop-secret.yaml" >
# file: deploy/base/shop-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: shop-secret
namespace: shop
labels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
app.kubernetes.io/part-of: aurora-shop
type: Opaque
stringData:
DATABASE_URL: "postgres://shop:aurora-local-dev@postgres.shop.svc.cluster.local:5432/shop?sslmode=disable"< / pre >
< p > < code > stringData< / code > 写明文, API server 收到后立刻 base64 存进 < code > data< / code > ;手写 < code > data< / code > 得自己算,改一个字符就要重算。口令是本地假口令。< / p >
< pre data-lang = "bash" data-name = "创建两个对象" >
# env: all
kubectl apply -f deploy/base/shop-config.yaml
kubectl apply -f deploy/base/shop-secret.yaml
kubectl get cm,secret -n shop< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
configmap/shop-config created
secret/shop-secret created
NAME DATA AGE
configmap/kube-root-ca.crt 1 2d4h
configmap/shop-config 4 4s
NAME TYPE DATA AGE
secret/shop-secret Opaque 1 4s< / pre >
< p > < b > 如果不一样< / b > :报命名空间不匹配就加 < code > -n shop< / code > ;报 < code > got "boolean"< / code > 就给 < code > true< / code > 加引号。< / p >
< p > 手算 base64 时,两个环境都容易多带一个换行。< / p >
< div class = "grid2" >
< div class = "box note" >
< h4 > Windows 11 + WSL2< / h4 >
< pre data-lang = "powershell" data-name = "PowerShell 生成 base64" >
# env: windows-ps
$s = 'postgres://shop:aurora-local-dev@postgres.shop.svc.cluster.local:5432/shop?sslmode=disable'
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($s))< / pre >
< p > < code > GetBytes< / code > 不补换行;< code > Get-Content< / code > 读文件时每行都带 CRLF。< / p >
< / div >
< div class = "box note" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< pre data-lang = "bash" data-name = "GNU coreutils 生成 base64" >
# env: linux
printf '%s' 'postgres://shop:aurora-local-dev@postgres.shop.svc.cluster.local:5432/shop?sslmode=disable' | base64 -w0< / pre >
< p > 用 < code > printf '%s'< / code > 而非 < code > echo< / code > : < code > echo< / code > 会补 < code > \n< / code > , < code > -w0< / code > 关掉折行。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > :算法一样,差别在输入是否多一个换行。< code > echo< / code > 与 < code > Get-Content< / code > 都会补,多一个字节照样解码成功,只是值悄悄变了。< / p >
< p > 除了 YAML, < code > kubectl create secret< / code > 还有三种建法。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 写法< / th > < th > 键从哪来< / th > < th > 适合< / th > < th > 坑< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > --from-literal=THEME=dark< / code > < / td > < td > 命令行< / td > < td > 一两个键的临时对象< / td > < td > 值里有空格或引号,得靠 shell 引号自己保护< / td > < / tr >
< tr > < td > < code > --from-file=deploy/base/config.yaml< / code > < / td > < td > 文件名< / td > < td > 整份配置文件< / td > < td > 键名就是文件名,< code > config.yaml< / code > 的键带点< / td > < / tr >
< tr > < td > < code > --from-file=config.yaml=deploy/base/config.yaml< / code > < / td > < td > < code > =< / code > 左边< / td > < td > 想换个键名< / td > < td > 两边都写,改文件名容易忘了改键名< / td > < / tr >
< tr > < td > < code > --from-env-file=deploy/base/shop.env< / code > < / td > < td > 文件里的 < code > KEY=< / code > < / td > < td > 一堆键,从 < code > .env< / code > 迁过来< / td > < td > 注释和空行跳过, CRLF 会留在值里< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 最后一种最容易出事:它把一个 Windows 侧的文件交给 Linux 侧的解析器。< / p >
< div class = "grid2" >
< div class = "box note" >
< h4 > Windows 11 + WSL2< / h4 >
< pre data-lang = "powershell" data-name = "看行尾字节" >
# env: windows-ps
Format-Hex -Path C:\Users\me\Desktop\shop.env | Select-Object -First 2< / pre >
< p > 出现 < code > 0D 0A< / code > 就是 CRLF; 从桌面拷进 < code > /mnt/c< / code > 再 < code > cp< / code > 进仓库,行尾原样保留。< / p >
< / div >
< div class = "box note" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< pre data-lang = "bash" data-name = "确认并修掉 CRLF" >
# env: all
file ~/work/aurora-shop/deploy/base/shop.env
sed -i 's/\r$//' ~/work/aurora-shop/deploy/base/shop.env
file ~/work/aurora-shop/deploy/base/shop.env< / pre >
< p > < code > file< / code > 先报 < code > with CRLF line terminators< / code > ,跑完 < code > sed< / code > 只剩 < code > ASCII text< / code > 。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > : < code > --from-env-file< / code > 只按 < code > =< / code > 切分,不剥 < code > \r< / code > , < code > DATABASE_URL< / code > 末尾多一个回车,应用解析 URI 时直接报错,终端里看却完全正常。< / p >
< h4 > Step 3 · 先用 env 一条一条注入< / h4 >
< p > < span class = "tag" > 意图< / span > 用最笨的方式接上,确认值确实来自对象。< / p >
< p > < code > env< / code > 精确到一个键,把原来写死的 < code > env.value< / code > 换成 < code > valueFrom< / code > 。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/shop-api.yaml( 节选) " >
# file: deploy/base/shop-api.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: shop-api
namespace: shop
labels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
app.kubernetes.io/part-of: aurora-shop
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
template:
metadata:
labels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
app.kubernetes.io/version: v1.2.3
app.kubernetes.io/part-of: aurora-shop
spec:
containers:
- name: shop-api
image: registry.local/aurora/shop-api:v1.2.3
ports:
- name: http
containerPort: 8080
env:
- name: APP_MODE
valueFrom:
configMapKeyRef:
name: shop-config
key: APP_MODE
- name: THEME
valueFrom:
configMapKeyRef:
name: shop-config
key: THEME
- name: FEATURE_NEW_CHECKOUT
valueFrom:
configMapKeyRef:
name: shop-config
key: FEATURE_NEW_CHECKOUT
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: shop-secret
key: DATABASE_URL< / pre >
< pre data-lang = "bash" data-name = "应用并验证注入" >
# env: all
kubectl apply -f deploy/base/shop-api.yaml
kubectl rollout status deployment/shop-api -n shop --timeout=120s
kubectl exec -n shop deploy/shop-api -- env | grep -E 'APP_MODE|THEME|FEATURE_NEW'< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
deployment.apps/shop-api configured
deployment "shop-api" successfully rolled out
APP_MODE=prod
FEATURE_NEW_CHECKOUT=true
THEME=dark< / pre >
< p > < b > 如果不一样< / b > : Pod 卡在 < code > CreateContainerConfigError< / code > ,先看 6.8 的故障 1。< / p >
< h4 > Step 4 · 换成 envFrom< / h4 >
< p > < span class = "tag" > 意图< / span > 把四段重复的 < code > valueFrom< / code > 压成两行,顺便看清整份注入的代价。< / p >
< p > 把 < code > env< / code > 整段换成 < code > envFrom< / code > ,完整文件在 Step 5。< / p >
< pre data-lang = "yaml" data-name = "替换 env 段" >
# file: deploy/base/shop-api.yaml( 用这段替换 Step 3 里的 env 段)
envFrom:
- configMapRef:
name: shop-config
- secretRef:
name: shop-secret< / pre >
< pre data-lang = "bash" data-name = "应用并检查 envFrom" >
# env: all
kubectl apply -f deploy/base/shop-api.yaml
kubectl rollout status deployment/shop-api -n shop --timeout=120s
kubectl get deploy shop-api -n shop -o jsonpath='{.spec.template.spec.containers[0].envFrom}'
echo
kubectl exec -n shop deploy/shop-api -- env | grep -c THEME< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
deployment.apps/shop-api configured
deployment "shop-api" successfully rolled out
[{"configMapRef":{"name":"shop-config"},"secretRef":{"name":"shop-secret"}}]
1< / pre >
< p > < b > 如果不一样< / b > : < code > envFrom< / code > 输出为空,说明替换时把 < code > env:< / code > 留在了原地。< / p >
< div class = "box warn" >
< span class = "bt" > 同名键会被 env 盖住< / span >
< p > < b > 现象< / b > : < code > envFrom< / code > 里 < code > THEME=dark< / code > ,容器里却是 < code > light< / code > ,改 ConfigMap 也没反应。< / p >
< p > < b > 原因< / b > : < code > env< / code > 与 < code > envFrom< / code > 同时存在时同名变量以 < code > env< / code > 为准,而它是写死的。< / p >
< p > < b > 怎么验证< / b > : < code > kubectl get deploy shop-api -n shop -o yaml | grep -A3 'env:'< / code > ,看两个字段是不是都在。< / p >
< / div >
< h4 > Step 5 · 挂载成文件< / h4 >
< p > < span class = "tag" > 意图< / span > 把 < code > config.yaml< / code > 这个键变成真实文件,为热更新观测做准备。< / p >
< p > 卷挂到 < code > /etc/shop< / code > ,选镜像里不存在的空目录,别盖应用自己的目录。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/shop-api.yaml( 完整) " >
# file: deploy/base/shop-api.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: shop-api
namespace: shop
labels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
app.kubernetes.io/part-of: aurora-shop
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
template:
metadata:
labels:
app.kubernetes.io/name: shop-api
app.kubernetes.io/instance: shop
app.kubernetes.io/version: v1.2.3
app.kubernetes.io/part-of: aurora-shop
spec:
containers:
- name: shop-api
image: registry.local/aurora/shop-api:v1.2.3
ports:
- name: http
containerPort: 8080
envFrom:
- configMapRef:
name: shop-config
- secretRef:
name: shop-secret
volumeMounts:
- name: config
mountPath: /etc/shop
readOnly: true
volumes:
- name: config
configMap:
name: shop-config< / pre >
< pre data-lang = "bash" data-name = "应用并查看挂载结果" >
# env: all
kubectl apply -f deploy/base/shop-api.yaml
kubectl rollout status deployment/shop-api -n shop --timeout=120s
kubectl exec -n shop deploy/shop-api -- ls -l /etc/shop
kubectl exec -n shop deploy/shop-api -- cat /etc/shop/config.yaml< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
deployment.apps/shop-api configured
deployment "shop-api" successfully rolled out
total 0
lrwxrwxrwx 1 root root 15 Nov 8 18:20 config.yaml -> ..data/config.yaml
lrwxrwxrwx 1 root root 13 Nov 8 18:20 APP_MODE -> ..data/APP_MODE
lrwxrwxrwx 1 root root 11 Nov 8 18:20 THEME -> ..data/THEME
lrwxrwxrwx 1 root root 23 Nov 8 18:20 FEATURE_NEW_CHECKOUT -> ..data/FEATURE_NEW_CHECKOUT
theme: dark
log_level: info
feature_new_checkout: true< / pre >
< p > < b > 如果不一样< / b > : < code > ls< / code > 报 < code > No such file< / code > ,先看 < code > describe pod< / code > 里有没有 < code > MountVolume.SetUp failed< / code > 。< / p >
< div class = "box warn" >
< span class = "bt" > 不写 items 就是全量投影< / span >
< p > < b > 现象< / b > :只想挂一个 < code > config.yaml< / code > , < code > /etc/shop< / code > 下却多出三个文件,还有两个 < code > ..< / code > 开头的符号链接。< / p >
< p > < b > 原因< / b > :卷挂载默认把 < code > data< / code > 的每个键投影成同名文件,< code > items< / code > 不写就是全量。< / p >
< p > < b > 怎么验证< / b > : < code > ls -la /etc/shop< / code > ,条目数等于键数。< / p >
< / div >
< p > 改 Pod 模板会触发滚动;改 < code > ConfigMap< / code > 不会。< / p >
< h4 > Step 6 · 改 ConfigMap, 看文件自己变< / h4 >
< p > < span class = "tag" > 意图< / span > 确认卷挂载会跟着变、环境变量不会。< / p >
< p > 改 < code > config.yaml< / code > 里的 < code > log_level< / code > ,别动 < code > THEME< / code > ,它要留在终态里。< / p >
< pre data-lang = "bash" data-name = "patch 配置对象" >
# env: all
kubectl patch configmap shop-config -n shop --type merge \
-p '{"data":{"config.yaml":"theme: dark\nlog_level: debug\nfeature_new_checkout: true\n"}}'
kubectl exec -n shop deploy/shop-api -- cat /etc/shop/config.yaml< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
configmap/shop-config patched
theme: dark
log_level: info
feature_new_checkout: true< / pre >
< p > 同一秒读到的还是 < code > info< / code > ,等一轮同步,最多 90 秒。< / p >
< pre data-lang = "bash" data-name = "等两轮同步后再读" >
# env: all
sleep 70
kubectl exec -n shop deploy/shop-api -- cat /etc/shop/config.yaml
kubectl exec -n shop deploy/shop-api -- env | grep LOG_LEVEL || echo "env 里没有 LOG_LEVEL"
kubectl exec -n shop deploy/shop-api -- ls -l /etc/shop/config.yaml< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
theme: dark
log_level: debug
feature_new_checkout: true
env 里没有 LOG_LEVEL
lrwxrwxrwx 1 root root 15 Nov 8 18:27 config.yaml -> ..data/config.yaml< / pre >
< p > < b > 如果不一样< / b > :等两分钟还是 < code > info< / code > ,先看卷上有没有 < code > subPath< / code > 。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 300" role = "img" aria-label = "kubelet 同步周期到点后写入新目录,再原子切换 data 符号链接; subPath 在容器启动时复制一次,之后不再跟随" >
< defs >
< marker id = "a6b" 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 >
< marker id = "a6c" markerWidth = "9" markerHeight = "9" refX = "8" refY = "4.5" orient = "auto" >
< path d = "M0,0 L9,4.5 L0,9 z" style = "fill:var(--danger)" > < / path >
< / marker >
< / defs >
< rect x = "16" y = "44" width = "140" height = "78" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "86" y = "72" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 18:02< / text >
< text x = "86" y = "94" text-anchor = "middle" font-size = "10.5" style = "fill:var(--ink)" > 改 ConfigMap< / text >
< text x = "86" y = "112" text-anchor = "middle" font-size = "10" style = "fill:var(--muted)" > 写进 etcd< / text >
< rect x = "200" y = "44" width = "140" height = "78" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "270" y = "72" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > ≤ 60 秒< / text >
< text x = "270" y = "94" text-anchor = "middle" font-size = "10.5" style = "fill:var(--ink)" > kubelet 同步< / text >
< text x = "270" y = "112" text-anchor = "middle" font-size = "10" style = "fill:var(--muted)" > 周期触发< / text >
< rect x = "384" y = "44" width = "140" height = "78" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "454" y = "72" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 新目录< / text >
< text x = "454" y = "94" text-anchor = "middle" font-size = "10.5" style = "fill:var(--ink)" > 2026_11_08_x< / text >
< text x = "454" y = "112" text-anchor = "middle" font-size = "10" style = "fill:var(--muted)" > 整份写入< / text >
< rect x = "568" y = "44" width = "140" height = "78" rx = "9" style = "fill:var(--ok-bg);stroke:var(--ok-line)" > < / rect >
< text x = "638" y = "72" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ok)" > 原子切换< / text >
< text x = "638" y = "94" text-anchor = "middle" font-size = "10.5" style = "fill:var(--ink)" > ..data 链接< / text >
< text x = "638" y = "112" text-anchor = "middle" font-size = "10" style = "fill:var(--muted)" > 容器立即可读< / text >
< path d = "M156,83 L196,83" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6b)" > < / path >
< path d = "M340,83 L380,83" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6b)" > < / path >
< path d = "M524,83 L564,83" style = "stroke:var(--muted);fill:none" marker-end = "url(#a6b)" > < / path >
< path d = "M86,122 L86,224 L196,224" style = "stroke:var(--danger);fill:none;stroke-dasharray:5 4" marker-end = "url(#a6c)" > < / path >
< rect x = "200" y = "186" width = "340" height = "76" rx = "9" style = "fill:var(--danger-bg);stroke:var(--danger-line)" > < / rect >
< text x = "216" y = "212" font-size = "10.5" style = "fill:var(--danger)" > 容器启动时: subPath 把内容复制进挂载点< / text >
< text x = "216" y = "234" font-size = "10.5" style = "fill:var(--ink)" > 之后 kubelet 再换 ..data, 它也不跟随< / text >
< text x = "350" y = "284" text-anchor = "middle" font-size = "10.5" style = "fill:var(--muted)" > 这条分支只发生一次< / text >
< / svg >
< div class = "figcap" > < b > 图 2 ·< / b > 卷的更新是「写新目录再换链接」;< code > subPath< / code > 把内容复制走,从此与链接无关。< / div >
< / figure >
< p > 见图 2。kubelet 不在原地改文件:新内容写进时间戳目录,再把 < code > ..data< / code > 链接指过去。< / p >
< p > 环境变量在容器创建时写进初始环境,之后没有机制回头改。< / p >
< p class = "take" > < b > 一句话:< / b > 挂载的文件是活的,环境变量是死的;要让它活,得让进程自己重新读一遍。< / p >
< h4 > Step 7 · 负向: subPath 挂载的文件不更新< / h4 >
< p > < span class = "tag" > 意图< / span > 踩一次真坑:加 < code > subPath< / code > 把文件挂到指定路径,热更新从此失效。< / p >
< p > 很多场景不想要 < code > /etc/shop< / code > 这一层,于是加 < code > subPath< / code > 直接挂成 < code > /etc/shop/config.yaml< / code > 。这一步只改集群对象,演练完还原。< / p >
< pre data-lang = "bash" data-name = "改成 subPath 挂载" >
# env: all
# 影响: 3 个 Pod 滚动重建,挂载点从目录变成一个文件
kubectl patch deployment shop-api -n shop --type json -p '[
{"op":"replace","path":"/spec/template/spec/containers/0/volumeMounts/0/mountPath","value":"/etc/shop/config.yaml"},
{"op":"add","path":"/spec/template/spec/containers/0/volumeMounts/0/subPath","value":"config.yaml"}
]'
kubectl rollout status deployment/shop-api -n shop --timeout=120s
kubectl exec -n shop deploy/shop-api -- cat /etc/shop/config.yaml< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
deployment.apps/shop-api patched
deployment "shop-api" successfully rolled out
theme: dark
log_level: debug
feature_new_checkout: true< / pre >
< p > 现在把 < code > log_level< / code > 改成 < code > warn< / code > ,等两分钟。< / p >
< pre data-lang = "bash" data-name = "改配置后再等两分钟" >
# env: all
kubectl patch configmap shop-config -n shop --type merge \
-p '{"data":{"config.yaml":"theme: dark\nlog_level: warn\nfeature_new_checkout: true\n"}}'
sleep 120
kubectl exec -n shop deploy/shop-api -- cat /etc/shop/config.yaml
kubectl exec -n shop deploy/shop-api -- ls -l /etc/shop/< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
configmap/shop-config patched
theme: dark
log_level: debug
feature_new_checkout: true
-rw-r--r-- 1 root root 57 Nov 8 18:31 /etc/shop/config.yaml< / pre >
< p > < b > 如果不一样< / b > :文件变成 < code > warn< / code > ,说明 < code > subPath< / code > 没加上去。< / p >
< div class = "box warn" >
< span class = "bt" > subPath 的文件永不更新< / span >
< p > < b > 现象< / b > : ConfigMap 里已是 < code > warn< / code > ,容器里的文件还是 < code > debug< / code > ,等多久都不变。< / p >
< p > < b > 原因< / b > : < code > subPath< / code > 在容器启动时把内容复制走,之后卷的原子替换只换 < code > ..data< / code > 链接,挂载点指向的却是实体文件。< / p >
< p > < b > 怎么验证< / b > : < code > ls -l< / code > 看它是普通文件还是符号链接,< code > inode< / code > 不随时间变。< / p >
< / div >
< p > 演练结束,把挂载改回去,文件在下一轮同步里变成 < code > warn< / code > ;随后把 < code > log_level< / code > 复位成 < code > info< / code > 。< / p >
< pre data-lang = "bash" data-name = "还原目录挂载" >
# env: all
# 影响:再滚动一次,挂载点恢复成目录,文件回到跟随状态
kubectl patch deployment shop-api -n shop --type json -p '[
{"op":"remove","path":"/spec/template/spec/containers/0/volumeMounts/0/subPath"},
{"op":"replace","path":"/spec/template/spec/containers/0/volumeMounts/0/mountPath","value":"/etc/shop"}
]'
kubectl rollout status deployment/shop-api -n shop --timeout=120s
kubectl patch configmap shop-config -n shop --type merge \
-p '{"data":{"config.yaml":"theme: dark\nlog_level: info\nfeature_new_checkout: true\n"}}'
git status --short< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
deployment.apps/shop-api patched
deployment "shop-api" successfully rolled out
configmap/shop-config patched
< / pre >
< p > < code > git status< / code > 干净,演练没污染仓库。< / p >
< p class = "take" > < b > 一句话:< / b > < code > subPath< / code > 是「只挂一个文件」的唯一办法,代价是这个文件从此不再跟随。< / p >
< h4 > Step 8 · immutable 与提交< / h4 >
< p > < span class = "tag" > 意图< / span > 看清不可变对象怎么用、怎么改。< / p >
< p > 在一次性对象上试,不动 < code > shop-config< / code > ,它后面还要用。< / p >
< pre data-lang = "bash" data-name = "演练 immutable" >
# env: all
kubectl create configmap shop-config-locked -n shop --from-literal=THEME=dark
kubectl patch configmap shop-config-locked -n shop --type merge -p '{"immutable":true}'
kubectl patch configmap shop-config-locked -n shop --type merge -p '{"data":{"THEME":"light"}}'
kubectl delete configmap shop-config-locked -n shop
# 影响: 只删这个演练用的对象, shop-config 与 shop-secret 不受影响< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
configmap/shop-config-locked created
configmap/shop-config-locked patched
The ConfigMap "shop-config-locked" is invalid: data: Forbidden: field is immutable when `immutable` is set
configmap "shop-config-locked" deleted< / pre >
< p > < b > 如果不一样< / b > :第二条 < code > patch< / code > 成功了,说明 < code > immutable< / code > 没写进去,它和 < code > data< / code > 平级。< / p >
< div class = "box warn" >
< span class = "bt" > 1 MiB 不是建议值< / span >
< p > < b > 现象< / b > : < code > apply< / code > 报 < code > etcdserver: request is too large< / code > ,对象进不去。< / p >
< p > < b > 原因< / b > :单个对象的硬上限是 1 MiB, 塞大 JSON、证书链或整份前端产物都会撞上。< / p >
< p > < b > 怎么验证< / b > : < code > get cm shop-config -n shop -o json | wc -c< / code > 超过 1048576 就该换对象。< / p >
< / div >
< details >
< summary > 可跳过 · 版本化配置: immutable 加名字里的序号< / summary >
< p > < code > shop-config-v7< / code > 建好不再改,< code > Deployment< / code > 改指新名字,回滚就指回 < code > v6< / code > :可追溯,但对象越攒越多。< / p >
< / details >
< p > < code > Makefile< / code > 里补一个 < code > verify-ch06< / code > ,形状与 < code > verify-ch05< / code > 一致。< / p >
< pre data-lang = "bash" data-name = "收尾验证与提交" >
# env: all
make verify-ch06
git add deploy/base/shop-config.yaml deploy/base/shop-secret.yaml deploy/base/shop-api.yaml Makefile
git commit -m "ch06: 配置与密钥搬出镜像"
git log --oneline -1< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
[ok] configmap shop-config has 4 keys
[ok] secret shop-secret decodes to a postgres:// URL
[ok] deployment references configmap and secret
[ok] env THEME=dark in running pod
[ok] mounted config.yaml matches configmap
[ok] volume refreshed within 90s, env unchanged
[main 4c1e8a7] ch06: 配置与密钥搬出镜像
4 files changed, 96 insertions(+), 21 deletions(-)
4c1e8a7 ch06: 配置与密钥搬出镜像< / pre >
< p > < b > 如果不一样< / b > : < code > volume refreshed< / code > 那条最容易假绿,它只比当前值。< / p >
< h3 id = "sec-6-7" > 6.7 验收清单< / h3 >
< p > 六条都过,才算拿到第 7 章的起点;前五条对应 6.2 的目标。< / p >
< ol class = "steps" >
< li > < code > kubectl get cm shop-config -n shop -o jsonpath='{.data}'< / code > 四个键齐全,< code > THEME< / code > 是 < code > dark< / code > 。< / li >
< li > < code > kubectl get secret shop-secret -n shop -o jsonpath='{.data.DATABASE_URL}' | base64 -d< / code > 以 < code > postgres://< / code > 开头,末尾没有多余换行。< / li >
< li > < code > kubectl exec -n shop deploy/shop-api -- env | grep THEME< / code > 输出 < code > THEME=dark< / code > 。< / li >
< li > < code > kubectl exec -n shop deploy/shop-api -- cat /etc/shop/config.yaml< / code > 第一行是 < code > theme: dark< / code > 。< / li >
< li > 改一次 < code > log_level< / code > , 90 秒内文件跟着变,< code > env< / code > 不变。< / li >
< li > < code > make verify-ch06< / code > 六行全绿,< code > git status< / code > 干净。< / li >
< / ol >
< h3 id = "sec-6-8" > 6.8 故障实验室< / h3 >
< p > 三个故障都来自真实排障,现象相似、原因不同,先按症状对号。< / p >
< div class = "box case" >
< span class = "bt" > 故障 1 · Pod 卡在 CreateContainerConfigError< / span >
< p > < b > 注入< / b > : < code > envFrom< / code > 引用的名字改成 < code > shop-cfg< / code > ,只差一个字母。< / p >
< p > < b > 症状< / b > :新 Pod 一直 < code > 0/1< / code > ,状态 < code > CreateContainerConfigError< / code > 。< / p >
< p > < b > 诊断< / b > : < code > describe pod< / code > 的 < code > Events< / code > 里只有 < code > configmap "shop-cfg" not found< / code > 。< / p >
< p > < b > 根因< / b > :容器没被创建。拼环境变量和挂载时对象不存在,这一步就断了,所以没有日志可看。< / p >
< p > < b > 修复< / b > : < code > kubectl rollout undo deployment/shop-api -n shop< / code > 。< / p >
< p > < b > 预防< / b > :把 < code > verify-ch06< / code > 放进 CI, 让机器查引用。< / p >
< / div >
< div class = "box case" >
< span class = "bt" > 故障 2 · 挂上了文件,应用读不到< / span >
< p > < b > 注入< / b > :挂载点从 < code > /etc/shop< / code > 改成应用读配置的 < code > /app/config< / code > ,那里面已有两个文件。< / p >
< p > < b > 症状< / b > : Pod 正常 < code > Running< / code > ,不报错,应用仍用默认配置。< / p >
< p > < b > 诊断< / b > : < code > exec -- ls -la /app/config< / code > ,只剩 < code > config.yaml< / code > ,镜像里那两个文件没了。< / p >
< p > < b > 根因< / b > :挂载点是目录级覆盖,镜像里同路径的目录被整个遮住,不是合并。< / p >
< p > < b > 修复< / b > :挂到空目录;要覆盖单个文件就用 < code > subPath< / code > ,并接受它不热更新。< / p >
< p > < b > 预防< / b > :挂载点永远选镜像里不存在的路径。< / p >
< / div >
< div class = "box case" >
< span class = "bt" > 故障 3 · stringData 与 data 混写,值被编码两次< / span >
< p > < b > 注入< / b > :同一个 Secret 里 < code > data< / code > 写 base64, < code > stringData< / code > 又写一串像 base64 的值。< / p >
< p > < b > 症状< / b > : < code > env | grep DATABASE_URL< / code > 拿到的是一串 base64, 连库时报 URL 非法。< / p >
< p > < b > 诊断< / b > : < code > get secret -o yaml< / code > 里是 base64 的 base64, 再解码一次还是 base64。< / p >
< p > < b > 根因< / b > : < code > stringData< / code > 的值会 base64 后覆盖同名 < code > data< / code > 键,写 base64 串等于编了两次。< / p >
< p > < b > 修复< / b > :一个键只在一处写,手写明文用 < code > stringData< / code > ,机器生成用 < code > data< / code > 。< / p >
< p > < b > 预防< / b > :验收脚本里加一条 < code > base64 -d< / code > ,解出来必须是可读的 URL。< / p >
< / div >
< h3 id = "sec-6-9" > 6.9 生产对照< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 做法< / th > < th > 本地( kind) < / th > < th > 生产< / th > < th > 为什么< / th > < / tr > < / thead >
< tbody >
< tr > < td > 配置来源< / td > < td > 手写 YAML 进 Git< / td > < td > 同样进 Git, 但按环境拆 overlay< / td > < td > 值的来源要可追溯,环境差异不该靠在集群里手改< / td > < / tr >
< tr > < td > Secret 存储< / td > < td > 明文 < code > stringData< / code > 进仓库< / td > < td > Sealed Secrets 或 External Secrets, 仓库里只有密文< / td > < td > base64 不是加密,仓库读权限等于密钥读权限< / td > < / tr >
< tr > < td > etcd 静态加密< / td > < td > 不开< / td > < td > 开 < code > EncryptionConfiguration< / code > < / td > < td > 加密的是落盘数据,防的是拿到磁盘的人< / td > < / tr >
< tr > < td > 配置更新< / td > < td > 手动 < code > patch< / code > < / td > < td > 改仓库、走流水线、自动滚动< / td > < td > 手改集群里的值,下次 apply 就被覆盖,且没人知道改过< / td > < / tr >
< tr > < td > 配置版本< / td > < td > 一个名字改到底< / td > < td > < code > immutable< / code > 加名字序号< / td > < td > 变更可追溯、可回滚,代价是对象会攒< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 本地这套唯一的优点是快, 代价是两笔债: Secret 明文进了 Git 历史,改口令也删不掉;配置和清单同一个提交,回滚会连配置一起回滚。两笔债在第 15、20 章各还一半。< / p >
< p class = "take" > < b > 一句话:< / b > Secret 的 base64 只是把明文换个写法,仓库的读权限就是密钥的读权限。< / p >
< h3 id = "sec-6-10" > 6.10 清理与残留检查< / h3 >
< p > 本章的对象第 7 章还要用, 不要删。要清的只有演练残留, Step 8 已经删了 < code > shop-config-locked< / code > 。< / p >
< pre data-lang = "bash" data-name = "检查命名空间里剩下的对象" >
# env: all
kubectl get cm,secret -n shop
kubectl get pods -n shop --field-selector=status.phase!=Running< / pre >
< pre data-lang = "text" data-name = "预期输出" >
# 预期输出
NAME DATA AGE
configmap/kube-root-ca.crt 1 2d5h
configmap/shop-config 4 48m
NAME TYPE DATA AGE
secret/shop-secret Opaque 1 48m
No resources found in shop namespace.< / pre >
< p > < code > kube-root-ca.crt< / code > 是自动注入的,不是残留;第二行没输出,说明没有非 Running 的 Pod。< / p >
< p > 要整体回退到第 5 章终态:< code > git checkout HEAD~1 -- deploy/base/shop-api.yaml< / code > ,删掉两个新文件,< code > apply< / code > 一遍,再跑 < code > make verify-ch05< / code > 。< / p >
< p class = "take" > < b > 一句话:< / b > 清理只清演练残留,主线对象留给下一章。< / p >
< h3 id = "sec-6-11" > 6.11 小结与自测< / h3 >
< p > 三件事值得带走。配置属于环境,不属于镜像;注入方式决定更新行为,< code > env< / code > 是复制, 卷是投影; Secret 只是换了写法的明文,安全性来自访问控制,不来自 base64。< / p >
< p class = "take" > < b > 一句话:< / b > 同一个镜像配不同值,才叫一份构建、多处运行。< / p >
< p class = "take" > < b > 一句话:< / b > 选 < code > env< / code > 还是文件,先问这个值改完之后,你希望它多久生效。< / p >
< details >
< summary > 自测 · 两道题< / summary >
< p > < b > 1.< / b > < code > envFrom< / code > 注入的键名带横线(如 < code > app-mode< / code > ),容器里看得到吗?< br > 看不到。环境变量名只认字母、数字和下划线,不合法的键被跳过,也不报错。< / p >
< p > < b > 2.< / b > 产品要求「改配置 5 秒内生效」,选 < code > subPath< / code > 还是滚动重启?< br > 都不选:滚动重启做不到 5 秒,< code > subPath< / code > 是永不生效。得让应用自己 watch 文件。< / p >
< / details >
< h3 id = "sec-6-12" > 6.12 衔接:第 7 章要用的东西< / h3 >
< p > 第 7 章加探针时要用到本章的两个前提:< code > /readyz< / code > 在启动后 3 秒内返 < code > 503< / code > ,所以 < code > readinessProbe< / code > 必须配 < code > initialDelaySeconds< / code > ; < code > THEME=dark< / code > 来自 ConfigMap, 改探针时别顺手把 < code > envFrom< / code > 删掉。< / p >
< p > 现在 < code > shop-api< / code > 没有任何探针。滚动更新时新 Pod 一启动就进 Endpoints, 而它头 3 秒返 < code > 503< / code > ,用户看到的是 502。< / p >
< h3 id = "sec-6-13" > 6.13 参考与验证版本< / h3 >
< ul >
< li > Kubernetes 文档:< a href = "https://kubernetes.io/docs/concepts/configuration/configmap/" > ConfigMaps< / a > 、< a href = "https://kubernetes.io/docs/concepts/configuration/secret/" > Secrets< / a > 、< a href = "https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" > Configure a Pod to Use a ConfigMap< / a > < / li >
< li > 卷更新的实现细节:< a href = "https://kubernetes.io/docs/concepts/storage/volumes/#configmap" > Volumes · configMap< / a > (符号链接与原子替换那段)< / li >
< li > 本章命令在 k8s v1.36( kind v0.30) 上验证; kubectl v1.36。< / li >
< / ul >
< p class = "foot" >
Kubernetes 专题 · S1 第 6 章 配置与密钥: ConfigMap 与 Secret< br >
< a href = "index.html" > 总目录< / a > · < a href = "../../index.html" > 知识库首页< / a > · < a href = "../../SRE工程师/sre-learning-path/index.html" > SRE 学习路径< / a >
< / p >
< nav class = "pager" >
< a class = "prev" href = "05-service.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 5 章 · Service 与集群 DNS< / span > < / a >
< a class = "next" href = "07-probe.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 7 章 · 探针与资源:让它活得像生产< / span > < / a >
< / nav >
< / article >
< / main >
< / div >
< button id = "toTop" > ↑< / button >
< script src = "assets/doc.js" > < / script >
< script src = "../../assets/back.js" data-portal-back = "knowledge/kubernetes" data-portal-label = "知识库 › Kubernetes 专题知识库" > < / script >
< / body >
< / html >