@@ -7,6 +7,10 @@ description: Creating Secret objects using kubectl command line.
7
7
8
8
<!-- overview -->
9
9
10
+ This page shows you how to create, edit, manage, and delete Kubernetes
11
+ {{<glossary_tooltip text="Secrets" term_id="secret">}} using the ` kubectl `
12
+ command-line tool.
13
+
10
14
## {{% heading "prerequisites" %}}
11
15
12
16
{{< include "task-tutorial-prereqs.md" >}}
@@ -15,64 +19,64 @@ description: Creating Secret objects using kubectl command line.
15
19
16
20
## Create a Secret
17
21
18
- A ` Secret ` can contain user credentials required by pods to access a database.
19
- For example, a database connection string consists of a username and password.
20
- You can store the username in a file ` ./username.txt ` and the password in a
21
- file ` ./password.txt ` on your local machine.
22
+ A ` Secret ` object stores sensitive data such as credentials
23
+ used by Pods to access services. For example, you might need a Secret to store
24
+ the username and password needed to access a database.
22
25
23
- ``` shell
24
- echo -n ' admin' > ./username.txt
25
- echo -n ' 1f2d1e2e67df' > ./password.txt
26
- ```
27
- In these commands, the ` -n ` flag ensures that the generated files do not have
28
- an extra newline character at the end of the text. This is important because
29
- when ` kubectl ` reads a file and encodes the content into a base64 string, the
30
- extra newline character gets encoded too.
26
+ You can create the Secret by passing the raw data in the command, or by storing
27
+ the credentials in files that you pass in the command. The following commands
28
+ create a Secret that stores the username ` admin ` and the password ` S!B\*d$zDsb= ` .
29
+
30
+ ### Use raw data
31
31
32
- The ` kubectl create secret ` command packages these files into a Secret and creates
33
- the object on the API server.
32
+ Run the following command:
34
33
35
34
``` shell
36
35
kubectl create secret generic db-user-pass \
37
- --from-file=./ username.txt \
38
- --from-file=./ password.txt
36
+ --from-literal= username=devuser \
37
+ --from-literal= password= ' S!B\*d$zDsb= '
39
38
```
39
+ You must use single quotes ` '' ` to escape special characters such as ` $ ` , ` \ ` ,
40
+ ` * ` , ` = ` , and ` ! ` in your strings. If you don't, your shell will interpret these
41
+ characters.
40
42
41
- The output is similar to:
43
+ ### Use source files
42
44
43
- ```
44
- secret/db-user-pass created
45
- ```
45
+ 1 . Store the credentials in files with the values encoded in base64:
46
46
47
- The default key name is the filename. You can optionally set the key name using
48
- ` --from-file=[key=]source ` . For example:
47
+ ``` shell
48
+ echo -n ' admin' | base64 > ./username.txt
49
+ echo -n ' S!B\*d$zDsb=' | base64 > ./password.txt
50
+ ```
51
+ The ` -n` flag ensures that the generated files do not have an extra newline
52
+ character at the end of the text. This is important because when ` kubectl`
53
+ reads a file and encodes the content into a base64 string, the extra
54
+ newline character gets encoded too. You do not need to escape special
55
+ characters in strings that you include in a file.
49
56
50
- ``` shell
51
- kubectl create secret generic db-user-pass \
52
- --from-file=username=./username.txt \
53
- --from-file=password=./password.txt
54
- ```
57
+ 1. Pass the file paths in the ` kubectl` command:
55
58
56
- You do not need to escape special characters in password strings that you
57
- include in a file.
59
+ ` ` ` shell
60
+ kubectl create secret generic db-user-pass \
61
+ --from-file=./username.txt \
62
+ --from-file=./password.txt
63
+ ` ` `
64
+ The default key name is the file name. You can optionally set the key name
65
+ using ` --from-file=[key= ]source` . For example:
58
66
59
- You can also provide Secret data using the ` --from-literal=<key>=<value> ` tag.
60
- This tag can be specified more than once to provide multiple key-value pairs.
61
- Note that special characters such as ` $ ` , ` \ ` , ` * ` , ` = ` , and ` ! ` will be
62
- interpreted by your [ shell ] ( https://en.wikipedia.org/wiki/Shell_(computing) )
63
- and require escaping.
67
+ ` ` ` shell
68
+ kubectl create secret generic db-user-pass \
69
+ --from-file=username=./username.txt \
70
+ --from-file=password=./password.txt
71
+ ` ` `
64
72
65
- In most shells, the easiest way to escape the password is to surround it with
66
- single quotes (` ' ` ). For example, if your password is ` S!B\*d$zDsb= ` ,
67
- run the following command:
73
+ With either method, the output is similar to:
68
74
69
- ``` shell
70
- kubectl create secret generic db-user-pass \
71
- --from-literal=username=devuser \
72
- --from-literal=password=' S!B\*d$zDsb='
75
+ ```
76
+ secret/db-user-pass created
73
77
```
74
78
75
- ## Verify the Secret
79
+ ### Verify the Secret {#verify-the-secret}
76
80
77
81
Check that the Secret was created:
78
82
@@ -87,10 +91,10 @@ NAME TYPE DATA AGE
87
91
db-user-pass Opaque 2 51s
88
92
```
89
93
90
- You can view a description of the ` Secret ` :
94
+ View the details of the Secret:
91
95
92
96
``` shell
93
- kubectl describe secrets/ db-user-pass
97
+ kubectl describe secret db-user-pass
94
98
```
95
99
96
100
The output is similar to:
@@ -113,52 +117,77 @@ The commands `kubectl get` and `kubectl describe` avoid showing the contents
113
117
of a ` Secret ` by default. This is to protect the ` Secret ` from being exposed
114
118
accidentally, or from being stored in a terminal log.
115
119
116
- To check the actual content of the encoded data, refer to [ Decoding the Secret ] ( #decoding-secret ) .
120
+ ### Decode the Secret { #decoding-secret}
117
121
118
- ## Decoding the Secret {#decoding-secret}
122
+ 1 . View the contents of the Secret you created:
119
123
120
- To view the contents of the Secret you created, run the following command:
124
+ ``` shell
125
+ kubectl get secret db-user-pass -o jsonpath=' {.data}'
126
+ ```
121
127
122
- ``` shell
123
- kubectl get secret db-user-pass -o jsonpath=' {.data}'
124
- ```
128
+ The output is similar to:
125
129
126
- The output is similar to:
130
+ ` ` ` json
131
+ {" password" :" UyFCXCpkJHpEc2I9" ," username" :" YWRtaW4=" }
132
+ ` ` `
127
133
128
- ``` json
129
- {"password" :" MWYyZDFlMmU2N2Rm" ,"username" :" YWRtaW4=" }
130
- ```
134
+ 1. Decode the ` password` data:
131
135
132
- Now you can decode the ` password ` data:
136
+ ` ` ` shell
137
+ echo ' UyFCXCpkJHpEc2I9' | base64 --decode
138
+ ` ` `
133
139
134
- ``` shell
135
- # This is an example for documentation purposes.
136
- # If you did things this way, the data 'MWYyZDFlMmU2N2Rm' could be stored in
137
- # your shell history.
138
- # Someone with access to you computer could find that remembered command
139
- # and base-64 decode the secret, perhaps without your knowledge.
140
- # It's usually better to combine the steps, as shown later in the page.
141
- echo ' MWYyZDFlMmU2N2Rm' | base64 --decode
142
- ```
140
+ The output is similar to:
143
141
144
- The output is similar to:
142
+ ```
143
+ S! B\* d$zDsb =
144
+ ```
145
145
146
- ```
147
- 1f2d1e2e67df
148
- ```
146
+ {{< caution> }}This is an example for documentation purposes. In practice,
147
+ this method could cause the command with the encoded data to be stored in
148
+ your shell history. Anyone with access to your computer could find the
149
+ command and decode the secret. A better approach is to combine the view and
150
+ decode commands.{{< /caution> }}
151
+
152
+ ` ` ` shell
153
+ kubectl get secret db-user-pass -o jsonpath=' {.data.password}' | base64 --decode
154
+ ` ` `
149
155
150
- In order to avoid storing a secret encoded value in your shell history, you can
151
- run the following command:
156
+ # # Edit a Secret {#edit-secret}
157
+
158
+ You can edit an existing ` Secret` object unless it is
159
+ [immutable](/docs/concepts/configuration/secret/# secret-immutable). To edit a
160
+ Secret, run the following command:
152
161
153
162
` ` ` shell
154
- kubectl get secret db-user-pass -o jsonpath= ' {.data.password} ' | base64 --decode
163
+ kubectl edit secrets < secret-name >
155
164
` ` `
156
165
157
- The output shall be similar as above.
166
+ This opens your default editor and allows you to update the base64 encoded
167
+ Secret values in the ` data` field, such as in the following example:
168
+
169
+ ` ` ` yaml
170
+ # Please edit the object below. Lines beginning with a '#' will be ignored,
171
+ # and an empty file will abort the edit. If an error occurs while saving this file, it will be
172
+ # reopened with the relevant failures.
173
+ #
174
+ apiVersion: v1
175
+ data:
176
+ password: UyFCXCpkJHpEc2I9
177
+ username: YWRtaW4=
178
+ kind: Secret
179
+ metadata:
180
+ creationTimestamp: " 2022-06-28T17:44:13Z"
181
+ name: db-user-pass
182
+ namespace: default
183
+ resourceVersion: " 12708504"
184
+ uid: 91becd59-78fa-4c85-823f-6d44436242ac
185
+ type: Opaque
186
+ ` ` `
158
187
159
- ## Clean Up
188
+ # # Clean up
160
189
161
- Delete the Secret you created :
190
+ To delete a Secret, run the following command :
162
191
163
192
` ` ` shell
164
193
kubectl delete secret db-user-pass
@@ -170,4 +199,4 @@ kubectl delete secret db-user-pass
170
199
171
200
- Read more about the [Secret concept](/docs/concepts/configuration/secret/)
172
201
- Learn how to [manage Secrets using config files](/docs/tasks/configmap-secret/managing-secret-using-config-file/)
173
- - Learn how to [ manage Secrets using kustomize] ( /docs/tasks/configmap-secret/managing-secret-using-kustomize/ )
202
+ - Learn how to [manage Secrets using kustomize](/docs/tasks/configmap-secret/managing-secret-using-kustomize/)
0 commit comments