Carla
Carla

Reputation: 3380

How to change a Persistent Volume to use a path on the Host machine?


I have created, entirely through the Web console, a Persistent Volume Claim and attached to the /www/log mount path of my Web Server. I see the Persistent Volume Claim works as if I restart the Pod, the logs are preserved.

I'd like, however, to use a local mount on the Host for the Persistent Volume so that I can tail my logs easily. On the OKD Web console you can only create new PVC but you cannot create a PV pointing to a local mount. Can you advice how to update the Persistent Volume picked up automatically to use a local mount (e.g. /mnt/data) ?

Upvotes: 1

Views: 2939

Answers (1)

Venkata Surya Lolla
Venkata Surya Lolla

Reputation: 114

This can be resolved by using hostPath option Persistent Volume YAML in OpenShift running through the command line. But, there are some things to watch out if you want to go down this path.

  1. All the files and directories created in the path /mnt/data are only writable by root user. In this case either you have to run a privileged container or give appropriate permission to the hostPath to be writable.

  2. The below YAML has the syntax of hostPath option in Persistent volume.

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: test-pv
    spec:
      capacity:
        storage: 1Mi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Recycle
      hostPath:
        path: /mnt/data
    
  3. Run the following command to create the persistent volume in the specific project. oc create -f test-pv.yaml -n <project>

  4. Once you have the Persistent volume available, create a Persistent volume claim that bounds to the above created Persistent volume.

  5. If you want to mount the hostPath directly on a Pod, please make sure that you use a node selector in the deployment config as the pods are ephemeral and they can be recreated at any point in time. If the pods with the hostPath is scheduled on another host that might not have the /mnt/data path available, it could result aCrashLoopBackOff error.

  6. The below YAML is an example for the hostPath on a Pod.

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
      - image: tomcat
        name: test-container
        volumeMounts:
        - mountPath: /www/log
          name: test-volume
      volumes:
      - name: test-volume
        hostPath:
          # directory location on host
          path: /mnt/data
          # this field is optional
          type: Directory
    

Upvotes: 3

Related Questions