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 in 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 in Kubernetes.
Step 1: Deploy the Python Sample App
Get a local version of the Python Sample App by executing the following commands:
$ git clone https://github.com/okteto/python-getting-started
$ cd python-getting-started
The k8s.yml
file contains the Kubernetes manifests to deploy the Go Sample App. Run the application by executing:
$ kubectl create -f k8s.yml
deployment.apps "hello-world" created
service "hello-world" created
One command and a dev version of your application is ready to go 😎.
Step 2: Install the Okteto CLI
The Okteto CLI is an open-source project that lets you develop your applications directly in Kubernetes while taking advantage of your language's toolkig. 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:
MacOS / Linux
$ curl https://get.okteto.com -sSfL | sh
Windows
Download https://downloads.okteto.com/cli/okteto.exe and add it to your `$PATH`.
Step 3: Start your development environment in Kubernetes
With the Python Sample Application deployed, run the following command:
$ okteto up
✓ Development environment activated
✓ Files synchronized
Namespace: rberrelleza
Name: hello-world
Forward: 8080 -> 8080
5678 -> 22
Reverse: 3500 <- 3500
Welcome to your development environment. Happy coding!
okteto>
The okteto up
command starts a Kubernetes development environment, which means:
- The Python Sample App container is updated with the docker image
okteto/python:3
. This image contains the required dev tools to build, test, debug and run the Python Sample App. - A file synchronization service is created to keep your changes up-to-date between your local filesystem and your application pods.
- Container port 8080 (the application) is forward to localhost.
- Container post 3500 (the debugger) is reverse forwarded to localhost.
- A remote shell is started in your Kubernetes development environment. Build, test and run your application as if you were in your local machine.
All of this (and more) can be customized via the okteto.yml
manifest file:
name: hello-world
image: okteto/python:3
command:
- bash
workdir: /okteto
forward:
- 8080:8080
reverse:
- 3500:3500
You can also use the file
.stignore
to skip files from file synchronization. This is useful to avoid synchronizing virtual environments or git metadata.
Working in your remote development environment is the same as working on your local machine. You first create your virtual environment using the remote shell:
okteto> python3 -m venv venv
Activate it:
okteto> source venv/bin/activate
Install the service dependencies:
(venv)okteto> pip install -r requirements.txt
Collecting Click==7.0 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
...
Using a virtual environment in your development environment has two great benefits: You don't need to rebuild your docker image every time you add new dependencies, and the virtual environment will be cached and restored automatically when you relaunch your development environment.
And then start the application:
(venv)okteto> python app.py
Starting hello-world server...
* Serving Flask app "app" (lazy loading)
* Environment: debug
* Debug mode: off
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
Test your application by running the command below in a local shell:
$ curl localhost:8080
Hello world!
Step 4: Develop directly in 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.
@app.route('/')
def hello_world():
return 'Hello World from the cluster!'
}
Okteto will synchronize your changes to your development environment in Kubernetes. Flask's auto-reloader will detect the changes automatically and restart the application with the new code.
* 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:
$ curl localhost:8080
Hello world from the cluster!
Cool! Your code changes were instantly applied to Kubernetes. No commit, build or push required 😎!
Step 5: 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.
First, open the project in PyCharm, remove the comments on app.py
line 20
.
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 script on the remote machine, and this script will connect to the Debug Server.
Finally, stop and star the service again:
(venv)okteto> python app.py
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 service 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:
$ 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 environment in Kubernetes, but is being debugged on the local machine. Pretty cool no? 😉
How does it works?
The PyCharm project includes a debugging configuration to start the remote debug server and to listen for connections on localhost:3500
. The Okteto manifest is configured to start a reverse tunnel on port 3500 when the development environment is launched.
This way, when the service it's started in your remote development environment, 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, hot reloaders or debuggers while developing your application directly in Kubernetes.
Accelerate your development and start developing directly in Kubernetes today. Let us know what you think about it on Twitter, or in the Okteto channel in the Kubernetes slack.
Top comments (0)