Learn how cloud development environments drive developer productivity.
Download the eBook!

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

The 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 4 CPUs and 8GB of RAM.

$ 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 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.12.13):

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: Create your okteto manifest

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

$ okteto init
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.

 ✓  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:

name: hello-world
command: bash
volumes:
  - /usr/local/bundle/cache
sync:
  - .:/opt/app
forward:
  - 8080:8080
  - 1234:1234

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

  • name: the name of the Kubernetes deployment you want to put on development mode.
  • command: the start command of the development container.
  • volumes: a list of paths in your development container to be mounted as persistent volumes. For example, this is useful to persist the bundle 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.

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 synchronizing binaries, build artifacts, git metadata, or dependencies like the vendor folders.

Step 4: Activate your development container

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

$ okteto up
 ✓  Persistent volume successfully attached
 ✓  Images successfully pulled
 ✓  Files synchronized
    Namespace: pchico83
    Name:      hello-world
    Forward:   8080 -> 8080
               1234 -> 1234

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 the application in hot-reload mode by running the following command:

default:hello-world app> 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 (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 development container, making it accessible via localhost. Test your application by running the command below in a local shell:

$ curl localhost:8080
Hello world!

Step 5: 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.

get "/" do
  message = "Hello world from the cluster!"
  message
end

Okteto will synchronize your changes to your development container and Sinatra automatically detects them and reloads your application. 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 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 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:

default:hello-world app> rdebug-ide --host 0.0.0.0 app.rb
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):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to okteto",
            "type": "Ruby",
            "request": "attach",
            "remotePort": "1234",
            "remoteHost": "localhost",
            "remoteWorkspaceRoot": "/opt/app",
            "cwd": "${workspaceRoot}",
        }
    ]
}

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

VS Code Debug View

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.

Pablo Chico de GuzmanCTO & Co-founder View all posts

Beginner’s Guide to Network Policies in Kubernetes

Everyone agrees that Kubernetes clusters are not secure by default. But the good thing is that Kubernetes gives you the tools to make it so. In this article,...

November 28, 2022
Avatar of Arsh SharmaAvatar of Arsh SharmaArsh Sharma

Beginner’s Guide to Kubernetes Deployments

Over the last five years, Kubernetes has seen rapid adoption as a container orchestrator. And for a good reason too. Containers solved the problem of being...

September 06, 2022
Avatar of Arsh SharmaAvatar of Arsh SharmaArsh Sharma