kubernetes persistent volume for nginx not showing the default index.html file

3.6k Views Asked by At

I am testing out something with the PV and wanted to get some clarification. We have an 18 node cluster(using Docker EE) and we have mounted NFS share on each of this node to be used for the k8s persistent storage. I created a PV (using hostPath) to bind it with my nginx deployment(mounting the /usr/share/nginx/html to PV).

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-test-namespace-pv
  namespace: test-namespace
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
  - ReadWriteMany
  hostPath:
    path: "/nfs_share/docker/mynginx/demo"

How to create the PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-test-namespace-pvc
  namespace: test-namespace
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 2Gi

Deployment File:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mynginx
specs:
  selector:
    matchLabels:
      run: mynginx-apps
  replicas:2
  template:
    metadata:
      labels:
        run: mynginx-apps
    spec:
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: nfs-test-namespace-pvc
      containers:
        - name: mynginx
          image: dtr.midev.spglobal.com/spgmi/base:mynginx-v1
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/usr/share/nginx/html"
              name: task-pv-storage

So i assume when my pod starts the default index.html file from the nginx image should be available at the /usr/share/nginx/html within my pod and it should also be copied/available at my /nfs_share/mynginx/demo.

However i am not seeing any file here and when i expose this deployment and access the service it gives me 403 error as the index file is not available. Now when i create an html file either from inside the pod or from the node on the nfs share mounted as PV, it works as expected.

Is my assumption of the default file getting copied to hostpath correct? or am i missing something?

2

There are 2 best solutions below

0
On

the "/mnt/data" directory should be created on the node which your pod running actually.

0
On

Your /nfs_share/docker/mynginx/demo will not be available in pod, explanation is available here:

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

The configuration file specifies that the volume is at /mnt/data on the cluster’s Node. The configuration also specifies a size of 10 gibibytes and an access mode of ReadWriteOnce, which means the volume can be mounted as read-write by a single Node. It defines the StorageClass name manual for the PersistentVolume, which will be used to bind PersistentVolumeClaim requests to this PersistentVolume.

You do not see PV on your pod, it's being used to utilize as PVC which then can be mounted inside a pod.

You can read the whole article Configure a Pod to Use a PersistentVolume for Storage which should answer all the questions.