How to Develop and Debug Python Applications on Kubernetes

Image of How to Develop and Debug Python Applications on Kubernetes

Kubernetes is an open-source project for automating deployment, scaling, and management of containers. It has rapidly become the standard to run production workloads and the community around it is just great!

But developing on Kubernetes presents some challenges. The typical development workflow looks like this: write code, build a Docker image, push it to the registry, redeploy, validate your changes and repeat. This workflow is slow, and as anti-python as it could be. Python is famous for it’s quick Read-Eval-Print loop, after all. We don’t want to give that away when bulding Cloud Native applications.

Okteto was created to solve this problem. On this blog post, we will show you how Okteto improves the developer experience in Kubernetes for Python developers. You will be able to take full advantage of using an instant development environment, dependency caching, hot-reloading and even the PyCharm debugger while developing your application directly on Kubernetes.

Step 1: Deploy the Python Sample App

Get a local version of the Python Sample App by executing the following commands:

1
2
$ git clone https://github.com/okteto/python-getting-started
$ cd python-getting-started

The k8s.yml file contains the Kubernetes manifests to deploy the Python Sample App. Run the application by executing:

You can deploy to your own Kubernetes cluster or give Okteto Cloud a try. Okteto Cloud is a development platform for Kubernetes applications. Sign up today to get a free developer account with 4 CPUs and 8GB of RAM.

1
$ kubectl create -f k8s.yml
1
2
deployment.apps "hello-world" created
service "hello-world" created

This is cool! You typed one command and a dev version of your application just runs 😎.

Step 2: Install the Okteto CLI

The Okteto CLI is an open-source project that lets you develop your applications directly on Kubernetes while taking advantage of your language’s toolkit. We will use it to speed up our development cycle instead of using the typical development workflow based on building docker images and redeploying containers.

Install the Okteto CLI >= 1.9.0:

MacOS / Linux
1
$ curl https://get.okteto.com -sSfL | sh
Windows Download https://downloads.okteto.com/cli/okteto.exe and add it to your `$PATH`.

Step 3: Create your okteto manifest

To start developing on the Python Sample App you first need to create an okteto manifest.
With the Python Sample App deployed, run the following command to create your okteto manifest:

1
$ okteto init
1
2
3
4
5
6
7
This command walks you through creating an okteto manifest.
It only covers the most common items, and tries to guess sensible defaults.
See https://okteto.com/docs/reference/manifest for the official documentation about the okteto manifest.
Use the arrow keys to navigate: ↓ ↑ β†’ ←
Select the deployment you want to develop:
β–Έ hello-world
Use default values

The okteto init command will scan the available deployments in your Kubernetes namespace, and ask you to pick one.
Select the hello-world deployment. It’s the one we deployed on the previous step.

1
2
3
4
βœ“  hello-world
βœ“ Deployment 'hello-world' successfully analyzed
βœ“ okteto manifest (okteto.yml) created
i Run 'okteto up' to activate your development container

The okteto init command creates the following okteto.yml file:

1
2
3
4
5
6
7
8
9
10
11
name: hello-world
image: okteto/python:3
command: bash
volumes:
- /root/.cache/pip
sync:
- .:/usr/src/app
forward:
- 8080:8080
reverse:
- 9000:9000

This file defines how to activate a development container for the Python Sample App:

  • name: the name of the Kubernetes deployment you want to put on development mode.
  • image: the image used by the development container. More information on development images here.
  • command: the start command of the development container.
  • volumes: a list of paths in your development container to be mounted as persistent volumes. This is useful to persist the pip cache.
  • sync: the folders that will be synchronized between your local machine and the development container.
  • forward: a list of ports to forward from your development container.
  • reverse: a list of ports to reverse forward from your development container to your local machine.
  • resources: the resources configuration (CPU and memory) of the development container.

Also, the okteto init command creates a .stignore file to indicate which files shouldn’t be synchronized to your development container.
This is useful to avoid virtual environments, build artifacts or git metadata.

Step 4: Activate your development container

Next, execute the following command to activate your development container:

1
$ okteto up
1
2
3
4
5
6
7
8
9
10
 βœ“  Development container activated
βœ“ Files synchronized
Namespace: default
Name: hello-world
SSH: 60224 -> 2222
Forward: 8080 -> 8080
Reverse: 9000 <- 9000

Welcome to your development container. Happy coding!
default:hello-world app>

Working in your development container is the same as working on your local machine.
Start by creating a virtual environment:

1
default:hello-world app> python -m venv env

and activate it:

1
default:hello-world app> source env/bin/activate

Install your pip dependencies (your dependencies will be persisted in future okteto up executions):

1
(env) default:hello-world app> pip install -r requirements.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Collecting Click==7.0
Downloading Click-7.0-py2.py3-none-any.whl (81 kB)
|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 81 kB 2.7 MB/s
Collecting Flask==1.1.1
Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 94 kB 2.8 MB/s
Collecting itsdangerous==1.1.0
Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2==2.10.3
Downloading Jinja2-2.10.3-py2.py3-none-any.whl (125 kB)
|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 125 kB 24.0 MB/s
Collecting MarkupSafe==1.1.1
Downloading MarkupSafe-1.1.1.tar.gz (19 kB)
Collecting pydevd-pycharm==192.7142.79
Downloading pydevd-pycharm-192.7142.79.tar.gz (3.2 MB)
|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 3.2 MB 23.2 MB/s
Collecting Werkzeug==0.16.0
Downloading Werkzeug-0.16.0-py2.py3-none-any.whl (327 kB)
|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 327 kB 34.3 MB/s
Using legacy 'setup.py install' for MarkupSafe, since package 'wheel' is not installed.
Using legacy 'setup.py install' for pydevd-pycharm, since package 'wheel' is not installed.
Installing collected packages: Click, MarkupSafe, Jinja2, Werkzeug, itsdangerous, Flask, pydevd-pycharm
Running setup.py install for MarkupSafe ... done
Running setup.py install for pydevd-pycharm ... done
Successfully installed Click-7.0 Flask-1.1.1 Jinja2-2.10.3 MarkupSafe-1.1.1 Werkzeug-0.16.0 itsdangerous-1.1.0 pydevd-pycharm-192.7142.79

Start the application in development mode by running the following command:

1
default:hello-world app> python app.py
1
2
3
4
5
Starting hello-world server...
* Serving Flask app "app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

Test your application by running the command below in another local shell:

1
$ curl localhost:8080
1
Hello world!

Step 5: Develop directly on Kubernetes

Open the app.py file in your favorite local IDE and modify the response message on line 7 to be Hello world from the cluster!. Save your changes.

1
2
3
4
@app.route('/')
def hello_world():
return 'Hello world from the cluster!'
}

Okteto will synchronize your changes to your development container.
Flask’s auto-reloader will detect the changes automatically and restart the application with the new code.

1
2
3
4
5
 * Detected change in '/okteto/app.py', reloading
* Restarting with stat
Starting hello-world server...
* Debugger is active!
* Debugger PIN: 281-298-026

Call your application from a local shell to validate the changes:

1
$ curl localhost:8080
1
Hello world from the cluster!

Cool! Your code changes were instantly applied to Kubernetes. No commit, build or push required 😎!

Step 6: Debug directly in Kubernetes

Okteto enables you to debug your applications directly from your favorite IDE.
Let’s take a look at how that works in one of python’s most popular IDE’s, PyCharm.

For VS Code users, this document explains how to configure the debugger with debugpy.

First, open the project in PyCharm, remove the comments on app.py line 20.

1
2
3
4
5
6
if __name__ == '__main__':
print('Starting hello-world server...')
# comment out to use Pycharm's remote debugger
attach()

app.run(host='0.0.0.0', port=8080)

Second, launch the Remote Debug Server by clicking on the Debug button on the top right. Ensure that the Debug Tool Window shows the Waiting for process connection.. message.
This message will be shown until you launch your app on the development container shell and it connects to the Debug Server.

1
2
3
4
5
6
7
8
Starting hello-world server...
* Serving Flask app "app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
* Restarting with stat
Starting hello-world server...
Connecting to debugger...

On your local machine, switch to the Debug Tool Window. Once the app connects it will show the connection to the pydev debugger.
Press the resume button to let the execution continue.

Add a breakpoint on app.py, line 10, and then call your application from your local shell:

1
$ curl localhost:8080

The execution will halt at your breakpoint. You can then inspect the request, the available variables, etc. Your code is executing in your development container in Kubernetes, but is being debugged on the local machine. Pretty cool no? πŸ˜‰

How does it work?

The PyCharm project includes a debugging configuration to start the remote debug server and to listen for connections on localhost:9000. The Okteto manifest is configured to start a reverse tunnel on port 9000 when the development container is activated.

This way, when the service it’s started in your development container, the connection is sent back to your local machine through the reverse tunnel, where the remote debug server receives and starts the debugging process.

This configuration allows you debug your application while fully integrated in Kubernetes. And the coolest thing is that since all of this is described in your .idea and Okteto manifests, everyone collaborating in your project will get the exact same configuration by simply running okteto up πŸ€–.

Conclusions

Kubernetes has the potential to be a great development platform, providing replicable, resource-efficient and production-like development environments. We have shown you how to use Okteto to create a development workflow that also lets you take advantage of features like incremental builds, debuggers or hot reloads while developing your application directly on Kubernetes.

Visit our website to know more about how to improve your team developer productivity with Okteto. Follow us on Twitter and join our #okteto channel in the Kubernetes community Slack to share your feedback with our community.