Skip to content

Latest commit

ย 

History

History
254 lines (149 loc) ยท 6.03 KB

ACTIONS_AWS.md

File metadata and controls

254 lines (149 loc) ยท 6.03 KB

Actions + CodeDeploy + EC2

์šฐ์„  EC2 ์— ๋ฐฐํฌ ํ…Œ์ŠคํŠธ๋ถ€ํ„ฐ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

์š”๊ธˆ์€

  • ์˜จ๋””๋งจ๋“œ

  • ์•„์‹œ์•„(์„œ์šธ)

  • Linux

์„ ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

image

https://aws.amazon.com/ko/ec2/pricing/on-demand/

์ตœ์†Œ ์‚ฌ์–‘์„ t3.micro๋กœ ์žก์œผ๋ฉด ํ•œ ๋‹ฌ์— 11232์› ์ •๋„ (ํ™˜์œจ : 1200์› ๊ธฐ์ค€)๊ฐ€ ์˜ˆ์ƒ๋œ๋‹ค.

EC2์—๋Š” jar ๋ฐฐํฌ ํŒŒ์ผ๊ณผ MySQL DB์„œ๋ฒ„๋งŒ ์žˆ์„ ์˜ˆ์ •์ด๋ฉฐ,

ํ˜„์žฌ develop๋ธŒ๋žœ์น˜ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  API ์— ๋Œ€ํ•˜์—ฌ tps 1์„ ๋•Œ๋ฆฌ๊ณ  ๋ชจ๋‹ˆํ„ฐ๋ง ์˜ˆ์ •์ด๋‹ค.


1) ๋กœ์ปฌ

์šฐ์„  jar๋ฅผ ๋งŒ๋“ ๋‹ค

./gradlew build

๊ทธ๋ฆฌ๊ณ  AWS EC2๋กœ ์˜ฌ๋ฆฐ๋‹ค.

scp -i [ํ‚ค์ด๋ฆ„].pem dev-event-server-0.0.1-SNAPSHOT.jar ubuntu@[์„œ๋ฒ„IP]:~/

2) ์„œ๋ฒ„

jvm๋ถ€ํ„ฐ ์„ค์น˜ํ•œ๋‹ค.

https://davelogs.tistory.com/71

$ sudo apt-get update
$ sudo apt-get upgrade

# JAVA11 ์„ค์น˜
$ sudo apt-get install openjdk-11-jdk

mysql ์„ค์น˜ํ•œ๋‹ค.

https://velog.io/@seungsang00/Ubuntu-%EC%9A%B0%EB%B6%84%ED%88%AC%EC%97%90-MySQL-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0

sudo apt-get update # ์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„ ์—…๋ฐ์ดํŠธ
sudo apt-get install mysql-server # mysql-server ์„ค์น˜
sudo ufw allow mysql # ์™ธ๋ถ€ ์ ‘์† ๊ธฐ๋Šฅ ์„ค์ • (ํฌํŠธ 3306 ์˜คํ”ˆ)
sudo systemctl start mysql # MySQL ์‹คํ–‰
sudo systemctl enable mysql # Ubuntu ์„œ๋ฒ„ ์žฌ์‹œ์ž‘์‹œ MySQL ์ž๋™ ์žฌ์‹œ์ž‘
sudo /usr/bin/mysql -u root -p # MySQL ์ ‘์†

CREATE DATABASE dev_event default CHARACTER SET UTF8; # DB์ƒ์„ฑ

์ฐธ๊ณ ๋กœ ํ˜„์žฌ port 9000์œผ๋กœ ํ”ฝ์Šค ํ•ด๋‘์—ˆ์œผ๋ฏ€๋กœ, ec2์—์„œ ๋ฐฉํ™”๋ฒฝ(์ธ๋ฐ”์šด๋“œ ๊ทœ์น™) 9000 ์—ด์–ด์•ผ ๋œ๋‹ค.


3) ๋ชจ๋‹ˆํ„ฐ๋ง

http://[์„œ๋ฒ„IP]:9000/admin/v1/events/2021/7 ๋กœ

ํ•ด๋ณด๋ฉด ๊ธฐ๋ณธ์ ์ธ API ์‘๋‹ต ๊ฐ€๋Šฅํ•œ ๊ฒƒ ํ™•์ธ๋˜์—ˆ๊ณ ,

์ด์ œ postman์œผ๋กœ ๋ชจ๋‹ˆํ„ฐ๋ง ์„ค์ •์„ ์ค€๋น„ํ•œ๋‹ค.

< ์‚ฌ์šฉ๋ฒ• >

JaeYoung ๋‹˜ ๋ธ”๋กœ๊ทธ ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  1. EC2์— ruby์„ค์น˜ ํ›„ code-deploy agent๋ฅผ ์„ค์น˜

    sudo apt-get update
    sudo apt-get install ruby
    sudo apt-get install wget
    cd /home/ubuntu
    
    wget https://`bucket-name`.s3.`region-identifier`.amazonaws.com/latest/install
    chmod +x ./install
    sudo ./install auto

    bucket-name๊ณผ region-identifier์€

    AWS ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค์ •ํ•˜๋˜ ์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    wget https://aws-codedeploy-ap-northeast-2.s3.ap-northeast-2.amazonaws.com/latest/install

    (์ฐธ๊ณ ๋กœ, ํ˜„์žฌ EC2์—๋Š” jvm๊ณผ mysql์ด ์„ค์น˜๋˜์–ด ์žˆ์–ด์„œ ์ž๋ฐ” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๊ฐ€๋Šฅํ•œ ์ƒํƒœ์—์„œ ์‹œ์ž‘ํ•˜์˜€์Šต๋‹ˆ๋‹ค.)

  2. AWS ์„ธํŒ…

    • EC2 IAM ์„ค์ •
    • CodeDeploy ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ ๋ฐ IAM ์„ค์ •
    • S3 ์ƒ์„ฑ
    • ์‚ฌ์šฉ์ž ์ƒ์„ฑ (accessKey, secretKey ํ•„์š”)
  3. EC2 ๋ฐฐํฌ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ

    ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๊ฒฝ๋กœ์—

    • appspec.yml

      version: 0.0
      os: linux
      
      files:
        - source: /
          destination: /home/ubuntu/sangjin-deploy
      permissions:
        - object: /home/ubuntu/sangjin-deploy/
          owner: ubuntu
          group: ubuntu
      hooks:
        AfterInstall:
          - location: scripts/deploy.sh
            timeout: 60
            runas: ubuntu
      
    • deploy.sh

      #!/usr/bin/env bash
      
      REPOSITORY=/home/ubuntu/sangjin-deploy
      cd $REPOSITORY
      
      APP_NAME=actions
      JAR_NAME=$(ls $REPOSITORY/build/libs/ | grep 'SNAPSHOT.jar' | tail -n 1)
      JAR_PATH=$REPOSITORY/build/libs/$JAR_NAME
      
      CURRENT_PID=$(pgrep -f $APP_NAME)
      
      if [ -z $CURRENT_PID ]
      then
        echo "> ์ข…๋ฃŒํ• ๊ฒƒ ์—†์Œ."
      else
        echo "> kill -9 $CURRENT_PID"
        kill -15 $CURRENT_PID
        sleep 5
      fi
      
      echo "> $JAR_PATH ๋ฐฐํฌ"
      nohup java -jar $JAR_PATH > /dev/null 2> /dev/null < /dev/null &
      
      
  4. .github/workflows์—์„œ yml ์ƒ์„ฑ

    # This is a basic workflow to help you get started with Actions
    
    name: github actions + heroku
    
    on:
      push:
        branches: [ main ]
    
      # Allows you to run this workflow manually from the Actions tab
      workflow_dispatch:
    
    env:
      PROJECT_NAME: CDapp
    
    # A workflow run is made up of one or more jobs that can run sequentially or in parallel
    jobs:
      # This workflow contains a single job called "build"
      build:
        # The type of runner that the job will run on
        runs-on: ubuntu-latest
    
        # Steps represent a sequence of tasks that will be executed as part of the job
        steps:
          # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
          - uses: actions/checkout@v2
    
          - name: Make zip file
            run: zip -qq -r ./$GITHUB_SHA.zip .   # GITHUB_SHA๋Š” ์˜ˆ์•ฝ๋ณ€์ˆ˜๋กœ, ์ปค๋ฐ‹ ํ•ด์‹œ๊ฐ’
            shell: bash
    
          - name: Configure AWS credentials
            uses: aws-actions/configure-aws-credentials@v1
            with:
              aws-access-key-id: ${{ secrets.accessKey }}
              aws-secret-access-key: ${{ secrets.secretKey }}
              aws-region: ${{ secrets.region }}
    
          - name: Upload to S3
            run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://sangjin-deploy/$PROJECT_NAME/$GITHUB_SHA.zip
    
          - name: Code Deploy
            run: aws deploy create-deployment --application-name CDapp --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name CDapp --s3-location bucket=sangjin-deploy,bundleType=zip,key=$PROJECT_NAME/$GITHUB_SHA.zip
  5. ์„ฑ๊ณต โญ๏ธ


    image