Bibi's DevLog ๐Ÿค“๐ŸŽ

[Spring boot] ์Šคํฌ๋ฆฝํŠธ์™€ S3 bucket์„ ์ด์šฉํ•œ ์Šคํ”„๋ง ์•ฑ ๋ฐฐํฌ ์ž๋™ํ™”(+ ์Šฌ๋ž™ ๋ด‡, crontab ํ™œ์šฉ) ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Spring ์Šคํ”„๋ง

[Spring boot] ์Šคํฌ๋ฆฝํŠธ์™€ S3 bucket์„ ์ด์šฉํ•œ ์Šคํ”„๋ง ์•ฑ ๋ฐฐํฌ ์ž๋™ํ™”(+ ์Šฌ๋ž™ ๋ด‡, crontab ํ™œ์šฉ)

๋น„๋น„ bibi 2021. 6. 2. 23:50

์Šคํฌ๋ฆฝํŠธ์™€ S3 bucket์„ ์ด์šฉํ•œ ์Šคํ”„๋ง ์•ฑ ๋ฐฐํฌ ์ž๋™ํ™”(+ ์Šฌ๋ž™ ๋ด‡, crontab ํ™œ์šฉ)

yeon์ด ์˜ค๋Š˜ ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐ€๋ฅด์ณ ์ฃผ์‹  ๋ฐฐํฌ์ž๋™ํ™” ๊ณผ์ •์„ ์ •๋ฆฌํ•ด ๋ณด์•˜๋‹ค! ๐Ÿ™‡โ€โ™‚๏ธ

(์ถœ์ฒ˜ : S3 bucket์„ ์ด์šฉํ•œ ๋ฐฐํฌ ์ž๋™ํ™”)

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž๋™ํ™”๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค.

  • ๋กœ์ปฌ์—์„œ ๋นŒ๋“œํ•œ ์Šคํ”„๋ง ์•ฑ์˜ ๋นŒ๋“œ ํŒŒ์ผ(jar)์„ S3์— ๋„ฃ๊ธฐ
  • ์ธ์Šคํ„ด์Šค๊ฐ€ s3๋ฅผ 1๋ถ„๋งˆ๋‹ค ์ฒดํฌ
  • ๋นŒ๋“œ ํŒŒ์ผ์ด ์ƒˆ๋กœ์šด ๋ฒ„์ „์ด๋ฉด s3์—์„œ ๋นŒ๋“œ ๋ฐ ๋ฐฐํฌ

[์ค€๋น„๋ฌผ] ์ž˜ ๋Œ์•„๊ฐ€๋Š” ์Šคํ”„๋ง ์•ฑ, aws ๊ณ„์ •๊ณผ ec2 ์ธ์Šคํ„ด์Šค, ์Šฌ๋ž™

  1. (๋กœ์ปฌ) ์Šคํ”„๋ง ์•ฑ์„ ๋นŒ๋“œํ•ด jarํŒŒ์ผ์„ ์ƒ์„ฑํ•จ

    • ํ”„๋กœ์ ํŠธ ํด๋”์—์„œ ./gradlew build jar
  2. (ec2) S3์—์„œ ๋ฐฐํฌ์šฉ bucket ์ƒ์„ฑ

    • aws์„œ๋น„์Šค ์ค‘ s3 ์„ ํƒ - ๋ฒ„ํ‚ท - ๋ฒ„ํ‚ท ๋งŒ๋“ค๊ธฐ
    • ๋ฒ„ํ‚ท ์ƒ์„ฑ์‹œ ์ด๋ฆ„์ด ๋‹ค๋ฅธ ๋ฒ„ํ‚ท๊ณผ ์ค‘๋ณต๋˜๋ฉด ์•ˆ ๋จ
    • ๊ธฐ๋ณธ์˜ต์…˜ ๊ทธ๋Œ€๋กœ ๋งŒ๋“ค๊ธฐ
  3. (๋กœ์ปฌ) aws cli ์„ค์น˜

    • aws ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ค์น˜ํ•จ.
    • https://velog.io/@trycatch/AWS-CLI-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0 ๋ฅผ ์ฐธ๊ณ ํ•ด ์„ค์น˜ํ•œ ๋‹ค์Œ,
      • ์œˆ๋„์šฐ๋Š” ์œ„ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•ด ์„ค์น˜ ํ”„๋กœ๊ทธ๋žจ ๋‹ค์šด๋กœ๋“œ, ๋งฅ/Linux๋Š” sudo apt install awscli
      • ๋งฅ/๋ฆฌ๋ˆ…์Šค ์„ค์น˜๊ฐ€ ์•ˆ ๋  ์‹œ sudo apt update ๋ฅผ ํ†ตํ•ด ์—…๋ฐ์ดํŠธ ํ•œ ๋‹ค์Œ ๋‹ค์‹œ ์‹œ๋„ํ•ด ๋ณด๊ธฐ
    • https://lovemewithoutall.github.io/it/aws-cli-configure/ ๋ฅผ ์ฐธ๊ณ ํ•ด ์ดˆ๊ธฐ ์„ค์ •์„ ๋งˆ์น˜๊ธฐ. (IAM์„ ๋งŒ๋“ค ๋•Œ ์ƒ์„ฑ๋˜๋Š” AWS Access Key ID, AWS Secret Access Key ํ•„์š”)
  4. (๋กœ์ปฌ -> ec2) jarํŒŒ์ผ์„ S3 ๋ฒ„ํ‚ท์œผ๋กœ ๋ณต์‚ฌ

    • jarํŒŒ์ผ์ด ์žˆ๋Š” ์œ„์น˜๋กœ ์ด๋™(๋ณดํ†ต ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์—์„œ cd /build/libs)
    • aws s3 cp jarํŒŒ์ผ๋ช….jar s3://๋ฒ„ํ‚ท์ด๋ฆ„/ํŒŒ์ผ์ด๋ฆ„.jar
    • *ํŒŒ์ผ์ด๋ฆ„์˜ ๊ฒฝ์šฐ, ๋ณต์‚ฌ๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ ์ด๋ฆ„์„ ๋œปํ•จ. ์ง์ ‘ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.
    • aws s3 ls s3://๋ฒ„ํ‚ท์ด๋ฆ„ ์œผ๋กœ s3๋ฒ„ํ‚ท์— ์ž˜ ๋“ค์–ด๊ฐ”๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  5. (ec2 - Role) s3 access ์ •์ฑ…์ด ์—ฐ๊ฒฐ๋œ Role(์—ญํ• ) ์ถ”๊ฐ€ ๋ฐ ec2 ์ธ์Šคํ„ด์Šค์— ๋“ฑ๋ก

    • ec2 ์ธ์Šคํ„ด์Šค์— ์ ‘์† ํ›„, 3.์˜ ๋ฐฉ๋ฒ•์„ ์ฐธ๊ณ ํ•ด aws cli๋ฅผ ๋™์ผํ•˜๊ฒŒ ์„ค์น˜ (sudo apt install awscli)
    • ์ด์ œ s3์— ์ ‘๊ทผํ•˜๋Š” ์ฃผ์ฒด์ธ ec2๊ฐ€ s3์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. = aws Role
    • aws์„œ๋น„์Šค ์ค‘ IAM์„ ์„ ํƒ - ์—ญํ• (role) - ์—ญํ•  ๋งŒ๋“ค๊ธฐ.
    • ๋งŒ๋“  ์—ญํ• ์— AmazonS3FulAccess๋ผ๋Š” ์ •์ฑ… ์—ฐ๊ฒฐ
    • ์—ญํ•  ์ƒ์„ฑ ํ›„, ๋‹ค์‹œ ec2 ์ธ์Šคํ„ด์Šค๋กœ ๋Œ์•„๊ธฐ
    • ์ธ์Šคํ„ด์Šค์— ์ฒดํฌ๋ฐ•์Šค ์ฒดํฌ ํ›„ [์ž‘์—…] - [๋ณด์•ˆ] - [IAM ์—ญํ•  ์ˆ˜์ •] - 'IAM์—ญํ• '์—์„œ ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ์—ญํ•  ์„ ํƒ ํ›„ ์ €์žฅ
    • ์ด์ œ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋Š” S3์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.
    • ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์—์„œ aws s3 ls๋ฅผ ์ž…๋ ฅํ•ด ์ž˜ ์กฐํšŒ๋˜๋Š”์ง€ ํ™•์ธ.
  6. (๋กœ์ปฌ) ์Šฌ๋ž™ web hook ์„ค์ •ํ•˜๊ธฐ

    • ๋ฐฐํฌ ์ž๋™ํ™”๋ฅผ ํ†ตํ•ด ๋ฐฐํฌ๊ฐ€ ์ด๋ฃจ์–ด์งˆ ๋•Œ, ์Šฌ๋ž™ ํŠน์ • ์ฑ„๋„์— ๋ด‡์„ ํ†ตํ•ด ์•Œ๋ฆผ์ด ์ „์†ก๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • https://jojoldu.tistory.com/552 ๋ฅผ ์ฐธ๊ณ ํ•ด ์•Œ๋ฆผ์„ ๋ฐ›์„ ์Šฌ๋ž™ ์ฑ„๋„์— webhook ์•ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ , WebhookURL์ฃผ์†Œ๋ฅผ ๋ฐ›์•„ ๋‘”๋‹ค.
  7. (ec2) ๋ฐฐํฌ ์ž๋™ํ™” - ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ

    • ec2 ์ธ์Šคํ„ด์Šค์— ์ ‘์†ํ•ด build.sh๋ผ๋Š” ์ด๋ฆ„์˜ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค(vi build.sh). ๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•œ๋‹ค.

    • build.sh

    • #!/bin/bash
      IP=`curl -s 169.254.169.254/latest/meta-data/public-ipv4`
      START="$IP start deploying app!"
      END="$IP end deploying app!"
      
      #real deploy script
      CK=`aws s3 cp jarํŒŒ์ผ๋ช… s3://๋ฒ„ํ‚ท์ด๋ฆ„/ํŒŒ์ผ์ด๋ฆ„`
      if [[ ! $CK ]]; then
          echo "no need to build"
          exit 0
      fi
      
      curl -X POST --data-urlencode "payload={\"text\": \"$START\"}" WebhookURL์ฃผ์†Œ
      
      /usr/bin/fuser -k 8080/tcp
      aws s3 cp s3://๋ฒ„ํ‚ท์ด๋ฆ„/jarํŒŒ์ผ๋ช….jar ./jarํŒŒ์ผ๋ช….jar
      nohup java -jar jarํŒŒ์ผ๋ช….jar &
      aws s3 rm s3://๋ฒ„ํ‚ท์ด๋ฆ„/jarํŒŒ์ผ๋ช….jar
      
      curl -X POST --data-urlencode "payload={\"text\": \"$END\"}" WebhookURL์ฃผ์†Œ
      
      echo "Deploy end: `date`"
    • #์˜ˆ์‹œ
      #!/bin/bash
      IP=`curl -s 169.254.169.254/latest/meta-data/public-ipv4`
      START="$IP start deploying app!"
      END="$IP end deploying app!"
      #real deploy script
      CK=`aws s3 ls s3://codesquad-issue-tracker-team7/issue-tracker.jar`
      if [[ ! $CK ]]; then
          echo "no need to build"
          exit 0
      fi
      
      curl -X POST --data-urlencode "payload={\"text\": \"$START\"}" https://hooks.slack.com/services/๋งํฌ๋งํฌ/๋งํฌ๋งํฌ/๋งํฌ๋งํฌ
      
      /usr/bin/fuser -k 8080/tcp
      aws s3 cp s3://codesquad-issue-tracker-team7/issue-tracker.jar ./
      nohup java -jar issue-tracker.jar &
      aws s3 rm s3://codesquad-issue-tracker-team7/issue-tracker.jar
      
      curl -X POST --data-urlencode "payload={\"text\": \"$END\"}" https://hooks.slack.com/services/๋งํฌ๋งํฌ/๋งํฌ๋งํฌ/๋งํฌ๋งํฌ
      
      echo "Deploy end: `date`"
    • ๋™์ž‘ : ./build.sh ์‹คํ–‰ ์‹œ, s3 ๋ฒ„ํ‚ท์— ๋นŒ๋“œ ํŒŒ์ผ(.jar)์ด ์žˆ์œผ๋ฉด ๋ฐฐํฌ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ๋ฐฐํฌ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋ฐ˜๋Œ€๋กœ s3๋ฒ„ํ‚ท์— ๋นŒ๋“œ ํŒŒ์ผ์ด ์—†์œผ๋ฉด ๋ฐฐํฌ๋ฅผ ํ•˜์ง€ ์•Š๊ณ  no need to build๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

    • curl -s 169.254.169.254/latest/meta-data/public-ipv4

      • ํ˜„์žฌ ec2์ธ์Šคํ„ด์Šค์˜ public ipv4 ์ฃผ์†Œ๋ฅผ ์•Œ์•„๋‚ธ๋‹ค.
    • WebhookURL์ฃผ์†Œ

      • 7.์—์„œ ๋ฐ›์•„ ๋‘” ์ฃผ์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค. ๋ฐฐํฌ ์‹œ์ž‘/์ข…๋ฃŒ ์‹œ ํ•ด๋‹น ์ฑ„๋„์— ์•Œ๋ฆผ์ด ์ „์†ก๋จ
    • /usr/bin/fuser -k 8080/tcp

      • ํฌํŠธ ๋ฒˆํ˜ธ(8080)๋ฅผ ์ด์šฉํ•ด ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ด๋Š” ์ฝ”๋“œ.
      • ์žฌ๋ฐฐํฌ ์ „ ์ด๋ฏธ ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์—ฌ์•ผ ํ•˜๋ฏ€๋กœ ํ•„์š”.
    • jarํŒŒ์ผ๋ช….jar

      • 4.์—์„œ ์ง€์ •ํ•œ ํŒŒ์ผ ์ด๋ฆ„์— .jar ํ™•์žฅ์ž๋ฅผ ๋ถ™์ž„. ์˜ˆ๋ฅผ ๋“ค์–ด airbnb.jar.
    • ์ด์ œ ๋กœ์ปฌ์—์„œ ec2 ์ธ์Šคํ„ด์Šค์— jarํŒŒ์ผ์„ ๋ณด๋‚ธ ํ›„, ec2์—์„œ ./build.sh ๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋ฐฐํฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋˜๋ฉฐ ์Šฌ๋ž™ ์•Œ๋ฆผ๋„ ์˜จ๋‹ค.

  8. (๋กœ์ปฌ) '๋กœ์ปฌ ๋นŒ๋“œ - jarํŒŒ์ผ s3๋กœ ๋ณต์‚ฌ' ๊ณผ์ • ์ž๋™ํ™”

    • ๋กœ์ปฌ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ ์œ„์น˜์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ build.shํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ, jarํŒŒ์ผ ๋นŒ๋“œ ๋ฐ aws s3์— ๋ณต์‚ฌํ•˜๋Š” ๊ณผ์ •์„ ์ž๋™ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

    • build.sh

    • #!/bin/bash
      ./gradlew build jar
      aws s3 cp build/libs/๋กœ์ปฌjarํŒŒ์ผ๋ช….jar s3://๋ฒ„ํ‚ท์ด๋ฆ„/jarํŒŒ์ผ๋ช….jar
      echo "Done!"
    • ์ดํ›„ ๋กœ์ปฌ์—์„œ๋„ ./build.sh ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋นŒ๋“œ, s3๋ณต์‚ฌ ๊ณผ์ •์„ ์ž๋™ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

    • ์ด์ œ ์žฌ๋ฐฐํฌ๋ฅผ ์›ํ•  ๋•Œ, ๋กœ์ปฌ์—์„œ ./build.sh ํ›„ ec2์ธ์Šคํ„ด์Šค์—์„œ ./build.sh ์ž…๋ ฅํ•˜๋ฉด ์žฌ๋ฐฐํฌ๊ฐ€ ๋๋‚œ๋‹ค.

  9. (ec2) crontab์œผ๋กœ 1๋ถ„๋งˆ๋‹ค build.sh์‹คํ–‰ํ•˜๊ธฐ

    • ec2 ์ธ์Šคํ„ด์Šค์— ์ ‘์† ํ›„ crontab -e๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

    • ์ดํ›„ ๋งจ ์•„๋ž˜์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

    • * * * * *      /home/ubuntu/build.sh >> log.txt 2>&1
    • 1๋ถ„๋งˆ๋‹ค build.sh๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๊ทธ ๋กœ๊ทธ๋ฅผ log.txt์— ๋‚จ๊ธด๋‹ค.

    • cat log.txt๋ฅผ ํ†ตํ•ด ๋กœ๊ทธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.