Fargate 및 DOCKER를 사용하여 Spring Boot 및 AWS로 마이크로서비스를 구축하는 방법 - 5부

EC2, ECS 및 Fargate를 사용하여 AWS에 애플리케이션 배포
먼저 이 종속성을 POM.XML에 추가해야 합니다.
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ec2</artifactId>
<version>${cdk.version}</version>
</dependency>
AwsSpringCdkStack 클래스를 복사하고 템플릿을 사용합니다.
이름이 VpcStack인 클래스 복사

VPC를 생성하자

그런 다음 vpc를 인스턴스화해야 합니다.

VPC 배포
이제 터미널에서 cdk list 명령을 작성하여 모든 스택을 확인하십시오.
cdk는 vpc를 배포하기 위해 Vpc를 배포합니다.

AWS에서 스택 상태를 볼 수 있습니다.

생성된 상태는 다음과 같습니다.

VPC 페이지에서 vpcs를 확인할 수 있습니다.

클러스터 생성
먼저 POM.XML에 두 개의 종속성을 추가합니다.
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ecs</artifactId>
<version>${cdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ecs-patterns</artifactId>
<version>${cdk.version}</version>
</dependency>
그런 다음 AwsSpringCdkStack 클래스를 ClusterStack이라는 새 클래스에 복사합니다.
다음으로 생성자 매개변수에서 Vpc라는 매개변수를 하나 더 추가해야 합니다. 클러스터는 VPC 내부에 있어야 합니다.

public class ClusterStack extends Stack {
public ClusterStack(final Construct scope, final String id, Vpc vpc) {
this(scope, id, null, vpc);
}
public ClusterStack(final Construct scope, final String id, final StackProps props, Vpc vpc) {
super(scope, id, props);
Cluster.Builder.create(this, id)
.clusterName("cluster-01")
.vpc(vpc)
.build();
}
}
ClusterStack에 대한 vpc를 전달하려면 VpcStack에서 변경해야 합니다.

이제 Vpc 유형으로 개인 속성을 생성하고 이에 대한 getter를 생성합니다.
package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.Vpc;
public class VpcStack extends Stack {
public VpcStack(final Construct scope, final String id) {
this(scope, id, null);
}
private Vpc vpc;
public VpcStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
//he need two parameters, scope and id
vpc = Vpc.Builder.create(this, "Vpc01")
.maxAzs(3) //Max availability quantity zones that our vpc will act inside the region
.build();
// The code that defines your stack goes here
}
public Vpc getVpc() {
return vpc;
}
}
이제 clusterStack에 인스턴스를 만들 것입니다.

터미널에 넣어:
cdk 차이

프로젝트와 aws의 차이점을 볼 수 있습니다.
이제 배포를

효과가있다

클러스터가 생성되었습니다!

애플리케이션 로드 밸런서
인스턴스에 대한 요청 균형을 만드는 책임자
우리는 볼 것입니다:
서비스
대상 그룹
자동 배율 규칙
작업 정의
de 코드를 사용하여 새 스택을 복사합니다.
package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.RemovalPolicy;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.ecs.AwsLogDriverProps;
import software.amazon.awscdk.services.ecs.Cluster;
import software.amazon.awscdk.services.ecs.ContainerImage;
import software.amazon.awscdk.services.ecs.LogDriver;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedFargateService;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedTaskImageOptions;
import software.amazon.awscdk.services.logs.LogGroup;
public class Service01Stack extends Stack {
// Specify the cluster
public Service01Stack(final Construct scope, final String id, Cluster cluster) {
this(scope, id, null, cluster);
}
public Service01Stack(final Construct scope, final String id, final StackProps props, Cluster cluster) {
super(scope, id, props);
ApplicationLoadBalancedFargateService service01 = ApplicationLoadBalancedFargateService
.Builder
.create(this, "ALB-01")
.serviceName("service-01")
.cluster(cluster)
.cpu(512) // how much cpu we need use
.desiredCount(2) // how much instances we want
.listenerPort(8080)
.memoryLimitMiB(1024)
.taskImageOptions(
//Container informations
ApplicationLoadBalancedTaskImageOptions.builder()
.containerName("aws_project01")
.image(ContainerImage.fromRegistry("pedrospiet/aws_training_01:1.0.0")) //Your repository on dockerHub
.containerPort(8080)
.logDriver(LogDriver.awsLogs(AwsLogDriverProps.builder()
.logGroup(LogGroup.Builder.create(this, "Service01LogGroup")
.logGroupName("Service01")
.removalPolicy(RemovalPolicy.DESTROY)
.build()
).streamPrefix("Service01")
.build()))
.build()
).publicLoadBalancer(true)
.build();
}
}
도커 컨테이너가 있는 새 스택을 생성하고 로그를 볼 수 있는 CloudWatch의 로그 정보를 기록합니다.
인스턴스화를 위해서는 클러스터를 전달해야 합니다. 먼저 ClusterStack 클래스를 변경해야 합니다.
public class ClusterStack extends Stack {
public ClusterStack(final Construct scope, final String id, Vpc vpc) {
this(scope, id, null, vpc);
}
private Cluster cluster;
public ClusterStack(final Construct scope, final String id, final StackProps props, Vpc vpc) {
super(scope, id, props);
cluster = Cluster.Builder.create(this, id)
.clusterName("cluster-01")
.vpc(vpc)
.build();
}
public Cluster getCluster() {
return cluster;
}
}
이제 Auto Scalling 및 Health Check를 추가합니다.
service01.getTargetGroup().configureHealthCheck(new HealthCheck.Builder()
.path("/actuator/health")
.port("8080")
.healthyHttpCodes("200")
.build());
ScalableTaskCount scalableTaskCount = service01.getService().autoScaleTaskCount(EnableScalingProps.builder()
.minCapacity(2)
.maxCapacity(4)
.build());
scalableTaskCount.scaleOnCpuUtilization("Service01AutoScaling", CpuUtilizationScalingProps.builder()
.targetUtilizationPercent(50)
.scaleInCooldown(Duration.seconds(60))
.scaleOutCooldown(Duration.seconds(60))
.build());
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ec2</artifactId>
<version>${cdk.version}</version>
</dependency>
이제 터미널에서 cdk list 명령을 작성하여 모든 스택을 확인하십시오.
cdk는 vpc를 배포하기 위해 Vpc를 배포합니다.

AWS에서 스택 상태를 볼 수 있습니다.

생성된 상태는 다음과 같습니다.

VPC 페이지에서 vpcs를 확인할 수 있습니다.

클러스터 생성
먼저 POM.XML에 두 개의 종속성을 추가합니다.
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ecs</artifactId>
<version>${cdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ecs-patterns</artifactId>
<version>${cdk.version}</version>
</dependency>
그런 다음 AwsSpringCdkStack 클래스를 ClusterStack이라는 새 클래스에 복사합니다.
다음으로 생성자 매개변수에서 Vpc라는 매개변수를 하나 더 추가해야 합니다. 클러스터는 VPC 내부에 있어야 합니다.

public class ClusterStack extends Stack {
public ClusterStack(final Construct scope, final String id, Vpc vpc) {
this(scope, id, null, vpc);
}
public ClusterStack(final Construct scope, final String id, final StackProps props, Vpc vpc) {
super(scope, id, props);
Cluster.Builder.create(this, id)
.clusterName("cluster-01")
.vpc(vpc)
.build();
}
}
ClusterStack에 대한 vpc를 전달하려면 VpcStack에서 변경해야 합니다.

이제 Vpc 유형으로 개인 속성을 생성하고 이에 대한 getter를 생성합니다.
package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.Vpc;
public class VpcStack extends Stack {
public VpcStack(final Construct scope, final String id) {
this(scope, id, null);
}
private Vpc vpc;
public VpcStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
//he need two parameters, scope and id
vpc = Vpc.Builder.create(this, "Vpc01")
.maxAzs(3) //Max availability quantity zones that our vpc will act inside the region
.build();
// The code that defines your stack goes here
}
public Vpc getVpc() {
return vpc;
}
}
이제 clusterStack에 인스턴스를 만들 것입니다.

터미널에 넣어:
cdk 차이

프로젝트와 aws의 차이점을 볼 수 있습니다.
이제 배포를

효과가있다

클러스터가 생성되었습니다!

애플리케이션 로드 밸런서
인스턴스에 대한 요청 균형을 만드는 책임자
우리는 볼 것입니다:
서비스
대상 그룹
자동 배율 규칙
작업 정의
de 코드를 사용하여 새 스택을 복사합니다.
package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.RemovalPolicy;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.ecs.AwsLogDriverProps;
import software.amazon.awscdk.services.ecs.Cluster;
import software.amazon.awscdk.services.ecs.ContainerImage;
import software.amazon.awscdk.services.ecs.LogDriver;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedFargateService;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedTaskImageOptions;
import software.amazon.awscdk.services.logs.LogGroup;
public class Service01Stack extends Stack {
// Specify the cluster
public Service01Stack(final Construct scope, final String id, Cluster cluster) {
this(scope, id, null, cluster);
}
public Service01Stack(final Construct scope, final String id, final StackProps props, Cluster cluster) {
super(scope, id, props);
ApplicationLoadBalancedFargateService service01 = ApplicationLoadBalancedFargateService
.Builder
.create(this, "ALB-01")
.serviceName("service-01")
.cluster(cluster)
.cpu(512) // how much cpu we need use
.desiredCount(2) // how much instances we want
.listenerPort(8080)
.memoryLimitMiB(1024)
.taskImageOptions(
//Container informations
ApplicationLoadBalancedTaskImageOptions.builder()
.containerName("aws_project01")
.image(ContainerImage.fromRegistry("pedrospiet/aws_training_01:1.0.0")) //Your repository on dockerHub
.containerPort(8080)
.logDriver(LogDriver.awsLogs(AwsLogDriverProps.builder()
.logGroup(LogGroup.Builder.create(this, "Service01LogGroup")
.logGroupName("Service01")
.removalPolicy(RemovalPolicy.DESTROY)
.build()
).streamPrefix("Service01")
.build()))
.build()
).publicLoadBalancer(true)
.build();
}
}
도커 컨테이너가 있는 새 스택을 생성하고 로그를 볼 수 있는 CloudWatch의 로그 정보를 기록합니다.
인스턴스화를 위해서는 클러스터를 전달해야 합니다. 먼저 ClusterStack 클래스를 변경해야 합니다.
public class ClusterStack extends Stack {
public ClusterStack(final Construct scope, final String id, Vpc vpc) {
this(scope, id, null, vpc);
}
private Cluster cluster;
public ClusterStack(final Construct scope, final String id, final StackProps props, Vpc vpc) {
super(scope, id, props);
cluster = Cluster.Builder.create(this, id)
.clusterName("cluster-01")
.vpc(vpc)
.build();
}
public Cluster getCluster() {
return cluster;
}
}
이제 Auto Scalling 및 Health Check를 추가합니다.
service01.getTargetGroup().configureHealthCheck(new HealthCheck.Builder()
.path("/actuator/health")
.port("8080")
.healthyHttpCodes("200")
.build());
ScalableTaskCount scalableTaskCount = service01.getService().autoScaleTaskCount(EnableScalingProps.builder()
.minCapacity(2)
.maxCapacity(4)
.build());
scalableTaskCount.scaleOnCpuUtilization("Service01AutoScaling", CpuUtilizationScalingProps.builder()
.targetUtilizationPercent(50)
.scaleInCooldown(Duration.seconds(60))
.scaleOutCooldown(Duration.seconds(60))
.build());
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ecs</artifactId>
<version>${cdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>ecs-patterns</artifactId>
<version>${cdk.version}</version>
</dependency>
public class ClusterStack extends Stack {
public ClusterStack(final Construct scope, final String id, Vpc vpc) {
this(scope, id, null, vpc);
}
public ClusterStack(final Construct scope, final String id, final StackProps props, Vpc vpc) {
super(scope, id, props);
Cluster.Builder.create(this, id)
.clusterName("cluster-01")
.vpc(vpc)
.build();
}
}
package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.Vpc;
public class VpcStack extends Stack {
public VpcStack(final Construct scope, final String id) {
this(scope, id, null);
}
private Vpc vpc;
public VpcStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
//he need two parameters, scope and id
vpc = Vpc.Builder.create(this, "Vpc01")
.maxAzs(3) //Max availability quantity zones that our vpc will act inside the region
.build();
// The code that defines your stack goes here
}
public Vpc getVpc() {
return vpc;
}
}
인스턴스에 대한 요청 균형을 만드는 책임자
우리는 볼 것입니다:
서비스
대상 그룹
자동 배율 규칙
작업 정의
de 코드를 사용하여 새 스택을 복사합니다.
package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.RemovalPolicy;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.ecs.AwsLogDriverProps;
import software.amazon.awscdk.services.ecs.Cluster;
import software.amazon.awscdk.services.ecs.ContainerImage;
import software.amazon.awscdk.services.ecs.LogDriver;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedFargateService;
import software.amazon.awscdk.services.ecs.patterns.ApplicationLoadBalancedTaskImageOptions;
import software.amazon.awscdk.services.logs.LogGroup;
public class Service01Stack extends Stack {
// Specify the cluster
public Service01Stack(final Construct scope, final String id, Cluster cluster) {
this(scope, id, null, cluster);
}
public Service01Stack(final Construct scope, final String id, final StackProps props, Cluster cluster) {
super(scope, id, props);
ApplicationLoadBalancedFargateService service01 = ApplicationLoadBalancedFargateService
.Builder
.create(this, "ALB-01")
.serviceName("service-01")
.cluster(cluster)
.cpu(512) // how much cpu we need use
.desiredCount(2) // how much instances we want
.listenerPort(8080)
.memoryLimitMiB(1024)
.taskImageOptions(
//Container informations
ApplicationLoadBalancedTaskImageOptions.builder()
.containerName("aws_project01")
.image(ContainerImage.fromRegistry("pedrospiet/aws_training_01:1.0.0")) //Your repository on dockerHub
.containerPort(8080)
.logDriver(LogDriver.awsLogs(AwsLogDriverProps.builder()
.logGroup(LogGroup.Builder.create(this, "Service01LogGroup")
.logGroupName("Service01")
.removalPolicy(RemovalPolicy.DESTROY)
.build()
).streamPrefix("Service01")
.build()))
.build()
).publicLoadBalancer(true)
.build();
}
}
도커 컨테이너가 있는 새 스택을 생성하고 로그를 볼 수 있는 CloudWatch의 로그 정보를 기록합니다.
인스턴스화를 위해서는 클러스터를 전달해야 합니다. 먼저 ClusterStack 클래스를 변경해야 합니다.
public class ClusterStack extends Stack {
public ClusterStack(final Construct scope, final String id, Vpc vpc) {
this(scope, id, null, vpc);
}
private Cluster cluster;
public ClusterStack(final Construct scope, final String id, final StackProps props, Vpc vpc) {
super(scope, id, props);
cluster = Cluster.Builder.create(this, id)
.clusterName("cluster-01")
.vpc(vpc)
.build();
}
public Cluster getCluster() {
return cluster;
}
}
이제 Auto Scalling 및 Health Check를 추가합니다.
service01.getTargetGroup().configureHealthCheck(new HealthCheck.Builder()
.path("/actuator/health")
.port("8080")
.healthyHttpCodes("200")
.build());
ScalableTaskCount scalableTaskCount = service01.getService().autoScaleTaskCount(EnableScalingProps.builder()
.minCapacity(2)
.maxCapacity(4)
.build());
scalableTaskCount.scaleOnCpuUtilization("Service01AutoScaling", CpuUtilizationScalingProps.builder()
.targetUtilizationPercent(50)
.scaleInCooldown(Duration.seconds(60))
.scaleOutCooldown(Duration.seconds(60))
.build());
메인 클래스에 Service01Stack 추가
public class AwsSpringCdkApp {
public static void main(final String[] args) {
App app = new App();
VpcStack vpcStack = new VpcStack(app, "Vpc");
ClusterStack clusterStack = new ClusterStack(app, "Cluster", null, vpcStack.getVpc());
clusterStack.addDependency(vpcStack);
Service01Stack service01Stack = new Service01Stack(app, "Service01", clusterStack.getCluster());
service01Stack.addDependency(clusterStack);
app.synth();
}
}
배포를 실행하려면

터미널에서 다음을 볼 수 있습니다.
서비스01.ALB01서비스URL774C913E
URL을 복사하여 INSOMNIA에 입력하십시오.

효과가있다!
****** 경고 *****
요금을 피하려면 인스턴스를 파괴해야 합니다.
터미널에 넣어:
cdk destroy Vpc Cluster Service01
그게 다야 :)
Reference
이 문제에 관하여(Fargate 및 DOCKER를 사용하여 Spring Boot 및 AWS로 마이크로서비스를 구축하는 방법 - 5부), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/pedrospiet/how-to-build-microsservices-with-spring-boot-and-aws-with-fargate-and-docker-part-5-dln
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Fargate 및 DOCKER를 사용하여 Spring Boot 및 AWS로 마이크로서비스를 구축하는 방법 - 5부), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/pedrospiet/how-to-build-microsservices-with-spring-boot-and-aws-with-fargate-and-docker-part-5-dln텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)