[JAVA] Fn Project Beginning-Introduction of Fn Server and UI

Introduction

According to Publickey's "Java-enabled serverless platform" Fn Project ", released as open source by Oracle", the other day in the United States It seems that the Fn Project was announced at JavaOne.

Some of you have already written an article on this Qiita, but let's introduce Fn Project by referring to "Fn Project" on GitHub.

The work procedure is as follows. Note that "#" is the command to be executed by the root user, and "$" is the command to be executed by the docker user.

CentOS 7 64-bit virtual machine creation

  1. Create a CentOS virtual machine in Oracle VM VirtualBox on Windows 64-bit
  2. Install CentOS on the created virtual machine
  3. Since you can set the network with GUI at the time of installation, decide the IP address (this time "192.168.1.10")

Docker input

Fn Project was developed on the assumption that Docker will be used. So, first, let's introduce Docker. Check the virtual machine for old Docker with the "rpm -qa" command.

# rpm -qa | grep docker

If docker-common, docker-client, docker, etc. are output, delete them with the "rpm -e" command.

# rpm -e docker-common docker-client docker

Introduce Docker.

# yum install -y yum-utils
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum makecache fast
# yum -y install docker-ce

Register and activate the Docker service. This will allow Docker to start when you start the virtual machine.

# systemctl start docker
# systemctl enable docker.service

Check the status of the Docker service.

# ls /usr/lib/systemd/system | grep docker
# systemctl status docker.service

Introducing Fn Server

Next, refer to README.md of "[Fn Server repository] on GitHub (https://github.com/fnproject/fn)" and install Fn Server. One shot with the curl command.

# curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh

Start Fn Server with the "fn start" command.

$ (fn start > /dev/null 2>&1 &)

Now, if you check with the ps command, you can see that the Docker container has been kicked.

docker 15285 0.0 0.5 123704 11260 pts/0 Sl 00:34 0:00 docker run --rm -i --name functions -v /home/docker/data:/app/data -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 fnproject/functions

You can also check the status of the Docker container by running the "docker ps -a" command. You can see that the name of the Docker container is "functions" and it is serving on port number 8080.

CONTAINER ID IMAGE               COMMAND                CREATED STATUS               PORTS                            NAMES
2dd93c867a93 fnproject/functions "preentry.sh ./fun..." 12 seconds ago Up 11 seconds 2375/tcp, 0.0.0.0:8080->8080/tcp functions

There is a sample in Fn Server, access it from a browser with "http: // : 8080", and if the next returns, it is successful.

{"goto":"https://github.com/fnproject/fn","hello":"world!"}

function creation

Multiple apps can be registered in Fn Server, and multiple functions can be deployed for each app. It seems strange to translate function as "function", so I will write it as function here.

To create a function, create a directory and change to it.

$ mkdir -p ~/fn/hello
$ cd ~/fn/hello

The "fn init" command creates a template for the function project (development environment). Specify in which language you want to develop with the "--runtime" option.

$ fn init -runtime java

This will generate the following files:

├ func.yaml
├ pom.xml
└ src/
  ├ main/java/com/example/fn/HelloFunction.java
  └ test/java/com/example/fn/HelloFunctionTest.java

The sample is the usual Hello world. It works immediately without modification, so let's build and run it with "fn run".

$ fn run

Build with "fn run" and execute function locally if successful. Of course, the usual "Hello, world" is output.

Unlike a normal Java application, it is built on Docker, so it is not necessary to put Maven etc. in the virtual machine. However, it creates a Docker image every time you build it, so if you leave it alone, it will take up disk space. If you use Fn Project in your business, you may need to investigate what to do about it.

function deploy

If the build is successful, make sure Fn Server is running and deploy the function.

We use the "fn deploy" command to deploy, but by default it automatically pushes the Docker image, which is the product of the build, to Docker Hub. However, you don't need to push to Docker Hub during development, so specify the "--local" option. Also, specify the app name in "--app".

$ API_URL=http://192.168.1.10:8080 fn deploy --local --app myapp

If you do not specify API_URL, the default is "http: // localhost: 8080" and you cannot access it from outside the Docker image. Therefore, specify the IP address of the machine that put Fn Server in.

With the above, the application called myapp is deployed to Fn Server, but what happens to the function name is determined by the name of the parent directory. This time, I ran the "fn deploy" command on the "~ / fn / hello /" directory, so the function name was hello.

If you want to push to Docker Hub, log in to Docker Hub in advance as follows, and then execute the "fn deploy" command without the "--local" option.

$ docker login
$ export FN_REGISTRY=<Docker Hub account name>
$ API_URL=http://192.168.1.10:8080 fn deploy --app myapp

After deploying the function, you can check the function's routing with the "fn routes" command.

$ API_URL=http://192.168.1.10:8080 fn routes list myapp
path   image       endpoint
/hello hello:0.0.5 192.168.1.10:8080/r/myapp/hello

The path, image and endpoint are output as above, but as mentioned above, the version goes up steadily every time "fn run" is executed, and it is already 0.0.5. Regarding the difference between path and endpoint, when executing the function with CLI on the Fn Server input machine, specify the path as follows, and when executing it from the browser, "http://192.168.1.10:" of endpoint Throw a GET to "8080 / r / myapp / hello". In both cases, "Hello, world!" Is returned after executing the function.

$ fn call myapp /hello
Hello, world!

Fn User Interface input

To check the operating status of Fn Server and function with a Web browser, it is necessary to separately install the Fn User Interface (hereinafter, FnUI). FnUI also runs as a Docker container, so run the command as follows: When you execute this command for the first time, you will get the Docker image of "fnproject / ui" from Docker Hub, and then FnUI will start. This kind of convenience is a feature of Docker.

$ docker run --name fnui --rm -it -d --link functions:api -p 4000:4000 -e "API_URL=http://api:8080" fnproject/ui

After starting FnUI, if you GET to "http://192.168.1.10:4000/" from your browser, the following screen will be displayed.

FnUI

End processing

As mentioned earlier, both Fn Server and FnUI are Docker containers, so use the docker stop command to stop them.

$ docker stop fnui
$ docker stop functions

home work

The Fn Server itself does not have a flow function, and it is necessary to introduce Fn Flow separately, but I think that the appeal of the Fn Project depends on this flow function.

If you refer to "Serverless Sagas with Fn Flow", it seems that you can realize the flow function. As you can see in this article, "Flow Saga" in "Tutorial GitHub Repository" is a sample of Fn Flow. Also, the scripts in the "scripts" directory will be helpful.

Summary

The above is my impression of touching the Fn Project.

――The appeal of Fn Project depends on the flow functions such as Fn Flow and Fn Flow UI. --When deploying Fn Project in the cloud, it is necessary to find out how to automatically calculate billing. --The "fn xxx" command is positioned as a simple command, and strengthening the command group is likely to be an issue. --In particular, I can't find the command to delete the function deployed on Fn Server. --Since a Docker image is created every time you build a function, you need to consider another build method, deployment method, etc. --Fn UI checks the execution status of the function at short intervals of about 3 seconds. I want to know how to adjust the check interval

Recommended Posts

Fn Project Beginning-Introduction of Fn Server and UI
Automatically set the width and height of the UI Label