This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.5.4. Mapping volumes using projected volumes
A projected volume maps several existing volume sources into the same directory.
The following types of volume sources can be projected:
- Secrets
- Config Maps
- Downward API
All sources are required to be in the same namespace as the pod.
5.4.1. Understanding projected volumes 复制链接链接已复制到粘贴板!
Projected volumes can map any combination of these volume sources into a single directory, allowing the user to:
- automatically populate a single volume with the keys from multiple secrets, config maps, and with downward API information, so that I can synthesize a single directory with various sources of information;
- populate a single volume with the keys from multiple secrets, config maps, and with downward API information, explicitly specifying paths for each item, so that I can have full control over the contents of that volume.
The following general scenarios show how you can use projected volumes.
- Config map, secrets, Downward API.
-
Projected volumes allow you to deploy containers with configuration data that includes passwords. An application using these resources could be deploying Red Hat OpenStack Platform (RHOSP) on Kubernetes. The configuration data might have to be assembled differently depending on if the services are going to be used for production or for testing. If a pod is labeled with production or testing, the downward API selector
metadata.labelscan be used to produce the correct RHOSP configs. - Config map + secrets.
- Projected volumes allow you to deploy containers involving configuration data and passwords. For example, you might execute a config map with some sensitive encrypted tasks that are decrypted using a vault password file.
- ConfigMap + Downward API.
-
Projected volumes allow you to generate a config including the pod name (available via the
metadata.nameselector). This application can then pass the pod name along with requests in order to easily determine the source without using IP tracking. - Secrets + Downward API.
-
Projected volumes allow you to use a secret as a public key to encrypt the namespace of the pod (available via the
metadata.namespaceselector). This example allows the Operator to use the application to deliver the namespace information securely without using an encrypted transport.
5.4.1.1. Example Pod specs 复制链接链接已复制到粘贴板!
The following are examples of Pod specs for creating projected volumes.
Pod with a secret, a Downward API, and a config map
- 1
- Add a
volumeMountssection for each container that needs the secret. - 2
- Specify a path to an unused directory where the secret will appear.
- 3
- Set
readOnlytotrue. - 4
- Add a
volumesblock to list each projected volume source. - 5
- Specify any name for the volume.
- 6
- Set the execute permission on the files.
- 7
- Add a secret. Enter the name of the secret object. Each secret you want to use must be listed.
- 8
- Specify the path to the secrets file under the
mountPath. Here, the secrets file is in /projected-volume/my-group/my-username. - 9
- Add a Downward API source.
- 10
- Add a ConfigMap source.
- 11
- Set the mode for the specific projection
If there are multiple containers in the pod, each container needs a volumeMounts section, but only one volumes section is needed.
Pod with multiple secrets with a non-default permission mode set
The defaultMode can only be specified at the projected level and not for each volume source. However, as illustrated above, you can explicitly set the mode for each individual projection.
5.4.1.2. Pathing Considerations 复制链接链接已复制到粘贴板!
- Collisions Between Keys when Configured Paths are Identical
If you configure any keys with the same path, the pod spec will not be accepted as valid. In the following example, the specified path for
mysecretandmyconfigmapare the same:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Consider the following situations related to the volume file paths.
- Collisions Between Keys without Configured Paths
- The only run-time validation that can occur is when all the paths are known at pod creation, similar to the above scenario. Otherwise, when a conflict occurs the most recent specified resource will overwrite anything preceding it (this is true for resources that are updated after pod creation as well).
- Collisions when One Path is Explicit and the Other is Automatically Projected
- In the event that there is a collision due to a user specified path matching data that is automatically projected, the latter resource will overwrite anything preceding it as before
5.4.2. Configuring a Projected Volume for a Pod 复制链接链接已复制到粘贴板!
When creating projected volumes, consider the volume file path situations described in Understanding projected volumes.
The following example shows how to use a projected volume to mount an existing secret volume source. The steps can be used to create a user name and password secrets from local files. You then create a pod that runs one container, using a projected volume to mount the secrets into the same shared directory.
Procedure
To use a projected volume to mount an existing secret volume source.
Create files containing the secrets, entering the following, replacing the password and user information as appropriate:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
userandpassvalues can be any valid string that is base64 encoded.The following example shows
adminin base64:echo -n "admin" | base64
$ echo -n "admin" | base64Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
YWRtaW4=
YWRtaW4=Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following example shows the password
1f2d1e2e67dfin base64:.echo -n "1f2d1e2e67df" | base64
$ echo -n "1f2d1e2e67df" | base64Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
MWYyZDFlMmU2N2Rm
MWYyZDFlMmU2N2RmCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the following command to create the secrets:
oc create -f <secrets-filename>
$ oc create -f <secrets-filename>Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example:
oc create -f secret.yaml
$ oc create -f secret.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
secret "mysecret" created
secret "mysecret" createdCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can check that the secret was created using the following commands:
oc get secret <secret-name>
$ oc get secret <secret-name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example:
oc get secret mysecret
$ oc get secret mysecretCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME TYPE DATA AGE mysecret Opaque 2 17h
NAME TYPE DATA AGE mysecret Opaque 2 17hCopy to Clipboard Copied! Toggle word wrap Toggle overflow oc get secret <secret-name> -o yaml
$ oc get secret <secret-name> -o yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow For example:
oc get secret mysecret -o yaml
$ oc get secret mysecret -o yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a pod configuration file similar to the following that includes a
volumessection:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the configuration file:
oc create -f <your_yaml_file>.yaml
$ oc create -f <your_yaml_file>.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow For example:
oc create -f secret-pod.yaml
$ oc create -f secret-pod.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
pod "test-projected-volume" created
pod "test-projected-volume" createdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the pod container is running, and then watch for changes to the pod:
oc get pod <name>
$ oc get pod <name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example:
oc get pod test-projected-volume
$ oc get pod test-projected-volumeCopy to Clipboard Copied! Toggle word wrap Toggle overflow The output should appear similar to the following:
Example output
NAME READY STATUS RESTARTS AGE test-projected-volume 1/1 Running 0 14s
NAME READY STATUS RESTARTS AGE test-projected-volume 1/1 Running 0 14sCopy to Clipboard Copied! Toggle word wrap Toggle overflow In another terminal, use the
oc execcommand to open a shell to the running container:oc exec -it <pod> <command>
$ oc exec -it <pod> <command>Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example:
oc exec -it test-projected-volume -- /bin/sh
$ oc exec -it test-projected-volume -- /bin/shCopy to Clipboard Copied! Toggle word wrap Toggle overflow In your shell, verify that the
projected-volumesdirectory contains your projected sources:/ # ls
/ # lsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
bin home root tmp dev proc run usr etc projected-volume sys var
bin home root tmp dev proc run usr etc projected-volume sys varCopy to Clipboard Copied! Toggle word wrap Toggle overflow