Where do I find Path to a PersistentVolume on Kubernetes Cluster that is running locally on Mac

2/18/2021

I need to copy a file (.txt) to a PersistentVolume?

kubectl cp <file-spec-src> <file-spec-dest>

I need to know the <file-spec-dest> for PersistentVolume.

Backgroud: I have a single-node Kubernetes-Clusterenter image description here (docker-desktop) running locally on my mac. I am trying to copy a .txt file to a PersistentVolume (PV). I have create the PV and the PersistentVolumeClaim (PVC).

Note: I have been asked if it would make more sense with a pod instead of persistentVolume. The aim is that an image that will run as a Kubernetes Job will be using the data in the .txt file.

PersistentVolume:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"

PersistentVolumeClaim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

here is what i get with kubectl get pvc -o yaml

apiVersion: v1
items:
- apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    annotations:
      pv.kubernetes.io/bind-completed: "yes"
      pv.kubernetes.io/bound-by-controller: "yes"
      volume.beta.kubernetes.io/storage-provisioner: docker.io/hostpath
    creationTimestamp: "2021-02-18T15:06:19Z"
    finalizers:
    - kubernetes.io/pvc-protection
    managedFields:
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:pv.kubernetes.io/bind-completed: {}
            f:pv.kubernetes.io/bound-by-controller: {}
            f:volume.beta.kubernetes.io/storage-provisioner: {}
        f:spec:
          f:volumeName: {}
        f:status:
          f:accessModes: {}
          f:capacity:
            .: {}
            f:storage: {}
          f:phase: {}
      manager: kube-controller-manager
      operation: Update
      time: "2021-02-18T15:06:19Z"
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:spec:
          f:accessModes: {}
          f:resources:
            f:requests:
              .: {}
              f:storage: {}
          f:volumeMode: {}
      manager: kubectl-create
      operation: Update
      time: "2021-02-18T15:06:19Z"
    name: task-pv-claim
    namespace: default
    resourceVersion: "113659"
    selfLink: /api/v1/namespaces/default/persistentvolumeclaims/task-pv-claim
    uid: 5b825c41-cf4f-4c08-b90e-47e3fca557a1
  spec:
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 3Gi
    storageClassName: hostpath
    volumeMode: Filesystem
    volumeName: pvc-5b825c41-cf4f-4c08-b90e-47e3fca557a1
  status:
    accessModes:
    - ReadWriteOnce
    capacity:
      storage: 3Gi
    phase: Bound
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
-- QBits
kubernetes
kubernetes-pod
persistent-volume-claims
persistent-volumes

1 Answer

2/19/2021

The destination directory is the one that you use in pod/job manifest as mountPath. So if you choose to mount it in /mnt/data it will be your destination directory. For example:

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      volumes:
        - name: task-pv-claim
          hostPath:
            path: /mnt/data
            type: Directory
      containers:
      - name: pi
        image: nginx
        command: ["some_job"]
        volumeMounts:
         - name: task-pv-claim
           mountPath: /mnt/data
      restartPolicy: Never

So if you want to copy file from host to directory you have mounted:

kubectl cp <some_host_file> <pod_name>/:/mnt/data

Besides that, if you are using hostPath it copies file from your host to pod in specified directory so you can just place your file there and it will be copied to the pod.

-- kool
Source: StackOverflow