Remain pod directory files while mounting a "hostpath PV " to pod

5/9/2019

What I want is that mount a hostpath pv to my pod directory for example /usr/share/nginx/html/ and my files at this directory be remain.

I have a nginx image that i have index.html file in it's /usr/share/nginx/html/ directory. And when i want to orchestrate it with Kubernetes and make a pod and use a PV for make it persistent, it mount the hostpath to container /usr/share/nginx/html/ directory and after that my index.html will be gone . these are my manifests

Dockerfile :

FROM nginx
WORKDIR /usr/share/nginx/html
COPY . /usr/share/nginx/html
EXPOSE 80
VOLUME /usr/share/nginx/html

And in my . directory I have index.html

pod.yaml :

kind: Pod
apiVersion: v1
metadata:
  labels:
    app: nginx
  name: task-pv-pod23
spec:
  volumes:
    - name: task-pv-storage2
      persistentVolumeClaim:
       claimName: task-pv-claim2
  containers:
    - name: task-pv-container
      image: meysambbb/nginx:2
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - name: task-pv-storage2
          mountPath: /usr/share/nginx/html/

pv.yaml:

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

pvc.yaml :

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

When I run my pod without VolumeMount and when i run curl <POD_IP> it show my index.html contents but when I use pv and VolumeMount it show 403 error Is it possible to mount hostpath and have container files in same directory?

-- meisam bahrami
docker
kubernetes
kubernetes-pvc
mount
volume

1 Answer

5/9/2019

Is this data static (you build it into the image), or does it change (you want a volume to persist it)? What behavior do you want when this content changes?

The combination of options you're trying to specify here doesn't really make sense. Kubernetes doesn't do the thing Docker does where if you mount an empty volume into a container it's filled with matching content from the image. Even so, if you make a change and rebuild the image, neither plain Docker nor Kubernetes will modify the volume; it will still have the old content.

My intuition for a tree of HTML files and related assets is that it's probably more "like data" than "like code", and so I would use a plain nginx image, copy the tree into the directory you're mounting as a hostpath, and proceed that way. When this tree changes, you'd make the changes in the hostpath directory.

-- David Maze
Source: StackOverflow