how to sharing volume each containers in Kubernetes?

11/8/2018

For example, run Drupal app using nginx:stable-alpine + drupal:8.6-fpm-alpine.

  • nginx container needs to share /var/www/html from drupal container to deliver static contents.
  • drupal container should persist or mount site-data /var/www/html/sites from external storage using volume such as GCP-PD.

in this case, locally docker-compose.yml is below.

version: "3"

volumes:
  www-data:

services:
  drupal:
    image: "drupal:8.6-fpm-alpine"
    volumes:
      - "www-data:/var/www/html"
      - "./sites:/var/www/html/sites"
    # ...

  nginx;
    image: "nginx:stable-alpine"
    depends_on:
      - drupal
    volumes:
      - "www-data:/var/www/html"
    # ...
# ...

how to translate to k8s deployment.yml?

(EDIT) I tried following and it didn't work.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydrupal
  labels:
    app.kubernetes.io/name: mydrupal
spec:
  replicas: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: mydrupal
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mydrupal
    spec:
      volumes:
        - name: drupal-data
          persistentVolumeClaim:
            claimName: "drupal-pvc"
      # keep default files for the drupal installer, and chown.
      initContainers:
        - name: init-drupal-data
          image: drupal:8.6-fpm-alpine
          imagePullPolicy: IfNotPresent
          command: ['sh', '-c']
          args: ['cp -r -u /var/www/html/sites/* /tmp/drupal; chown -R www-data:www-data /tmp/drupal']
          volumeMounts:
            - name: drupal-data
              mountPath: /tmp/drupal
              subPath: sites
      securityContext:
        # www-data
        fsGroup: 33
      containers:
        - name: drupal
          image: drupal:8.6-fpm-alpine
          imagePullPolicy: IfNotPresent
          volumeMounts:
            # I want to sharing this directory with nginx container.
            - name: drupal-data
              mountPath: /var/www/html
            # I want to persist this directory using external managed storage.
            - name: drupal-data
              mountPath: /var/www/html/sites
              subPath: sites
          resources:
            limits:
              cpu: 800m
              memory: 512Mi
            requests:
              cpu: 200m
              memory: 256Mi
        - name: nginx
          image: nginx:1.14-alpine
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: drupal-data
              mountPath: /usr/share/nginx/html
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 120
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 30
          resources:
            limits:
              cpu: 500m
              memory: 512Mi
            requests:
              cpu: 100m
              memory: 256Mi

I read the volume, pv, pvc docs. but not found any solutions for how to expose directory inside container as volume.

any ideas?

-- hidekuro
docker
drupal
kubernetes

1 Answer

11/8/2018

Take a look at hostPath that will allow you to use a local folder as persistent storage. https://kubernetes.io/docs/concepts/storage/volumes/#hostpath

There are some learning curve and configuration differences based on how your kubernetes cluster was setup for other types of pv/pvc.

-- ZPrime
Source: StackOverflow