<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chris Ng on Medium]]></title>
        <description><![CDATA[Stories by Chris Ng on Medium]]></description>
        <link>https://medium.com/@chrisng_39139?source=rss-ae596acc137a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*b0iEkoo7VKlO6M2JcdJTvg.jpeg</url>
            <title>Stories by Chris Ng on Medium</title>
            <link>https://medium.com/@chrisng_39139?source=rss-ae596acc137a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 00:20:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chrisng_39139/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An easier way to optimize Kubernetes resources for PHP application]]></title>
            <link>https://medium.com/scmp-inside-the-wonton/an-easier-way-to-optimize-kubernetes-resources-for-php-application-2615a01aeb22?source=rss-ae596acc137a------2</link>
            <guid isPermaLink="false">https://medium.com/p/2615a01aeb22</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[grafana]]></category>
            <category><![CDATA[performance-testing]]></category>
            <dc:creator><![CDATA[Chris Ng]]></dc:creator>
            <pubDate>Thu, 05 Sep 2019 03:11:55 GMT</pubDate>
            <atom:updated>2019-09-06T09:51:39.883Z</atom:updated>
            <content:encoded><![CDATA[<h3>An easier way to optimize Kubernetes resources for PHP applications</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YaqYpUnzVVrd7QoL2HrPAg.png" /></figure><p>At the <a href="https://corp.scmp.com/"><em>South China Morning Post</em></a><em>,</em> we’ve recently been working on a way to leverage <a href="https://kubernetes.io/">Kubernetes</a> with PHP. We struggled with random errors from the backend between Nginx and PHP-FPM, and we found an easier way to resolve these issues. Let’s dive in to find out more.</p><p>Unlike other common programming languages and runtimes like NodeJS and Golang, PHP doesn’t have native WebSocket support. This means you normally would have to use a web server like Nginx as a proxy to serve your PHP requests, and it is required to run in an individual worker process to handle such requests.</p><p>At times, requests may return an error code like 502 (Bad Gateway) or 504 (Gateway Timeout) due to the following reasons:</p><ul><li>High system loads cause the PHP process manager to be too busy to accept requests from Nginx</li><li>Timeouts between Nginx and the PHP process manager: requests either take too long to be processed by PHP or are queued for too long in the process manager</li></ul><p>Using the PHP process manager, you have to configure as many child processes as your system can run. Essentially, this is the total number of parallel requests your system can handle in terms of CPU and memory.</p><p>Requests passed from Nginx may be queued or exceed the timeout limit when they surpass the number of idle PHP worker processes. Thus, in order to avoid errors caused by insufficient worker processes, you have to increase the number of worker processes.</p><p><em>Sounds easy right?</em></p><p>Unfortunately, it’s not. Because more worker processes are running at the same time, this consumes more CPU and memory. Therefore, one has to find the balance between the number of worker process and the processing time for each request. Otherwise, your requests will likely get stuck and time-out as overall CPUs are exhausted with too many workers processing the requests.</p><p><em>So how do we solve this?</em></p><p>Kubernetes enables you to dynamically orchestrate the system resources so you don’t need to perform mind numbing calculations. Instead, you can simply observe the usage patterns on a line graph. This is a massive time- and pain- saving upgrade from the typical standard of doing calculations based on the number of CPU cores or memory available in the system.</p><p>Let’s see how to deploy Nginx and distribute PHP requests to worker processes by TCP/IP on a Kubernetes cluster. We will also set up a PHP status dashboard using Prometheus and Grafana, which will allow us to fine-tune how many workers we need for each pod in a replica set by visualizing PHP worker process utilization and CPU usage.</p><p>To reduce the work of setting up the benchmark environment, we have provided a <a href="https://github.com/scmp/php-nginx-demo">repository</a> (<a href="https://github.com/scmp/php-nginx-demo">https://github.com/scmp/php-nginx-demo</a>) for this demonstration. You can easily replicate the setup in your cluster or even local Kubernetes through Minikube.</p><h4>Step 1 — Set up your environment</h4><p>Bring up all components in there including the namespace monitoring for monitoring tools:</p><pre>kubectl apply \<br>  --filename https://raw.githubusercontent.com/scmp/php-nginx-demo/master/k8s/all.yaml</pre><p>If you already have Prometheus and Grafana in the cluster, use the following command to apply only the Nginx and PHP-FPM, as this will only bring up PHP-FPM benchmark components without monitoring tools:</p><pre>kubectl apply \<br>  --filename https://raw.githubusercontent.com/scmp/php-nginx-demo/master/k8s/without-monitoring.yaml</pre><p>If everything is fine, you will see four pods running in your default namespace:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*EPUzByv6_Obzs492vSydtQ.png" /><figcaption>Screenshot after applied resources YAML.</figcaption></figure><p>To test the connectivity of the application, use port forward to forward Nginx service port to your local machine port:</p><pre>kubectl port-forward svc/php-benchmark-nginx 8080:8080</pre><p>Then we can use curl to test the result from the application:</p><pre>curl -i <a href="http://127.0.0.1:8080/bench.php?io=fast">http://127.0.0.1:8080/bench.php?io=fast</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*B-EZSk5zRxMOTkF3hb7TFQ.png" /><figcaption>curl result for benchmark app.</figcaption></figure><h4>Step 2— Set up your datasource</h4><p>You can access the Grafana by using the port forward function:</p><pre>kubectl port-forward --namespace monitoring service/grafana 3000:3000</pre><p>After that, you can access to the Grafana via browser at <em>http://127.0.0.1:3000</em></p><p>The login username and password are both grafana if you use the default setup.</p><p>To display metrics with graphs in Grafana, you need to configure the data source at first. The setting page is under Configuration / Data Sources / Add data source. Choose data source type as Prometheus. Use the following values for the configuration of the data source.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6fz4YCk5L8GLIWpMaLPm2A.png" /></figure><h4>Step 3— Setup Dashboard</h4><p>Import the dashboard using id 10757. The dashboard import page is under Dashboards / Manage / Import.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqiLWKMyxAJduTGt1ZKYjA.png" /><figcaption>Screenshot of the dashboard.</figcaption></figure><p>Reference: <a href="https://grafana.com/grafana/dashboards/10757">https://grafana.com/grafana/dashboards/10757</a></p><h3>Benchmarks and analysis</h3><p>We performed the following benchmarks using monitoring tools we set up and analyzed the performance bottleneck, which could be either PHP pool size or CPU.</p><ul><li><em>Small </em>PHP-FPM pool<em> </em>with <em>high latency</em> of I/O</li><li><em>Small </em>PHP-FPM pool with <em>no latency </em>of I/O</li><li><em>Large</em> PHP-FPM pool with <em>high latency</em> of I/O</li><li><em>Large </em>PHP-FPM pool<em> </em>with <em>no latency</em> of I/O</li></ul><h4>Small PHP-FPM pool</h4><pre>pm = static<br>pm.max_children = 10</pre><h4>Large PHP-FPM pool</h4><pre>pm = static<br>pm.max_children = 20</pre><p>In each benchmark, we used <a href="https://httpd.apache.org/docs/2.4/programs/ab.html">AB</a> commands to make a total of 1000 requests in 50 concurrent requests to a page.</p><p>To avoid any timeout caused by K8s cluster Ingress or your cloud provider’s load balancer, we will run the test against a service port directly using port forward.</p><pre>kubectl port-forward svc/php-benchmark-nginx 8080:8080</pre><h4>Small PHP-FPM pool with <em>high latency</em> of I/O</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0rcch2PTzdKOREqsNokunQ.png" /><figcaption><em>#1 High latency of I/O — Small Pool</em></figcaption></figure><p>Result:</p><pre>ab -c 50 -n 1000 -s 60 <a href="http://127.0.0.1:8080/bench.php?io=slow">http://127.0.0.1:8080/bench.php?io=slow</a><br>…<br>Document Path: /bench.php?io=slow<br>Document Length: 9 bytes<br>Concurrency Level: 50<br>Time taken for tests: 724.055 seconds<br>Complete requests: 1000<br>Failed requests: 0<br>Time per request: 36202.735 [ms] (mean)<br>…</pre><h4>Small PHP-FPM pool with no latency of I/O</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O87oHf4qnhYYFeLHHt4Eeg.png" /><figcaption><em>#2 No latency of I/O — Small Pool</em></figcaption></figure><p>Result:</p><pre>ab -c 50 -n 1000 -s 60 <a href="http://127.0.0.1:8080/bench.php">http://127.0.0.1:8080/bench.php</a><br>…<br>Document Path: /bench.php<br>Document Length: 9 bytes<br>Concurrency Level: 50<br>Time taken for tests: 554.607 seconds<br>Complete requests: 1000<br>Failed requests: 0<br>Time per request: 27730.363 [ms] (mean)<br>…</pre><h4>Large PHP-FPM pool with <em>high latency</em> of I/O</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FyiI1ItmjiOLkFVKRkzVBA.png" /><figcaption><em>#3 High latency of I/O — Large Pool</em></figcaption></figure><p>Result:</p><pre>ab -c 50 -n 1000 -s 60 <a href="http://127.0.0.1:8080/bench.php?io=slow">http://127.0.0.1:8080/bench.php?io=slow</a><br>…<br>Document Path: /bench.php?io=slow<br>Document Length: 9 bytes<br>Concurrency Level: 50<br>Time taken for tests: 578.587 seconds<br>Complete requests: 1000<br>Failed requests: 0<br>Time per request: 28929.370 [ms] (mean)<br>…</pre><h4>Large PHP-FPM pool with <em>no latency</em> of I/O</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aT6V5SbLrnepw9GagfgYbw.png" /><figcaption><em>#4 No latency of I/O — Large Pool</em></figcaption></figure><p>Result:</p><pre>ab -c 50 -n 1000 -s 60 <a href="http://127.0.0.1:8080/bench.php">http://127.0.0.1:8080/bench.php</a><br>…<br>Document Path: /bench.php<br>Document Length: 9 bytes<br>Concurrency Level: 50<br>Time taken for tests: 545.958 seconds<br>Complete requests: 1000<br>Failed requests: 0<br>Time per request: 27297.883 [ms] (mean)<br>…</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*2kCR6GRQRfsDRWgq1veR4w.png" /><figcaption>Time taken for the tests</figcaption></figure><h4>When CPUs are not utilized</h4><p>The first benchmark (<em>High latency of I/O — Small Pool</em>) took the longest time to finish. Some people tackle this issue by simply increasing the number of replicas for PHP deployment. In other words, increasing the number of PHP-FPM pool size horizontally to ensure you can satisfy the total increase in worker processes. However, this wastes CPU resources and perhaps not surprisingly, turns out more costly.</p><p>According to the usage graphs #1, we can see Worker Process utilization is always 100% (You can see some missing data caused by not enough worker process for scraping PHP status metric), but the CPU usage is not hitting the requested CPU (4 cores) at all, this proves PHP process is in idle state while waiting for I/O response, that means the PHP-FPM pool size can be increased to handle more concurrent requests as CPU is not fully utilized.</p><h4>When CPUs <em>are</em> utilized</h4><p>Conversely, in tests without I/O latency(Benchmark #2 &amp; #4), the run time is similar (554.607s vs 545.958s). Benchmark #3 (High latency of I/O — Large Pool) is a bit higher at 578.587s which makes sense, since each request was required to wait for an I/O response.</p><p>These results and the CPU usage graphs for Benchmarks #2, #3, and #4 reflects that CPUs are utilized, and while you still can increase the number PHP-FPM pool size per pod, but this means the average processing time per-request may take longer and hit a timeout.</p><p>If you are facing any 5xx error without PHP runtime error, you probably have to increase the CPU request/limit for PHP-FPM deployment or increase the number of replicas.</p><p>When increasing replicas are needed, the Horizontal Pod Autoscaler (<a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/">HPA</a>) is the best option. Increasing pod replica counts when average CPU usage is over 50% of requested CPU, allows PHP-FPM pool to have another 50% CPU as buffer to handle spike traffic before HPA spins up a new replica.</p><h4>Conclusion</h4><p>Kubernetes is our preferred system when configuring a web server for PHP applications. With monitoring tools for PHP-FPM to visualize PHP worker process and CPU utilization, it can give you a better way to optimize cluster resources for the following reasons:</p><ul><li>A better picture of the usage of the PHP-FPM pool</li><li>Auto scalability without manual configuration or calculations</li><li>More cost-effective, maximum CPU usage by handling just enough PHP requests.</li></ul><p>If you have any thoughts or questions, we would love to hear from you! Please leave us a comment below.</p><p><em>Credits to </em><a href="https://medium.com/u/7824769c4f8c"><em>Kelvin Yue</em></a><em> </em><a href="https://medium.com/u/bc84100ce7a6"><em>Rohit Tiwari</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2615a01aeb22" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scmp-inside-the-wonton/an-easier-way-to-optimize-kubernetes-resources-for-php-application-2615a01aeb22">An easier way to optimize Kubernetes resources for PHP application</a> was originally published in <a href="https://medium.com/scmp-inside-the-wonton">SCMP — Inside the Wonton</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>