Karan Kumar
Karan Kumar

Reputation: 3186

Kubernetes - How do I mention hostPath in PVC?

I need to make use of PVC to specify the specs of the PV and I also need to make sure it uses a custom local storage path in the PV.

I am unable to figure out how to mention the hostpath in a PVC?

This is the PVC config:

apiVersion: v1 
kind: PersistentVolumeClaim
metadata: 
  name: mongo-pvc 
spec: 
  accessModes: 
    - ReadWriteOnce
  resources: 
    requests: 
      storage: 1Gi

And this is the mongodb deployment:

spec:
    replicas: 1
    selector:
        matchLabels:
            app: mongo
    template:
        metadata:
            labels: 
                app: mongo
        spec:
            volumes: 
                - name: mongo-volume 
                  persistentVolumeClaim: 
                    claimName: mongo-pvc 
            containers:
                - name: mongo
                  image: mongo
                  ports:
                    - containerPort: 27017
                  volumeMounts: 
                    - name: mongo-volume 
                      mountPath: /data/db 

How and where do I mention the hostPath to be mounted in here?

Upvotes: 6

Views: 6543

Answers (2)

David Maze
David Maze

Reputation: 160063

You don't (and can't) force a specific host path in a PersistentVolumeClaim.

Typically a Kubernetes cluster will be configured with a dynamic volume provisioner and that will create the matching PersistentVolume for you. Depending on how your cluster was installed that could be an Amazon EBS volume, a Google Cloud Platform persistent disk, an iSCSI volume, or some other type of storage; as an application author you don't really control that. (You tagged this question for GKE, and the GKE documentation has a section on dynamic volume provisioning.) You don't need to specify where on the host the volume might be mounted, and there's no way to provide this detail in the PersistentVolumeClaim.

With the YAML you show, and the context of this being on GKE, I'd expect Google to automatically provision a GCE persistent disk. If the pod gets rescheduled on a different node, the persistent disk will follow the pod to the new node. You don't need to worry about what specific host directory is being used; Kubernetes will manage this for you.

In most cases you'll want to avoid hostPath storage. You don't directly control which node your pods will run on, so you're not guaranteed that the pod will actually be scheduled on the node that has the data volume. It's appropriate for something like a log collector running in a DaemonSet, where you can guarantee that there is interesting content in that path on every node, but not for your general application database storage.

Upvotes: 5

Roar S.
Roar S.

Reputation: 11374

Doc says that you set hostPath when creating a PV (the step before creating PVC).

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

After you create the PersistentVolumeClaim, the Kubernetes control plane looks for a PersistentVolume that satisfies the claim's requirements. If the control plane finds a suitable PersistentVolume with the same StorageClass, it binds the claim to the volume.

Please see https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

Upvotes: 8

Related Questions