I tried to move the Java compatible FaaS form "Fn Project"

As described in the article below, in Keynote of JavaOne 2017, the FaaS platform "[fn project] that Java also runs from Oracle ](Http://fnproject.io/) "has been released.

-[Breaking news] Java-compatible serverless platform "Fn Project", Oracle released as open source. JavaOne 2017

The main feature is

I can't measure how seriously I plan to grow it, but as far as I saw the presentation, it seemed quite interesting, so I tried it. The environment I tried is Windows 10 + Docker toolbox + WSL (Ubuntu), so please read it in your own environment as appropriate.

Installation

First, install the fn command

% curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
[sudo] password for koduki:
fn version 0.4.6
% fn -v
fn version 0.4.6

Let's run the sample program as it is.

% fn start
mount: permission denied (are you root?)
Could not mount /sys/kernel/security.
AppArmor detection and --privileged mode might break.
mount: permission denied (are you root?)
time="2017-10-03T12:25:53Z" level=info msg="datastore dialed" datastore=sqlite3 max_idle_connections=256
time="2017-10-03T12:25:53Z" level=info msg="no docker auths from config files found (this is fine)" error="open /root/.dockercfg: no such file or directory"
time="2017-10-03T12:25:53Z" level=info msg="available memory" ram=593510400
time="2017-10-03T12:25:53Z" level=info msg="Serving Functions API on address `:8080`"

        ______
       / ____/___
      / /_  / __ \
     / __/ / / / /
    /_/   /_/ /_/
        v0.3.135

^C2017/10/03 21:56:12 interrupt caught, exiting

The image of Docker is dropped behind the scenes, and the server starts up on port 8080 of localhost. Check below to see if it works. By the way, since I am using docker toolbox, I use the IP obtained by docker-machine ip default instead of localhost.

% curl http://192.168.99.100:8080
{"goto":"https://github.com/fnproject/fn","hello":"world!"}

I was able to confirm that the server started.

Try to create an app

I will continue to create Hello World-like apps.

First, create a directory to create the app.

% mkdir example-fn
% cd example-fn

Then create the application. This time I decided to try it in Java.

% fn init --runtime java

        ______
       / ____/___
      / /_  / __ \
     / __/ / / / /
    /_/   /_/ /_/

Runtime: java
Function boilerplate generated.
func.yaml created.
% find . -type f
./func.yaml
./pom.xml
./src/main/java/com/example/fn/HelloFunction.java
./src/test/java/com/example/fn/HelloFunctionTest.java

You have created a template for the configuration files func.yaml and Maven. Contents is like this.

% cat ./src/main/java/com/example/fn/HelloFunction.java
package com.example.fn;

public class HelloFunction {

    public String handleRequest(String input) {
        String name = (input == null || input.isEmpty()) ? "world"  : input;

        return "Hello, " + name + "!";
    }

}%

It's rather simple. Well, the recommended way to write it is to use Java FDK, which was introduced in the demo, but I will try it later.

Build

Let's build the created code.

% fn run
Building image koduki/example-fn:0.0.1
Sending build context to Docker daemon  13.82kB
Step 1/11 : FROM fnproject/fn-java-fdk-build:jdk9-latest as build-stage
.
.
.
Step 11/11 : CMD com.example.fn.HelloFunction::handleRequest
 ---> Running in 0cd0c81caa5a
 ---> c6e00fb47d8d
Removing intermediate container 0cd0c81caa5a
Successfully built c6e00fb47d8d
Successfully tagged koduki/example-fn:0.0.1
Hello, world!%

After running various builds with Docker, the execution result was output. As far as I can see from the log, it seems that the test is being executed at this timing. Awesome.

Deploy

Finally, deploy. As a precaution for deployment, if there is no option, Push to Docker Hub will be automatically inserted, so you need to log in to Docker Hub and create a project in advance. Please note that the project name will be the root directory name if nothing is described in func.yaml. Login to Docker Hub is as follows.

% #Magic to use Docker from WSL
% alias docker="docker -H tcp://192.168.99.100:2376 --tlsverify --tlscacert /mnt/c/Users/$USER/.docker/machine/machines/default/ca.pem --tlscert /mnt/c/Users/$USER/.docker/machine/machines/default/cert.pem --tlskey /mnt/c/Users/$USER/.docker/machine/machines/default/key.pem"
% docker login
Login Succeeded

Then, it is the important login. Do fn start in another terminal etc. to start the deployment destination server.

% API_URL=http://192.168.99.100:8080 fn deploy --app myapp
Deploying example-fn to app: myapp at path: /example-fn
Bumped to version 0.0.2
Building image koduki/example-fn:0.0.2
.
.
.
Successfully tagged koduki/example-fn:0.0.2
Updating route /example-fn using image koduki/example-fn:0.0.2...

Note that if you do not specify ʻAPI_URL, the default is http: // localhost: 8080, so it will not work with Docker Toolbox. You can also skip Pushing to Docker Hub by adding the --local` option.

Let's check that it has been deployed. First, check the routing

% API_URL=http://192.168.99.100:8080 fn routes list myapp
path            image                   endpoint
/example-fn     koduki/example-fn:0.0.3 192.168.99.100:8080/r/myapp/example-fn

It's very convenient to see which version is mapped. Try to access the listed endpoints.

% curl http://192.168.99.100:8080/r/myapp/example-fn
Hello, world!%

It was output properly!

Summary

Since it is Docker Native FaaS, it seems that it can be combined with k8s etc. and various things can be done. Next time, I would like to try Fn Flow and FDK.

Since it is not a pure managed service such as AWS Lambda, there may be some differences, but can it be used as the basis of a system that is easy to scale in an on-premise or cloud environment? I'm expecting it.

At the moment, there is a lack of documentation, so I hope that development will continue in the future.

Then Happy Hacking!

Recommended Posts

I tried to move the Java compatible FaaS form "Fn Project"
I tried to implement the Euclidean algorithm in Java
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
I tried to interact with Java
I tried to explain the method
I tried the Java framework "Quarkus"
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to translate the error message when executing Eclipse (Java)
I tried to summarize the methods of Java String and StringBuilder
[Java] I tried to make a maze by the digging method ♪
I tried to display the calendar on the Eclipse console using Java.
I tried to summarize the methods used
I tried to summarize Java lambda expressions
I tried the new era in Java
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I went to the Java Women's Club # 1
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I tried to make Basic authentication with Java
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
java I tried to break a simple block
I tried to implement deep learning in Java
I tried to output multiplication table in Java
I tried to set tomcat to run the Servlet.
I tried to create Alexa skill in Java
I tried to break a block with java (1)
[Small story] I tried to make the java ArrayList a little more convenient
I tried to organize the cases used in programming
[Java] I want to calculate the difference from the date
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Firebase push notification in Java
[Java 11] I tried to execute Java without compiling with javac
I tried to summarize the state transition of docker
[Java] I tried to solve Paiza's B rank problem
I tried to decorate the simple calendar a little
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried to reduce the capacity of Spring Boot
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried to implement Stalin sort with Java Collector
[Java] I tried to implement Yahoo API product search
~ I tried to learn functional programming in Java now ~
I tried to find out what changed in Java 9
I finished watching The Rose of Versailles, so I tried to reproduce the ending song in Java
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I tried to create a java8 development environment with Chocolatey
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I tried to modernize a Java EE application with OpenShift.
I tried to increase the processing speed with spiritual engineering
[Swift] I tried to implement the function of the vending machine
I want to use the Java 8 DateTime API slowly (now)
I want to create a form to select the [Rails] category
I tried to make Java Optional and guard clause coexist
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment little by little using docker
I tried to convert a string to a LocalDate type in Java
I tried to build the environment of WSL2 + Docker + VSCode