Kubernetes hostPath volume not showing files

4/27/2020

I'm trying to create PersistentVolume to share directory on master (not on nodes) with the pods, for that, I'm using the following .yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/home/myhostuser/shared"

And I have the following Deployment .yaml where I'm trying to use the shared above as follow:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  selector:
    matchLabels:
      app: my-app
      replicas: 1
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image 
        volumeMounts:
         - mountPath: /shared_host_path
           name: my-volume
        securityContext:
          privileged: true      
        ports:
        - containerPort: 5000
      volumes:
      - name: my-volume
        hostPath: 
        # directory location on host
          path: "/home/myhostuser/shared/"

When I'm trying to run ls /home/myhostuser/shared on host as except I see files but when I run ls /shared_host_path inside the pod I not see files.

What I'm doing wrong?

-- samisaviv
kubernetes

2 Answers

4/27/2020

if you want to have the volume on the master, you have to specify this in the persistentVolume specification, like this

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  local:
    path: /home/myhostuser/shared
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - master

this is how the Kubernetes scheduler understands that this PersistentVolume is tied to a specific node

-- Hamed
Source: StackOverflow

4/27/2020

Define a PVC as below

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

And use the PVC in deployment as below

  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/shared_host_path"
          name: task-pv-storage

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

-- Arghya Sadhu
Source: StackOverflow