Kubernetes persistent storage causing files to disappear inside container

12/5/2016

Trying to create a Drupal container on Kubernetes with the apache drupal image.

When a persistent volume is mounted at /var/www/html and inspecting the Drupal container with docker exec -it <drupal-container-name> bash there are no files visible. Thus no files can be served.

Workflow

1 - Create google compute disk

gcloud compute disks create --size=20GB --zone=us-central1-c drupal-1

2 - Register the newly created google compute disk to the kubernetes cluster instance

kubectl create -f gce-volumes.yaml

3 - Create Drupal pod

kubectl create -f drupal-deployment.yaml

The definition files are inspired from the wordpress example, my drupal-deployment.yaml:

apiVersion: v1
kind: Service
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  ports:
    - port: 80
  selector:
    app: drupal
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dp-pv-claim
  labels:
    app: drupal
spec:
  accessModes:
    - ReadWriteOnce
  resources:
      requests:
          storage: 20Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: drupal
        tier: frontend
    spec:
      containers:
      - name: drupal
        image: drupal:8.2.3-apache
        ports:
        - name: drupal
          containerPort: 80
        volumeMounts:
        - name: drupal-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: drupal-persistent-storage
        persistentVolumeClaim:
          claimName: dp-pv-claim

And the gce-volumes.yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: drupal-pv-1
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: drupal-1
    fsType: ext4

What's causing the files to disappear? How can I successfully persist the Drupal installation files at /var/www/html inside the container?

-- Hyperfocus1337
drupal
drupal-8
kubernetes

1 Answer

12/5/2016

You are mounting the persistentStorage at /var/www/html, so if you had files at that location in your base image, the folder is replaced by the mount, and the files from the base image are no longer available.

If your content will be dynamic and you want to save those files in the persistentStorage, this is the way to do it, however you will need to populate the persistentStorage initially.

One solution would be to have the files in a different folder in the base image, and copy them over when you run the Pod, however this will happen every time you start the Pod, and may overwrite your changes, unless your copy script checks first if the folder is empty.

Another option is to have a Job do this only once (before you run your Drupal Pod and mount this storage.)

Note:

GKEPersistentStorage only allows ReadWriteOnce (which can be mounted only on a single Pod) or ReadOnlyMany (i.e. readable only but can be mounted on many Pods) and it cannot be mounted with different modes at the same time, so in the end you can only run one of these Pods. (i.e. it will not scale)

-- MrE
Source: StackOverflow