如何处理 Azure 应用服务部署中的长启动时间

问题描述 投票:0回答:1

我正在将 Spring Boot 应用程序部署到 Azure 应用程序服务。此应用程序连接到配置为使用 Flyway 迁移的数据库。

当尝试发布我的最新版本时,我有一个飞路迁移脚本需要很长时间才能完成。这是预料之中的,因为我正在使用一个非常大的数据库,并且迁移脚本正在运行一些大量的查询。

不幸的是,我无法完成此飞路迁移,因为应用程序将在 5 分钟后自动重新启动,导致迁移过程崩溃并切断与数据库的连接。发生这种情况是因为它认为启动序列已失败,而迁移仍在进行中。

我在部署日志中收到错误:

Starting container for site
INFO  - docker run (... my site info)
ERROR - Container xxx for site yyy did not start within expected time limit. Elapsed time = 230.8677441 sec

我相信这是预期的行为,因为部署过程正在等待应用程序返回成功的运行状况检查,然后再考虑它是否已启动。迁移正在进行时,应用程序未启动且不响应任何运行状况检查。

我尝试将 WEBSITES_CONTAINER_START_TIME_LIMIT 环境变量设置为允许的最大值 1800 秒(30 分钟)。但这还不足以让我的迁移完全完成,迁移过程会在 30 分钟后终止,导致整个部署失败。

在 Azure 终止应用程序之前,我应该如何继续为应用程序分配更多时间来执行迁移?

非常感谢

spring-boot azure docker azure-web-app-service flyway
1个回答
0
投票

将 Flyway 迁移与应用程序启动分离。 在 CI/CD 管道中添加一个步骤,以便在将应用程序部署到 Azure Web 服务之前使用相同的映像运行 Flyway 迁移。

Azure DevOps 的管道步骤:

- task: Bash@3 displayName: "Run Flyway migrations" inputs: targetType: "inline" script: | docker run --rm \ -e FLYWAY_URL=jdbc:<db-url> \ -e FLYWAY_USER=<db-user> \ -e FLYWAY_PASSWORD=<db-password> \ <acr-name>.azurecr.io/<image-name>:<tag> \ flyway migrate
专门为迁移目的标记映像,允许您部署应用程序而无需在启动期间运行迁移。

Dockerfile

中添加命令:

CMD ["java", "-Dspring.profiles.active=migration", "-jar", "app.jar"]
在 CI/CD 期间,使用 

migration

 配置文件拉取并运行映像,然后将其部署到 Azure。

docker run -e SPRING_PROFILES_ACTIVE=migration <acr-name>.azurecr.io/<image-name>:<tag>

    调整应用程序中的健康检查逻辑,以在迁移期间返回
  • 200 OK
    。例如,使用像 
    "migration in progress"
     这样的轻量级响应。
@RestController public class HealthCheckController { @GetMapping("/health") public ResponseEntity<String> health() { if (migrationInProgress) { return ResponseEntity.ok("Migration in progress"); } return ResponseEntity.ok("Healthy"); } }
如果 CI/CD 管道由于时间限制而无法容纳迁移,请在 

Azure 容器实例 (ACI) 中运行它们

az container create \ --resource-group <resource-group> \ --name db-migration \ --image <acr-name>.azurecr.io/<image-name>:<tag> \ --registry-login-server <acr-name>.azurecr.io \ --registry-username <acr-username> \ --registry-password <acr-password> \ --command-line "java -Dspring.profiles.active=migration -jar app.jar"
管道日志

构建阶段

2024-11-20 10:00:00 Starting job: Build 2024-11-20 10:00:01 Checking out repository... > actions/checkout@v4 ✓ Checked out repository code 2024-11-20 10:00:10 Setting up Java version... > actions/setup-java@v1 ✓ Set Java version to 17 2024-11-20 10:00:20 Building project with Maven... $ mvn clean install [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.example:my-app >------------------------ [INFO] Building my-app 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ my-app --- [INFO] Deleting /home/runner/work/my-app/target [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ my-app --- [INFO] Copying resources to /home/runner/work/my-app/target/classes [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ my-app --- [INFO] Compiling 20 source files to /home/runner/work/my-app/target/classes [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ my-app --- [INFO] Copying resources to /home/runner/work/my-app/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ my-app --- [INFO] Tests are passing [INFO] [INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ my-app --- [INFO] Building jar: /home/runner/work/my-app/target/my-app-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ my-app --- [INFO] Installing /home/runner/work/my-app/target/my-app-1.0-SNAPSHOT.jar to local repository [INFO] Build success 2024-11-20 10:05:00 Build completed successfully.

迁徙

2024-11-20 10:05:10 Starting job: Migrations 2024-11-20 10:05:11 Pulling Docker image from ACR... $ docker pull myacr.azurecr.io/my-app:latest latest: Pulling from myacr.azurecr.io/my-app Digest: sha256:abcdef1234567890 Status: Image is up to date for myacr.azurecr.io/my-app:latest 2024-11-20 10:05:30 Running Flyway migrations... $ docker run --rm \ -e FLYWAY_URL=jdbc:postgresql://mydb.postgres.database.azure.com:5432/mydatabase \ -e FLYWAY_USER=myuser \ -e FLYWAY_PASSWORD=mypassword \ myacr.azurecr.io/my-app:latest \ flyway migrate 2024-11-20 10:05:31 INFO - Starting Flyway Command-Line Tool 2024-11-20 10:05:31 INFO - Database: jdbc:postgresql://mydb.postgres.database.azure.com:5432/mydatabase (PostgreSQL 12) 2024-11-20 10:05:31 INFO - Flyway Community Edition 9.0.0 by Redgate 2024-11-20 10:05:31 INFO - Validating migrations ... 2024-11-20 10:05:32 INFO - Successfully validated 5 migrations (execution time 00:01.001s) 2024-11-20 10:05:32 INFO - Current version of schema `public`: 2.3 2024-11-20 10:05:32 INFO - Migrating schema `public` to version 2.4 - add new indexes 2024-11-20 10:15:40 INFO - Successfully applied migration to version 2.4 (execution time 00:10:08.123s) 2024-11-20 10:15:40 INFO - Migrating schema `public` to version 2.5 - update large table 2024-11-20 10:45:45 INFO - Successfully applied migration to version 2.5 (execution time 00:30:04.891s) 2024-11-20 10:45:45 INFO - Successfully applied 2 migrations (execution time 00:40:15.015s) 2024-11-20 10:45:45 INFO - Schema `public` is now at version 2.5 2024-11-20 10:45:45 INFO - Migration completed successfully. 2024-11-20 10:46:00 Flyway migrations completed.

部署应用程序:

2024-11-20 10:46:10 Starting job: Deploy 2024-11-20 10:46:11 Downloading artifact from build job... > actions/download-artifact@v3 Artifact 'java-app' downloaded successfully. 2024-11-20 10:46:20 Deploying to Azure Web App... > azure/webapps-deploy@v2 [INFO] Using publish profile from secret. [INFO] Deploying jar file: /home/runner/work/my-app/target/my-app-1.0-SNAPSHOT.jar to app 'sp-az-webapp' 2024-11-20 10:47:00 INFO - Pulling image from ACR: myacr.azurecr.io/my-app:latest 2024-11-20 10:47:20 INFO - Image pulled successfully 2024-11-20 10:47:25 INFO - Deploying container to Azure Web App: sp-az-webapp 2024-11-20 10:47:30 INFO - Starting container for site 2024-11-20 10:47:32 INFO - Initiating container start-up 2024-11-20 10:47:40 INFO - Waiting for site to become ready ... 2024-11-20 10:48:00 INFO - Site is up and running 2024-11-20 10:48:00 INFO - Deployment completed successfully. Web App URL: https://sp-az-webapp.azurewebsites.net
    
© www.soinside.com 2019 - 2024. All rights reserved.