gpt4 book ai didi

pulumi - 如何使用 pulumi 为 aws fargate 设置容器端口和负载均衡器?

转载 作者:行者123 更新时间:2023-12-03 08:46:50 24 4
gpt4 key购买 nike

我正在尝试使用 Pulumi 在 aws fargate 上部署简单的 Flask python 应用程序。 python应用程序的dockerfile从容器公开端口8000。我如何使用 pulumi 设置负载均衡器?到目前为止,我已经使用index.ts(pulumi)尝试了以下操作:

import * as awsx from "@pulumi/awsx";

// Step 1: Create an ECS Fargate cluster.
const cluster = new awsx.ecs.Cluster("first_cluster");

// Step 2: Define the Networking for our service.
const alb = new awsx.elasticloadbalancingv2.ApplicationLoadBalancer(
"net-lb", { external: true, securityGroups: cluster.securityGroups });
const web = alb.createListener("web", { port: 80, external: true });

// Step 3: Build and publish a Docker image to a private ECR registry.
const img = awsx.ecs.Image.fromPath("app-img", "./app");

// Step 4: Create a Fargate service task that can scale out.
const appService = new awsx.ecs.FargateService("app-svc", {
cluster,
taskDefinitionArgs: {
container: {
image: img,
cpu: 102 /*10% of 1024*/,
memory: 50 /*MB*/,
portMappings: [{ containerPort: 8000, }],
},
},
desiredCount: 5,
});

// Step 5: Export the Internet address for the service.
export const url = web.endpoint.hostname;

当我 curl URL curl http://$(pulumi stack output url) 时,我得到:

<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body bgcolor="white">
<center><h1>503 Service Temporarily Unavailable</h1></center>
</body>
</html>

如何将负载均衡器端口映射到容器端口 8000?

最佳答案

您可以在应用程序负载均衡器上指定目标端口:

const atg = alb.createTargetGroup(
"app-tg", { port: 8000, deregistrationDelay: 0 });

然后您可以简单地将监听器传递给服务端口映射:

const appService = new awsx.ecs.FargateService("app-svc", {
// ...
taskDefinitionArgs: {
container: {
// ...
portMappings: [web],
},
},
});

这是一个带有公共(public) docker 容器的完整重现,以便任何人都可以从工作示例开始:

import * as awsx from "@pulumi/awsx";

const cluster = new awsx.ecs.Cluster("first_cluster");

const alb = new awsx.elasticloadbalancingv2.ApplicationLoadBalancer(
"app-lb", { external: true, securityGroups: cluster.securityGroups });
const atg = alb.createTargetGroup(
"app-tg", { port: 8080, deregistrationDelay: 0 });
const web = atg.createListener("web", { port: 80 });

const appService = new awsx.ecs.FargateService("app-svc", {
cluster,
taskDefinitionArgs: {
container: {
image: "gcr.io/google-samples/kubernetes-bootcamp:v1",
portMappings: [web],
},
},
desiredCount: 1,
});

export const url = web.endpoint.hostname;

关于pulumi - 如何使用 pulumi 为 aws fargate 设置容器端口和负载均衡器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61132945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com