We're running a php-fpm container in Kubernetes. This is a test setup, so to preserve resources, php-fpm (8.1) is configured with:
pm = ondemand
pm.max_children = 5
pm.max_requests = 1000
The application is not in use (e.g. no requests to the application, nothing in the access log). Still, there is always one worker process running in this pool.
Question: How can I find out what is causing php-fpm to spawn this worker?
Things I looked at:
- netstat: php-fpm: master process listening on port 9000, and connected to two unix sockets (which would be stdout and stderr), nothing else
- access logs: empty
- config: pm.min_spare_servers is set to 1 (which is the default) but the config file states this is only used with the 'dynamic' scheduler
- when running php-fpm with identical config outside of Kubernetes no children are spawned (or all of them exit) when the site is idle
You are correct in your analysis. The reason you see one worker process running in your php-fpm container with the ondemand process manager and pm.min_spare_servers not set to 1 is likely due to the default behavior of the ondemand mode.
In ondemand Mode, the worker process only begins in response to requests. It does, however, maintain the existence of one worker process indefinitely to handle possible increases in traffic or the initial request. This keeps each request from starting fresh.
pm.min_spare_servers As you mentioned the settings only apply to dynamic process managers, which pre-forks a certain number of worker processes to be ready. It doesn’t influence the ondemand behavior.
While there might not be an external cause spawning this worker in your current scenario. In your test environment keeping one worker process alive with ondemand mode is a reasonable approach to balance resource conservation with handling potential traffic increases.
Consider static Mode for production if you move this setup to production and know the expected traffic pattern, consider using the static process manager.This mode starts a fixed number of worker process based on your configuration. (eg; pm.max_children)
Consider using a php-fpm process manager strategy that better suits your kubernetes setup.
Refer to this PHP Manual configuration for more information.
EDIT 1
The ondemand mode in php-fpm should only spawn worker processes when requests arrive.Once a request is served, the worker processes should terminate by default.
In a non Kubernetes environment,running php-fpm with ondemand mode and no incoming requests should result in only the master process being active, just as you are experiencing.
Some Kubernetes deployments utilize Liveness and readiness probes to monitor the health of pods. These probes might periodically trigger worker spawning even with no incoming requests.