@@ -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
0 commit comments