@@ -112,12 +112,14 @@ Here is a configuration file you can use to create a Pod:
112
112
```
113
113
114
114
Output:
115
+
115
116
```
116
117
NAME READY STATUS RESTARTS AGE
117
118
secret-test-pod 1/1 Running 0 42m
118
119
```
119
120
120
121
1 . Get a shell into the Container that is running in your Pod:
122
+
121
123
``` shell
122
124
kubectl exec -i -t secret-test-pod -- /bin/bash
123
125
```
@@ -126,22 +128,28 @@ Here is a configuration file you can use to create a Pod:
126
128
` /etc/secret-volume ` .
127
129
128
130
In your shell, list the files in the ` /etc/secret-volume ` directory:
131
+
129
132
``` shell
130
133
# Run this in the shell inside the container
131
134
ls /etc/secret-volume
132
135
```
136
+
133
137
The output shows two files, one for each piece of secret data:
138
+
134
139
```
135
140
password username
136
141
```
137
142
138
143
1 . In your shell, display the contents of the ` username ` and ` password ` files:
144
+
139
145
``` shell
140
146
# Run this in the shell inside the container
141
147
echo " $( cat /etc/secret-volume/username ) "
142
148
echo " $( cat /etc/secret-volume/password ) "
143
149
```
150
+
144
151
The output is your username and password:
152
+
145
153
```
146
154
my-app
147
155
39528$vdg7Jb
@@ -153,8 +161,8 @@ in this directory.
153
161
154
162
### Project Secret keys to specific file paths
155
163
156
- You can also control the paths within the volume where Secret keys are projected. Use the ` .spec.volumes[].secret.items ` field to change the target
157
- path of each key:
164
+ You can also control the paths within the volume where Secret keys are projected. Use the
165
+ ` .spec.volumes[].secret.items ` field to change the target path of each key:
158
166
159
167
``` yaml
160
168
apiVersion : v1
@@ -260,13 +268,14 @@ secrets change.
260
268
kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml
261
269
` ` `
262
270
263
- - In your shell, display the content of `SECRET_USERNAME` container environment variable
271
+ - In your shell, display the content of `SECRET_USERNAME` container environment variable.
264
272
265
273
` ` ` shell
266
274
kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME'
267
275
` ` `
268
276
269
- The output is
277
+ The output is similar to :
278
+
270
279
```
271
280
backend-admin
272
281
```
@@ -290,12 +299,14 @@ secrets change.
290
299
kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml
291
300
```
292
301
293
- - In your shell, display the container environment variables
302
+ - In your shell, display the container environment variables.
294
303
295
304
``` shell
296
305
kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c ' env | grep _USERNAME'
297
306
```
298
- The output is
307
+
308
+ The output is similar to:
309
+
299
310
```
300
311
DB_USERNAME=db-admin
301
312
BACKEND_USERNAME=backend-admin
@@ -313,7 +324,8 @@ This functionality is available in Kubernetes v1.6 and later.
313
324
kubectl create secret generic test-secret --from-literal=username=' my-app' --from-literal=password=' 39528$vdg7Jb'
314
325
```
315
326
316
- - Use envFrom to define all of the Secret's data as container environment variables. The key from the Secret becomes the environment variable name in the Pod.
327
+ - Use envFrom to define all of the Secret's data as container environment variables.
328
+ The key from the Secret becomes the environment variable name in the Pod.
317
329
318
330
{{% code file="pods/inject/pod-secret-envFrom.yaml" %}}
319
331
@@ -323,13 +335,14 @@ This functionality is available in Kubernetes v1.6 and later.
323
335
kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml
324
336
```
325
337
326
- - In your shell, display `username` and `password` container environment variables
338
+ - In your shell, display ` username ` and ` password ` container environment variables.
327
339
328
340
``` shell
329
341
kubectl exec -i -t envfrom-secret -- /bin/sh -c ' echo "username: $username\npassword: $password\n"'
330
342
```
331
343
332
- The output is
344
+ The output is similar to:
345
+
333
346
```
334
347
username: my-app
335
348
password: 39528$vdg7Jb
@@ -364,72 +377,76 @@ another Pod which consumes a secret with test environment credentials.
364
377
secret "test-db-secret" created
365
378
```
366
379
367
- {{< note >}}
368
- Special characters such as `$`, `\`, `*`, `=`, and `!` will be interpreted by your
369
- [shell](https://en.wikipedia.org/wiki/Shell_(computing)) and require escaping.
380
+ {{< note >}}
381
+ Special characters such as ` $ ` , ` \ ` , ` * ` , ` = ` , and ` ! ` will be interpreted by your
382
+ [ shell] ( https://en.wikipedia.org/wiki/Shell_(computing) ) and require escaping.
370
383
371
- In most shells, the easiest way to escape the password is to surround it with single quotes (`'`).
372
- For example, if your actual password is `S!B\*d$zDsb=`, you should execute the command as follows:
384
+ In most shells, the easiest way to escape the password is to surround it with single quotes (` ' ` ).
385
+ For example, if your actual password is ` S!B\*d$zDsb= ` , you should execute the command as follows:
373
386
374
- ```shell
375
- kubectl create secret generic dev-db-secret --from-literal=username=devuser --from-literal=password='S!B\*d$zDsb='
376
- ```
387
+ ``` shell
388
+ kubectl create secret generic dev-db-secret --from-literal=username=devuser --from-literal=password=' S!B\*d$zDsb='
389
+ ```
377
390
378
- You do not need to escape special characters in passwords from files (`--from-file`).
379
- {{< /note >}}
391
+ You do not need to escape special characters in passwords from files (` --from-file ` ).
392
+ {{< /note >}}
380
393
381
394
1 . Create the Pod manifests:
382
395
383
- ``` shell
384
- cat << EOF > pod.yaml
385
- apiVersion: v1
386
- kind: List
387
- items:
388
- - kind: Pod
389
- apiVersion: v1
390
- metadata:
391
- name: prod-db-client-pod
392
- labels:
393
- name: prod-db-client
394
- spec:
395
- volumes:
396
- - name: secret-volume
397
- secret:
398
- secretName: prod-db-secret
399
- containers:
400
- - name: db-client-container
401
- image: myClientImage
402
- volumeMounts:
403
- - name: secret-volume
404
- readOnly: true
405
- mountPath: "/etc/secret-volume"
406
- - kind: Pod
407
- apiVersion: v1
408
- metadata:
409
- name: test-db-client-pod
410
- labels:
411
- name: test-db-client
412
- spec:
413
- volumes:
414
- - name: secret-volume
415
- secret:
416
- secretName: test-db-secret
417
- containers:
418
- - name: db-client-container
419
- image: myClientImage
420
- volumeMounts:
421
- - name: secret-volume
422
- readOnly: true
423
- mountPath: "/etc/secret-volume"
424
- EOF
425
- ` ` `
426
- Note how the specs for the two Pods differ only in one field; this facilitates creating Pods with different capabilities from a common Pod template.
396
+ ``` shell
397
+ cat << EOF > pod.yaml
398
+ apiVersion: v1
399
+ kind: List
400
+ items:
401
+ - kind: Pod
402
+ apiVersion: v1
403
+ metadata:
404
+ name: prod-db-client-pod
405
+ labels:
406
+ name: prod-db-client
407
+ spec:
408
+ volumes:
409
+ - name: secret-volume
410
+ secret:
411
+ secretName: prod-db-secret
412
+ containers:
413
+ - name: db-client-container
414
+ image: myClientImage
415
+ volumeMounts:
416
+ - name: secret-volume
417
+ readOnly: true
418
+ mountPath: "/etc/secret-volume"
419
+ - kind: Pod
420
+ apiVersion: v1
421
+ metadata:
422
+ name: test-db-client-pod
423
+ labels:
424
+ name: test-db-client
425
+ spec:
426
+ volumes:
427
+ - name: secret-volume
428
+ secret:
429
+ secretName: test-db-secret
430
+ containers:
431
+ - name: db-client-container
432
+ image: myClientImage
433
+ volumeMounts:
434
+ - name: secret-volume
435
+ readOnly: true
436
+ mountPath: "/etc/secret-volume"
437
+ EOF
438
+ ` ` `
439
+
440
+ {{< note > }}
441
+ How the specs for the two Pods differ only in one field; this facilitates creating Pods
442
+ with different capabilities from a common Pod template.
443
+ {{< /note > }}
427
444
428
445
1. Apply all those objects on the API server by running:
429
446
430
- ` ` ` shell
431
- kubectl create -f pod.yaml
432
- ` ` `
447
+ ` ` ` shell
448
+ kubectl create -f pod.yaml
449
+ ` ` `
433
450
434
451
Both containers will have the following files present on their filesystems with the values
435
452
for each container' s environment:
0 commit comments