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