Pod not writing on a local path

11/20/2018

I'm new with containers and kubernetes. What I'm trying to do is to create a pod with access to a local directory.

I have followed the directions from: configure persistent volume storage

Created my Persistent Volume, Persistent Volume Claim and my pod.

The problem is that the tomcat is not able to write on the shared directory

This is the Persistent Volume:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-webapp6
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/opt/test_tomcat/app"

This is the Persistent Volume Claim:

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

This is the tomcat Pod I'm trying to create:

apiVersion: v1
kind: Pod
metadata:
  name: webapp6
spec:
  containers:
  - image: tomcat:8.0
    name: webapp6
    ports:
      - containerPort: 8080
        name: webapp6
    volumeMounts:
    - mountPath: /usr/local/tomcat/webapps
      name: test-volume
  volumes:
  - name: test-volume
    persistentVolumeClaim:
      claimName: pvc-webapp6

Its a bit obvious, but this the error on the pod.

[root@testserver webapp6-test]# kubectl exec -it webapp6 -- /bin/bash
root@webapp6:/usr/local/tomcat# mkdir /usr/local/tomcat/webapps/sample
mkdir: cannot create directory ‘/usr/local/tomcat/webapps/sample’: Permission denied

-- radicaled
docker-volume
kubernetes
persistent-storage

1 Answer

11/20/2018

The issue is in your PVC yaml file where you're not specifying the storageClassName. Hence the PV and PVC couldn't bound to each other. Please replace the PVC yaml file with the following file:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
   name: pvc-webapp6
spec:
   storageClassName: manual
   accessModes:
      - ReadWriteOnce
   resources:
      requests:
         storage: 3Gi

Now everything should work. Hope this helps.

I quickly used your yaml to deploy pod and everything is working fine at my end:

[root@Master admin]# kubectl exec -it webapp6 bash
root@webapp6:/usr/local/tomcat#  mkdir /usr/local/tomcat/webapps/sample 
root@webapp6:/usr/local/tomcat# touch /usr/local/tomcat/webapps/sample/a
root@webapp6:/usr/local/tomcat# ls /usr/local/tomcat/webapps/sample/
a

Now when I look at host, I can see the newly created a file

[root@Master admin]# ls /opt/test_tomcat/app/sample/
a

So, at least yaml files are working fine.

-- Prafull Ladha
Source: StackOverflow