Monday, March 13, 2023

TruffleRuby on OCI Container Instances

Elegance at Speed


Ruby is one of my favourite programming languages. I really appreciate how expressive it is, how productive (and how much fun) it makes development.

Unfortunately Ruby has a reputation for being slow compared to other languages such as JavaScript.

While the performance of the default Ruby interpreter has improved, it still hasn't had the engineering investment that goes into V8.

I want to introduce you to an alternative Ruby engine, and a really simple way to deploy Ruby applications in the cloud.

TruffleRuby


TruffleRuby is a high - performance implementation of Ruby, built on top of GraalVM.

GraalVM is a high - performance, polyglot virtual machine.

In benchmark tests, TruffleRuby often delivers superior performance to other Ruby implementations.

You can install TruffleRuby using commonly used Ruby managers such as RVM, chruby && ruby-install, and rbenv.

Once you've installed TruffleRuby, you can use it just like the standard Ruby engine. Typically I set TruffleRuby as my default runtime.

TruffleRuby is an open-source project. GraalVM is developed via the open - source project. GraalVM is available in two flavours:

◉ Community Edition (GraalVM CE) - the open - source edition
◉ Enterprise Edition - includes premium enhancements not included in the open - source edition

Enterprise Edition requires a subscription for production use. This is included at no additional cost for workloads running on Oracle Cloud infrastructure (OCI).

OCI Container Instances


Since I like Ruby, it's probably not a surprise that I like to keep things simple.

OCI Container Instances is a simple, serverless, cloud service for running applications in containers.

Essentially you just create an instance, specifying the image you want to use, and the service runs it for you.

So I thought I'd quickly show you how you can use Container Instances to run a simple Sinatra web application in a container, with TruffleRuby as the Ruby runtime.

Hands - On


Pre - requisites

◉ OCI account (paid or trial with credits)
◉ VCN with public subnet created

App

The app is about the simplest Sinatra app you can create:

require 'sinatra'

get '/' do
  'Hello world!'
end

Dockerfile

I'm going to use publicly available GraalVM CE images containing TruffleRuby to build from.

These don't require any license.

I install some dependencies for building native extensions and then the sinatra and thin gems.

Then copy in the app from above:

FROM ghcr.io/graalvm/truffleruby:latest as build
RUN dnf install -y xz patch
RUN gem install sinatra thin

FROM ghcr.io/graalvm/truffleruby:slim as runner
COPY --from=build /opt/truffleruby-22.3.0/lib/gems /opt/truffleruby-22.3.0/lib/gems
COPY myapp.rb .
ENV APP_ENV=production
EXPOSE 4567
CMD ["ruby","myapp.rb"]

Then build the image and push it to your repository of choice.

In my case, I have chosen to push it to a private repository in OCI. Since the plan is to deploy to OCI, this makes sense, but you could push it to any repository you have access to.

In my case the image is:
oracledeveloper/burton7/truffle:ce

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

Container Instance

Click on "Container Instances" and then "Create Container Instance":

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

Just accept the defaults for the moment and click "Next" (make sure your subnet is public):

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

Select your image and enter your credentials if you're using a private repository:

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

Then click "Next".

The final screen should look something like this:

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

Click create. You should get a screen showing an orange "CI" icon and the message "CREATING":

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

After a few minutes, the icon should change to green, and the public IP address of the instance will be displayed:

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

If you then open another tab with the URL http://<public IP>:4567 you should get the traditional "Hello world!" message:

OCI Container Instances, Oracle Java Certification, Oracle Java Prep, Oracle Java Preparation, Java Learning, Java Skills, Java Jobs, Java OCI, Java Learning

Source: oracle.com

Related Posts

0 comments:

Post a Comment