쿠버네티스/cka
백엔드(Spring boot) 서버에 환경변수 등록하여 사용하기
몽자비루
2025. 3. 7. 20:31
- 이전 포스팅에서 1.2 까지 진행하기
- AppController.java 생성하기
package com.example.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class AppController { @Value("${MY_ACCOUNT:default}") // `:` 앞 공백 제거 private String myAccount; @Value("${MY_PASSWORD:default}") // `:` 앞 공백 제거 private String myPassword; @GetMapping("/") public String home() { return "myAccount : " + myAccount + ", myPassword : " + myPassword; } }
- DemoApplication.java 실행 후 localhost:8080 접속하기
- build 진행
- 이미지를 생성할 때 기준이 되는 jar파일을 생성한다.
- 도커파일을 바탕으로 이미지 build
- 이미지를 생성할 때 기준이 되는 jar파일을 생성한다.
- spring-deployement.yaml 파일 생성 및 환경 변수 생성하기
apiVersion : apps/v1 kind: Deployment metadata : name : spring-deployment spec: replicas : 3 selector: matchLabels: app: backend-app template: metadata: labels: app: backend-app spec: containers : - name : spring-container image : spring-server imagePullPolicy : IfNotPresent ports: - containerPort: 8080 env: - name: MY_ACCOUNT value: rusharp - name: MY_PASSWORD value: test123!
- 아래의 부분이 환경변수 생성에 해당한다.
- 아래의 부분이 환경변수 생성에 해당한다.
- spring-service.yaml 파일 생성하기
apiVersion : v1 kind: Service metadata: name: spring-service spec: type: NodePort selector: # deployment 파일의 sepc/template/metadata/labels/app 과 동일해야 한다. app: backend-app ports: - protocol : TCP nodePort : 30000 targetPort: 8080 port: 8080
- 오브젝트 생성하기
- 오브젝트 생성 결과 확인하기
- 오브젝트 생성 결과 확인하기
- 서버 접속 및 환경변수 확인
- AppController.java 생성하기