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.
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
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
One command and a dev version of your application is ready to go 😎.
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
curl https://get.okteto.com -sSfL | sh
WindowsDownload https://downloads.okteto.com/cli/okteto.exe and add it to your `$PATH`.
With the Ruby Sample Application deployed, run the following command:
✓ Development container activated
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:
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:
[2020-03-20 09:23:04] INFO WEBrick 1.6.0
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:
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
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:
Hello world from the cluster!
Cool! Your code changes were instantly applied to Kubernetes. No commit, build or push required 😎!
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:
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):
Add a breakpoint on
app.rb, line 8. Call your application by executing from your local shell:
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.