![](/img/trans.png)
[英]How to configure AWS ECS Fargate Tasks/Cluster to access private Docker Container Registry (like GitHub Container Registry) with Pulumi?
[英]AWS ECS Fargate Tasks caught in restart-loop - How to configure LoadBalancer TargetGroup health checks with Pulumi?
我創建了一個簡單的 Pulumi TypeScript 程序示例,該程序應將 Spring 引導應用程序部署到 AWS ECS Fargate 集群中。 Spring 引導應用程序在 Cloud Native Buildpacks/Paketo.io 的幫助下進行了容器化/Docker 化,並發布到 GitHub 容器注冊表示例,位於ghcr.io/jonashackt/microservice-api-spring-boot
我已經閱讀了一些 Pulumi 教程,並從通常的pulumi new aws-typescript
開始。 我現在有以下index.ts
:
import * as awsx from "@pulumi/awsx";
// Create a load balancer to listen for requests and route them to the container.
let loadbalancer = new awsx.lb.ApplicationListener("alb", { port: 8098, protocol: "HTTP" });
// Define Container image published to the GitHub Container Registry
let service = new awsx.ecs.FargateService("microservice-api-spring-boot", {
taskDefinitionArgs: {
containers: {
microservice_api_spring_boot: {
image: "ghcr.io/jonashackt/microservice-api-spring-boot:latest",
memory: 768,
portMappings: [ loadbalancer ],
},
},
},
desiredCount: 2,
});
// Export the URL so we can easily access it.
export const apiUrl = loadbalancer.endpoint.hostname;
選擇dev
堆棧后,正常的pulumi up
運行並為我提供 ApplicationLoadBalancer URL。 這也是我准備展示一切順利的 asciicast:
我現在的問題是 Fargate 服務不斷停止和啟動。 我查看了 CloudWatch 日志,我看到 Spring 啟動應用程序正在啟動 - 並且在幾秒鍾后再次停止。 我已經檢查了 ApplicationLoadBalancer 的 TargetGroup,我看到Registered Targets
一次又一次地變得unhealthy
。 我該如何解決?
默認的 AWS TargetGroup HealthCheckPath
就是/
(請參閱文檔)。 作為標准的 Spring 引導應用程序通常會響應HTTP 404
,如下所示:
ApplicationLoadBalancers 運行狀況檢查 TargetGroups go 中的Status
為unhealthy
,從而觸發 Fargate 服務的重啟。
我們如何解決這個問題? 在 Spring Boot 中,您通常會使用spring-boot-actuator 。 將其添加到您的pom.xml
應用程序響應localhost:yourPort/actuator/health
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
所以我們需要配置 Pulumi 創建的 TargetGroup 使用健康檢查路徑/actuator/health
而不是/
。
Pulumi 文檔告訴我們如何手動配置目標組,但是如何將這些完全集成到 TypeScript 代碼中? 答案隱藏在@pulumi/awsx/lb 文檔中! Pulumi 教程中的示例代碼從一行中完成了多項操作let loadbalancer = new awsx.lb.ApplicationListener("alb", { port: 8098, protocol: "HTTP" });
:
我們只需要手動創建每個組件,因為這樣我們可以配置 TargetGroup 的 healthCheck: healthCheck: path
屬性:
import * as awsx from "@pulumi/awsx";
// Spring Boot Apps port
const port = 8098;
// Create a ApplicationLoadBalancer to listen for requests and route them to the container.
const alb = new awsx.lb.ApplicationLoadBalancer("fargateAlb");
// Create TargetGroup & Listener manually (see https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/awsx/lb/)
// so that we can configure the TargetGroup HealthCheck as described in (https://www.pulumi.com/docs/guides/crosswalk/aws/elb/#manually-configuring-target-groups)
// otherwise our Spring Boot Containers will be restarted every time, since the TargetGroup HealthChecks Status always
// goes to unhealthy
const albTargetGroup = alb.createTargetGroup("fargateAlbTargetGroup", {
port: port,
protocol: "HTTP",
healthCheck: {
// Use the default spring-boot-actuator health endpoint
path: "/actuator/health"
}
});
const albListener = albTargetGroup.createListener("fargateAlbListener", { port: port, protocol: "HTTP" });
// Define Container image published to the GitHub Container Registry
const service = new awsx.ecs.FargateService("microservice-api-spring-boot", {
taskDefinitionArgs: {
containers: {
microservice_api_spring_boot: {
image: "ghcr.io/jonashackt/microservice-api-spring-boot:latest",
memory: 768,
portMappings: [ albListener ]
},
},
},
desiredCount: 2,
});
// Export the URL so we can easily access it.
export const apiUrl = albListener.endpoint.hostname;
現在有了這個配置,我們的 Fargate 服務在啟動后應該會恢復正常。 我們應該能夠在 AWS 控制台的 ALB 的 TargetGroup 中看到這一點:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.