# 使用 Kustomize 管理 Secret

> 使用 kustomization.yaml 文件创建 Secret 对象。

---

LLMS index: [llms.txt](/llms.txt)

---

<!-- 
title: Managing Secrets using Kustomize
content_type: task
weight: 30
description: Creating Secret objects using kustomization.yaml file.
-->

<!-- overview -->

<!--  
`kubectl` supports using the [Kustomize object management tool](/docs/tasks/manage-kubernetes-objects/kustomization/) to manage Secrets
and ConfigMaps. You create a *resource generator* using Kustomize, which
generates a Secret that you can apply to the API server using `kubectl`.
-->
`kubectl` 支持使用 [Kustomize 对象管理工具](/zh-cn/docs/tasks/manage-kubernetes-objects/kustomization/)来管理
Secret 和 ConfigMap。你可以使用 Kustomize 创建**资源生成器（Resource Generator）**，
该生成器会生成一个 Secret，让你能够通过 `kubectl` 应用到 API 服务器。

## 准备开始

<!--
You need to have a Kubernetes cluster, and the kubectl command-line tool must
be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a
cluster, you can create one by using
[minikube](https://minikube.sigs.k8s.io/docs/tutorials/multi_node/)
or you can use one of these Kubernetes playgrounds:
-->
<p>你必须拥有一个 Kubernetes 的集群，且必须配置 kubectl 命令行工具让其与你的集群通信。
建议运行本教程的集群至少有两个节点，且这两个节点不能作为控制平面主机。
如果你还没有集群，你可以通过 <a href="https://minikube.sigs.k8s.io/docs/tutorials/multi_node/">Minikube</a>
构建一个你自己的集群，或者你可以使用下面的 Kubernetes 练习环境之一：</p>
<!--
* [iximiuz Labs](https://labs.iximiuz.com/playgrounds?category=kubernetes&filter=all)
* [Killercoda](https://killercoda.com/playgrounds/scenario/kubernetes)
* [KodeKloud](https://kodekloud.com/public-playgrounds)
-->
<ul>
<li><a href="https://labs.iximiuz.com/playgrounds?category=kubernetes&filter=all">iximiuz Labs</a></li>
<li><a href="https://killercoda.com/playgrounds/scenario/kubernetes">Killercoda</a></li>
<li><a href="https://kodekloud.com/public-playgrounds">KodeKloud</a></li>
</ul>


<!-- steps -->

<!--
## Create a Secret

You can generate a Secret by defining a `secretGenerator` in a
`kustomization.yaml` file that references other existing files, `.env` files, or
literal values. For example, the following instructions create a kustomization
file for the username `admin` and the password `1f2d1e2e67df`.

### Create the kustomization file
-->
## 创建 Secret    {#create-a-secret}

你可以在 `kustomization.yaml` 文件中定义 `secreteGenerator` 字段，
并在定义中引用其它本地文件、`.env` 文件或文字值生成 Secret。
例如：下面的指令为用户名 `admin` 和密码 `1f2d1e2e67df` 创建 kustomization 文件。


<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
The `stringData` field for a Secret does not work well with server-side apply.
-->
<p>Secret 的 <code>stringData</code> 字段与服务端应用不兼容。</p></div>


### 创建 kustomization 文件   {#create-the-kustomization-file}

<ul class="nav nav-tabs" id="tabs-secret-data" role="tablist"><li class="nav-item"><a data-bs-toggle="tab" class="nav-link active" href="#tabs-secret-data-0" role="tab" aria-controls="tabs-secret-data-0" aria-selected="true">文字</a></li>
	  
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-secret-data-1" role="tab" aria-controls="tabs-secret-data-1">文件</a></li>
		<li class="nav-item"><a data-bs-toggle="tab" class="nav-link" href="#tabs-secret-data-2" role="tab" aria-controls="tabs-secret-data-2">.env 文件</a></li></ul>

<div class="tab-content" id="tabs-secret-data-content"><div class="tab-body tab-pane fadeshow active"
        id="tabs-secret-data-0" role="tabpanel" aria-labelledby="tabs-secret-data-0-tab" tabindex="secret-data"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nt">secretGenerator</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl">- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">database-creds</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">literals</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">username=admin</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">password=1f2d1e2e67df</span><span class="w">
</span></span></span></code></pre></div></div><div class="tab-body tab-pane fade"
        id="tabs-secret-data-1" role="tabpanel" aria-labelledby="tabs-secret-data-1-tab" tabindex="secret-data"><!--
1.  Store the credentials in files. The filenames are the keys of the secret:
-->
<ol>
<li>
<p>将凭据存储在文件中。文件名是 Secret 的 key 值：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> -n <span class="s1">&#39;admin&#39;</span> &gt; ./username.txt
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> -n <span class="s1">&#39;1f2d1e2e67df&#39;</span> &gt; ./password.txt
</span></span></code></pre></div><!--
The `-n` flag ensures that there's no newline character at the end of your
files.
-->
<p><code>-n</code> 标志确保文件结尾处没有换行符。</p>
</li>
</ol>
<!--
1.  Create the `kustomization.yaml` file:
-->
<ol start="2">
<li>
<p>创建 <code>kustomization.yaml</code> 文件：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">secretGenerator</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl">- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">database-creds</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">files</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">username.txt</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">password.txt</span><span class="w">
</span></span></span></code></pre></div></li>
</ol>
</div><div class="tab-body tab-pane fade"
        id="tabs-secret-data-2" role="tabpanel" aria-labelledby="tabs-secret-data-2-tab" tabindex="secret-data"><!-- 
You can also define the secretGenerator in the `kustomization.yaml` file by
providing `.env` files. For example, the following `kustomization.yaml` file
pulls in data from an `.env.secret` file:
-->
<p>你也可以使用 <code>.env</code> 文件在 <code>kustomization.yaml</code> 中定义 <code>secretGenerator</code>。
例如下面的 <code>kustomization.yaml</code> 文件从 <code>.env.secret</code> 文件获取数据：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">secretGenerator</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl">- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">db-user-pass</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">envs</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">.env.secret</span><span class="w">
</span></span></span></code></pre></div></div></div>


<!--
In all cases, you don't need to encode the values in base64. The name of the YAML
file **must** be `kustomization.yaml` or `kustomization.yml`.
-->
在所有情况下，你都不需要对取值作 base64 编码。
YAML 文件的名称**必须**是 `kustomization.yaml` 或 `kustomization.yml`。

<!--
### Apply the kustomization file

To create the Secret, apply the directory that contains the kustomization file:
-->
### 应用 kustomization 文件   {#apply-the-kustomization-file}

若要创建 Secret，应用包含 kustomization 文件的目录。

```shell
kubectl apply -k <目录路径>
```

<!--
The output is similar to:
-->
输出类似于：

```
secret/database-creds-5hdh7hhgfk created
```

<!--
When a Secret is generated, the Secret name is created by hashing
the Secret data and appending the hash value to the name. This ensures that
a new Secret is generated each time the data is modified.

To verify that the Secret was created and to decode the Secret data,

```shell
kubectl get -k <directory-path> -o jsonpath='{.data}' 
```
-->
生成 Secret 时，Secret 的名称最终是由 `name` 字段和数据的哈希值拼接而成。
这将保证每次修改数据时生成一个新的 Secret。

要验证 Secret 是否已创建并解码 Secret 数据，

```shell
kubectl get -k <目录路径> -o jsonpath='{.data}' 
```

<!--
The output is similar to:
-->
输出类似于：

```
{ "password": "MWYyZDFlMmU2N2Rm", "username": "YWRtaW4=" }
```

```
echo 'MWYyZDFlMmU2N2Rm' | base64 --decode
```

<!--
The output is similar to:
-->
输出类似于：

```
1f2d1e2e67df
```

<!--
For more information, refer to
[Managing Secrets using kubectl](/docs/tasks/configmap-secret/managing-secret-using-kubectl/#verify-the-secret) and
[Declarative Management of Kubernetes Objects Using Kustomize](/docs/tasks/manage-kubernetes-objects/kustomization/).
-->
更多信息参阅
[使用 kubectl 管理 Secret](/zh-cn/docs/tasks/configmap-secret/managing-secret-using-kubectl/#verify-the-secret)和
[使用 Kustomize 对 Kubernetes 对象进行声明式管理](/zh-cn/docs/tasks/manage-kubernetes-objects/kustomization/)

<!--
## Edit a Secret {#edit-secret}

1.  In your `kustomization.yaml` file, modify the data, such as the `password`.
1.  Apply the directory that contains the kustomization file:

    ```shell
    kubectl apply -k <directory-path>
    ```
-->
## 编辑 Secret {#edit-secret}

1. 在 `kustomization.yaml` 文件中，修改诸如 `password` 等数据。
1. 应用包含 kustomization 文件的目录：

   ```shell
   kubectl apply -k <目录路径>
   ```

   <!--
   The output is similar to:
   -->

   输出类似于：

   ```
   secret/db-user-pass-6f24b56cc8 created
   ```

<!--
The edited Secret is created as a new `Secret` object, instead of updating the
existing `Secret` object. You might need to update references to the Secret in
your Pods.
-->
编辑过的 Secret 被创建为一个新的 `Secret` 对象，而不是更新现有的 `Secret` 对象。
你可能需要在 Pod 中更新对该 Secret 的引用。

<!--
## Clean up

To delete a Secret, use `kubectl`:
-->
## 清理   {#clean-up}

要删除 Secret，请使用 `kubectl`：

```shell
kubectl delete secret db-user-pass
```

## 接下来

<!-- 
- Read more about the [Secret concept](/docs/concepts/configuration/secret/)
- Learn how to [manage Secrets using kubectl](/docs/tasks/configmap-secret/managing-secret-using-kubectl/)
- Learn how to [manage Secrets using config file](/docs/tasks/configmap-secret/managing-secret-using-config-file/)
-->
- 进一步阅读 [Secret 概念](/zh-cn/docs/concepts/configuration/secret/)
- 了解如何[使用 kubectl 管理 Secret](/zh-cn/docs/tasks/configmap-secret/managing-secret-using-kubectl/)
- 了解如何[使用配置文件管理 Secret](/zh-cn/docs/tasks/configmap-secret/managing-secret-using-config-file/)
