Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How application folder should be properly shared between nginx and php-fpm containers in kubernetes?

I have php application with public folder, that contains as index.php that handles php requests as static files.

I would like to deliver static files via nginx container directly, and use php-fpm only for delivering dynamic requests.

How that can be achieved? As i understand, there are two ways:

  1. create two separate containers with same application folder: the first will be with nginx, that serves static and delegates dynamic requests to the second, where php-fpm would be located.
  2. Second solution is to use persistentVolumeClaim, but that also requires some kind of magic for synchronization (initContainers).

What is the best practice?

like image 452
avasin Avatar asked Oct 28 '22 22:10

avasin


1 Answers

Let's begin by saying that there are 2 pods which are Nginx Pod and Php pod. Now There are two things need to be associated with these pods.

  • Static files (Public folder)
  • Configuration file (nginx.conf)

I would prefer to use Persistent Volume claim for static files and ConfigMap for the configuration file.

Solutions to your issue.

Php pod will have following k8s resources

  • Deployment( containers )
  • Service (to expose it internal)
  • PersistentVolumeClaim (for public folder)
  • ConfigMap ( Environment Variable such as information about database )

Nginx pod will have following k8s resources

  • Deployment (Container)
  • Service (to expose it internal or external)
  • ConfigMap (customised nginx.conf)

Now Deployment Resouces will have a relationship with other resources such as Persistent volume claim and configmap etc.

As I understand by InitContainer, It helps Pod to have a consistent Network Namespace as well as organise file system for the main container it provides all the necessary resources to the main container such as service account, pvc, configmap.

For example, If the main container restart then init container make sure that IP address does not change.However, if you create pod again it means you are creating initcontainer again then Pod will have new IP address.

like image 152
Suresh Vishnoi Avatar answered Nov 14 '22 00:11

Suresh Vishnoi