@@ -7,134 +7,122 @@ description: Creating Secret objects using kustomization.yaml file.
7
7
8
8
<!-- overview -->
9
9
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 ` .
16
13
17
14
## {{% heading "prerequisites" %}}
18
15
19
16
{{< include "task-tutorial-prereqs.md" >}}
20
17
21
18
<!-- steps -->
22
19
23
- ## Create the Kustomization file
20
+ ## Create a Secret
24
21
25
22
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 ` .
29
26
30
- ``` yaml
31
- secretGenerator :
32
- - name : db-user-pass
33
- files :
34
- - username.txt
35
- - password.txt
36
- ` ` `
27
+ ### Create the Kustomization file
37
28
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" >}}
44
31
secretGenerator:
45
- - name: db-user-pass
32
+ - name: database-creds
46
33
literals:
47
34
- username=admin
48
35
- 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:
55
61
56
62
```yaml
57
63
secretGenerator:
58
64
- name: db-user-pass
59
65
envs:
60
66
- .env.secret
61
67
```
68
+ {{% /tab %}}
69
+ {{< /tabs >}}
62
70
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` .
64
73
65
- # # Create the Secret
74
+ # ## Apply the kustomization file
66
75
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:
68
77
69
78
` ` ` shell
70
- kubectl apply -k .
79
+ kubectl apply -k < directory-path >
71
80
` ` `
72
81
73
82
The output is similar to:
74
83
75
84
```
76
- secret/db-user-pass-96mffmfh4k created
85
+ secret/database-creds-5hdh7hhgfk created
77
86
```
78
87
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
80
89
the Secret data and appending the hash value to the name. This ensures that
81
90
a new Secret is generated each time the data is modified.
82
91
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).
84
95
85
- You can check that the secret was created :
96
+ ## Edit a Secret {#edit- secret}
86
97
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:
90
100
91
- The output is similar to :
101
+ ```shell
102
+ kubectl apply -k <directory-path>
103
+ ```
92
104
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:
111
106
112
- Type: Opaque
107
+ ```
108
+ secret/db-user-pass-6f24b56cc8 created
109
+ ```
113
110
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.
125
114
126
- # # Clean Up
115
+ ## Clean up
127
116
128
- To delete the Secret you have created :
117
+ To delete a Secret, use `kubectl` :
129
118
130
119
```shell
131
- kubectl delete secret db-user-pass-96mffmfh4k
120
+ kubectl delete secret <secret-name>
132
121
```
133
122
134
123
<!-- Optional section; add links to information related to this topic. -->
135
124
## {{% heading "whatsnext" %}}
136
125
137
126
- Read more about the [ Secret concept] ( /docs/concepts/configuration/secret/ )
138
127
- 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