How to share files between containers in the same kubernetes pod

6/15/2021

I have two containers, one running phpfpm and the other running nginx. I'd like the container running nginx to access the webroot files which has css and javascript files. Right now, nginx successfully passes off php requests to phpfpm, but no styles are showing up for instance when the webpage is rendered.

This is running off of minikube on a linux system.

kind: ConfigMap
apiVersion: v1
metadata:
  name: php-ini
  namespace: mixerapi-docker
data:
  php.ini: |
    extension=intl.so
    extension=pdo_mysql.so
    extension=sodium
    extension=zip.so
    zend_extension=opcache.so

    [php]
    session.auto_start = Off
    short_open_tag = Off
    opcache.interned_strings_buffer = 16
    opcache.max_accelerated_files = 20000
    opcache.memory_consumption = 256
    realpath_cache_size = 4096K
    realpath_cache_ttl = 600
    expose_php = off
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-conf
  namespace: mixerapi-docker
data:
  default.conf: |-
    server {
        listen 80;

        root /srv/app/webroot/;
        index index.php;

        location / {
          try_files $uri /index.php?$args;
        }

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
            include fastcgi_params;
        }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-nginx-deployment
  namespace: mixerapi-docker
  labels:
    app: php-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-nginx
  template:
    metadata:
      labels:
        app: php-nginx
    spec:
      containers:
        - name: php
          image: mixerapidev/demo:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 9000
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: php-secret
                  key: database-url
            - name: SECURITY_SALT
              valueFrom:
                secretKeyRef:
                  name: php-secret
                  key: cakephp-salt
          volumeMounts:
            - name: php-ini
              mountPath: /usr/local/etc/php/conf.d
            - name: php-application
              mountPath: /application

        - name: nginx
          image: nginx:1.19-alpine
          ports:
            - containerPort: 80
          volumeMounts:
            - name: nginx-conf
              mountPath: /etc/nginx/conf.d
            - name: php-application
              mountPath: /application

      volumes:
        - name: php-ini
          configMap:
            name: php-ini
        - name: php-application
          persistentVolumeClaim:
            claimName: php-application-pv-claim
        - name: nginx-conf
          configMap:
            name: nginx-conf
            items:
              - key: default.conf
                path: default.conf
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: php-application-pv-claim
  namespace: mixerapi-docker
  labels:
    app: php-nginx
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: mixerapi-docker
spec:
  selector:
    app: php-nginx
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30002
---
apiVersion: v1
kind: Service
metadata:
  name: php
  namespace: mixerapi-docker
spec:
  selector:
    app: php-nginx
  ports:
    - protocol: TCP
      port: 9000
      targetPort: 9000
-- cnizzardini
kubernetes

1 Answer

6/29/2021

Not sure if this is the best approach, but I based it off of this: https://stackoverflow.com/questions/67993636/how-to-share-files-between-containers-in-the-same-kubernetes-pod

kind: ConfigMap
apiVersion: v1
metadata:
  name: php-ini
  namespace: mixerapi-docker
data:
  php.ini: |
    extension=intl.so
    extension=pdo_mysql.so
    extension=sodium
    extension=zip.so
    zend_extension=opcache.so

    [php]
    session.auto_start = Off
    short_open_tag = Off
    opcache.interned_strings_buffer = 16
    opcache.max_accelerated_files = 20000
    opcache.memory_consumption = 256
    realpath_cache_size = 4096K
    realpath_cache_ttl = 600
    expose_php = off
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-conf
  namespace: mixerapi-docker
data:
  default.conf: |-
    server {
        listen 80;

        root /application/webroot/;
        index index.php;

        location / {
          try_files $uri /index.php?$args;
        }

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
            include fastcgi_params;
        }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-nginx-deployment
  namespace: mixerapi-docker
  labels:
    app: php-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-nginx
  template:
    metadata:
      labels:
        app: php-nginx
    spec:
      containers:
        - name: php
          image: mixerapidev/demo:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 9000
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: php-secret
                  key: database-url
            - name: SECURITY_SALT
              valueFrom:
                secretKeyRef:
                  name: php-secret
                  key: cakephp-salt
          volumeMounts:
            - name: php-ini
              mountPath: /usr/local/etc/php/conf.d
            - name: application
              mountPath: /application
          lifecycle:
            postStart:
              exec:
                command:
                  - "/bin/sh"
                  - "-c"
                  - >
                    cp -r /srv/app/. /application/.

        - name: nginx
          image: nginx:1.19-alpine
          ports:
            - containerPort: 80
          volumeMounts:
            - name: nginx-conf
              mountPath: /etc/nginx/conf.d
            - name: application
              mountPath: /application

      volumes:
        - name: php-ini
          configMap:
            name: php-ini
        - name: nginx-conf
          configMap:
            name: nginx-conf
            items:
              - key: default.conf
                path: default.conf
        - name: application
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: mixerapi-docker
spec:
  selector:
    app: php-nginx
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30002
---
apiVersion: v1
kind: Service
metadata:
  name: php
  namespace: mixerapi-docker
spec:
  selector:
    app: php-nginx
  ports:
    - protocol: TCP
      port: 9000
      targetPort: 9000

I had composer install running in a docker entrypoint, so I needed to move that into the Dockerfile for this to work. I updated my Dockerfile with this and removed install from my entrypoint if the ENV is prod:

RUN if [[ "$ENV" = "prod" ]]; then \
    composer install --prefer-dist --no-interaction --no-dev; \
fi
-- cnizzardini
Source: StackOverflow