Skip to content

Instantly share code, notes, and snippets.

@matthewpalmer
Created July 21, 2018 04:13
Show Gist options
  • Save matthewpalmer/741dc7a4c418318f85f2fa8da7de2ea1 to your computer and use it in GitHub Desktop.
Save matthewpalmer/741dc7a4c418318f85f2fa8da7de2ea1 to your computer and use it in GitHub Desktop.
kubernetes nginx php-fpm pod
# Create a pod containing the PHP-FPM application (my-php-app)
# and nginx, each mounting the `shared-files` volume to their
# respective /var/www/html directories.
kind: Pod
apiVersion: v1
metadata:
name: phpfpm-nginx-example
spec:
volumes:
# Create the shared files volume to be used in both pods
- name: shared-files
emptyDir: {}
# Add the ConfigMap we declared above as a volume for the pod
- name: nginx-config-volume
configMap:
name: nginx-config
containers:
# Our PHP-FPM application
- image: my-php-app:1.0.0
name: app
volumeMounts:
- name: shared-files
mountPath: /var/www/html
# Important! After this container has started, the PHP files
# in our Docker image aren't in the shared volume. We need to
# get them into the shared volume. If we tried to write directly
# to this volume from our Docker image the files wouldn't appear
# in the nginx container.
#
# So, after the container has started, copy the PHP files from this
# container's local filesystem (/app -- added via the Docker image)
# to the shared volume, which is mounted at /var/www/html.
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "cp -r /app/. /var/www/html"]
# Our nginx container, which uses the configuration declared above,
# along with the files shared with the PHP-FPM app.
- image: nginx:1.7.9
name: nginx
volumeMounts:
- name: shared-files
mountPath: /var/www/html
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
@rsidhaarth
Copy link

@lstellway methods work perfectly. But I wanted to know is there any issue running both nginx and php-fpm together?

@patricknelson
Copy link

@rsidhaarth This doc at least provides a reasonable argument against running them both together: https://cloud.google.com/architecture/best-practices-for-building-containers#package_a_single_app_per_container

I still feel like a good middle ground here might be to have a single Dockerfile (or multiple Dockerfile’s) based on the same core app image containing all your app files which then is used as a COPY --from source for your separate php-fpm and nginx daemon images. But, I know the practicality of that may depend heavily on your use case (e.g. build times). Personally I like shifting that to build time, since granted the images will be larger and the build time may increase (e.g. new nginx image for each deploy) but then the result is more granular and easily reproducible and less prone to ephemeral failures at runtime or lower startup cost (since files are already there). But that’s just my 2c since it’s hypothetical for me (right now I still roll php-apache which integrates PHP already in an Apache server container via mod_php, lol 😅)

@rsidhaarth
Copy link

rsidhaarth commented Mar 21, 2023

@patricknelson thanks for your reply

I have already done that in the docker swarm setup. I got both nginx and php-fpm in a single image and deployed them there. If that is the best way, I will do the same for k8s.

Also, for persistent storage, what method is recommended?

Longhorn, NFS, GFS etc

I am trying to use K8s for Magento 2 setup.

@patricknelson
Copy link

Also, for persistent storage, what method is recommended?

I guess whatever the default is from your provider. Are you talking about shared storage? If so, I've used NFS in Google Cloud which is Filestore for shared persistent volumes (minimum is 1TB basic HDD @ $204/mo for a single instance). Or I guess you could roll our own GlusterFS or NFS server. Otherwise, just go with whatever the default is.

https://kubernetes.io/docs/concepts/storage/persistent-volumes/

@rsidhaarth
Copy link

rsidhaarth commented Mar 21, 2023

Yes, I am speaking about shared storage that supports ReadWriteMany, and my provider needs to provide one. So I have to build my one storage. For Magento 2, the recommended is GFS, and I am exploring Longhorn now.

BTW, I just tried to deploy both nginx and php-fpm as a single images and now my deployment file is straightforward, and execution is also faster.

  1. there is no need of initcontainer
  2. there is no need for copy commands as the image has all the files
  3. deployment is faster as it just going to pull one image and deploys one container

@patricknelson
Copy link

Next up to test/validate your cluster, do some pre-prod testing. Scale it up and do some load testing! Pound it with a bunch of traffic (e.g. Gatling). Then, maybe delete a line of code on one of the containers, see how easy it is for you to see/diagnose errors when only some of your requests are failing. When your new persistent storage is setup, put a 3mb .jpg on one of the containers in the shared volume and see if it’s available on all the others and/or how long it takes to sync.

… good luck!

@lstellway
Copy link

For the record, I have since moved towards bundling PHP + the web server in the same image.
I believe I followed TrafeX/docker-php-nginx as an example.

@rsidhaarth
Copy link

For the record, I have since moved towards bundling PHP + the web server in the same image.
I believe I followed TrafeX/docker-php-nginx as an example.

Thanks for sharing this. I will make use of it :)

@patricknelson
Copy link

Same, that looks like a mature and well implemented image @lstellway.

One of those times I really wish Github Gists had reactions. That way we could just react with 👍 or whatever instead of spamming the thread. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment