Skip to content

Commit 3436e2b

Browse files
committed
Add overview and modify create steps
- Add brief intro to the page - Improve the intro to the create secret section - Split the steps for creating into subheadings for raw and file - Keep commands the same but change the password string to be uniform - Keep explanation of -n flag same but add line at end of note for escaping - Keep explanation of default key name same (line 64-65)
1 parent c285ac1 commit 3436e2b

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

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

Lines changed: 46 additions & 42 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

0 commit comments

Comments
 (0)