Stuart winchester in Openshift 5 minutes

Mounting Volumes Using Oc Run With Overrides

Small post to explain that if you ever need to use oc run and need to mount a volume for the process, then it is possible to do this by using overrides:

Initially create the bash-claim pvc:

oc create -f https://gist.githubusercontent.com/welshstew/4f9532fd8737de1a43277c9154caa95e/raw/f3a705cebd65bf9c7188cfdeadb4976f32eed29c/bash-claim-pvc.yml

Once the PVC is created you can use oc run with overrides to attach the PVC, note that the image is notused, since the image itself is overriden by the json overrides:

oc run some-pod --overrides='
{
        "spec": {
            "containers": [
                {
                    "command": [
                        "/bin/bash",
                        "-c",
                        "for i in {1..5}; do echo hi stuff; sleep 5; done"
                    ],
                    "image": "registry.access.redhat.com/rhel7/rhel:latest",
                    "name": "some-pod",
                    "volumeMounts": [{
                        "mountPath": "/var/data",
                        "name": "some-data"
                    }]
                }
            ],        
            "volumes": [
                {
                    "name": "some-data",
                    "persistentVolumeClaim": {
                        "claimName": "bash-claim"
                    }
                }
            ]
        }
}
'  --image=notused --restart=Never

It can be seen that the pod has mounted the volume.

[swinches@localhost ~]$ oc describe pods/some-pod
Name:         some-pod
...
    Environment:    <none>
    Mounts:
      /var/data from some-data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xg5mj (ro)
Conditions:
  Type           Status
  Initialized    True 
...

All available on this public gist