만들기/EagleEye

[EagleEye][서비스구현] Spring boot with K8s

pythaac 2022. 5. 15. 20:55

https://pythaac.tistory.com/437

 

[EagleEye][서비스구현] Spring boot with Docker

https://spring.io/guides/gs/spring-boot-docker/ Spring Boot with Docker this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and technique..

pythaac.tistory.com

목표 변경

  • 위 글에서 간단한 서비스를 구현하고 테스트하기로 했었지만,
  • 그보다 더욱더 간단하게 만들어 테스트하기로 함
    • 1차 목표 : Hello world처럼 간단한 서버를 쿠버네티스에 배포하여 테스트해보기
    • 2차 목표 : 테스트 환경에서 쿠버네티스 자체가 지원하는 모니터링 확인 (Metric server, Prometheuse/Grafana 등)
    • 3차 목표 : 서버에서 로깅하는 데이터를 수집하기 (아마도 fluentd?)
    • 4차 목표 : 모은 데이터들을 가공하여 다른 결과 도출하기
    • 5차 목표 : 위 서비스 구현하여 테스트
  • 먼저 쿠버네티스에 Spring 어플리케이션을 쿠버네티스에 배포하여 테스트해보자

 

테스트 서비스

  • fast-service
    - {fast-service}/fast : 1_000_000까지의 합을 반환 (-500 ~ +500)
  • normal-service
    - {normal-service}/normal : 1_000_000_000까지의 합을 반환 (-500 ~ +500)
  • slow-service
    - {slow-service}/slow : 10_000_000_000까지의 합을 반환 (-500 ~ +500)

 

서비스 빌드/테스트

https://spring.io/guides/gs/spring-boot-kubernetes/

 

Spring Boot Kubernetes

this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team

spring.io

  • 애플리케이션 가져오기
    >> git clone ~
    >> git pull
  • 애플리케이션에는 actuator dependency가 추가되어 있음
  • +++ prometheus dependency도 추가되어야함 (22.05.17)
// build.gradle
implementation 'org.springframework.boot:spring-boot-starter-actuator'
+++ implementation 'io.micrometer:micrometer-registry-prometheus' (22.05.17)

// application.properties
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
  • gradlew 권한 변경
    >> sudo chmod 777 gradlew
  • 빌드
    >> ./gradlew build

  • 실행 및 actuator 동작 확인
    >> java -jar ./build/libs/~~.jar
    >> curl -X GET {host}:8080/actuator

 

컨테이너화

Docker를 다시 살려야할듯...
Container 이미지를 (도커 없이)만들 수 있는 방법도 많은 듯 하나 Docker를 이용
  • Docker 살리기 (삭제하지 않고 이전 포스트에 따라 죽인 경우)
    >> sudo systemctl start docker.servcie
    >> sudo systemctl start docker.socket

 

  • 컨테이너 이미지 빌드
    >> ./gradlew bootBuildImage
  • [라즈베리파이] 라즈베리파이는 위 방법으로 안되는듯 (exec format error)
    • Dockerfile 생성
      >> vi Dockerfile
    • 아래 내용 작성
    • 빌드
      >> sudo docker build --tag fast-service .
FROM hypriot/rpi-java

ADD build/libs/*SNAPSHOT.jar /opt/app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/opt/app.jar"]

 

 

Building SpringBoot application for Kubernetes on RaspberryPi

Prerequisite Raspberry Pi board with CentOS installed: ilyoan.tistory.com/entry/Installation-of-CentOS-8-on-RaspberryPi-4-B?category=901156 Kubernetes Cluster installed on the Raspberry Pi boards: i..

ilyoan.tistory.com

  • (필수가 아닐수도 있음)이미지 레지스트리에 push
    - docker hub에 가입
    >> sudo docker login
    >> sudo docker tag {현재 태그이름} {유저이름/레포지토리이름}
    >> sudo docker push pythaac/fast-service

 

쿠버네티스에 배포

  • "fast-service"로 yaml 파일 만들기
    >> kubectl create deployment fast-service --image=pythaac/fast-service --dry-run=client -o=yaml > deployment.yaml
    >> echo --- >> deployment.yaml
    >> kubectl create service clusterip fast-service --tcp=8080:8080 --dry-run=client -o=yaml >> deployment.yaml

  • 쿠버네티스에 배포
    >> kubectl apply -f deployment.yaml
  • 실행 확인
    >> kubectl get all

  • 테스트
    • 8080 포트를 fast-service로 포워딩
      >> kubectl port-forward service/fast-service 8080:8080
    • (로컬 환경에서) 테스트
      >> curl localhost:8080/actuator/health
      >> curl localhost:8080/fast (api 호출 테스트)