すべてのプロダクト
Search
ドキュメントセンター

Container Service for Kubernetes:Knative を使用した Knative Functions のデプロイ

最終更新日:Oct 15, 2026

Knative Functions は、Knative、Kubernetes、コンテナの基盤となる詳細の多くを隠蔽し、Knative 上で関数を作成およびデプロイするための簡素化されたプログラミングモデルを提供します。関数を作成すると、システムが自動的にコンテナイメージを生成します。イメージビルドとデプロイのパラメーターをカスタマイズした後、関数を Knative Service としてデプロイできます。

前提条件

Knativeはクラスターにデプロイされています。 詳細については、「」「Knativeのデプロイ」をご参照ください。

手順1:コマンドラインツールのインストール

  1. func のリリースページ で、お使いのオペレーティングシステムに対応する func バイナリファイルをダウンロードします。

    このトピックでは、Linux を例に func コマンドラインツールのインストール方法を説明します。

  2. ダウンロードが完了したら、次のコマンドを実行して、バイナリファイルの名前を func に変更します。

    mv <path-to-binary-file> func # <path-to-binary-file> は、バイナリファイルのローカルパス (例: func_darwin_amd64、func_linux_amd64) を示します。
  3. 次のコマンドを実行して、バイナリファイルに実行権限を付与します。

    chmod +x func
  4. 次のコマンドを実行して、func コマンドをシステム PATH 内のディレクトリに移動して、どこからでもコマンドを実行できるようにします。

    mv func /usr/local/bin
  5. 次のコマンドを実行して、func コマンドラインツールがインストールされていることを確認します。

    func version

    インストールされた func ツールのバージョン情報が表示されたら、コマンドラインツールは正しくインストールされています。

手順2:関数の作成

Knative Functions は、複数の言語と呼び出しメソッドに対応したテンプレートを提供します。CloudEvent および HTTP 呼び出しでは、次のテンプレートを利用できます。

次の例では、Go テンプレートを使用して関数を作成する方法を説明します。

  1. 次のコマンドを実行して関数を作成します。

    func create -l <language> <function-name>

    たとえば、Go でサンプル関数を作成します。

    func create -l go hello

    期待される出力:

    Created go function in /usr/local/bin/hello
  2. hello ディレクトリで ls コマンドを実行し、作成されたプロジェクトディレクトリを確認します。

    func.yaml  go.mod  handle.go  handle_test.go  README.md
  3. hello ディレクトリで、次のコマンドを実行して、自動的に作成された func.yaml ファイルを表示します。

    cat func.yaml

    期待される出力:

    specVersion: 0.35.0
    name: hello
    runtime: go
    created: 2023-12-13T06:48:45.419587147Z
  4. func.yaml ファイルを編集して、ビルドとデプロイのパラメーターをカスタマイズします。func.yaml ファイルには、関数プロジェクトの設定情報が含まれています。詳細については、「func_yaml.md」をご参照ください。

    func.yaml ファイルのサンプルを次に示します。

    specVersion: 0.35.0
    name: hello
    runtime: go
    created: 2023-11-29T14:47:34.101658+08:00
    registry: registry.cn-beijing.aliyuncs.com/knative-release-xxx
    image: registry.cn-beijing.aliyuncs.com/knative-release-xxx/hello:latest
    build:
      builderImages:
        pack: registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latest
    deploy:
      namespace: default
    • registry:ビルド後にイメージのプッシュ先となるターゲットリポジトリのアドレス。

    • builderImages:イメージのビルドに使用されるコンテナです。例: registry-cn-beijing.ack.aliyuncs.com/acs/knative-func-builder-jammy-tiny:latest。

    • deploy: 名前空間などのデプロイ関連の設定。

手順3:Knative Functions のデプロイ

関数を作成したら、Knative Functions を直接デプロイできます。

  1. 関数プロジェクトのディレクトリで次のコマンドを実行して、関数をデプロイします。

    deploy は、デフォルトでは関数プロジェクト名を Knative Service 名として使用します。関数がビルドされると、プロジェクト名とイメージリポジトリ名を使用して、関数の完全なイメージアドレスが構築されます。

    func deploy

    期待される出力

    Building function image
    Still building
    Still building
    Yes, still building
    Don't give up on me
    Still building
    This is taking a while
    Still building
    Still building
    Yes, still building
    Function built: registry.cn-beijing.aliyuncs.com/knative-release-xxx/hello:latest
    Pushing function image to the registry "registry.cn-beijing.aliyuncs.com" using the "xxx" user credentials
    Deploying function to the cluster
    Function deployed in namespace "default" and exposed at URL:
       http://hello.default.example.com
  2. 次のコマンドを実行して、関数のデプロイ情報を表示します。

    func info

    期待される出力:

    Function name:
      hello
    Function is built in image:
    
    Function is deployed in namespace:
      default
    Routes:
      http://hello.default.example.com

手順4:Knative Functions の呼び出し

  1. hosts ファイルにエントリを追加して、Knative のアクセスゲートウェイアドレスをアクセス先のドメイン名にマッピングします。

    設定例を次に示します。

    121.xx.xxx.xx hello.default.example.com # 121.xx.xxx.xx を Knative Service のゲートウェイアドレスに、hello.default.example.com をお使いのサービスドメイン名に置き換えます。
  2. 次のコマンドを実行して関数を呼び出します。

    func invoke

    期待される出力:

    POST / HTTP/1.1 hello.default.example.com
      Content-Type: application/json
      Forwarded: for=192.168.102.101;proto=http
      Knative-Serving-Default-Route: true
      X-Forwarded-Proto: http
      User-Agent: Go-http-client/1.1
      Content-Length: 25
      Accept-Encoding: gzip
      K-Proxy-Request: activator
      X-Forwarded-For: 192.168.102.101, 192.168.102.97
      X-Request-Id: 3d82cfc8-f9df-4935-84cd-c6561b4587f6
    Body:

    この出力から、HTTP リクエストが Knative Functions に送信され、関数の呼び出しが成功したことがわかります。

  3. 次のコマンドを実行して、クラスター内の Pod のステータスを表示します。

    kubectl get pod

    期待される出力:

    NAME                                      READY   STATUS    RESTARTS   AGE
    hello-00001-deployment-7b65fcdc4c-gfcbp   2/2     Running   0          34s

    この出力から、関数の呼び出しによって Pod が作成されたことがわかります。

関連ドキュメント

Knative の使用時に発生する可能性のある一般的な問題とその解決策については、「Knative のよくある質問」をご参照ください。