Skip to content

Commit 27daefa

Browse files
authored
Merge pull request #34853 from shannonxtreme/manage-secret-kustomize
Add create and edit to kustomize steps
2 parents d942d08 + ce1e4b1 commit 27daefa

File tree

1 file changed

+65
-77
lines changed

1 file changed

+65
-77
lines changed

content/en/docs/tasks/configmap-secret/managing-secret-using-kustomize.md

Lines changed: 65 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,134 +7,122 @@ description: Creating Secret objects using kustomization.yaml file.
77

88
<!-- overview -->
99

10-
Since Kubernetes v1.14, `kubectl` supports
11-
[managing objects using Kustomize](/docs/tasks/manage-kubernetes-objects/kustomization/).
12-
Kustomize provides resource Generators to create Secrets and ConfigMaps. The
13-
Kustomize generators should be specified in a `kustomization.yaml` file inside
14-
a directory. After generating the Secret, you can create the Secret on the API
15-
server with `kubectl apply`.
10+
`kubectl` supports using the [Kustomize object management tool](/docs/tasks/manage-kubernetes-objects/kustomization/) to manage Secrets
11+
and ConfigMaps. You create a *resource generator* using Kustomize, which
12+
generates a Secret that you can apply to the API server using `kubectl`.
1613

1714
## {{% heading "prerequisites" %}}
1815

1916
{{< include "task-tutorial-prereqs.md" >}}
2017

2118
<!-- steps -->
2219

23-
## Create the Kustomization file
20+
## Create a Secret
2421

2522
You can generate a Secret by defining a `secretGenerator` in a
26-
`kustomization.yaml` file that references other existing files.
27-
For example, the following kustomization file references the
28-
`./username.txt` and the `./password.txt` files:
23+
`kustomization.yaml` file that references other existing files, `.env` files, or
24+
literal values. For example, the following instructions create a Kustomization
25+
file for the username `admin` and the password `1f2d1e2e67df`.
2926

30-
```yaml
31-
secretGenerator:
32-
- name: db-user-pass
33-
files:
34-
- username.txt
35-
- password.txt
36-
```
27+
### Create the Kustomization file
3728

38-
You can also define the `secretGenerator` in the `kustomization.yaml`
39-
file by providing some literals.
40-
For example, the following `kustomization.yaml` file contains two literals
41-
for `username` and `password` respectively:
42-
43-
```yaml
29+
{{< tabs name="Secret data" >}}
30+
{{< tab name="Literals" codelang="yaml" >}}
4431
secretGenerator:
45-
- name: db-user-pass
32+
- name: database-creds
4633
literals:
4734
- username=admin
4835
- password=1f2d1e2e67df
49-
```
50-
51-
You can also define the `secretGenerator` in the `kustomization.yaml`
52-
file by providing `.env` files.
53-
For example, the following `kustomization.yaml` file pulls in data from
54-
`.env.secret` file:
36+
{{< /tab >}}
37+
{{% tab name="Files" %}}
38+
1. Store the credentials in files with the values encoded in base64:
39+
40+
```shell
41+
echo -n 'admin' > ./username.txt
42+
echo -n '1f2d1e2e67df' > ./password.txt
43+
```
44+
The `-n` flag ensures that there's no newline character at the end of your
45+
files.
46+
47+
1. Create the `kustomization.yaml` file:
48+
49+
```yaml
50+
secretGenerator:
51+
- name: database-creds
52+
files:
53+
- username.txt
54+
- password.txt
55+
```
56+
{{% /tab %}}}
57+
{{% tab name=".env files" %}}
58+
You can also define the secretGenerator in the `kustomization.yaml` file by
59+
providing `.env` files. For example, the following `kustomization.yaml` file
60+
pulls in data from an `.env.secret` file:
5561
5662
```yaml
5763
secretGenerator:
5864
- name: db-user-pass
5965
envs:
6066
- .env.secret
6167
```
68+
{{% /tab %}}
69+
{{< /tabs >}}
6270
63-
Note that in all cases, you don't need to base64 encode the values.
71+
In all cases, you don't need to base64 encode the values. The name of the YAML
72+
file **must** be `kustomization.yaml` or `kustomization.yml`.
6473

65-
## Create the Secret
74+
### Apply the kustomization file
6675

67-
Apply the directory containing the `kustomization.yaml` to create the Secret.
76+
To create the Secret, apply the directory that contains the kustomization file:
6877

6978
```shell
70-
kubectl apply -k .
79+
kubectl apply -k <directory-path>
7180
```
7281

7382
The output is similar to:
7483

7584
```
76-
secret/db-user-pass-96mffmfh4k created
85+
secret/database-creds-5hdh7hhgfk created
7786
```
7887
79-
Note that when a Secret is generated, the Secret name is created by hashing
88+
When a Secret is generated, the Secret name is created by hashing
8089
the Secret data and appending the hash value to the name. This ensures that
8190
a new Secret is generated each time the data is modified.
8291
83-
## Check the Secret created
92+
To verify that the Secret was created and to decode the Secret data, refer to
93+
[Managing Secrets using
94+
kubectl](/docs/tasks/configmap-secret/managing-secret-using-kubectl/#verify-the-secret).
8495
85-
You can check that the secret was created:
96+
## Edit a Secret {#edit-secret}
8697
87-
```shell
88-
kubectl get secrets
89-
```
98+
1. In your `kustomization.yaml` file, modify the data, such as the `password`.
99+
1. Apply the directory that contains the kustomization file:
90100
91-
The output is similar to:
101+
```shell
102+
kubectl apply -k <directory-path>
103+
```
92104
93-
```
94-
NAME TYPE DATA AGE
95-
db-user-pass-96mffmfh4k Opaque 2 51s
96-
```
97-
98-
You can view a description of the secret:
99-
100-
```shell
101-
kubectl describe secrets/db-user-pass-96mffmfh4k
102-
```
103-
104-
The output is similar to:
105-
106-
```
107-
Name: db-user-pass-96mffmfh4k
108-
Namespace: default
109-
Labels: <none>
110-
Annotations: <none>
105+
The output is similar to:
111106
112-
Type: Opaque
107+
```
108+
secret/db-user-pass-6f24b56cc8 created
109+
```
113110
114-
Data
115-
====
116-
password.txt: 12 bytes
117-
username.txt: 5 bytes
118-
```
119-
120-
The commands `kubectl get` and `kubectl describe` avoid showing the contents of a `Secret` by
121-
default. This is to protect the `Secret` from being exposed accidentally to an onlooker,
122-
or from being stored in a terminal log.
123-
To check the actual content of the encoded data, please refer to
124-
[decoding secret](/docs/tasks/configmap-secret/managing-secret-using-kubectl/#decoding-secret).
111+
The edited Secret is created as a new `Secret` object, instead of updating the
112+
existing `Secret` object. You might need to update references to the Secret in
113+
your Pods.
125114
126-
## Clean Up
115+
## Clean up
127116
128-
To delete the Secret you have created:
117+
To delete a Secret, use `kubectl`:
129118
130119
```shell
131-
kubectl delete secret db-user-pass-96mffmfh4k
120+
kubectl delete secret <secret-name>
132121
```
133122

134123
<!-- Optional section; add links to information related to this topic. -->
135124
## {{% heading "whatsnext" %}}
136125

137126
- Read more about the [Secret concept](/docs/concepts/configuration/secret/)
138127
- Learn how to [manage Secrets with the `kubectl` command](/docs/tasks/configmap-secret/managing-secret-using-kubectl/)
139-
- Learn how to [manage Secrets using config file](/docs/tasks/configmap-secret/managing-secret-using-config-file/)
140-
128+
- Learn how to [manage Secrets using config file](/docs/tasks/configmap-secret/managing-secret-using-config-file/)

0 commit comments

Comments
 (0)