Score:2

Contents in Kubernetes pod gets empty when persistent volume is mounted

us flag
aks

persistent volume claim and persistent volume yaml file

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/datatypo"


---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-claim
spec:
  storageClassName: manual
  volumeName: my-volume
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

Deployment yaml file

apiVersion: v1
kind: Service
metadata:
  name: typo3
  labels:
    app: typo3
spec:
  type: NodePort
  ports:
    - nodePort: 31021
      port: 80
      targetPort: 80
  selector:
    app: typo3
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: typo3
spec:
  selector:
    matchLabels:
      app: typo3
  replicas: 1
  template:
    metadata:
      labels:
        app: typo3
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: app
                operator: In
                values:
                - typo3
      containers:
      - image: image:typo3
        name: typo3
        imagePullPolicy: Never
        ports:
        - containerPort: 80
        volumeMounts:
         - name: my-volume
           mountPath: /var/www/html/
      volumes:
           - name: my-volume
             persistentVolumeClaim:
                 claimName: my-claim

Note: if the persistent volume is not added, then contents were showing inside the pod (in var/www/html). But after adding the persistent volume then it's not showing any contents inside the same folder and the external mount path /mnt/datatypo.

jp flag
do you have content in `/mnt/datatypo` on the host itself? It won't get copied from your container `/var/www/html/`.
aks avatar
us flag
aks
Thank you @AlexD.
Score:0
in flag

This is expected behaviour: when persistent volume is mounted, it overwrites content of the folder which is specified in mountPath.

Therefore you have two options:

  • have content of that directory presented on your host machine already
  • mount hostPath into another directory in container and then copy content to the final destination folder. (Can be achieved with command in container)

Also you can mount a single file, there are different options of hostPath types. Please get familiar with hostPath types.

Note! Using hostPath mount can be used only for testing some features locally, it's very insecure approach in production systems:

Warning: HostPath volumes present many security risks, and it is a best practice to avoid the use of HostPaths when possible. When a HostPath volume must be used, it should be scoped to only the required file or directory, and mounted as ReadOnly

Volumes - hostPath.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.