Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

아주 간단한 스프링부트 애플리케이션을 쿠버네티스 파드로 띄워보자 #38

Open
skarltjr opened this issue Nov 1, 2021 · 2 comments
Labels

Comments

@skarltjr
Copy link
Owner

skarltjr commented Nov 1, 2021

https://betterprogramming.pub/hello-kubernetes-spring-boot-a20e47d57872 참고

코드 : https://github.com/skarltjr/Simple-k8s_with_Spring

@skarltjr skarltjr added the Record label Nov 1, 2021
@skarltjr
Copy link
Owner Author

skarltjr commented Nov 1, 2021

1. 초간단 애플리케이션 생성

2. Dockerfile 작성

  • 파드를 deployment로 관리할 것 => deployment yaml을 작성할 때 컨테이너에서 사용할 이미지를 먼저 만들어야한다
  • 도커 파일을 작성 후 이를 이미지로 생성 => 도커 허브에 올리기
FROM adoptopenjdk:11-jre-hotspot
          # 11 이미지 내 home/spring 없으면 만들어서 이동하고
WORKDIR /home/spring
         # /home/spring/application.jar에 jar를 복사하여
COPY src/main/jar/*.jar /home/spring/application.jar
VOLUME /tmp
EXPOSE 8080
         # 실행
ENTRYPOINT ["java", "-jar", "/home/spring/application.jar"]

3. 도커 허브에 이미지 올리기

화면 캡처 2021-11-01 215545

  • 이제 소스코드와 도커파일을 활용하여 이미지를 모두 만든 상태
  • 남은 일은 해당 이미지를 활용하여 쿠버네티스에서 deployment를 생성하고 동작시키는 것

4. 쿠버네티스 접속

  • 나는 master1에 접속
  • deployment.yaml 작성할 차례
  • 우선 deployment yaml 생성
  • kubectl create deployment k8s-spring --image=skarltjr/k8s-spring:1.0 --dry-run=client -o yaml spring.yaml
  • 참고로 처음에 이름을 k8s_spring으로 했다가 _는 사용할 수 없어서 수정
  • 중간에 ---는 그냥 있는게 아니다 yaml문법이다 / 문서의 시작 / deployment와 로드밸런싱을 위한 서비스 생성 yaml을 한 번에 -> ---로 나눠서
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: k8s-spring
  name: k8s-spring
spec:
  replicas: 2
  selector:
    matchLabels:
      app: k8s-spring
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: k8s-spring
        visualize: "true"
    spec:
      containers:
      - image: skarltjr/k8s-spring:1.0
        name: k8s-spring
        ports:
        - containerPort: 8080

---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    visualize: "true"
  name: k8s-spring-service
spec:
  selector:
    app: k8s-spring
  ports:
  - name: http
    protocol: TCP
    port: 8080
    targetPort: 8080
  type: ClusterIP

화면 캡처 2021-11-01 230942

  • 레플리카를 2로 지정했기 때문에 파드가 2개 생성된것을 확인
  • service를 생성했고 해당 서비스는 app: k8s-spring을 가진 파드로 로드밸런싱한다

5. 마지막으로 정상적으로 동작하는지 확인

화면 캡처 2021-11-01 231431

  • 정상적으로 8080포트에 "/"를 요청했을 때 hello를 리턴해주는것을 확인

@skarltjr
Copy link
Owner Author

skarltjr commented Nov 1, 2021

  1. 어플리케이션 + dockerfile을 intellij로 만들고
  2. 이를 우분투에서 클론받는다
  3. 클론받은 코드의 dockerfile을 활용하여 이미지를 만들고
  4. 이 이미지를 도커 허브에 push
  5. 쿠버네티스에 접속
  6. 쿠버네티스에서 deployment yaml을 작성하고 이 때 container에 활용할 이미지를 도커 허브에서 받아 사용한다
  7. 작성한 yaml을 apply하여 pod와 service생성
  8. 정상동작 확인

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant