Displaying running PHP 7.4 stack trace on Red Hat UBI Container
When running a PHP application in container, sometimes we need to find out what bottleneck is impacting the app's response time. This post documents the approach to do so, but still requiring access to root user in the container. The platform where I was running the PHP app is Red Hat Openshift Community (OKD) version 3.6, which is still using docker to run containers instead of Red Hat's podman.
Preparation - Locating the pod and the worker node
- Open openshift's console home
- Select your Project (namespace) containing the pod
- Select Application => Pods
- Choose your pod from the list.
- Note the worker node hosting the pod.
Install php debug-info
Normally, the openshift platform doesn't allow containers running as root. So are in this case, the container will be running as normal random-uid-user. But we need to install php debuginfo packages, thus we need to run as root. The trick is to access the node running the application's pods, so we need admin access on the worker node to do so. I am assuming you already have root access on the worker node.
- Access the worker node, locate the pod's container.
- Run bash shell inside the container as root user
docker exec -it -u root <containerid> bash - Install debuginfo for php
dnf debuginfo-install php - Exit the bash shell.
Install php gdbinit
Next, we need to use gdbinit from php sources.
- Open this url in your web browser. Replace PHP version with the one matching the one being used. Url : https://github.com/php/php-src/blob/PHP-7.4.30/.gdbinit
- Click on the 'Raw' button. The resulting url is like : https://raw.githubusercontent.com/php/php-src/PHP-7.4.30/.gdbinit
- Open pod's terminal from openshift console (or use oc rsh)
- Get the file using wget :
wget https://raw.githubusercontent.com/php/php-src/PHP-7.4.30/.gdbinit
Displaying stack trace
- Check pid of running apache process
ps afxw - Restart apache2 process
apachectl -k graceful - Check current pid of running apache processes
ps afxw - Now, check the pid list from step 1 and 3. The common number between these two list means these pids were not being restarted (still running some process), so debug them (debug only the child, not the parent)
gdb -p pid - Inside the gdb shell, dump stack trace
dump_bt executor_globals.current_execute_data - Quit the gdb (otherwise the php script would not continue)
quit
y
Closing remarks
This procedure is more generic than the previous tricks I have shared in this blog. This might not work on podman-based containers. Outside openshift ecosystem, php-fpm already has built in capability to dump stack trace for running scripts longer than some time treshold.
Comments