kubernetes php nginx deployment share volume

7/30/2018

In kubernetes we try to have a immutable deployment of our PHP code by deploying the php code in a prepackaged container.

By nature kubernetes volume replace the directory with an empty volume, but I would like to keep the data of the php container so we can share that with the nginx container which has a vhost configured to connect to the php container.

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: serviceability
spec:
  replicas: 1
  template:
    metadata:
     spec:
      containers:
      - name: my-stuff-php
        image: our-php-service-in-fpm-container:latest
        ports:
        - containerPort: 9000
          name: transport
          protocol: TCP
        volumeMounts:
        - name: my-volume
          mountPath: /var/www/html
      - name: my-stuff-nginx
        image: nginx:latest
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        volumeMounts:
        - name: my-volume
          mountPath: /var/www/html
      volumes:
      - name: my-volume
        emptyDir: {}

A similar setup on docker-compose works as docker-compose behaves different with regards to volumes.

How can I share the existing data in /var/www/html from my php container with the nginx container?

-- Marco
docker
docker-compose
kubernetes
nginx
php

2 Answers

12/4/2018

Ran in the same situation. This is an example, Attach Handlers to Container Lifecycle Events to your php-fpm image, my-stuff-php, in your case:

lifecycle:
    postStart:
      exec:
        command: ["/bin/sh", "-c", "cp -r /app/. /var/www/html"]
  1. Build your php-fpm with code at /app directory.
  2. Create the shared volume with emptyDir, which is also the nginx document root.
  3. Use lifecycle.postStart.exec.command as example above to copy source code to that shared volume.

But not sure if it's the best approach. Another way is to combine nginx and php-fpm in a single image.

-- Mr3381
Source: StackOverflow

7/31/2018

Kubernetes hasn't mechanism like docker-compose to share some folder as volume. But you can create Persistent Volume (PV) and Persistent Volume Claim (PVC) and share your data between containers. It is described in documentation Examples from Docs:

YAML for creating PV

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

Than you make PVC from this volume

YAML:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

Your YAML for deployment will looks like:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: serviceability
spec:
  replicas: 1
  template:
    metadata:
     spec:
      containers:
      - name: my-stuff-php
        image: our-php-service-in-fpm-container:latest
        ports:
        - containerPort: 9000
          name: transport
          protocol: TCP
        volumeMounts:
        - name: name: task-pv-storage
          mountPath: /var/www/html
      - name: my-stuff-nginx
        image: nginx:latest
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        volumeMounts:
        - name: name: task-pv-storage
          mountPath: /var/www/html
      volumes:
      - name: task-pv-storage
        ersistentVolumeClaim:
        claimName: task-pv-claim

As a result you will have volume with data which you share between two container in pod.

-- Nick Rak
Source: StackOverflow