Skip to content

Commit 39e34c9

Browse files
authored
Merge pull request #34855 from shannonxtreme/manage-secrets-kubectl
Add create and edit to kubectl secrets
2 parents c711f74 + 6f1eed2 commit 39e34c9

File tree

1 file changed

+104
-75
lines changed

1 file changed

+104
-75
lines changed

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

Lines changed: 104 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ description: Creating Secret objects using kubectl command line.
77

88
<!-- overview -->
99

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+
1014
## {{% heading "prerequisites" %}}
1115

1216
{{< include "task-tutorial-prereqs.md" >}}
@@ -15,64 +19,64 @@ description: Creating Secret objects using kubectl command line.
1519

1620
## Create a Secret
1721

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.
2225

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
3131

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:
3433

3534
```shell
3635
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='
3938
```
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.
4042

41-
The output is similar to:
43+
### Use source files
4244

43-
```
44-
secret/db-user-pass created
45-
```
45+
1. Store the credentials in files with the values encoded in base64:
4646

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.
4956

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:
5558

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:
5866

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+
```
6472

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:
6874

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
7377
```
7478
75-
## Verify the Secret
79+
### Verify the Secret {#verify-the-secret}
7680
7781
Check that the Secret was created:
7882
@@ -87,10 +91,10 @@ NAME TYPE DATA AGE
8791
db-user-pass Opaque 2 51s
8892
```
8993

90-
You can view a description of the `Secret`:
94+
View the details of the Secret:
9195

9296
```shell
93-
kubectl describe secrets/db-user-pass
97+
kubectl describe secret db-user-pass
9498
```
9599

96100
The output is similar to:
@@ -113,52 +117,77 @@ The commands `kubectl get` and `kubectl describe` avoid showing the contents
113117
of a `Secret` by default. This is to protect the `Secret` from being exposed
114118
accidentally, or from being stored in a terminal log.
115119

116-
To check the actual content of the encoded data, refer to [Decoding the Secret](#decoding-secret).
120+
### Decode the Secret {#decoding-secret}
117121

118-
## Decoding the Secret {#decoding-secret}
122+
1. View the contents of the Secret you created:
119123

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+
```
121127

122-
```shell
123-
kubectl get secret db-user-pass -o jsonpath='{.data}'
124-
```
128+
The output is similar to:
125129

126-
The output is similar to:
130+
```json
131+
{"password":"UyFCXCpkJHpEc2I9","username":"YWRtaW4="}
132+
```
127133

128-
```json
129-
{"password":"MWYyZDFlMmU2N2Rm","username":"YWRtaW4="}
130-
```
134+
1. Decode the `password` data:
131135

132-
Now you can decode the `password` data:
136+
```shell
137+
echo 'UyFCXCpkJHpEc2I9' | base64 --decode
138+
```
133139

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:
143141

144-
The output is similar to:
142+
```
143+
S!B\*d$zDsb=
144+
```
145145

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+
```
149155

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:
152161

153162
```shell
154-
kubectl get secret db-user-pass -o jsonpath='{.data.password}' | base64 --decode
163+
kubectl edit secrets <secret-name>
155164
```
156165

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+
```
158187

159-
## Clean Up
188+
## Clean up
160189

161-
Delete the Secret you created:
190+
To delete a Secret, run the following command:
162191

163192
```shell
164193
kubectl delete secret db-user-pass
@@ -170,4 +199,4 @@ kubectl delete secret db-user-pass
170199

171200
- Read more about the [Secret concept](/docs/concepts/configuration/secret/)
172201
- 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

Comments
 (0)