Как настроить порт контейнера и балансировщик нагрузки для aws fargate, используя pulumi? - PullRequest
1 голос
/ 10 апреля 2020

Я пытаюсь развернуть простое приложение flask python на aws fargate, используя Pulumi. Dockerfile приложения python предоставляет порт 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;

И когда я сверну 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?

1 Ответ

1 голос
/ 10 апреля 2020

Вы можете указать целевой порт на балансировщике нагрузки приложения:

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

Затем вы можете просто передать прослушиватель на сопоставления сервисного порта:

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

Вот полный repro с publi c 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;
...