Skip to content

An example of remote debugging a Python Application(Flask) on Kubernetes/OpenShift with VSCode

Notifications You must be signed in to change notification settings

MoOyeg/python-openshift-remote-debugging-vscode-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

python-openshift-remote-debugging-vscode-example

Repo provides an example of remote debugging a Python Application(Flask) on Kubernetes/OpenShift with VSCode.

Contents

  • Repo uses python debugpy as debugger.
  • Application used for example is testFlask Application
  • Example uses a Dockerfile to build application image.
  • Dockerfile entrypoint points to bash script that helps determine if remote debugging is needed via the use of environment variables.
  • if Debugging is needed bash script will run python flask entrypoint file which imports debug intializer from another file if required.

Helpful-Sources

General Prerequisites

  • OpenShift Cluster - Version>=4.9
  • oc client >= 4.9
  • oc client must be logged in with user with sufficent privileges to run below commands.

Steps to Run

  • Source Environment Variables

    eval "$(curl https://raw.githubusercontent.com/MoOyeg/testFlask/master/sample_env)"
  • Create necessary projects
    oc new-project $NAMESPACE_DEV
    oc new-project $NAMESPACE_PROD

  • Create a new application on openshift, using the oc new-app command.We will specificy environment variables we pre-configured in our entrypoint bash script to enable debugging for our application

    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
  • If the previous command gives this error - error: unable to locate any images in image streams, local docker images with name "image-registry.openshift-image-registry.svc:5000/openshift/ubi8"

    oc tag --source=docker registry.redhat.io/ubi8/ubi:latest ubi8:latest -n openshift
  • Expose the service to the outside world with an openshift route

    oc expose svc/$APP_NAME --port 8080 -n $NAMESPACE_DEV
  • If app build above is successful our application pod should start in debug mode. We can confirm by looking at application logs.

    oc logs deployment/$APP_NAME -n $NAMESPACE_DEV

    Sample Logs:

  • With our application debugger listening we need to port-forward from our debug service to allow vscode connect.

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

    Sample Port-Forward:

  • With our debugger accessible we need to configure our vscode instance to connect to the debugging session. Port-Forward session has to continously run for duration of debugging

    • Clone the original source code

         git clone https://github.com/MoOyeg/testFlask.git
    • Open folder in VSCode

    • Set your vscode setting.json to point to our created instance above and use the remote debug configuration. I have provided a sample settings.json with a remote debugger attach and local development example under .vscode folder.

      Sample Port-Forward:

    • Start debug session in vscode, we should see log in our application show a debug connection has been made.

      oc logs deployment/$APP_NAME -n $NAMESPACE_DEV

      Sample Connected Logs:

  • Calling our route URL for the application should trigger our breakpoints.

     curl $(oc get route/$APP_NAME -n $NAMESPACE_DEV -o jsonpath='{.spec.host}')

About

An example of remote debugging a Python Application(Flask) on Kubernetes/OpenShift with VSCode

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages