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

[DEPLOY] nginx 무중단 배포 설정 #125

Merged
merged 2 commits into from
Aug 25, 2024
Merged

[DEPLOY] nginx 무중단 배포 설정 #125

merged 2 commits into from
Aug 25, 2024

Conversation

JungYoonShin
Copy link
Member

@JungYoonShin JungYoonShin commented Aug 25, 2024

📄 Work Description

  • nginx 무중단 배포 설정

⚙️ ISSUE

📷 Screenshot

  • 동영상, 사진, 로그 등등
  • ex) 큐알 성공 이미지, 스웨거, 포스트맨 등

💬 To Reviewers

  • appspec.yml 파일 수정했습니다.
    ApplicationStart라는 수명 주기에 세 가지 스크립트를 차례로 실행시키도록 했습니다.

  • 배포 스크립트를 추가했습니다

  1. run_new_was.sh
    -> 새로운 WAS를 띄우는 스크립트
# run_new_was.sh

#!/bin/bash

CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

echo "> Current port of running WAS is ${CURRENT_PORT}."

if [ ${CURRENT_PORT} -eq 8081 ]; then
  TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
  TARGET_PORT=8081
else
  echo "> No WAS is connected to nginx"
fi

TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+')

if [ ! -z ${TARGET_PID} ]; then
  echo "> Kill WAS running at ${TARGET_PORT}."
  sudo kill ${TARGET_PID}
fi

nohup java -jar \
        -Dserver.port=${TARGET_PORT} \
        -Dspring.config.location=/home/ubuntu/app/application.yml \
        /home/ubuntu/app/build/libs/* > /home/ubuntu/nohup.out 2>&1 &
echo "> Now new WAS runs at ${TARGET_PORT}."
sleep 10s
exit 0

  1. health_check.sh
    -> 새로 띄운 WAS가 완전히 실행되기까지 health check 하는 스크립트
# health_check.sh

#!/bin/bash

# Crawl current connected port of WAS
CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

# Toggle port Number
if [ ${CURRENT_PORT} -eq 8081 ]; then
    TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
    TARGET_PORT=8081
else
    echo "> No WAS is connected to nginx"
    exit 1
fi


echo "> Start health check of WAS at 'http://127.0.0.1:${TARGET_PORT}' ..."

for RETRY_COUNT in 1 2 3 4 5 6 7 8 9 10
do
    echo "> #${RETRY_COUNT} trying..."
    RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}"  http://127.0.0.1:${TARGET_PORT}/health)

    if [ ${RESPONSE_CODE} -eq 200 ]; then
        echo "> New WAS successfully running"
        exit 0
    elif [ ${RETRY_COUNT} -eq 10 ]; then
        echo "> Health check failed."
        exit 1
    fi
    sleep 10
done

  1. switch.sh
    -> nginx 리로드를 통해 서비스하는 포트를 스위칭하는 스크립트
CURRENT_PORT=$(cat /home/ubuntu/service_url.inc  | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

echo "> Nginx currently proxies to ${CURRENT_PORT}."

# Toggle port number
if [ ${CURRENT_PORT} -eq 8081 ]; then
    TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
    TARGET_PORT=8081
else
    echo "> No WAS is connected to nginx"
    exit 1
fi

# Change proxying port into target port
echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" | tee /home/ubuntu/service_url.inc

echo "> Now Nginx proxies to ${TARGET_PORT}."

# Reload nginx
sudo service nginx reload

echo "> Nginx reloaded."

🔗 Reference

👉 블로그1, 블로그2

@JungYoonShin JungYoonShin self-assigned this Aug 25, 2024
@JungYoonShin JungYoonShin merged commit 147994d into develop Aug 25, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DEPLOY] nginx 무중단 배포 설정
1 participant