使用ACA(Azure容器应用程序)在Docker Hub的自定义容器镜像上启动Spring Boot WEB服务

使用 Azure Container Apps(ACA)来启动 Spring Boot WEB 服务(使用 Docker Hub 自定义容器映像)。

为了

在 Azure 容器应用环境中启动 Spring Boot web 服务来加深理解。

实现

将Spring Boot WEB应用程序的自定义容器映像部署到Microsoft Azure容器应用程序(ACA)。

请注意,在构建实际系统时,我们使用了在Docker Hub推送的具有公共访问权限的自定义容器映像。

此外还有一篇文章提供使用Microsoft Azure容器注册表(ACR)进行配置的示例。

 

技术背景

微软Azure容器应用(Azure Container Apps)是什么?

您可以展开并查看这个。Microsoft Azure 容器应用(ACA)

ACA是在Azure上运行的容器应用平台。
ACA支持Docker容器和Kubernetes群集环境,开发人员可以创建Docker容器映像,并将其部署为Kubernetes资源。此外,ACA支持自定义域名,可以保持自己的品牌。
ACA的主要优点如下:

简单设置和部署
ACA具有简单的用户界面,您可以通过几次点击来设置和部署容器应用。

可扩展性
ACA可以自动进行扩展。如果应用程序需要更多资源,ACA将自动添加必要的资源并进行分布处理。

安全性
ACA提供在运行容器应用程序时所需的安全功能。例如,与Azure Active Directory的集成和容器级的访问控制等。

成本效益
ACA可以根据需要在需要时添加所需的资源,最大限度地减少资源的浪费。此外,只对所需的资源进行付费,可以以成本效益的方式使用。

研发环境

    • Windows 11 Home 22H2 を使用しています。

WSL の Ubuntu を操作していきますので macOS の方も参考にして頂けます。

WSL(Microsoft Store 版)
> wsl –version
WSL 版本:1.0.3.0
内核版本:5.15.79.1
WSLg 版本:1.0.47Ubuntu
$ lsb_release -a
没有可用的 LSB 模块。
发行商 ID:Ubuntu
描述:Ubuntu 22.04.1 LTS
版本:22.04

Java JDK ※ 安装 Java JDK 和 Hello World!
$ java -version
openjdk version “11.0.17” 2022-10-18
OpenJDK 运行时环境(构建 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64 位服务器虚拟机(构建 11.0.17+8-post-Ubuntu-1ubuntu222.04,混合模式,共享)

Maven ※ 安装 Maven 和 Hello World!
$ mvn -version
Apache Maven 3.6.3
Maven 家目录:/usr/share/maven
Java 版本:11.0.17,供应商:Ubuntu,运行时目录:/usr/lib/jvm/java-11-openjdk-amd64

Docker Desktop
版本 4.16.3(96739)

$ docker –version
Docker 版本 20.10.22,构建 3a2c30b

$ docker-compose –version
Docker Compose 版本 v2.15.1

在这篇文章中,我们基本上会在Ubuntu的终端上进行操作。

显示“Hello World”的步骤

创建Spring Boot WEB服务。

请您参考这里。

 

请转到项目文件夹中。

将”~/tmp/hello-spring-boot”指定为项目文件夹。

$ cd ~/tmp/hello-spring-boot

构建 Java 应用程序

※会生成 target/app.jar。

※即使在创建下面的容器映像的操作中也会同时创建 target/app.jar,但这并非必需。

$ mvn clean install

创建容器映像

构建容器镜像

在本地的 Docker 环境(Docker Desktop)中创建了 app-hello-spring-boot 容器镜像。
创建容器镜像的时间遵循 spring-boot:build-image 的规范,将使用 Unix Epoch 时间。

$ mvn spring-boot:build-image \
    -Dspring-boot.build-image.imageName=app-hello-spring-boot

确认容器映像

$ docker images | grep app-hello-spring-boot
app-hello-spring-boot   latest   e37cc77f2b36   43 years ago   262MB

在本地启动容器映像并进行确认。

$ docker run --name app-local -p 8080:8080 app-hello-spring-boot

请使用curl命令从另一个终端进行确认。

我会确认在8080端口是否可以监听。

$ curl -v http://localhost:8080/api/data
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /api/data HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Mon, 27 Feb 2023 07:18:25 GMT
<
* Connection #0 to host localhost left intact
{"message":"Hello World!"}

在Docker Hub上注册容器映像。

请将$USER替换为您自己的容器注册表。

标签化

$ docker tag app-hello-spring-boot $USER/app-hello-spring-boot:latest

$ docker push $USER/app-hello-spring-boot:latest

我们可以将自定义容器映像发布到Docker Hub上,命名为$USER/app-hello-spring-boot:latest。

获取Azure账号

使用Azure免费帐户在云中构建

使用Azure CLI登录

请您参考这里。

 

登录

$ az login

※ 如果要更新到最新版本

$ az upgrade

蔚蓝环境

资源组

创建资源组

$ az group create \
    --name rg-hello \
    --location japaneast

显示资源组列表

$ az group list

如果要删除资源组的话

$ az group delete -n <group>

容器应用环境

只需一种选择,将以下句子以汉语进行本地化:安装扩展插件 ※仅初次安装

展示步骤如下:扩展功能
安装
$ az extension add –name containerapp –upgrade

注册Microsoft.App命名空间
$ az provider register –namespace Microsoft.App

注册Microsoft.OperationalInsights提供商
$ az provider register –namespace Microsoft.OperationalInsights

创建容器化应用环境
※ 由于可能是基于Kubernetes的环境,所以可能需要一些时间。

$ az containerapp env create \
    --resource-group rg-hello \
    --name cae-hello \
    --location japaneast

列出容器应用程序环境

$ az containerapp env list

• 如果要删除容器应用环境

$ az containerapp env delete -n <env-name> -g <group>

容器应用

创建和部署容器应用程序。
※ 请将 $USER 替换为您自己的容器注册表。

$ az containerapp create \
    --resource-group rg-hello \
    --environment cae-hello \
    --name ca-hello-spring-boot \
    --image $USER/app-hello-spring-boot:latest \
    --target-port 8080 \
    --ingress 'external' \
    --min-replicas 1

容器应用程序已创建完毕。

Container app created. Access your app at https://ca-hello-spring-boot.agreeablepond-79ba8527.japaneast.azurecontainerapps.io/

如果要删除容器应用

$ az containerapp delete -n <name> -g <group>

请使用网络浏览器进行确认
※ 请依据环境更改URL。

https://ca-hello-spring-boot.agreeablepond-79ba8527.japaneast.azurecontainerapps.io/api/data

在网页浏览器上显示 {“message”:”你好,世界!”} 并成功获取了JSON数据。

※ 从另一个终端使用curl命令进行确认

$ curl -v https://ca-hello-spring-boot.agreeablepond-79ba8527.japaneast.azurecontainerapps.io/api/data
*   Trying 20.210.29.255:443...
* Connected to ca-hello-spring-boot.agreeablepond-79ba8527.japaneast.azurecontainerapps.io (20.210.29.255) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.0 (OUT), TLS header, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS header, Finished (20):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS header, Finished (20):
* TLSv1.2 (IN), TLS header, Certificate Status (22):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-CHACHA20-POLY1305
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; CN=agreeablepond-79ba8527.japaneast.azurecontainerapps.io
*  start date: Feb 25 03:06:09 2023 GMT
*  expire date: Feb 20 03:06:09 2024 GMT
*  subjectAltName: host "ca-hello-spring-boot.agreeablepond-79ba8527.japaneast.azurecontainerapps.io" matched cert's "*.agreeablepond-79ba8527.japaneast.azurecontainerapps.io"
*  issuer: C=US; O=Microsoft Corporation; CN=Microsoft Azure TLS Issuing CA 01
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* Using Stream ID: 1 (easy handle 0x55c86464be80)
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
> GET /api/data HTTP/2
> Host: ca-hello-spring-boot.agreeablepond-79ba8527.japaneast.azurecontainerapps.io
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 200
< content-type: application/json
< date: Mon, 27 Feb 2023 07:31:07 GMT
<
* Connection #0 to host ca-hello-spring-boot.agreeablepond-79ba8527.japaneast.azurecontainerapps.io left intact
{"message":"Hello World!"}

在终端上显示{“message”:”你好世界!”},成功获取到了JSON数据。

※ Azure会在其端使用SSL/TLS和HTTP/2协议。

连接到容器应用程序

$ az containerapp exec \
    --resource-group rg-hello \
    --name ca-hello-spring-boot 
INFO: Connecting to the container 'ca-hello-spring-boot'...
Use ctrl + D to exit.
INFO: Successfully connected to container: 'ca-hello-spring-boot'. 

连接到容器后

$ pwd
/workspace
$ ls -la
total 20
drwxr-xr-x 1 cnb  cnb  4096 Jan  1  1980 .
drwxr-xr-x 1 root root 4096 Feb 27 07:30 ..
drwxr-xr-x 1 cnb  cnb  4096 Jan  1  1980 BOOT-INF
drwxr-xr-x 3 cnb  cnb  4096 Jan  1  1980 META-INF
drwxr-xr-x 3 cnb  cnb  4096 Jan  1  1980 org

容器的信息

$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.6 LTS"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
NAME="Ubuntu"
ID=ubuntu
PRETTY_NAME="Paketo Buildpacks Base Bionic"
VERSION_ID="18.04"
HOME_URL="https://github.com/paketo-buildpacks/bionic-base-stack"
UBUNTU_CODENAME=bionic
VERSION="18.04.6 LTS (Bionic Beaver)"
ID_LIKE=debian
SUPPORT_URL="https://github.com/paketo-buildpacks/bionic-base-stack/blob/main/README.md"
BUG_REPORT_URL="https://github.com/paketo-buildpacks/bionic-base-stack/issues/new"
VERSION_CODENAME=bionic

总结

    Ubuntu のシンプルな構成の Java 開発環境で Spring Boot WEBサービスのカスタムコンテナイメージを ACA (Azure Container Apps) 環境で起動させることが出来ました。

个人观点

    • Azure でコンテナ化されたアプリをデプロイする環境としては一番分かりやすいと思いました。

JVM コンテナで Hollo World するだけなら、世の中の PaaS で一番初心者向きではないでしょうか? 環境依存の特殊な設定などなく、素の JVM コンテナイメージをそのまま実行出来ました。
今後、他の Azure サービスでカスタムコンテナイメージを使用してアプリをデプロイする方式を比較したいと思います。

メリット

SSH の設定なしてデフォルトでコンテナに接続出来るのは手軽で良いと思います。

デメリット

強いて上げれば初回の環境の構築に時間がかかります。

请举一个例子。

【微软学习】Azure容器应用命令参考手册

 

bannerAds