devchang
ECS pipeline 구성 본문
ECS 생성
CodePipeline 구성
ECS 생성
먼저 간단한 스프링부트 프로젝트를 만든다
index.java
package com.example.wsitest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@RestController
@RequestMapping("/")
public class index implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") // 프론트엔드 도메인
.allowedMethods("GET", "POST", "PUT", "DELETE") // 허용할 HTTP 메서드
.allowedHeaders("*"); // 모든 헤더 허용
}
@PostMapping("/data")
public String receiveData(@RequestBody String data) {
// 데이터를 받아와서 원하는 처리를 수행합니다.
System.out.println("Received data from frontend: " + data);
return "{\"message\": \"Hello from backend!\"}";
}
}
dockerfile
FROM openjdk:17-jdk-alpine AS build
WORKDIR /app
COPY . .
RUN chmod +x ./gradlew
RUN ./gradlew clean build -x test
FROM openjdk:17-jdk-alpine
WORKDIR /app
COPY --from=build /app/build/libs/*.jar /app/app.jar
CMD ["java", "-jar", "/app/app.jar"]
ecs를 먼저 생성하기 전에 ALB를 생성 해줄것이다
ALB생성
만약 blue/green으로 배포를 할 경우에는 대상그룹이 2개가 있어야 한다
fargate형식으로 배포할 것이기 때문에 대상그룹의 대상유형은 ip로 설정한다
ECS 생성
ECS를 생성 해준다
ECS를 생성할 때 서비스를 blue/green배포방식을 선택하려면 AWSCodeDeployRoleForECS 정책이 연결 된 codedeploy 역할이 필요하다
역할의 사용서비스는 CodeDeploy - ECS로 한다
ECS CodePipeline 구성
appspec.yaml 작성
version: 0.0
Resources:
- TargetService:
Type: AWS::ECS::Service
Properties:
TaskDefinition: #"작업정의 arn"
LoadBalancerInfo:
ContainerName: "stress"
ContainerPort: 8080
buildspec.yaml 작성
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
pre_build:
commands: #ecr 로그인 하는 명령어
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 702661606257.dkr.ecr.ap-northeast-2.amazonaws.com
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t htmlspring . #ecr image build 명령어
- docker tag htmlspring:latest 702661606257.dkr.ecr.ap-northeast-2.amazonaws.com/htmlspring:latest #ecr image 태그변경 명령어
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image to Amazon ECR...
- curl https://github.com/ggomchang #지림
- docker push 702661606257.dkr.ecr.ap-northeast-2.amazonaws.com/htmlspring:latest #ecr push하는 명령어
artifacts:
files:
- appspec.yaml
- taskdef.json
taskdef.json 작성(ECS 작업정의 만들고 그냥 json보기로 복붙하면 됨)
{
"taskDefinitionArn": "arn:aws:ecs:ap-northeast-2:702661606257:task-definition/htmlspring:2",
"containerDefinitions": [
{
"name": "asd",
"image": "702661606257.dkr.ecr.ap-northeast-2.amazonaws.com/htmlspring:latest",
"cpu": 0,
"portMappings": [
{
"name": "asd-8080-tcp",
"containerPort": 8080,
"hostPort": 8080,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"environment": [],
"environmentFiles": [],
"mountPoints": [],
"volumesFrom": [],
"ulimits": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/htmlspring",
"awslogs-region": "ap-northeast-2",
"awslogs-stream-prefix": "ecs"
},
"secretOptions": []
},
"systemControls": []
}
],
"family": "htmlspring",
"taskRoleArn": "arn:aws:iam::702661606257:role/ecsTaskExecutionRole",
"executionRoleArn": "arn:aws:iam::702661606257:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"revision": 2,
"volumes": [],
"status": "ACTIVE",
"requiresAttributes": [
{
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
},
{
"name": "ecs.capability.execution-role-awslogs"
},
{
"name": "com.amazonaws.ecs.capability.ecr-auth"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
},
{
"name": "com.amazonaws.ecs.capability.task-iam-role"
},
{
"name": "ecs.capability.execution-role-ecr-pull"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
},
{
"name": "ecs.capability.task-eni"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.29"
}
],
"placementConstraints": [],
"compatibilities": [
"EC2",
"FARGATE"
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
},
"registeredAt": "2024-03-30T10:27:29.675Z",
"registeredBy": "arn:aws:iam::702661606257:root",
"tags": []
}
git push를 하기 전에 이거를 해주면 자격증명을 입력을 안해도 된다
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
CodeCommit
-새로운 사용자 만들어서 codecommit 자격증명 생성하기
-codecommitadmin 이나 codecommitpoweruser 권한을 준다
-자격증명 생성 후 저장을 잘 해야 한다
CodeBuild
-이거를 켜줘야 Dockerfile을 build함
-그리고 만들고 난 뒤에 역할에다가 정책을 추가해줘야 함
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:BatchCheckLayerAvailability",
"ecr:PutImage"
],
"Resource": "*"
}
]
}
CodeDeploy
blue/green배포방식으로 만들어준다
만약 롤링 배포방식이면
imagedefinitions.json 작성하고 buildspec.yaml의 artifacts에도 추가해줘야 한다
[
{
"name": "asd",
"imageUri": "702661606257.dkr.ecr.ap-northeast-2.amazonaws.com/htmlspring:latest"
}
]
CodePipeline
codepipeline을 생성해준다
'클라우드' 카테고리의 다른 글
Docker 자주 쓰는 명령어 정리 (0) | 2024.05.03 |
---|---|
Docker 간단하게 정리 (0) | 2024.05.03 |
EKS + ArgoCD + CodePipeline 으로 CI/CD 구성 하기 (0) | 2024.05.02 |