Using volumes to persist container data

    Volumes are mounted file systems available to pods and their containers which may be backed by a number of host-local or network attached storage endpoints. Containers are not persistent by default; on restart, their contents are cleared.

    To ensure that the file system on the volume contains no errors and, if errors are present, to repair them when possible, OKD invokes the utility prior to the mount utility. This occurs when either adding a volume or updating an existing volume.

    The simplest volume type is emptyDir, which is a temporary directory on a single machine. Administrators may also allow you to request a persistent volume that is automatically attached to your pods.

    Working with volumes using the OKD CLI

    You can use the CLI command oc set volume to add and remove volumes and volume mounts for any object that has a pod template like replication controllers or deployment configs. You can also list volumes in pods or any object that has a pod template.

    The oc set volume command uses the following general syntax:

    Object selection

    Specify one of the following for the object_selection parameter in the oc set volume command:

    Table 1. Object Selection
    SyntaxDescriptionExample

    <object_type> <name>

    Selects <name> of type <object_type>.

    deploymentConfig registry

    <object_type>/<name>

    Selects <name> of type <object_type>.

    deploymentConfig/registry

    <object_type> —selector=<object_label_selector>

    Selects resources of type <object_type> that matched the given label selector.

    deploymentConfig —selector=”name=registry”

    <object_type> —all

    Selects all resources of type <object_type>.

    deploymentConfig —all

    -f or —filename=<file_name>

    File name, directory, or URL to file to use to edit the resource.

    -f registry-deployment-config.json

    Operation

    Specify --add or --remove for the operation parameter in the oc set volume command.

    Mandatory parameters

    Any mandatory parameters are specific to the selected operation and are discussed in later sections.

    Options

    Any options are specific to the selected operation and are discussed in later sections.

    You can list volumes and volume mounts in pods or pod templates:

    Procedure

    To list volumes:

    1. $ oc set volume <object_type>/<name> [options]

    List volume supported options:

    OptionDescriptionDefault

    —name

    Name of the volume.

    -c, —containers

    ‘‘

    For example:

    • To list all volumes for pod p1:

      1. $ oc set volume pod/p1
    • To list volume v1 defined on all deployment configs:

      1. $ oc set volume dc --all --name=v1

    Adding volumes to a pod

    You can add volumes and volume mounts to a pod.

    Procedure

    To add a volume, a volume mount, or both to pod templates:

    1. $ oc set volume <object_type>/<name> --add [options]
    Table 2. Supported Options for Adding Volumes
    OptionDescriptionDefault

    —name

    Name of the volume.

    Automatically generated, if not specified.

    -t, —type

    Name of the volume source. Supported values: emptyDir, hostPath, secret, configmap, persistentVolumeClaim or projected.

    emptyDir

    -c, —containers

    Select containers by name. It can also take wildcard that matches any character.

    ‘‘

    -m, —mount-path

    Mount path inside the selected containers. Do not mount to the container root, /, or any path that is the same in the host and the container. This can corrupt your host system if the container is sufficiently privileged, such as the host /dev/pts files. It is safe to mount the host by using /host.

    —path

    Host path. Mandatory parameter for —type=hostPath. Do not mount to the container root, /, or any path that is the same in the host and the container. This can corrupt your host system if the container is sufficiently privileged, such as the host /dev/pts files. It is safe to mount the host by using /host.

    —secret-name

    Name of the secret. Mandatory parameter for —type=secret.

    —configmap-name

    Name of the configmap. Mandatory parameter for —type=configmap.

    —claim-name

    Name of the persistent volume claim. Mandatory parameter for —type=persistentVolumeClaim.

    —source

    Details of volume source as a JSON string. Recommended if the desired volume source is not supported by —type.

    -o, —output

    Display the modified objects instead of updating them on the server. Supported values: json, yaml.

    —output-version

    Output the modified objects with the given version.

    api-version

    For example:

    • To add a new volume source emptyDir to the registry DeploymentConfig object:

      1. $ oc set volume dc/registry --add

      You can alternatively apply the following YAML to add the volume:

      Sample deployment config with an added volume
      1. kind: DeploymentConfig
      2. metadata:
      3. name: registry
      4. namespace: registry
      5. spec:
      6. replicas: 3
      7. selector:
      8. app: httpd
      9. template:
      10. metadata:
      11. labels:
      12. app: httpd
      13. spec:
      14. volumes: (1)
      15. - name: volume-pppsw
      16. emptyDir: {}
      17. containers:
      18. - name: httpd
      19. image: >-
      20. image-registry.openshift-image-registry.svc:5000/openshift/httpd:latest
      21. ports:
      22. - containerPort: 8080
      23. protocol: TCP
      1. $ oc set volume rc/r1 --add --name=v1 --type=secret --secret-name='secret1' --mount-path=/data

      You can alternatively apply the following YAML to add the volume:

      Sample replication controller with added volume and secret
      1Add the volume and secret.
      2Add the container mount path.
    • To add existing persistent volume v1 with claim name pvc1 to deployment configuration dc.json on disk, mount the volume on container c1 at /data, and update the DeploymentConfig object on the server:

      1. $ oc set volume -f dc.json --add --name=v1 --type=persistentVolumeClaim \
      2. --claim-name=pvc1 --mount-path=/data --containers=c1

      You can alternatively apply the following YAML to add the volume:

      Sample deployment config with persistent volume added
      1. kind: DeploymentConfig
      2. apiVersion: apps.openshift.io/v1
      3. metadata:
      4. name: example
      5. namespace: example
      6. replicas: 3
      7. selector:
      8. app: httpd
      9. template:
      10. metadata:
      11. labels:
      12. app: httpd
      13. spec:
      14. volumes:
      15. - name: volume-pppsw
      16. emptyDir: {}
      17. - name: v1 (1)
      18. persistentVolumeClaim:
      19. claimName: pvc1
      20. containers:
      21. - name: httpd
      22. image: >-
      23. image-registry.openshift-image-registry.svc:5000/openshift/httpd:latest
      24. ports:
      25. - containerPort: 8080
      26. protocol: TCP
      27. volumeMounts: (2)
      28. - name: v1
      29. mountPath: /data
      1Add the persistent volume claim named `pvc1.
      2Add the container mount path.
    • To add a volume v1 based on Git repository https://github.com/namespace1/project1 with revision 5125c45f9f563 for all replication controllers:

      1. $ oc set volume rc --all --add --name=v1 \
      2. --source='{"gitRepo": {
      3. "repository": "https://github.com/namespace1/project1",
      4. "revision": "5125c45f9f563"
      5. }}'

    You can modify the volumes and volume mounts in a pod.

    Procedure

    Updating existing volumes using the --overwrite option:

    1. $ oc set volume <object_type>/<name> --add --overwrite [options]

    For example:

    • To replace existing volume v1 for replication controller r1 with existing persistent volume claim pvc1:

      1. $ oc set volume rc/r1 --add --overwrite --name=v1 --type=persistentVolumeClaim --claim-name=pvc1
    • To change the DeploymentConfig object d1 mount point to /opt for volume v1:

      1. $ oc set volume dc/d1 --add --overwrite --name=v1 --mount-path=/opt

      You can alternatively apply the following YAML to change the mount point:

      Sample deployment config with mount point set to opt.
      1Set the mount point to /opt.

    Removing volumes and volume mounts from a pod

    You can remove a volume or volume mount from a pod.

    Procedure

    To remove a volume from pod templates:

    1. $ oc set volume <object_type>/<name> --remove [options]
    Table 3. Supported options for removing volumes
    OptionDescriptionDefault

    —name

    Name of the volume.

    -c, —containers

    Select containers by name. It can also take wildcard that matches any character.

    ‘‘

    —confirm

    Indicate that you want to remove multiple volumes at once.

    -o, —output

    Display the modified objects instead of updating them on the server. Supported values: json, yaml.

    —output-version

    Output the modified objects with the given version.

    api-version

    For example:

    • To remove a volume v1 from the DeploymentConfig object d1:

      1. $ oc set volume dc/d1 --remove --name=v1
    • To unmount volume v1 from container c1 for the DeploymentConfig object d1 and remove the volume v1 if it is not referenced by any containers on d1:

      1. $ oc set volume dc/d1 --remove --name=v1 --containers=c1
    • To remove all volumes for replication controller r1:

      1. $ oc set volume rc/r1 --remove --confirm

    You can configure a volume to allows you to share one volume for multiple uses in a single pod using the volumeMounts.subPath property to specify a subPath value inside a volume instead of the volume’s root.

    Procedure

    1. View the list of files in the volume, run the oc rsh command:

      1. $ oc rsh <pod>

      Example output

      1. sh-4.2$ ls /path/to/volume/subpath/mount
      2. example_file1 example_file2 example_file3
    2. Specify the subPath:

      1. apiVersion: v1
      2. kind: Pod
      3. metadata:
      4. name: my-site
      5. spec:
      6. containers:
      7. - name: mysql
      8. image: mysql
      9. volumeMounts:
      10. - mountPath: /var/lib/mysql
      11. name: site-data
      12. subPath: mysql (1)
      13. - name: php
      14. image: php
      15. volumeMounts:
      16. - mountPath: /var/www/html
      17. name: site-data
      18. subPath: html (2)
      19. volumes:
      20. - name: site-data
      21. claimName: my-site-data