зеркало из
1
0
Форкнуть 0
This commit is contained in:
Julien Dubois 2022-10-07 10:39:24 +02:00
Родитель 8d67380405 e16ee75a67
Коммит e24d9856e3
9 изменённых файлов: 247 добавлений и 37 удалений

5
.github/workflows/deploy-native.yml поставляемый
Просмотреть файл

@ -4,6 +4,11 @@ on:
push:
branches: [main]
workflow_dispatch:
paths:
- .github/workflows/*.yml
- micronaut-app/**
- springboot-app/**
- quarkus-app/**
env:
REGISTRY_URL: javaruntimesregistrysinedied.azurecr.io

5
.github/workflows/deploy.yml поставляемый
Просмотреть файл

@ -4,6 +4,11 @@ on:
push:
branches: [main]
workflow_dispatch:
paths:
- .github/workflows/*.yml
- micronaut-app/**
- springboot-app/**
- quarkus-app/**
# tag::adocGitHubActionsWorkflowBase[]
env:

Просмотреть файл

@ -73,9 +73,11 @@
<!-- ============= -->
<workshop-url>https://azure.github.io/aca-java-runtimes-workshop/</workshop-url>
<workshop-github-url>https://github.com/Azure/aca-java-runtimes-workshop</workshop-github-url>
<template-github-url>https://github.com/Azure/aca-java-runtimes-workshop-template</template-github-url>
<workshop-github-issue>https://github.com/Azure/aca-java-runtimes-workshop/issues</workshop-github-issue>
<workshop-github-clone>https://github.com/Azure/aca-java-runtimes-workshop.git</workshop-github-clone>
<workshop-github-raw>https://raw.githubusercontent.com/Azure/aca-java-runtimes-workshop/main</workshop-github-raw>
<azure-pass-url>https://azcheck.in/dbe221010</azure-pass-url>
</attributes>
</configuration>
<executions>

Просмотреть файл

@ -105,7 +105,7 @@ This workshop should be as self-explanatory as possible. So your job is to follo
Oh, and be ready to have some fun!
If you're attending this workshop at Devoxx BE, you can use this link to redeem an Azure Pass: https://azcheck.in/dbe221010.
If you're attending this workshop at Devoxx BE, you can use this link to redeem an Azure Pass: {azure-pass-url}.
Once you have your code, copy it and click on the button *Get on board with Azure*, then follow the instructions.
image::../images/qrcode-azure-pass.png[]

Просмотреть файл

@ -9,19 +9,23 @@ This includes:
== Using the GitHub Template
Open https://github.com/Azure/aca-java-runtimes-workshop[this GitHub repository] and click on `Use this template`.
Open {template-github-url}[this GitHub repository,window=_blank] and click on `Use this template`.
image::../images/github-template.png[Screenshot of GitHub showing the "Use this template" button]
You will be redirected to the repository creation page. Enter a name for your new repository, set the repo visibility to `public` and click on `Create repository from template`.
You will be redirected to the repository creation page.
Select the owner of the repository, enter a name for your new repository (eg.
`aca-java-runtimes-workshop`), set the repo visibility to `public` and click on `Create repository from template`.
Once the repository is created, clone it locally using git.
1. Select the **Code** button, then copy your repository url.
1.
Select the **Code** button, then copy your repository url.
+
image::../images/github-clone.png[Screenshot of GitHub showing the repository URL]
2. Open a terminal and run:
2.
Open a terminal and run:
+
[source,shell]
----
@ -54,6 +58,8 @@ docker --version
az --version
----
If everything is installed correctly, you should see the version of each tool.
== Setting Up Your VSCode Environment
If you have VS Code and Docker installed on your machine, the quickest way to set up your environment is to use the pre-configured https://code.visualstudio.com/docs/remote/containers[dev container] of the template project.
@ -79,17 +85,51 @@ Once the container is ready, you will see the following message in the bottom ri
image::../images/vscode-connected.png[]
== Building the Inital Project
No matter if you have installed the tools manually or used the dev container, you can now build the initial project.
Go to the directory where you cloned the repository and build the code with the following Maven command:
[source,shell]
----
cd <your_repo_url>
mvn install
----
You should see the following output:
[source,shell]
----
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Azure Container Apps and Java Runtimes Workshop 1.0.0-SNAPSHOT SUCCESS
[INFO] Azure Container Apps and Java Runtimes Workshop :: Micronaut 1.0.0-SNAPSHOT SUCCESS
[INFO] Azure Container Apps and Java Runtimes Workshop :: Springboot 1.0.0-SNAPSHOT SUCCESS
[INFO] Azure Container Apps and Java Runtimes Workshop :: Quarkus 1.0.0-SNAPSHOT SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.097 s
[INFO] Finished at: 2022-10-07T09:46:16+02:00
[INFO] ------------------------------------------------------------------------
----
== Setting Up Azure
To be able to deploy the application to Azure, you first need an Azure subscription. If you don't have one, go to https://azure.microsoft.com/free/[azure.microsoft.com/free] and register.
Ok, now that you have the initial project built, let's first setup Azure before starting coding our microservices.
To be able to deploy the application to Azure, you first need an Azure subscription.
If you don't have one, go to https://azure.microsoft.com/free/[azure.microsoft.com/free] and register.
[NOTE]
====
If you're attending this workshop at Devoxx BE, you can use this link to redeem an Azure Pass: https://azcheck.in/dbe221010.
If you're attending this workshop at Devoxx BE, you can use this link to redeem an Azure Pass: {azure-pass-url}.
Once you have your code, copy it and click on the button *Get on board with Azure*, then follow the instructions.
====
Once you have created your Azure subscription, we'll spend some time to create all the resources needed to deploy our Apps. We'll do that mostly using the Azure CLI, but at any point you can go to https://portal.azure.com[Azure Portal] to have a visual look at the resources we'll be configuring.
Once you have created your Azure subscription, we'll spend some time to create all the resources needed to deploy our Apps.
We'll do that mostly using the Azure CLI, but at any point you can go to https://portal.azure.com[Azure Portal] to have a visual look at the resources we'll be configuring.
First, sign in to your Azure account using the Azure CLI:
@ -105,7 +145,8 @@ Make sure you are using the right subscription with:
az account show
----
Now we'll configure the CLI to automatically install extensions when needed. The Azure CLI is extensible, some commands are part of the core, but others are provided by extensions, such as the `az containerapp` command we'll be using later.
Now we'll configure the CLI to automatically install extensions when needed.
The Azure CLI is extensible, some commands are part of the core, but others are provided by extensions, such as the `az containerapp` command we'll be using later.
[source,shell]
----
@ -120,6 +161,15 @@ We'll create the infrastructure for our application so we can later deploying th
Let's first set a few environment variables that will help us in creating the Azure infrastructure.
[IMPORTANT]
====
Some resources in Azure need to have a unique name across the globe (for example Azure Registry).
For that, we use the `UNIQUE_IDENTIFIER` environment variable to make sure we don't have any name collision.
If you are developping in your local machine, the `UNIQUE_IDENTIFIER` will be your username (which is not totally unique, but it's a good start).
On the other hand, if you're using the DevContainer, you must manually set this to a unique value (like your name) to avoid conflicts with other users
So, if you use DevContainer, make sure to set the `UNIQUE_IDENTIFIER` environment variable to a unique value.
====
[source,shell]
----
include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocEnvironmentVariables, indent=0]
@ -129,7 +179,10 @@ Now let's create the Azure resources.
=== Resource Group
A https://learn.microsoft.com/azure/azure-resource-manager/management/manage-resource-groups-portal[resource group] is a container that holds related resources for an Azure solution. The resource group can include all the resources for the solution, or only those resources that you want to manage as a group. In our workshop, all the databases, all the microservices, etc. will be grouped into a single resource group.
A https://learn.microsoft.com/azure/azure-resource-manager/management/manage-resource-groups-portal[resource group] is a container that holds related resources for an Azure solution.
The resource group can include all the resources for the solution, or only those resources that you want to manage as a group.
In our workshop, all the databases, all the microservices, etc.
will be grouped into a single resource group.
Run the following command to create the Java Runtimes resource group:
@ -140,7 +193,9 @@ include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocResourceGroup, ind
=== Log Analytics Workspace
https://learn.microsoft.com/azure/azure-monitor/logs/quick-create-workspace?tabs=azure-portal[Log Analytics workspace] is the environment for Azure Monitor log data. Each workspace has its own data repository and configuration, and data sources and solutions are configured to store their data in a particular workspace. We will use the same workspace for most of the Azure resources we will be creating.
https://learn.microsoft.com/azure/azure-monitor/logs/quick-create-workspace?tabs=azure-portal[Log Analytics workspace] is the environment for Azure Monitor log data.
Each workspace has its own data repository and configuration, and data sources and solutions are configured to store their data in a particular workspace.
We will use the same workspace for most of the Azure resources we will be creating.
Create a Log Analytics workspace with the following command:
@ -158,7 +213,9 @@ include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocLogAnalyticsSecret
=== Azure Container Registry
In the next chapters we will be creating Docker containers and pushing them to the Azure Container Registry. https://azure.microsoft.com/products/container-registry/[Azure Container Registry] is a private registry for hosting container images. Using the Azure Container Registry, you can store Docker-formatted images for all types of container deployments.
In the next chapters we will be creating Docker containers and pushing them to the Azure Container Registry.
https://azure.microsoft.com/products/container-registry/[Azure Container Registry] is a private registry for hosting container images.
Using the Azure Container Registry, you can store Docker-formatted images for all types of container deployments.
First, let's created an Azure Container Registry with the following command:
@ -174,29 +231,42 @@ Update the repository to allow anonymous users to pull the images:
include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocRegistryUpdate, indent=0]
----
Get the URL of the Azure Container Registry and set it to the REGISTRY_URL variable with the following command:
Get the URL of the Azure Container Registry and set it to the `REGISTRY_URL` variable with the following command:
[source,shell]
----
include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocRegistryShow, indent=0]
----
If you log into the https://portal.azure.com[Azure Portal] and search for the "java-runtimes" resource group, you should see the following created resources.
If you log into the https://portal.azure.com[Azure Portal] and search for the `java-runtimes` resource group, you should see the following created resources.
image::../images/azure-rg.png[Screenshot of Azure Portal showing the Java Runtimes resource group]
=== Creating the Container Apps environment
A container apps environment acts as a boundary for our containers. Containers deployed on the same environment use the same virtual network and the same Log Analytics workspace. Create the container apps environment with the following command:
A container apps environment acts as a boundary for our containers.
Containers deployed on the same environment use the same virtual network and the same Log Analytics workspace.
Create the container apps environment with the following command:
[source,shell]
----
include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocACAEnv, indent=0]
----
[NOTE]
====
Some Azure CLI commands can take some time to execute.
Don't hesitate to have a look at the next chapters to know what you will have to do.
And then, come back to this one when the command is done and execute the next one.
====
==== Creating the Container Apps
Now that we have created the container apps environment, we can create the container apps. A container app is a containerized application that is deployed to a container apps environment. We will create threee container apps, one for each of our Java runtimes. Since we don't have any container images ready yet, we'll use a default "Hello World" image just get things running. We'll update the container apps with the actual images later.
Now that we have created the container apps environment, we can create the container apps.
A container app is a containerized application that is deployed to a container apps environment.
We will create threee container apps, one for each of our Java runtimes.
Since we don't have any container images ready yet, we'll use a default "Hello World" image just get things running.
We'll update the container apps with the actual images later.
[source,shell]
----
@ -205,13 +275,23 @@ include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocACACreate, indent=
// TODO: other apps!
The `create` command returns the URL for the container apps. Copy this location to a web browser, and you should see the following page:
The `create` command returns the URL for the container apps.
You can also get the URLs with the following commands:
[source,shell]
----
include::{workshop-github-raw}/scripts/infra/deploy.sh[tag=adocIngressHosts, indent=0]
----
Get these locations amd copy them into a web browser.
You should see the following page:
image::../images/aca-hello.png[Screenshot of the Hello World page from the Azure Container Apps]
=== Creating the Azure Database for PostgreSQL
We'll use a PostgreSQL database to store analytics data from our microservices. Because we also want to access these database from an external SQL client, we make them available to the outside world thanks to the `-public all` parameter.
We'll use a PostgreSQL database to store analytics data from our microservices.
Because we also want to access these database from an external SQL client, we make them available to the outside world thanks to the `-public all` parameter.
Create the database with the following command (this one will take a few minutes):
@ -220,7 +300,9 @@ Create the database with the following command (this one will take a few minutes
include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocPostgresCreate, indent=0]
----
Now that we have our database setup, it's time to create the tables. We'll use a database initialization SQL file for that. Thanks to Azure CLI, we can execute these SQL scripts directly in our newly created database.
Now that we have our database setup, it's time to create the tables.
We'll use a database initialization SQL file for that.
Thanks to Azure CLI, we can execute these SQL scripts directly in our newly created database.
Create the tables using the following command (make sure you are in the repository root directory before your execute this command):
@ -238,7 +320,8 @@ include::{workshop-github-raw}/scripts/infra/azure.sh[tag=adocPostgresSelect, in
[NOTE]
====
If you get the error `No module named 'psycopg2._psycopg'` that means that some of your Azure CLI dependencies are not correctly installed. Check https://github.com/Azure/azure-cli/issues/21998 for help.
If you get the error `No module named 'psycopg2._psycopg'` that means that some of your Azure CLI dependencies are not correctly installed.
Check https://github.com/Azure/azure-cli/issues/21998 for help.
====
If you'd like to see the connection string to the database (so you can access your database from an external SQL client), use the following command:

Просмотреть файл

@ -339,14 +339,7 @@ We will set a new scale rule for our Quarkus app using the Azure CLI:
[source,shell]
----
az containerapp update \
--name "$QUARKUS_APP" \
--resource-group "$RESOURCE_GROUP" \
--scale-rule-name "cpu-scaling" \
--scale-rule-type "cpu" \
--scale-rule-metadata type=Utilization value=10 \
--min-replicas 1 \
--max-replicas 10
include::{workshop-github-raw}/scripts/infra/scale.sh[tag=adocAutoScalingCpu, indent=0]
----
This will automatically scale out the application when the CPU usage is above 10% (we set it low deliberately to make it easy to go up).
@ -372,14 +365,7 @@ This we will set the scale rule for our Micronaut app using the command:
[source,shell]
----
az containerapp update \
--name "$MICRONAUT_APP" \
--resource-group "$RESOURCE_GROUP" \
--scale-rule-name "memory-scaling" \
--scale-rule-type "memory" \
--scale-rule-metadata type=Utilization value=15 \
--min-replicas 1 \
--max-replicas 10
include::{workshop-github-raw}/scripts/infra/scale.sh[tag=adocAutoScalingMemory, indent=0]
----
This will automatically scale out the application when the memory usage is above 15% (we set it low deliberately to make it easy to go up).

Просмотреть файл

@ -342,6 +342,30 @@ exportEnvironment() {
--query "loginServer" \
--output tsv)
export QUARKUS_HOST=$(
az containerapp show \
--name "$QUARKUS_APP" \
--resource-group "$RESOURCE_GROUP" \
--query "properties.configuration.ingress.fqdn" \
--output tsv \
)
export MICRONAUT_HOST=$(
az containerapp show \
--name "$MICRONAUT_APP" \
--resource-group "$RESOURCE_GROUP" \
--query "properties.configuration.ingress.fqdn" \
--output tsv \
)
export SPRING_HOST=$(
az containerapp show \
--name "$SPRING_APP" \
--resource-group "$RESOURCE_GROUP" \
--query "properties.configuration.ingress.fqdn" \
--output tsv \
)
echo "Exported environment variables for project '${project_name}'."
}

105
scripts/infra/scale.sh Executable file
Просмотреть файл

@ -0,0 +1,105 @@
#!/usr/bin/env bash
##############################################################################
# Usage: ./scaling.sh
# Switch auto scaling modes
# Make sure you have the correct environment variables set.
# For that, first run: source ./azure.sh env
##############################################################################
# Dependencies: Azure CLI, GitHub CLI, jq
##############################################################################
set -e
cd $(dirname ${BASH_SOURCE[0]})
cd ../..
command=${1}
showUsage() {
script_name="$(basename "$0")"
echo "Usage: ./$script_name setup|restore|ping"
echo "Setup auto-scaling mode for apps."
echo
echo "Commands:"
echo " setup Setup auto-scaling as in the docs"
echo " restore Restore default auto-scaling"
echo " ping Send a curl to the 3 apps"
echo
}
if [ -z "$command" ]; then
showUsage
exit 1
fi
if [ "$command" == "setup" ]; then
echo "Setting up auto-scaling..."
# tag::adocAutoScalingCpu[]
az containerapp update \
--name "$QUARKUS_APP" \
--resource-group "$RESOURCE_GROUP" \
--scale-rule-name "cpu-scaling" \
--scale-rule-type "cpu" \
--scale-rule-metadata type=Utilization value=10 \
--min-replicas 1 \
--max-replicas 10
# end::adocAutoScalingCpu[]
# tag::adocAutoScalingMemory[]
az containerapp update \
--name "$MICRONAUT_APP" \
--resource-group "$RESOURCE_GROUP" \
--scale-rule-name "memory-scaling" \
--scale-rule-type "memory" \
--scale-rule-metadata type=Utilization value=15 \
--min-replicas 1 \
--max-replicas 10
# end::adocAutoScalingMemory[]
echo "Done."
elif [ "$command" == "restore" ]; then
echo "Restoring default auto-scaling..."
az containerapp update \
--name "$QUARKUS_APP" \
--resource-group "$RESOURCE_GROUP" \
--scale-rule-name "http-scaling" \
--scale-rule-metadata concurrentRequests=10 \
--min-replicas 0 \
--max-replicas 10
az containerapp update \
--name "$MICRONAUT_APP" \
--resource-group "$RESOURCE_GROUP" \
--scale-rule-name "http-scaling" \
--scale-rule-metadata concurrentRequests=10 \
--min-replicas 0 \
--max-replicas 10
az containerapp update \
--name "$SPRING_APP" \
--resource-group "$RESOURCE_GROUP" \
--scale-rule-name "http-scaling" \
--scale-rule-metadata concurrentRequests=10 \
--min-replicas 0 \
--max-replicas 10
echo "Done."
elif [ "$command" == "ping" ]; then
echo "Pinging apps..."
curl https://${QUARKUS_HOST}/quarkus
echo
curl https://${MICRONAUT_HOST}/micronaut
echo
curl https://${SPRING_HOST}/springboot
echo
else
showUsage
exit
fi

Просмотреть файл

@ -10,7 +10,7 @@
</parent>
<groupId>io.containerapps.javaruntime.workshop</groupId>
<artifactId>springboot-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
<description>Azure Container Apps and Java Runtimes Workshop :: SpringBoot</description>
<properties>
<java.version>17</java.version>