How to Develop and Debug Ruby Applications in 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 Kubernetes is not designed for developers. 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 flow is not only slow, but it also prevents us from benefiting from standard features of Ruby tools such as hot reloaders or debuggers.
Okteto was created to solve this problem. On this blog post, we will show you how Okteto improves the developer experience in Kubernetes for Ruby developers. You will be able to take full advantage of using an instant development environment, dependency caching, hot-reloading and even the Ruby debugger while developing your application directly in Kubernetes.
Step 1: Deploy the Ruby Sample App
For this post, we’ll be using a very simple Sinatra web app to show you how Okteto speeds up your development cycle. Execute the following commands to get a local version of the app.
$ git clone https://github.com/okteto/ruby-getting-started
$ cd ruby-getting-started
k8s.yml file contains the Kubernetes manifests to deploy the Ruby 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 4CPUs and 8GB of RAM.
$ kubectl create -f k8s.ymldeployment.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 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 by running the following command:
$ curl https://get.okteto.com -sSfL | sh
$ wget https://downloads.okteto.com/cli/okteto-Windows-x86_64 -OutFile c:\windows\system32\okteto.exe
Step 3: Activate your development container in Kubernetes
With the Ruby Sample Application deployed, run the following command:
$ okteto up ✓ Development container activated
✓ Files synchronized
Forward: 8080 -> 8080
1234 -> 1234Welcome to your development container. Happy coding!
okteto up command starts a development container, which means:
- The Ruby Sample App container is updated with the docker image
okteto/hello-world:ruby-dev. This image contains the required dev tools to build, test, debug and run the Ruby Sample App. Check the Dockerfile to see how it is generated.
- A file synchronization service is created to keep your changes up-to-date between your local filesystem and your development container.
- Container port 8080 (the application) and 1234 (the debugger) is forward to localhost.
- A remote shell is started in your development container. 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:
command: [ “bash” ]
You can also use the file
.stignoreto skip files from file synchronization. This is useful to avoid synchronizing
vendordependencies or git metadata.
Working in your development container is the same as working on your local machine. Start the application in hot-reload mode by running the following command:
okteto> ruby app.rb[2020–03–20 09:23:04] INFO WEBrick 1.6.0
[2020–03–20 09:23:04] INFO ruby 2.7.0 (2019–12–25) [x86_64-linux]
== Sinatra (v184.108.40.206) has taken the stage on 8080 for development with backup from WEBrick
[2020–03–20 09:23:04] INFO WEBrick::HTTPServer#start: pid=66 port=8080
Okteto automatically forwards port
8080 from your local computer to the development container, making it accessible via
localhost. Test your application by running the command below in a local shell:
$ curl localhost:8080Hello world!
Step 4: Develop directly in Kubernetes
app.rb file in your favorite local IDE and modify the response message on line 7 to be Hello world from the cluster!. Save your changes.
get “/” do
message = “Hello world from the cluster!”
Okteto will synchronize your changes to your development container in Kubernetes and Sinatra automatically detects them and reloads your application. Call your application from a local shell to validate the changes:
$ curl localhost:8080Hello 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 VS Code, one of the most popular IDEs for Ruby development. If you haven’t done it yet, install the Ruby extension available from Visual Studio marketplace. This extension comes with debug definitions covering the default
ruby-debug-ide client setup.
Now, cancel the execution of
ruby app.rb from the development container shell by pressing
ctrl + c. Rerun your application in debug mode:
okteto> rdebug-ide — host 0.0.0.0 app.rbFast Debugger (ruby-debug-ide 0.7.0, debase 0.2.4.1, file filtering is supported) listens on 0.0.0.0:1234
Open the Run view in VS Code and run the Connect to okteto debug configuration (or press the F5 shortcut):
“name”: “Connect to okteto”,
Add a breakpoint on
app.rb, line 8. Call your application by executing from your local shell:
$ curl localhost:8080
The execution will halt at your breakpoint. You can then inspect the request, the available variables, etc…
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 hot reloaders or debuggers while developing your application directly in Kubernetes.