Remote Debugging Flask/Python Applications in OpenShift/Kubernetes and VS Code — Part 1.

Moyo Oyegunle
3 min readFeb 17, 2023

--

Remote debugging in OpenShift and Kubernetes can be an interesting problem. To speed up development it helps to debug and test your application with the platform services and other applications that might be required in production. This article series covers sample methods to accomplish remote debugging with your Python application running in OpenShift/Kubernetes.

Notes:

Github code for this example can be found here

Github code for Application being debugged can be found here.

We will use debugpy as our debugger due to it’s VS Code support.

Sources that inspired this:

Architecture:

So one of my goals was to make sure I could control when I wanted the application to run normally or when I needed it to run in debug mode. I wanted that control to be seamless and not require much application code change. So this is what I did:

Remote Debugging Flow Chart

Example:

Let’s set our environment variables on the shell:

eval "$(curl https://raw.githubusercontent.com/MoOyeg/testFlask/master/sample_env)"

We will create a new namespace for our Application:

oc new-project $NAMESPACE_DEV

Lets create our application on OpenShift, with our bash script as entrypoint and our REMOTE_DEBUG flag enabled:

oc new-app https://github.com/MoOyeg/testFlask.git --name=$APP_NAME -l app=testflask --env=REMOTE_DEBUG="true" --env=DEBUG_PORT=5679 -n $NAMESPACE_DEV --strategy=docker

Let’s expose our application with an OpenShift Route:

oc expose svc/$APP_NAME --port 8080 -n $NAMESPACE_DEV

Since REMOTE_DEBUG was enabled our application should start in debug mode. We can confirm our pod is in debug mode from its’ logs:

Debugger Pod logs

Since our application pods is now listening in debug mode we need to port-forward from our local machine to access. The below command is how we can do our port-forward:

oc port-forward service/$APP_NAME -n $NAMESPACE_DEV --address 0.0.0.0  5679:5679

Since our debug port is now available on our local machine we need to configure our VS Code instance to attach to the port-forward port to achieve debug. Here’s a sample settings.json, but the important bit is the remote configuration below:

{
//Debugger to allow Remote Attach to our application
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5679
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"justMyCode": true
},

You should be able to start your debug session with the “remote attach” configuration and set your breakpoints:

And your pod logs should show your VS Code debug session is attached:

oc logs deployment/$APP_NAME -n $NAMESPACE_DEV

Hopefully this helps you implement your own remote debugging option.

--

--

Moyo Oyegunle

Curious about technology. Write about technology that I work on and stories in the technology space.