๐Ÿ”ฅ TIL - Day 58

Kim Dae Hyunยท2021๋…„ 11์›” 17์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
69/93

โœ… ์˜ค๋Š˜ ์‹ค์Šต

  • Springboot ํ”„๋กœ์ ํŠธ๋ฅผ ๋„์ปค ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œ
  • ๋นŒ๋“œ๋œ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ AWS ECR ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— Push
  • ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ ECR ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ด์šฉํ•ด์„œ Springboot, MySQL Pod ๋ฐ Service ์ƒ์„ฑ
  • ๋‘ Pod๊ฐ€ ์„œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ • (Springboot API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด MySQL์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ)


๐Ÿ“Œ Springboot ํ”„๋กœ์ ํŠธ๋ฅผ ๋„์ปค ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œ

ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ๋ฐ Dockerfile ์ž‘์„ฑ

FROM openjdk:8-jdk-alpine
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

๋„์ปค ์ด๋ฏธ์ง€๋กœ ๋นŒ๋“œ

docker build -t springboot-mysql-app .



๐Ÿ“Œ ๋นŒ๋“œ๋œ ์ด๋ฏธ์ง€๋ฅผ AWS ECR ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— push

์ผ๋‹จ AWS ECR ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
์ด๋ฆ„๋งŒ ์„ค์ •ํ•œ ํ›„ ์ƒ์„ฑํ•˜๊ณ  URI ๋ถ€๋ถ„์„ ๋ณต์‚ฌํ•œ๋‹ค.

์ด์ œ ๋กœ์ปฌ์—์„œ aws configure์˜ IAM secret ์ •๋ณด๋กœ ECR์— ๋กœ๊ทธ์ธ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin [AWS_ECR_URI]

Login Succeded ๋ฉ”์‹œ์ง€๊ฐ€ ๋–ด๋‹ค๋ฉด ์ด์ œ ์ด๋ฏธ์ง€๋ฅผ ECR์— push ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋นŒ๋“œ๋œ ์ด๋ฏธ์ง€๋ฅผ ํƒœ๊น… ํ›„ ํ‘ธ์‹œํ•œ๋‹ค.

[ ํƒœ๊น… ]
docker tag ์ด๋ฏธ์ง€์ด๋ฆ„ [ECR_URI]/ํƒœ๊ทธ๋ช…

[ Push ]
docker push [ECR_URI]/ํƒœ๊ทธ๋ช…

ECR์— push๋œ ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค.


๐Ÿ“Œ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ ECR ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ด์šฉํ•ด์„œ Springboot, MySQL Pod ๋ฐ Service ์ƒ์„ฑ

mysql์„ ๊ตฌ๋™์‹œํ‚ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋‹ด์Œ Pod์™€ Service๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

vi mysql.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod
  labels:
    app: mysql-pod # Pod ์ด๋ฆ„
spec:
  containers:
  - name: mysql-db
    image: mysql:5.7.36 # mysql docker hub ์ด๋ฏธ์ง€
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: "1234"
    ports:
    - containerPort: 3306
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-svc # ์„œ๋น„์Šค ์ด๋ฆ„
spec:
  selector:
    app: mysql-pod # Pod ์ด๋ฆ„
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306
  type: NodePort

์ƒ์„ฑํ•œ ์„ค์ •ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ๋‹ค.

kubectl apply -f mysql.yaml


์ƒ์„ฑ ํ›„ Service์˜ ํฌํŠธ๋ฅผ ํ™•์ธํ•œ๋‹ค.

kubectl get service ์„œ๋น„์Šค์ด๋ฆ„

Pod์—์„œ ๊ตฌ๋™๋˜๋Š” MySQL์˜ IP์™€ ํฌํŠธ๋ฅผ ๋ชจ๋‘ ์•Œ์•˜์œผ๋ฏ€๋กœ ์ด์ œ ๋กœ์ปฌ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ๋ฏธ๋ฆฌ ๋กœ์ปฌ์—์„œ ์ ‘๊ทผํ•ด์„œ ์Šคํ‚ค๋งˆ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์ž.
IP๋Š” ํ•ด๋‹น Pod๊ฐ€ ์˜ฌ๋ผ๊ฐ€์žˆ๋Š” node์˜ EC2 IP์ด๋‹ค.


Springboot application์— ๋Œ€ํ•œ ์ž‘์—… ์ „์— ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ๋งˆ์Šคํ„ฐ ์„œ๋ฒ„์—์„œ ECR์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

[ awscli ์„ค์น˜ ]
sudo apt install -y awscli

[ IAM secret ์„ค์ • ]
aws configure

[ ECR ๋กœ๊ทธ์ธ ]
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin [ECR URI]

[ Secret ์ƒ์„ฑ ]
kubectl create secret generic ecr-secret --from-file=.dockerconfigjson=/home/ubuntu/.docker/config.json --type=kubernetes.io/dockerconfigjson

๋‹ค์Œ์œผ๋กœ Springboot app ์ด๋ฏธ์ง€์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ Pod์— ์˜ฌ๋ ค์ค€๋‹ค.

์ด๋ฅผ ์œ„ํ•œ ์„ค์ •ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.

vi springboot-app.yaml
apiVersion: v1
kind: Pod
metadata:
  name: memo-springboot-pod # Service ์ด๋ฆ„
  labels:
    app: memo-springboot-pod # Pod ์ด๋ฆ„
spec:
  containers:
  - name: memo-springboot-app
    # ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ pushํ•œ ECR์˜ ์—”๋“œํฌ์ธํŠธ
    image: 771839060615.dkr.ecr.ap-northeast-2.amazonaws.com/memo-springboot
    ports:
    - containerPort: 8080
  imagePullSecrets:
  - name: ecr-secret
---
apiVersion: v1
kind: Service
metadata:
  name: memo-springboot-svc
spec:
  selector:
    app: memo-springboot-pod # Pod ์ด๋ฆ„
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  type: NodePort

Service์™€ Pod๋ฅผ ํ™œ์„ฑํ™”? ์ ์šฉ? ํ•œ๋‹ค.

kubectl apply -f springboot-app


๐Ÿ“Œ Pod ๊ฐ„ ํ†ต์‹ 

์ด์ œ Springboot application๋„ ๊ตฌ๋™๋˜๊ณ , MySQL๋„ ๊ตฌ๋™๋˜๊ณ  ์Šคํ‚ค๋งˆ๋„ ๋งŒ๋“ค์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ Springboot์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด MySQL์— ์ž˜ ์ €์žฅ์ด ๋˜์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์•„์ง ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Springboot์˜ ์„ค์ •ํŒŒ์ผ์—์„œ DB์— ์—ฐ๊ฒฐํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ณด์ž.

spring.datasource.url=jdbc:mysql://localhost/kuber_schema?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul

ํ˜„์žฌ localhost๋กœ DB์˜ ์ปค๋„ฅ์…˜์„ ์–ป์–ด๋‚ด๋ ค ํ•˜๊ณ  ์žˆ๋‹ค. Pod๋Š” ๊ฐ๊ฐ IP๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ™์€ ๋…ธ๋“œ์— ์žˆ๋”๋ผ๋„ localhost๋กœ ์„œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜๋Š” ์—†๋‹ค.

IP๊ฐ€ ์•„๋‹Œ ์„œ๋น„์Šค์˜ ์ด๋ฆ„์œผ๋กœ ์‹๋ณ„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์„œ๋น„์Šค์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
(MySQL Pod์˜ Service๋ช…: mysql-svc)

spring.datasource.url=jdbc:mysql://mysql-svc/kuber_schema?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul

springboot ์„ค์ •ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๊ณ  ๋‹ค์‹œ ์ด๋ฏธ์ง€ ๋นŒ๋“œ/push ํ•œ๋‹ค.
์ด๋ฏธ์ง€๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์œผ๋‹ˆ Pod์— ์˜ฌ๋ผ๊ฐ„ ์ปจํ…Œ์ด๋„ˆ๋„ ์ƒˆ๋กœ ๋นŒ๋“œ๋œ ์ด๋ฏธ์ง€์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์ผ๋‹จ ๊ธฐ์กด์˜ Pod๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

kubectl delete pod ํŒŒ๋“œ์ด๋ฆ„

Service์™€ Pod์˜ ์„ค์ •ํŒŒ์ผ์„ ํ•œ ๊ณณ์— ์ž‘์„ฑํ–ˆ์ง€๋งŒ Pod๋งŒ ์‚ญ์ œํ•˜๊ณ  ํ•ด๋‹น ํŒŒ์ผ์„ ์ ์šฉํ•˜๋ฉด ๋ฐ”๋€ ๋ถ€๋ถ„๋งŒ ์ ์šฉ๋œ๋‹ค. ๐Ÿ‘



๐Ÿ“Œ ํ…Œ์ŠคํŠธ

postman์œผ๋กœ api ํ˜ธ์ถœ

DB์— ์ž˜ ์ €์žฅ ใ…Ž




๐Ÿ“Œ ์ฐธ๊ณ 

ECR ๋กœ๊ทธ์ธ ์ฐธ๊ณ 
https://www.44bits.io/ko/post/amazon-ecr-login-by-awscliv2

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€