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

Moyo Oyegunle
4 min readFeb 12, 2024

--

This article series covers sample methods to accomplish remote debugging your python application running in OpenShift/Kubernetes. In Part 1 we showed a remote debugging method for Python Applications that used Port-forwarding to enable an external debugger session. While this allowed us to effectively debug our applications there are some limitations:

  • Application changes were not hot re-loadable, this required that we rebuilt a new image for any code changes pulled from version control. This made it hard to quickly test changes.
  • We had to maintain the port-forward session continuously in a separate terminal. This increased the overhead of that method.

What would be nice would be some sort of middleware that synced our local files into our remote pod and manged our port-forwarded session. This is exactly what ODO helps us do. ODO tries to help streamline and improve container based application development. It uses a Devfile to describe the application and it’s resources to be built,run and debugged.

#Annotated DevFile Example
commands:
- exec:
commandLine: Build Command here
component: container-name
group:
kind: build #Specifies this is a command to build container environment
workingDir: ${PROJECTS_ROOT}
id: build
- exec:
commandLine: Run Command Here
component: container-name
group:
kind: run #Specifies this a command to be run at startup
hotReloadCapable: true
id: run
- exec:
commandLine: Debug Command Here
component: container-name
group:
kind: debug
id: debug #Specifies this is a command for a debug session
components:
- container: #container-spec
endpoints: #Application and Debug Endpoints
env: #Env variable
name: #name
metadata:
architectures:
- amd64
description: Devfile to help deploy and debug a demo flask application
displayName: Python
icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/python.svg
language: Python
schemaVersion: 2.2.0

There is a devfile registry which provides sample devfiles for different application types. Let’s use OpenShift Toolkit as part of an example below. OpenShift ToolKit is an extension that can leverage ODO functionality in the VSCode IDE.

OpenShift Toolkit — Build and Debug Application

Using the ‘Start Dev’ option via the OpenShift Toolkit Extension will create the container resources we specified in our DevFile, copy our project code into the created container and run the command steps(build,run,debug) against our Kubernetes Cluster.

Debug Session Created

So a few things to notice about the screenshot above

  • The terminal logs show that ODO ran our build steps — and that was to run the python requirements file.
  • ODO has automatically created port forwarding sessions to the ports we specified in our spec file. It forwarded 20001 to port 5679 which was the port we specified for our debug port. It forwarded 20002 to 5000 which is our flask port. This reduces the effort required to maintain the port-forwarding sessions ourselves.
  • ODO is watching our project directory for any file changes ready to sync them into the pod.

Running a debug session is as simple as added our forwarded port to our VSCode launch.json file. An example provided below.

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

And with that we can start the remote debug session.

Debug Session Active

As discussed ODO also solves our second problem of keeping files in sync between our local and remote

Automatic Syncing Files into Container

From our screenshot above our sample_env file changed. ODO which was watching the directory synced our changed file into the container. If your debugger supports Hot Reload which debugpy does, the changes can be made without closing the VS Code debug session. This significantly speeds up the ease of prototyping changes.

An extra feature that ODO provides is a Web Console,and it’s quite useful. The Web Console provides a UI/Builder to make changes to the Devfile and the resources required for debugging. The UI also provides a chart that shows the steps involved in the Devfile.

ODO Web Console DevFile Chart

--

--

Moyo Oyegunle

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