0
0

OpenFaaS 関数のデプロイと実行呼び出し on ラズパイ5, Kubernetes

Posted at

環境構築済み前提

とりあえずデフォルトのやつ使ってみる

ブラウザでopenfaasのgatewayにアクセス(ログインは構築記事参照)
image.png

「Deploy New Function」からNodeInfoを選択してデプロイ。

  • ブラウザから実行
    「INVOKE」をクリック
    image.png

  • curlで呼び出し
    アクセス先は関数のURLになる。(http://< gateway or gateway-externalのアドレスおよびポート番号 >/function/nodeinfo)

$ curl https://faas.example.com/function/nodeinfo
もしくは
$ curl -X POST https://faas.example.com/function/nodeinfo
Hostname: nodeinfo-77c6756589-qgmtm

Arch: arm64
CPUs: 4
Total mem: 8048MB
Platform: linux
Uptime: 395436.03

プロキシ挟んでる(私の場合はcloudflareのトンネル使っている)場合、301が返される場合がありました。

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>cloudflare</center>
</body>
</html>

その場合はcurl -L -X POSTでリダイレクト有効にしてください。

関数を自作してデプロイしてみる

テンプレートの取得

faas-cli listでデプロイされている関数一覧が取得できる。

Function                        Invocations     Replicas
nodeinfo                        18              1

関数を保管するディレクトリ(Functions)を作っておいて、そこで実行するのがいいかと思います。

利用可能な言語テンプレートは

faas-cli template store list

今回はpython3-httpを使います。

$ faas-cli new fizzbuzz --lang python3-http
2024/07/20 10:23:46 No templates found in current directory.
2024/07/20 10:23:46 Attempting to expand templates from https://github.com/openfaas/templates.git
2024/07/20 10:23:49 Fetched 18 template(s) : [bun csharp dockerfile go java11 java11-vert-x node node14 node16 node17 node18 node20 php7 php8 python27 python3 python3-debian ruby] from https://github.com/openfaas/templates.git
Template: "python3-http" was not found in the templates directory

が、not foundと言われてしまいました。
一旦pullしてからにします。

$ faas-cli template store pull python3-http
$ faas-cli new fizzbuzz --lang python3-http

$ ls
fizzbuzz  fizzbuzz.yml  template

$ tree fizzbuzz
fizzbuzz
├── handler.py
├── handler_test.py
├── requirements.txt
└── tox.ini

1 directory, 4 files

プログラム作成

fizzbuzz/handler.py
def handle(event, context):
    if event.method != "GET":
        return {
            "statusCode": 405,
            "body": "Method not allowed\n"
        }
    response = ""
    try:
        number = int(event.query["number"])
        if number % 3 == 0 and number % 5 == 0:
            response = "FizzBuzz"
        elif number % 3 == 0:
            response = "Fizz"
        elif number % 5 == 0:
            response = "Buzz"
        else:
            response = str(number)
        return {
            "statusCode": 200,
            "body": response + "\n"
        }
    except (KeyError, ValueError):
        return {
            "statusCode": 406,
            "body": "Invalid input\n"
        }

GETメソッドのみ許可、クエリパラメータで値を渡す形式にしました。
POSTならevent.bodyになります。

handler_test.pyにてテストプログラム書けそうに見えますが、
構文エラーにしてもデプロイできたので、使えなさそうです。
(--build-argにTEST_ENABLED=trueいれてもダメでした。)

いざデプロイ

手順だけ載せます。
本当は色々あったのですが、詳細は↓

事前準備

  • Docker Hubのアカウント作成
  • コンソールでDockerログイン
$ docker login -u [Docker Account Name] --password ********
  • イメージ名の最初にDockerのアカウント名入れる
fizzbuzz.yml
image: [Docker Account Name]/fizzbuzz:latest

デプロイ

$ DOCKER_BUILDKIT=1 faas-cli up -f fizzbuzz.yml --no-cache --build-arg platform=linux/arm64/v8

成功すれば

$ faas-cli list
Function                        Invocations     Replicas
fizzbuzz                        32              1    
nodeinfo                        18              1

ブラウザでも確認できる
fizzbuzzimage.png

実行してみる

ブラウザで表示されているURLにアクセス
クエリパラメータにnumber=5など適当な数字(数字以外でも)を入れてあげる
もしくはcurlからでも

$ curl https://faas.genserver.net/function/fizzbuzz?number=5
Buzz

ちなみにOpenFaaSの画面からINVOKEを実行した場合はPOSTメソッドとなるので、このプログラムでは405を返す。

お片付け

$ faas-cli remove fizzbuzz

またビルドしたdockerのイメージがローカル、Docker Hubにそれぞれ残っているので、不要であれば削除する。

$ docker rmi [Docker Account Name]/fizzbuzz

Docker Hubのイメージはブラウザから削除。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0