How to Develop and Debug Ruby Applications in Kubernetes

Image of 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.

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

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

1
$ kubectl create -f k8s.yml
1
2
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 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:

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: Start your development environment in Kubernetes

With the Ruby Sample Application deployed, run the following command:

1
$ okteto up
1
2
3
4
5
6
7
8
9
 ✓  Development environment activated
✓ Files synchronized
Namespace: pchico83
Name: hello-world
Forward: 8080 -> 8080
1234 -> 1234

Welcome to your development environment. Happy coding!
okteto>

The okteto up command starts a Kubernetes development environment, 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 application pods.
  • Container port 8080 (the application) and 1234 (the debugger) is forward 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:

1
2
3
4
5
6
7
name: hello-world
image: okteto/hello-world:ruby-dev
command: [ "bash" ]
workdir: /opt/app/
forward:
- 8080:8080
- 1234:1234

You can also use the file .stignore to skip files from file synchronization. This is useful to avoid synchronizing vendor dependencies or git metadata.

Working in your remote development environment is the same as working on your local machine. Start the application in hot-reload mode by running the following command:

1
okteto> ruby app.rb
1
2
3
4
[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 (v2.0.8.1) 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 remote development environment, making it accessible via localhost. Test your application by running the command below in a local shell:

1
$ curl localhost:8080
1
Hello world!

Step 4: Develop directly in Kubernetes

Open the 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.

1
2
3
4
get "/" do
message = "Hello world from the cluster!"
message
end

Okteto will synchronize your changes to your development environment in Kubernetes and Sinatra automatically detects them and reloads your application. 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 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 remote shell by pressing ctrl + c. Rerun your application in debug mode:

1
okteto> rdebug-ide --host 0.0.0.0 app.rb
1
Fast 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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to okteto",
"type": "Ruby",
"request": "attach",
"remotePort": "1234",
"remoteHost": "localhost",
"remoteWorkspaceRoot": "/app",
"cwd": "${workspaceRoot}",
}
]
}

Add a breakpoint on app.rb, line 8. Call your application by executing 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…

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 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 our #okteto channel in the Kubernetes community Slack.