본문 바로가기

⭐ SpringBoot/SpringBootFramework

Springboot "helloword" 띄우기 with docker, pod

# spring boot 간단하게 hello word 띄우기

1. start.spring.io/ 접속

2. 디펜던시 에서 Spring Web 추가

3. Generate 클릭 후 프로젝트 생성 후 압축해제

4. 인텔리j로 프로젝트 오픈

5. 아레 코드 입력 후 실행

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

 

6. 포트 변경 (기본 포트는 8080 이므로 8899로 변경하여 실행)

- main → resource → application.properties 에서 아래의 코드를 입력

server.port = 8899

 

7. 실행 후 확인

- localhost:8899

8. 추가로 jar 파일로 빌드 후 실행

- Gradle → build → bootJar를 더블클릭

- build → libs → demo.0.0.1-SNAPSHOT.jar 생성 확인

터미널 열고 java -jar demo-0.0.1-SNAPSHOT.jar 명령어로 실행

 

9. eks pod에서 실행하기 위해 .jar 파일을 포함하여 docker  image로 생성

- 일단 jar 파일이 생성된 폴더에 아래의 dockerfile을 생성

FROM openjdk:17-jdk-slim
ARG JAR_FILE=demo-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} test-boot.jar
ENTRYPOINT ["java","-jar","/test-boot.jar"]

 

- dockerfile 빌드 및 ECR로 이미지 업로드, ECR Repository가 사전에 생성 되어 있어야 한다.

- ECR에 있는 도움말과 내용은 같다.

인증 토큰을 검색하고 레지스트리에 대해 Docker 클라이언트를 인증합니다.
AWS CLI 사용:

실행전 윈도우에서는 docker 실행되어있어야 하고, 리눅스도 설치가 되어 있고 프로세스 살아 있어야 한다.

명령어: aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 11111111111.dkr.ecr.ap-northeast-2.amazonaws.com

참고: AWS CLI을(를) 사용하는 중 오류가 발생하면 최신 버전의 AWS CLI 및 Docker가 설치되어 있는지 확인하세요.
다음 명령을 사용하여 도커 이미지를 빌드합니다. 도커 파일을 처음부터 새로 빌드하는 방법에 대한 자세한 내용은 여기  지침을 참조하십시오. 이미지를 이미 빌드한 경우에는 이 단계를 건너뛸 수 있습니다.

명령어: docker build -t test-011 .
빌드가 완료되면 이미지에 태그를 지정하여 이 리포지토리에 푸시할 수 있습니다.

명령어: docker tag test-011:latest 11111111111.dkr.ecr.ap-northeast-2.amazonaws.com/test-011:latest
다음 명령을 실행하여 이 이미지를 새로 생성한 AWS 리포지토리로 푸시합니다.

docker push 11111111111.dkr.ecr.ap-northeast-2.amazonaws.com/test-011:latest

 

10. 업로드 확인

- ECR

- 이미지 태그를 클릭하여 URL 정보를 확인한다. → 111111111.dkr.ecr.ap-northeast-2.amazonaws.com/test-011:v_01

 

11. pod로 실행

- deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld
  namespace: helloworld
  labels:
    app: helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      hostNetwork: true
      containers:
      - name: helloworld
        image: 1234567891011.dkr.ecr.ap-northeast-2.amazonaws.com/test-011:v_01
        ports:
        - containerPort: 8899

- service.yaml

apiVersion: v1
kind: Service
metadata:
  name: helloworld
  namespace: helloworld
  annotations:
    alb.ingress.kubernetes.io/healthcheck-path: "/healthy"
spec:
  selector:
     app: helloworld
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8899

- ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: helloword-ingress
  namespace: helloworld
  annotations:
    #kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
spec:
  ingressClassName: alb
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: "helloworld"
                port:
                  number: 80

 

12. 실행하기

- ALB에서 DNS 주소를 조회하면 아래와 같은 화면을 볼 수 있다.

- 추가로 백엔드 pod의 로그를 확인하면 아래와 같다.

kubectl logs {pod_name} -n {namespace_name} → 정상 실행 확인

 

- 끝 -

 

'⭐ SpringBoot > SpringBootFramework' 카테고리의 다른 글

Framework 프레임워크의 개념  (0) 2023.01.16
SpringBoot 구조 정리  (0) 2022.08.31