Azure Container Apps への Java Azure 関数のデプロイ

WBOY
リリース: 2024-09-09 22:30:47
オリジナル
388 人が閲覧しました

Deploying a Java Azure Function on Azure Container Apps

Azure Functions は、Azure Container Apps でのコンテナー化された関数アプリの開発、デプロイ、管理のための統合サポートを提供します。これにより、Azure Kubernetes Service (AKS) などのコンテナー環境で Azure Functions を個別に実行する場合と比較して、統合された Azure 管理ポータルを使用した Azure Functions の実行と管理が容易になります。さらに、Azure Container Apps が提供する機能を活用することで、Azure Functions の KEDA、Dapr、Envoy、スケーリング、監視、セキュリティ、アクセス制御などの機能を簡単に利用できます。

【参考】

Azure Functions の Azure Container Apps ホスティング

Azure Container Apps で最初のコンテナー化された関数を作成します

環境変数の設定

以下は、Azure Container Apps リソースの作成に関連する環境変数です。ここでは、作成するリソースのさまざまな名前とインストール場所、コンテナー イメージの名前とタグを指定します。

# Azure Container Apps resource names
export LOCATION=eastus
export RESOURCE_GROUP_NAME=yoshio-rg
export CONTAINER_REGISTRY_NAME=cajava2411
export CONTAINER_ENVIRONMENT=YoshioContainerEnvironment
export STORAGE_NAME=yoshiojavastorage
export AZURE_FUNCTION_NAME=yoshiojavafunc

# Container image name and tag
export C_IMAGE_NAME=tyoshio2002/java-function-on-aca
export C_IMAGE_TAG=1.0
ログイン後にコピー

Java Azure 関数プロジェクトの作成とテスト

1. Azure Functions for Java Maven プロジェクトを作成する

まず、Azure Functions for Java の Maven プロジェクトを作成します。この Maven プロジェクトは、Java 21 を使用して Azure Functions を作成するように設計されています。 mvn Archetype:generate コマンドを使用してプロジェクトを作成し、必要に応じてパラメーターを変更します。

mvn archetype:generate \
-DinteractiveMode=false \
-DarchetypeGroupId=com.microsoft.azure \
-DarchetypeArtifactId=azure-functions-archetype \
-DgroupId=com.yoshio3 \
-Dpackage=com.yoshio3 \
-DartifactId=yoshiojavafunc \
-DappName=Java-Azure-Functions \
-DappRegion=$LOCATION \
-DjavaVersion=21 \
-Dversion=1.0-SNAPSHOT \
-Ddocker
ログイン後にコピー

上記のコマンドを実行すると、ディレクトリ構造が自動的に作成され、Function.java には HTTP トリガーを備えた Azure 関数のサンプル コードが含まれます。 Dockerfile も作成されます。これには、Docker コンテナー環境で Azure Functions を実行するための構成が含まれます。

├── Dockerfile
├── host.json
├── local.settings.json
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── yoshio3
    │               └── Function.java
    └── test
        └── java
            └── com
                └── yoshio3
                    ├── FunctionTest.java
                    └── HttpResponseMessageMock.java
ログイン後にコピー

2. Azure 関数をローカルで実行する

Maven プロジェクトをビルドし、Azure Functions をローカルで実行します。次のコマンドを実行して、HTTP トリガーで Azure Functions を開始します。

mvn clean package
mvn azure-functions:run
ログイン後にコピー

Azure Functions が実行されたら、別のターミナルを開き、次のコマンドを実行して HTTP トリガーにリクエストを送信します。 「Hello, World」という応答が返されるはずです。

curl "http://localhost:7071/api/HttpExample?name=World"
# Output: 
Hello, World
ログイン後にコピー

Azure Functions コンテナー イメージの作成とテスト

1. Docker イメージをビルドしてテストする

自動生成された Dockerfile を使用して、Azure Functions コンテナー イメージを構築します。次のコマンドを実行してイメージをビルドします。

docker build -t $C_IMAGE_NAME:$C_IMAGE_TAG -f ./Dockerfile .
ログイン後にコピー

ビルドが完了したら、次のコマンドを実行してイメージが作成されたかどうかを確認します。

docker images | grep $C_IMAGE_NAME
# Output: 
tyoshio2002/java-function-on-aca    1.0    bcf471e6f774   9 hours ago     1.46GB
ログイン後にコピー

イメージが作成されたら、次のコマンドを実行して、Azure Functions コンテナー イメージをローカルでテストします。 Azure Functions コンテナーは内部で HTTP ポート 80 を使用するため、ローカル アクセス用にポート 8080 にマップします。コンテナーが起動したら、curl コマンドを実行して、Azure Functions HTTP トリガーに要求を送信します。すべてが正しく動作している場合は、「Hello, World」を受け取るはずです。

docker run -p 8080:80 -it $C_IMAGE_NAME:$C_IMAGE_TAG
curl "http://localhost:8080/api/HttpExample?name=World"
# Output: 
Hello, World
ログイン後にコピー

コンテナー イメージを Azure Container Registry にプッシュする

1. Azure CLIにログインします。

まず、Azure CLI を使用して Azure にログインします。以下のコマンドを実行してログインします。

az login
ログイン後にコピー

2. リソースグループの作成

Azure でリソース グループを作成します。このリソース グループは、Azure Container Registry と Azure Container Apps に関連するリソースをグループ化するために使用されます。

az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
ログイン後にコピー

3. Azure Container Registry を作成してログインする

Azure Container Registry を作成し、ログインします。Azure Container Registry は、コンテナー イメージをプッシュするためのプライベート コンテナー レジストリです。

az acr create --resource-group $RESOURCE_GROUP_NAME --name $CONTAINER_REGISTRY_NAME --sku Basic
az acr login --name $CONTAINER_REGISTRY_NAME
ログイン後にコピー

4. Azure Container Registry サーバー名の取得

作成された Azure Container Registry のサーバー名を取得します。サーバー名の形式は $CONTAINER_REGISTRY_NAME.azurecr.io.
になります。

CONTAINER_REGISTRY_SERVER=$(az acr show --name $CONTAINER_REGISTRY_NAME --query loginServer --output tsv)
ログイン後にコピー

5. イメージを Azure Container Registry にプッシュします

ローカルで作成されたコンテナー イメージを Azure Container Registry にプッシュするには、tag コマンドを使用してイメージにタグを付けます。タグ付け後、push コマンドを使用して画像をプッシュします。

docker tag $C_IMAGE_NAME:$C_IMAGE_TAG $CONTAINER_REGISTRY_SERVER/$C_IMAGE_NAME:$C_IMAGE_TAG
docker push $CONTAINER_REGISTRY_SERVER/$C_IMAGE_NAME:$C_IMAGE_TAG
ログイン後にコピー

Azure コンテナー アプリの作成と Java Azure 関数のデプロイ

1. Azure CLI で拡張機能とリソース プロバイダーを登録する

Azure CLI から Azure Container Apps を作成および管理するには、必要な拡張機能とリソース プロバイダーを登録します。

az upgrade
az extension add --name containerapp --upgrade -y
az provider register --namespace Microsoft.Web
az provider register --namespace Microsoft.App
az provider register --namespace Microsoft.OperationalInsights
ログイン後にコピー

2. Azureコンテナアプリ環境の作成

Azure Container Apps の環境を作成します。このコマンドは、Azure Container Apps をホストするために必要な構成をセットアップします。

az containerapp env create --name $CONTAINER_ENVIRONMENT --enable-workload-profiles --resource-group $RESOURCE_GROUP_NAME --location $LOCATION
ログイン後にコピー

3. Create Storage Account for Azure Functions

Azure Functions requires a storage account when creating a Function App instance. Therefore, create a general-purpose storage account for Azure Functions.

az storage account create --name $STORAGE_NAME --location $LOCATION --resource-group $RESOURCE_GROUP_NAME --sku Standard_LRS
ログイン後にコピー

4. Create an Instance of Java Azure Function in Azure Container Apps

Create an instance of the Java Azure Function in Azure Container Apps. Execute the following command to create the instance. Since the Azure Function is created using Java 21, specify --runtime java.

az functionapp create --name $AZURE_FUNCTION_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--environment $CONTAINER_ENVIRONMENT \
--storage-account $STORAGE_NAME \
--workload-profile-name "Consumption" \
--max-replicas 15 \
--min-replicas 1 \
--functions-version 4 \
--runtime java \
--image $CONTAINER_REGISTRY_SERVER/$C_IMAGE_NAME:$C_IMAGE_TAG \
--assign-identity
ログイン後にコピー

5. Assign Role for Azure Function to Access Azure Container Registry

Finally, configure secure access for Azure Functions to Azure Container Registry. Enable the system-managed identity for Azure Functions and assign the ACRPull role for access.

FUNCTION_APP_ID=$(az functionapp identity assign --name $AZURE_FUNCTION_NAME --resource-group $RESOURCE_GROUP_NAME --query principalId --output tsv)
ACR_ID=$(az acr show --name $CONTAINER_REGISTRY_NAME --query id --output tsv)
az role assignment create --assignee $FUNCTION_APP_ID --role AcrPull --scope $ACR_ID
ログイン後にコピー

6. Retrieve the URL of the Azure Function

Finally, retrieve the HTTP trigger function URL of the deployed Azure Function. Use the az functionapp function show command to get the details of the Azure Functions function.

az functionapp function show --resource-group $RESOURCE_GROUP_NAME --name $AZURE_FUNCTION_NAME --function-name HttpExample --query invokeUrlTemplate
# Output: "https://yoshiojavafunc.niceocean-********.eastus.azurecontainerapps.io/api/httpexample"
ログイン後にコピー

You can then send a request to the retrieved URL using curl command to confirm that the Azure Functions is working correctly.

curl "https://yoshiojavafunc.niceocean-********.eastus.azurecontainerapps.io/api/httpexample?name=World"
# Expected Output: 
Hello, World
ログイン後にコピー

If everything is set up correctly, you should receive a response saying "Hello, World", confirming that your Azure Function is functioning as expected.

Summary

In this guide, you learned how to:

  1. Set up environment variables for your Azure resources.
  2. Create a Java Azure Function project using Maven.
  3. Run the Azure Function locally for testing.
  4. Build a Docker image for the Azure Function.
  5. Push the Docker image to Azure Container Registry.
  6. Create an Azure Container Apps environment and deploy the Java Azure Function.
  7. Assign necessary roles for secure access to Azure Container Registry.
  8. Retrieve and test the URL of the deployed Azure Function.

By following these steps, you can successfully deploy a Java Azure Function on Azure Container Apps, leveraging the benefits of containerization and Azure's integrated management capabilities. If you have any further questions or need assistance with specific steps, feel free to ask!

以上がAzure Container Apps への Java Azure 関数のデプロイの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!