Remote Debugging Kubernetes Application with Xdebug and ngrok
Remote debugging a php application inside a kubernetes pod could be difficult. Most often our IDE is behind a NAT router that prevents direct communication between the pod and the IDE. In this case, out best bet is to deploy a DBGp proxy server on the NAT machine that connects our IDE with the pod. But not all XDebug clients support it - there is no SublimeText package or VIM plugin that supports DBGp proxy. Even worse is you don’t have control to configure the NAT machine and install the proxy server.
Kubernetes supports port-forwarding but that only works one way i.e. it’ll forward ports from local to the pod. But we need the other way around - run a server locally and listen to it in the pod. There is an open issue to have a support for this. Until they add the feature we need to find another way.
ngrok could expose our local network to internet by creating a secure tunnel between a public endpoint and a locally running network service. ngrok TCP tunnels allow you to expose any networked service that runs over TCP. To start a TCP tunnel:
After running the command, we’ll see an status like following:
You can find the public endpoint that exposes our local XDebug client running on port 9000. Now we need to copy this endpoint and put it in our php.ini and deploy the php application again:
It’s better to load these values from environment variables. Before starting the server, replace the php.ini settings with corresponding environment variables. A sample docker start script would like following:
We can also change these settings using ini_set() from within code, but I didn’t try if that works.
Now start debugging and if you have enabled remote logging, you’ll see logs like following: