先日、JavaOne San Francisco でファンクションのプラットフォームである Fn Project が発表されました。
Fn Project - The Container Native Serverless Framework
プロジェクトの Github によると、拡張性かつ高性能なコンテナネイティブなプラットフォームをクラウドやオンプレ問わずさまざまな環境でJavaのみならずGo/Ruby/Python/PHP/JSなど様々な言語を簡単に動かすができます。
また、このプラットフォームには次の図のようなダッシュボードも含まれています。
今回は、チュートリアルにある内容を Windows 上で実行してみます。Docker Host として VirtualBox 上で Oracle Linux を使用しています。各種コマンドの実行は Windows 上のコマンドプロンプトから実行しています。
ダウンロード
まずはじめに CLI ツールをダウンロードしましょう。こちら(Releases · fnproject/cli · GitHub)からfn.exe
をダウンロードしましょう。複数のバージョンがありますが、今回は 0.49 を使用しています。
この CLI ツールは Mac の場合はbrew
コマンド、Linux の場合はシェルを実行してインストールします。
Fnサーバを起動
Windows 以外の場合は、fn start
で起動しますが、Windows の場合はdocker
コマンドで直接起動します。ボリュームのマウント先や、ポートのフォワード先は任意で変えてください。
>docker run --rm --name functions -it -v /var/run/docker.sock:/var/run/docker.sock -v /mnt/docker/func_data:/app/data -p 8080:8080 fnproject/functions Unable to find image 'fnproject/functions:latest' locally latest: Pulling from fnproject/functions (略) Digest: sha256:82fa7797799ab0c78eaad05be0712b01ac992dc1b2d5b036146bdc4db752a836 Status: Downloaded newer image for fnproject/functions:latest sh: overlay: unknown operand 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?) INFO[0000] datastore dialed datastore=sqlite3 max_idle_connections=256 INFO[0000] no docker auths from config files found (this is fine) error="open /root/.dockercfg: no such file or directory" INFO[0000] available memory ram=5592580096 ______ / ____/___ / /_ / __ \ / __/ / / / / /_/ /_/ /_/ v0.3.147 INFO[0000] Serving Functions API on address `:8080`
ファンクションの作成とデプロイ
ファンクションとして動かしたいアプリケーションを用意します。今回は、チュートリアルにある Go言語で書かれた物をそのまま使用します。まずは、hello というディレクトリを作り、そこに次の様なfunc.goを作成します。
mkdir hello cd hello
func.go
package main import ( "fmt" ) func main() { fmt.Println("Hello from Fn!") }
次に設定ファイルを作成します。
>fn init ______ / ____/___ / /_ / __ \ / __/ / / / / /_/ /_/ /_/ Found go function, assuming go runtime. func.yaml created.
設定ファイルの中身は以下のようになっています。
>type func.yaml version: 0.0.1 runtime: go entrypoint: ./func
テストとデプロイ
Docker Hub のアカウントを環境変数へ設定し、ファンクションをテストします。
>set FN_REGISTRY=<DOCKERHUB_USERNAME> >fn run Building image chiroito/hello:0.0.1 Sending build context to Docker daemon 16.96MB Step 1/8 : FROM funcy/go:dev as build-stage dev: Pulling from funcy/go (略) Digest: sha256:8a53001c0cef87e0337f03d15c3bf3c7b9c9bcf15d336417729466cbed101999 Status: Downloaded newer image for funcy/go:dev ---> 4cccab7fc828 Step 2/8 : WORKDIR /function ---> 4e6d23ef7acd Removing intermediate container 1cc30b5988b8 Step 3/8 : ADD . /go/src/func/ ---> 902b5b474602 Removing intermediate container 50cc791f1345 Step 4/8 : RUN cd /go/src/func/ && go build -o func ---> Running in 6d5aa72dbb7b ---> f77fa78f12fd Removing intermediate container 6d5aa72dbb7b Step 5/8 : FROM funcy/go latest: Pulling from funcy/go 758a6ad3cc15: Already exists 09ee77a602f9: Already exists Digest: sha256:d5ffce43c4cb60216c5e4f35676f8dfd6a10cd2f791857be960acee1164a26a6 Status: Downloaded newer image for funcy/go:latest ---> 573e8a7edc05 Step 6/8 : WORKDIR /function ---> 40d283771de3 Removing intermediate container 826525a268ee Step 7/8 : COPY --from=build-stage /go/src/func/func /function/ ---> 3ddaaa6f3eda Removing intermediate container 980ca6d4d93b Step 8/8 : ENTRYPOINT ./func ---> Running in 3d782d7b71b7 ---> d2ed92e6669e Removing intermediate container 3d782d7b71b7 [Warning] One or more build-args [HTTPS_PROXY HTTP_PROXY] were not consumed Successfully built d2ed92e6669e Successfully tagged chiroito/hello:0.0.1 SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. Hello from Fn!
作成したファンクションをFnサーバへデプロイします。
>fn deploy --app myapp Deploying hello to app: myapp at path: /hello Bumped to version 0.0.2 Building image chiroito/hello:0.0.2 Sending build context to Docker daemon 16.96MB Step 1/8 : FROM funcy/go:dev as build-stage ---> 4cccab7fc828 Step 2/8 : WORKDIR /function ---> Using cache ---> 4e6d23ef7acd Step 3/8 : ADD . /go/src/func/ ---> 72730657c8b1 Removing intermediate container b3398cc82476 Step 4/8 : RUN cd /go/src/func/ && go build -o func ---> Running in f782794a22d1 ---> 0683bf980d16 Removing intermediate container f782794a22d1 Step 5/8 : FROM funcy/go ---> 573e8a7edc05 Step 6/8 : WORKDIR /function ---> Using cache ---> 40d283771de3 Step 7/8 : COPY --from=build-stage /go/src/func/func /function/ ---> Using cache ---> 3ddaaa6f3eda Step 8/8 : ENTRYPOINT ./func ---> Using cache ---> d2ed92e6669e [Warning] One or more build-args [HTTPS_PROXY HTTP_PROXY] were not consumed Successfully built d2ed92e6669e Successfully tagged chiroito/hello:0.0.2 SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. Pushing chiroito/hello:0.0.2 to docker registry...The push refers to a repository [docker.io/chiroito/hello] 51de03e01e07: Pushed 865894dbd5a9: Pushed e9474866f7d4: Layer already exists c0e205d473e9: Layer already exists 0.0.2: digest: sha256:f3aa4a73b01b891f4614a7c832e36003533b9acb1b79adbcaa7addf15192848f size: 1154 Updating route /hello using image chiroito/hello:0.0.2...
ファンクションを実行する
実際にファンクションを呼び出してみましょう。ファンクションの呼び出しもfn
コマンドで行なえます。
>fn call myapp /hello Hello from Fn!
curl
などでアクセスするにはhttp://localhost:8080/r/myapp/hello
へアクセスします。
UIの起動
Dashboard も docker コンテナとして起動します。
>docker run --rm -it --link functions:api -p 4000:4000 -e "API_URL=http://api:8080" fnproject/ui Unable to find image 'fnproject/ui:latest' locally latest: Pulling from fnproject/ui (略) Digest: sha256:e427ef89ca78497bda431a76c81cc8bbfaf96146fdea661bd7e798cef7490610 Status: Downloaded newer image for fnproject/ui:latest > FunctionsUI@0.0.15 start /app > node server Using API url: api:8080 Server running on port 4000
ブラウザでhttp://<Docker HostのIP>:4000
へアクセスします。
一定の間隔でFnサーバから値を取っているため、ファンクションを実行するたびにグラフの値が変わっていきます。