|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes v1.34: 使用 Init 容器定义应用环境变量" |
| 4 | +date: 2025-09-01 |
| 5 | +slug: kubernetes-v1-34-env-files |
| 6 | +author: > |
| 7 | + HirazawaUi |
| 8 | +translator: Michael Yao (DaoCloud) |
| 9 | +--- |
| 10 | +<!-- |
| 11 | +layout: blog |
| 12 | +title: "Kubernetes v1.34: Use An Init Container To Define App Environment Variables" |
| 13 | +date: 2025-09-01 |
| 14 | +draft: true |
| 15 | +slug: kubernetes-v1-34-env-files |
| 16 | +author: > |
| 17 | + HirazawaUi |
| 18 | +--> |
| 19 | + |
| 20 | +<!-- |
| 21 | +Kubernetes typically uses ConfigMaps and Secrets to set environment variables, |
| 22 | +which introduces additional API calls and complexity, |
| 23 | +For example, you need to separately manage the Pods of your workloads |
| 24 | +and their configurations, while ensuring orderly |
| 25 | +updates for both the configurations and the workload Pods. |
| 26 | +
|
| 27 | +Alternatively, you might be using a vendor-supplied container |
| 28 | +that requires environment variables (such as a license key or a one-time token), |
| 29 | +but you don’t want to hard-code them or mount volumes just to get the job done. |
| 30 | +--> |
| 31 | +Kubernetes 通常使用 ConfigMap 和 Secret 来设置环境变量, |
| 32 | +这会引入额外的 API 调用和复杂性。例如,你需要分别管理工作负载的 Pod 和它们的配置, |
| 33 | +同时还要确保配置和工作负载 Pod 的有序更新。 |
| 34 | + |
| 35 | +另外,你可能在使用一个供应商提供的、需要环境变量(例如许可证密钥或一次性令牌)的容器, |
| 36 | +但你又不想对这些变量进行硬编码,或者仅仅为了完成工作而挂载卷。 |
| 37 | + |
| 38 | +<!-- |
| 39 | +If that's the situation you are in, you now have a new (alpha) way to |
| 40 | +achieve that. Provided you have the `EnvFiles` |
| 41 | +[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) |
| 42 | +enabled across your cluster, you can tell the kubelet to load a container's |
| 43 | +environment variables from a volume (the volume must be part of the Pod that |
| 44 | +the container belongs to). |
| 45 | +this feature gate allows you to load environment variables directly from a file in an emptyDir volume |
| 46 | +without actually mounting that file into the container. |
| 47 | +It’s a simple yet elegant solution to some surprisingly common problems. |
| 48 | +--> |
| 49 | +如果你正面对这种情况,现在有一种新的(Alpha)方式来实现。只要你在集群中启用了 `EnvFiles` |
| 50 | +[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/), |
| 51 | +你就可以告诉 kubelet 从一个卷中加载容器的环境变量(此卷必须是容器所属的 Pod)。 |
| 52 | +这个特性门控允许你直接从 `emptyDir` 卷中的文件加载环境变量,而不需要将该文件实际挂载到容器中。 |
| 53 | +这是一个简单而优雅的解决方案,可以应对一些出乎意料的常见问题。 |
| 54 | + |
| 55 | +<!-- |
| 56 | +## What’s this all about? |
| 57 | +At its core, this feature allows you to point your container to a file, |
| 58 | +one generated by an `initContainer`, |
| 59 | +and have Kubernetes parse that file to set your environment variables. |
| 60 | +The file lives in an `emptyDir` volume (a temporary storage space that lasts as long as the pod does), |
| 61 | +Your main container doesn’t need to mount the volume. |
| 62 | +The kubelet will read the file and inject these variables when the container starts. |
| 63 | +--> |
| 64 | +## 特性概述 {#what-s-this-all-about} |
| 65 | + |
| 66 | +从核心上来说,这个特性允许你将容器指向一个文件,该文件由 `initContainer` 生成, |
| 67 | +然后让 Kubernetes 解析该文件以设置你的环境变量。此文件位于一个 `emptyDir` |
| 68 | +卷中(这是一种临时存储空间,只要 Pod 存在就会保留),你的主容器不需要挂载此卷。 |
| 69 | +kubelet 会在容器启动时读取文件并注入这些变量。 |
| 70 | + |
| 71 | +<!-- |
| 72 | +## How It Works |
| 73 | +Here's a simple example: |
| 74 | +--> |
| 75 | +## 工作原理 {#how-it-works} |
| 76 | + |
| 77 | +这里有一个简单的例子: |
| 78 | + |
| 79 | +```yaml |
| 80 | +apiVersion: v1 |
| 81 | +kind: Pod |
| 82 | +spec: |
| 83 | + initContainers: |
| 84 | + - name: generate-config |
| 85 | + image: busybox |
| 86 | + command: ['sh', '-c', 'echo "CONFIG_VAR=HELLO" > /config/config.env'] |
| 87 | + volumeMounts: |
| 88 | + - name: config-volume |
| 89 | + mountPath: /config |
| 90 | + containers: |
| 91 | + - name: app-container |
| 92 | + image: gcr.io/distroless/static |
| 93 | + env: |
| 94 | + - name: CONFIG_VAR |
| 95 | + valueFrom: |
| 96 | + fileKeyRef: |
| 97 | + path: config.env |
| 98 | + volumeName: config-volume |
| 99 | + key: CONFIG_VAR |
| 100 | + volumes: |
| 101 | + - name: config-volume |
| 102 | + emptyDir: {} |
| 103 | +``` |
| 104 | +
|
| 105 | +<!-- |
| 106 | +Using this approach is a breeze. |
| 107 | +You define your environment variables in the pod spec using the `fileKeyRef` field, |
| 108 | +which tells Kubernetes where to find the file and which key to pull. |
| 109 | +The file itself resembles the standard for .env syntax (think KEY=VALUE), |
| 110 | +and (for this alpha stage at least) you must ensure that it is written into |
| 111 | +an `emptyDir` volume. Other volume types aren't supported for this feature. |
| 112 | +At least one init container must mount that `emptyDir` volume (to write the file), |
| 113 | +but the main container doesn’t need to—it just gets the variables handed to it at startup. |
| 114 | +--> |
| 115 | +使用这种方法非常简单。你在 Pod 规约中使用 `fileKeyRef` 字段定义环境变量, |
| 116 | +此字段告诉 Kubernetes 去哪里找到文件以及要提取哪个键。 |
| 117 | +此文件本身类似于 `.env` 语法的标准格式(即 `KEY=VALUE`), |
| 118 | +并且(至少在这个 Alpha 阶段)你必须确保它被写入到一个 `emptyDir` 卷中。 |
| 119 | +其他类型的卷在此特性中不受支持。至少有一个 Init 容器必须挂载该 `emptyDir` 卷(以写入文件), |
| 120 | +但主容器不需要挂载它——它在启动时就能直接获取这些变量。 |
| 121 | + |
| 122 | +<!-- |
| 123 | +## A word on security |
| 124 | +While this feature supports handling sensitive data such as keys or tokens, |
| 125 | +note that its implementation relies on `emptyDir` volumes mounted into pod. |
| 126 | +Operators with node filesystem access could therefore |
| 127 | +easily retrieve this sensitive data through pod directory paths. |
| 128 | + |
| 129 | +If storing sensitive data like keys or tokens using this feature, |
| 130 | +ensure your cluster security policies effectively protect nodes |
| 131 | +against unauthorized access to prevent exposure of confidential information. |
| 132 | +--> |
| 133 | +## 关于安全性 {#a-word-on-security} |
| 134 | + |
| 135 | +虽然此特性支持处理密钥或令牌等敏感数据,但需要注意它的实现依赖于挂载到 Pod 的 `emptyDir` 卷。 |
| 136 | +具有节点文件系统访问权限的操作人员因此可以通过 Pod 目录路径轻易获取这些敏感数据。 |
| 137 | + |
| 138 | +如果使用此特性存储密钥或令牌等敏感数据,确保你的集群安全策略能够有效保护节点免受未经授权的访问, |
| 139 | +以防止机密信息泄露。 |
| 140 | + |
| 141 | +<!-- |
| 142 | +## Summary |
| 143 | +This feature will eliminate a number of complex workarounds used today, simplifying |
| 144 | +apps authoring, and opening doors for more use cases. Kubernetes stays flexible and |
| 145 | +open for feedback. Tell us how you use this feature or what is missing. |
| 146 | +--> |
| 147 | +## 总结 {#summary} |
| 148 | + |
| 149 | +此特性将消除如今使用的许多复杂变通方法,简化应用编写,并为更多使用场景打开大门。 |
| 150 | +Kubernetes 保持灵活性,欢迎反馈。请告诉我们你是如何使用这个特性的,或者此特性还缺少什么。 |
0 commit comments