728x90
๐ก ์ด ๊ธ์ 2ํธ์ผ๋ก 1ํธ์ธhttps://suzuworld.tistory.com/419์ ์ด์ด ์ฐ๋ ๊ธ์ด๋ค.
๐ค ์ ์คํฌ๋ฆฝํธ ์์ฑ ์ค๋น
- ์ด์ ํ ์ผ์ ๋จผ์ ์์คํฌ๋ฆฝํธ๋ก ๋ฐฐํฌ ํ์ผ์ ๋ง๋๋ ๊ฒ์ด๋ค.
- ์คํ๋ง ๋ถํธ ํ๋ก์ ํธ ์์ ํด๋์ script๋ผ๋ ๋๋ ํฐ๋ฆฌ๋ฅผ ๋ง๋ ๋ค์์ deploy.sh๋ผ๋ ์ด๋ฆ์ ํ์ผ์ ๋ง๋ค์.
๐ค ๋ค์ด๊ฐ๊ธฐ ์ ์
- ๋ณธ๊ฒฉ์ ์ผ๋ก ๋ค์ด๊ฐ๊ธฐ ์ ์ ์ ๊ทธ๋ฆผ์ ํ ๋ฒ ๋ ๋ณด๊ณ ์์ํ๋ฉด ์คํฌ๋ฆฝํธ ์ดํด์ ๋ง์ ๋์์ด ๋ ๊ฒ์ด๋ค.
- ์ด๋ฒ์ ์ ์คํฌ๋ฆฝํธ ํ์ผ์ ์ฒ์ ๋ง์ ธ๋ด์ ์๋นํ ์ ๋จน์๋๋ฐ ๋๋ถ๋ถ์ ๋ธ๋ก๊ทธ๊ฐ ๊ฐ ๋ช ๋ น์ด์ ๋ํด ์์ธํ ์ค๋ช ํ์ง ์์ ์๊ฐ์ด ๋ ๊ฑธ๋ ธ๋ ๊ฒ ๊ฐ๋ค.
- ๋์ฒ๋ผ ์ ์คํฌ๋ฆฝํธ ์์ฑ์ด ์ฒ์์ธ ์ฌ๋์ ์ํด ์ต๋ํ ์์ธํ ์ค๋ช ํด ๋ณด๊ฒ ๋ค.(์ค๋ช ์ด ๋ค์ ์ฅํฉํ๊ณ ํ๋ฆด ์๋ ์์๐ )
- ๋ฌถ์ ๋จ์๋ก ์ค๋ช ํ๊ณ ๋งจ ์๋์๋ ์์ฑ๋ณธ์ ๋ณด์ฌ์ฃผ๋ ๋ฐฉ์์ผ๋ก ์งํํ๊ฒ ๋ค.
- ๋ค ์ดํดํ๋ค๋ฉด ์คํฌ๋ฆฝํธ ํ์ผ์ ๋ํ ์์ฉ ๋์๋ ๋ง๋ค์ด ๋ผ ์ ์์ ๊ฒ์ด๋ค.
โ๐ป ์ ์คํฌ๋ฆฝํธ ์์ฑ
#!/bin/bash
- /bin/bash ๊ฒฝ๋ก์ ์๋ Bash ํด์๊ธฐ๋ฅผ ํด๋น ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ ๋ ์ฌ์ฉํ๊ฒ ๋ค๊ณ ๋ช ์ํ๋ ๊ฒ์ด๋ค.
SWITCH_SCRIPT="./switch.sh"
DEFAULT_SWITCH_SCRIPT="./default_switch.sh"
SERVER_HOME=์ ํจ์ค ์ํฌ์คํ์ด์ค ๋ด ๋น๋ ํ ์คํธ ๊ฒฝ๋ก
DEFAULT_SWITCH_SCRIPT="./default_switch.sh"
SERVER_HOME=์ ํจ์ค ์ํฌ์คํ์ด์ค ๋ด ๋น๋ ํ ์คํธ ๊ฒฝ๋ก
- SWITCH_SCRIPT์ DEFAULT_SWITCH_SCRIPT๋ ๋ณ์๋ฅผ ์ ์ธํ ๊ฒ์ด๋ค.
- ์ด ๋ ๋ณ์๋ deploy.sh ํ์ผ ๋ด์์ ์กฐ๊ฑด๋ฌธ์ผ๋ก ๋ถ๊ธฐํ์ฌ ์คํ๋๋ ๋ ๋ค๋ฅธ ์์คํฌ๋ฆฝํธ ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ๋ด๊ณ ์๋ค.
- SWITCH_SCRIPT๋ ๋ธ๋ฃจ, ๊ทธ๋ฆฐ ํฌํธ ์ค ํ ๊ณณ์ด ์คํ์ค์ผ ๋, ๋ค๋ฅธ ๊ณณ์ผ๋ก ๋ฆฌ๋ฒ์ค ํ๋ก์๋ฅผ ์ค์์นญํ๋ ๋ฐ์ ์ฌ์ฉ๋๋ค.
- DEFAULT_SWITCH_SCRIPT์ ๊ฒฝ์ฐ ๋ ํฌํธ ๋ชจ๋ ์ฃฝ์ด์์ ๋(์คํ๋ง ๋ถํธ ์๋ฒ๊ฐ ๊บผ์ ธ์์ ๋) ๋ํดํธ์ธ ๋ธ๋ฃจ ํฌํธ๋ฅผ ์คํ์ํค๊ณ NGINX์ ํ๋ก์ ๋ฐฉํฅ์ ๋ธ๋ฃจ๋ก ๋ง์ถฐ๋๋ ๋ฐ์ ์ฌ์ฉ๋๋ค.
- SERVER_HOME์ ํ๋ก์ ํธ์ ์ ํจ์ค์์ ๋น๋ํ jar ํ์ผ์ด ์๋ ๊ฒฝ๋ก์ด๋ค.
- ๋์ ๊ฒฝ์ฐ์๋ ์์ ํ ๋ก์ปฌ PC ํ ๊ณณ์์๋ง ํ ์คํธํ๊ธฐ ๋๋ฌธ์ ์ ํจ์ค์ ์ํฌ์คํ์ด์ค ํด๋ ๋ด์ ๋น๋ ํ์ผ์ ์ ์ฅํ๊ณ ์๋ฒ๋ฅผ ์คํ์ํจ๋ค๋ ์๋๋ฆฌ์ค๋ก ์ ๊ทผํ๋ค.
- ๋ง์ฝ ๋ฐฐํฌ๋ฅผ ์ํ ์ธ๋ถ ์๋ฒ(AWS ์ธ์คํด์ค ๋ฑ)์ ๋ณด๋ผ ๊ฒ์ด๋ผ๋ฉด ๋ค๋ฅธ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํ๋ฉด์ ๋ฐ๋ผํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
echo "-> ๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์์>"
CURRENT_SERVER_PROFILE=$(curl -s http://localhost:28080/profile | head -n 1)
CURRENT_SERVER_PROFILE=$(curl -s http://localhost:28080/profile | head -n 1)
- echo ์ถ๋ ฅ์ผ๋ก ์ ํจ์ค ๋ฐฐํฌ๊ฐ ์์๋จ์ ์๋ฆฐ๋ค.
- ์ ์คํฌ๋ฆฝํธ๊ฐ ์์๊ฒ ์ถ๋ ฅ๋๋ฉด ๋ฐฐํฌํ ๋ ๊ธฐ๋ถ๋ ์ข์์ง์ง ์์๊น ํ๋ ์๊ฐ์ ์ด๋ชจ์ง๋ฅผ ๊ณ ๋ฅผ ๋ ์ ์ค์ ๊ฐํ๋ค.๐ค
- ์ ๋ฆฌํ๋ฉด ๋ฐฐํฌ ์์์ ์๋ฆฌ๋ ์ถ๋ ฅ๋ฌธ์ ๋ด๋ณด๋ด๊ณ curl์ ์ด์ฉํด ํด๋น ํ๋ก์ ํธ์ ํ์ด์ง์์ ๋งจ ์ ์ค ์ฒซ ๋ฒ์งธ ํค์๋๋ง์ ๋ณ์์ ๋ฃ๋๋ค.
- ์ฌ๊ธฐ์ ๋ณ์์ ๋ด๊ธฐ๋ ๋ด์ฉ์ ๋ฐ๋ก ์์ ๊ธ์์ ์ค์ ํ๋ application.yml์ ํ๋กํ ์ด๋ฆ์ธ blue, green์ด๋ค.
- blue๋ก ์๋ฒ๊ฐ ์คํ๋๋ 8081 ํฌํธ๋ผ๋ฉด blue๊ฐ ๋ด๊ธธ๊ฒ์ด๊ณ , green์ผ๋ก ์คํ๋๋ 8082 ํฌํธ๋ผ๋ฉด green์ด ๋ด๊ธธ ๊ฒ์ด๋ค.
- 28080์ nginx๋ก ํฌํธํฌ์๋ฉํ ๊ฒ์ด๋ค.
- nginx์ ๋ํ ์ค๋ช ์ ์๋์ ์๋ค.
- if blue) 28080 -> 8081
- else if green) 28080 -> 8082
- echo : ์ฃผ์ด์ง ์ธ์๋ฅผ ํ์ค ์ถ๋ ฅ์ผ๋ก ์ถ๋ ฅํ๋ ๋ช ๋ น์ด์ด๋ค.
- curl : ์ฃผ์ด์ง URL์ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ฑฐ๋ ๋ฐ์์ค๋ ๋ช ๋ น์ด์ด๋ค.
- -s : curl์ด ์คํ๋๋ ๋์ ์งํ ์ํฉ์ด๋ ์ง๋จ ๋ฉ์์ง ๋ฑ์ ์ถ๋ ฅ์ ํ์ํ์ง ์์์ ์๋ฏธํ๋ค.(silent)
- head : ํ ์คํธ์ ์ ๋ถ๋ถ์ ์๋ฏธํ๋ค.
- -n 1 : ์ฒซ๋ฒ์งธ ์ค์ ์๋ฏธํ๋ค.
if [ "$CURRENT_SERVER_PROFILE" == blue ]
then
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Profile : $CURRENT_SERVER_PROFILE"
CURRENT_SERVER_PORT=8081
SWITCH_SERVER_PROFILE=green
SWITCH_SERVER_PORT=8082
elif [ "$CURRENT_SERVER_PROFILE" == green ]
then
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Profile : $CURRENT_SERVER_PROFILE"
CURRENT_SERVER_PORT=8082
SWITCH_SERVER_PROFILE=blue
SWITCH_SERVER_PORT=8081
else
echo "-> โ ๏ธ profile not found : ์ผ์นํ๋ Profile์ด ์์ต๋๋ค."
echo "-> โ ๏ธ set default profile : ๊ธฐ๋ณธ Profile์ธ blue๋ฅผ ํ ๋นํฉ๋๋ค."
echo "-> โ ๏ธ ๋ธ๋ฃจ, ๊ทธ๋ฆฐ์ด ๋ชจ๋ ๋์์ค์ด ์๋์์ผ๋ฏ๋ก, blue๋ง ๋ฐฐํฌํ๊ณ ์คํฌ๋ฆฝํธ๋ฅผ ์ข ๋ฃํฉ๋๋ค."
SWITCH_SERVER_PROFILE=blue
SWITCH_SERVER_PORT=8081
fi
then
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Profile : $CURRENT_SERVER_PROFILE"
CURRENT_SERVER_PORT=8081
SWITCH_SERVER_PROFILE=green
SWITCH_SERVER_PORT=8082
elif [ "$CURRENT_SERVER_PROFILE" == green ]
then
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Profile : $CURRENT_SERVER_PROFILE"
CURRENT_SERVER_PORT=8082
SWITCH_SERVER_PROFILE=blue
SWITCH_SERVER_PORT=8081
else
echo "-> โ ๏ธ profile not found : ์ผ์นํ๋ Profile์ด ์์ต๋๋ค."
echo "-> โ ๏ธ set default profile : ๊ธฐ๋ณธ Profile์ธ blue๋ฅผ ํ ๋นํฉ๋๋ค."
echo "-> โ ๏ธ ๋ธ๋ฃจ, ๊ทธ๋ฆฐ์ด ๋ชจ๋ ๋์์ค์ด ์๋์์ผ๋ฏ๋ก, blue๋ง ๋ฐฐํฌํ๊ณ ์คํฌ๋ฆฝํธ๋ฅผ ์ข ๋ฃํฉ๋๋ค."
SWITCH_SERVER_PROFILE=blue
SWITCH_SERVER_PORT=8081
fi
- ํ์ฌ ์คํ์ค์ธ ํฌํธ๊ฐ 8081์ด๋ผ๋ฉด green๊ณผ 8082๋ฅผ ๊ฐ switch ๋ณ์์ ๋ด๊ณ , 8082๋ผ๋ฉด blue์ 8081์ ๊ฐ switch ๋ณ์์ ๋ด๋๋ค.
- ๋ ๋ค ์๋๋ผ๋ฉด ๋ํดํธ ํ๋กํ์ธ blue์ ํฌํธ 8081์ ๊ฐ switch ๋ณ์์ ๋ด๋๋ค.
- fi : if๋ฌธ์ด ์ข ๋ฃ๋จ์ ๋ปํ๋ค.
echo "-> ๐ $SWITCH_SERVER_PROFILE ๋ฐฐํฌ"
SWITCH_PORT_PID=$(netstat -anv | grep $SWITCH_SERVER_PORT | head -n 1 | awk '{print $9}')
if [ ! -z "$SWITCH_PORT_PID" ]
then
echo "-> ๐ฆ $SWITCH_SERVER_PROFILE ์ PID : $SWITCH_PORT_PID"
kill $SWITCH_PORT_PID >/dev/null 2>&1
fi
SWITCH_PORT_PID=$(netstat -anv | grep $SWITCH_SERVER_PORT | head -n 1 | awk '{print $9}')
if [ ! -z "$SWITCH_PORT_PID" ]
then
echo "-> ๐ฆ $SWITCH_SERVER_PROFILE ์ PID : $SWITCH_PORT_PID"
kill $SWITCH_PORT_PID >/dev/null 2>&1
fi
- swith ๋ณ์์ ๋ด๊ธด ํ๋กํ๋ก ์คํ๋ง ๋ถํธ ์๋ฒ ๋ฐฐํฌ๋ฅผ ์์ํ๋ค.
- ๋ง์ฝ switch ํฌํธ๊ฐ ์ด๋ฏธ ์๋ฒ์์ ์คํ ์ค์ด๋ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์์ผ๋ก ๋จผ์ switch ํฌํธ๋ฅผ ์ฃฝ์ด๊ณ ์์ํ๋ค.
- ์ด๋ ํต์ฌ์ ์ด์์ฒด์ ํ๊ฒฝ๋ง๋ค ํด๋น ํฌํธ๋ฅผ ์ฐพ๊ณ ๊ทธ ํฌํธ์ pid๋ฅผ ์ฐพ๋ ๊ฒ์ด ๋ค๋ฅผ ์ ์๋ค๋ ์ ์ธ๋ฐ ์ ๋ช ๋ น์ด๋ฅผ ์์ฉํ์ฌ ์ถ๋ ฅ๋ฌธ์ ๋ณด๊ณ ํด๋น pid์ ์์น๋ฅผ ์ฐพ๊ฒ ๋ง๋ค๋ฉด ๋๋ค.
wonyonghwang@Wonyongs-MacBook-Pro jenkins % netstat -anv | grep 8081
tcp46 0 0 *.8081 *.* LISTEN 131072 131072 3953 0 00000 00000006 00000000000057ce 00000000 00000800 2 0 000001
wonyonghwang@Wonyongs-MacBook-Pro jenkins % netstat -anv | grep 8082
tcp46 0 0 *.8082 *.* LISTEN 131072 131072 4392 0 00000 00000006 0000000000008378 00000000 00000800 2 0 000001
- netstat -anv : ๋ฌด์ํ ๋ง์ ํ๋ก์ธ์ค ๊ด๋ จ ๋ด์ฉ์ด ์ถ๋ ฅ๋๋ค.
- grep : ์ฌ๊ธฐ์ grep์ผ๋ก ์ค์์นํ ํฌํธ ๋ฒํธ๋ฅผ ์ถ๋ ค๋ธ๋ค.
- head -n 1 : ๋งจ ์ ์ค๋ง ์ถ๋ ฅํ๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.
- awk '{print $9}' : 9 ๋ฒ์งธ ๋ฌธ์๋ฅผ ์ถ๋ ฅํ๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.
- ์์ ์ถ๋ ฅ๋ฌธ์ ์ธ์ด๋ณด๋ฉด 3953, 4392๊ฐ 9๋ฒ์งธ์ ์์นํด ์์์ ํ์ธํ ์ ์๋ค.
wonyonghwang@Wonyongs-MacBook-Pro jenkins % netstat -anv | grep 8081 | head -n 1 | awk '{print $9}'
3953
wonyonghwang@Wonyongs-MacBook-Pro jenkins % netstat -anv | grep 8082 | head -n 1 | awk '{print $9}'
4392
- ์ด๋ ๊ฒ ์ถ๋ ฅํ๋ฉด PID๋ง ์ถ๋ ฅ์ด ๊ฐ๋ฅํ๋ค.
- ์ด๋ฅผ PID ๋ณ์์ ๋ฃ๊ณ ํด๋น ํฌํธ์ PID๋ก ํฌํธ๋ฅผ kill ํ๋ ๊ฒ์ด๋ค.
- /dev/null 2>&1 : ๋ช
๋ น์ด ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ์ง ์๊ฒ ๋ค๋ ๋ป์ด๋ค.
- kill ํ์ ๋ ์ถ๋ ฅ๋ฌธ์ด ๋์ค๋๊ฒ ๋ณด๊ธฐ ์ซ์ด ๋ฃ์๋ค.
echo "-> ๐ nohup java -jar $SERVER_HOME/forJenkins.jar --spring.profiles.active=$SWITCH_SERVER_PROFILE > /dev/null &"
nohup java -jar $SERVER_HOME/forJenkins.jar --spring.profiles.active=$SWITCH_SERVER_PROFILE > /dev/null &
echo "-> ๐ $SWITCH_SERVER_PROFILE : 10์ด ํ Health Check Start"
echo "-> ๐ curl -s http://localhost:$SWITCH_SERVER_PORT/actuator/health"
sleep 10
nohup java -jar $SERVER_HOME/forJenkins.jar --spring.profiles.active=$SWITCH_SERVER_PROFILE > /dev/null &
echo "-> ๐ $SWITCH_SERVER_PROFILE : 10์ด ํ Health Check Start"
echo "-> ๐ curl -s http://localhost:$SWITCH_SERVER_PORT/actuator/health"
sleep 10
- ์ค์์นญํ ํ๋กํ ์คํ๋ง๋ถํธ ์๋ฒ๋ฅผ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ์คํ์ ํ๋ค.(nohup)
- SERVER_HOME์ ์์ jar ํ์ผ์ด ์ ์ฅ๋ ์์น์ด๋ฉฐ, PROFILE์ switch ํ ํ๋กํ์ด๋ค.
- ์๋ฒ๊ฐ ์คํ๋๋ฉด sleep์ผ๋ก 10์ด ํ health check๊ฐ ์์๋๋ค.
- ์คํ๋ง๋ถํธ ์คํ ์๋ฃ ์๊ฐ์ ๋๋ํ๊ฒ ์ก์ ์ค์ ํ ๊ฒ์ด๋ค.
- echo๋ ์ง๊ธ ์ด ์ถ๋ ฅ๋ฌธ์ด ์คํ๋๋ค๋ ๊ฒ์ ์๋ฆฌ๊ธฐ ์ํ ์ถ๋ ฅ๋ฌธ์ผ ๋ฟ์ด๋ ํฐ ์๋ฏธ์ ๊ฐ์กฐํ๋ค. ์์ ๋กญ๊ฒ ๋ณ๊ฒฝํ์.
for retry_count in {1..10}
do
response=$(curl -s http://localhost:$SWITCH_SERVER_PORT/actuator/health)
count=$(echo $response | grep "UP" | wc -l)
if [ $count -ge 1 ]
then
echo "-> ๐จ [$retry_count ๋ฒ์งธ Health Check]"
echo "-> โ Health Check ์ฑ๊ณต!!!"
break
else
echo "-> โ Health Check ์คํจ, ์๋ฒ๋ก๋ถํฐ ์๋ต์ ๋ฐ์ง ๋ชปํ์ต๋๋ค."
echo "-> ๐ฃ ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต : $response"
fi
if [ $retry_count -eq 10 ]
then
echo "-> ๐ต๐ซ Health Check $retry_count ํ ์๋ ๋ชจ๋ ์คํจ..."
echo "-> ๐ซ ๋ฐฐํฌ ์ข ๋ฃ"
exit 1
fi
echo "-> ๐ค Health Check ์คํจ, 5์ด ํ ์ฌ์๋..."
sleep 5
done
do
response=$(curl -s http://localhost:$SWITCH_SERVER_PORT/actuator/health)
count=$(echo $response | grep "UP" | wc -l)
if [ $count -ge 1 ]
then
echo "-> ๐จ [$retry_count ๋ฒ์งธ Health Check]"
echo "-> โ Health Check ์ฑ๊ณต!!!"
break
else
echo "-> โ Health Check ์คํจ, ์๋ฒ๋ก๋ถํฐ ์๋ต์ ๋ฐ์ง ๋ชปํ์ต๋๋ค."
echo "-> ๐ฃ ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต : $response"
fi
if [ $retry_count -eq 10 ]
then
echo "-> ๐ต๐ซ Health Check $retry_count ํ ์๋ ๋ชจ๋ ์คํจ..."
echo "-> ๐ซ ๋ฐฐํฌ ์ข ๋ฃ"
exit 1
fi
echo "-> ๐ค Health Check ์คํจ, 5์ด ํ ์ฌ์๋..."
sleep 5
done
- wc -l : ์ ๋ ฅ๋ ํ ์คํธ์ ์ค ์๋ฅผ ์ธ๋ ์ญํ ์ ํ๋ค.
- -ge : "Greater than or equal to"๋ผ๋ ์๋ฏธ๋ก, ์ฌ๊ธฐ์๋ ์ ์ฅ๋ ๊ฐ์ด 1๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์์ง๋ฅผ ํ์ธํ๋ค.
- -eq : "Equal to"๋ผ๋ ์๋ฏธ๋ก, ์ฃผ์ด์ง ๊ฐ์ด ๋ค๋ฅธ ๊ฐ๊ณผ ๊ฐ์์ง๋ฅผ ๋น๊ตํ๋ ์ญํ ์ ํ๋ค.
- exit 1 : 0 ์ด์ธ์ ๊ฐ์ ์ค๋ฅ์์ ๋ช ์ํ๊ณ ํ๋ก๊ทธ๋จ์ ์ข ๋ฃํ๋ฉฐ, 0์ผ ๊ฒฝ์ฐ ์ ์ ์ข ๋ฃ๋ฅผ ์๋ฏธํ๋ค.
- ์ด 10 ๋ฒ์ ์ฒดํฌํ๋ค. do done ์์๋ ๋ฐ๋ณต๋ฌธ ๋ด์ฉ์ด ๋ค์ด๊ฐ๋ค.
if [ -z "$CURRENT_SERVER_PORT" ]
then
echo "-> ๐ NGINX : Default Port๋ก ํ๋ก์ ์ค์ "
if [ ! -x "$DEFAULT_SWITCH_SCRIPT" ]
then
chmod +x "$DEFAULT_SWITCH_SCRIPT"
fi
"$DEFAULT_SWITCH_SCRIPT"
echo "->๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์ฑ๊ณต> ์คํฌ๋ฆฝํธ๋ฅผ ์ข ๋ฃํฉ๋๋ค."
exit 0
fi
then
echo "-> ๐ NGINX : Default Port๋ก ํ๋ก์ ์ค์ "
if [ ! -x "$DEFAULT_SWITCH_SCRIPT" ]
then
chmod +x "$DEFAULT_SWITCH_SCRIPT"
fi
"$DEFAULT_SWITCH_SCRIPT"
echo "->๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์ฑ๊ณต> ์คํฌ๋ฆฝํธ๋ฅผ ์ข ๋ฃํฉ๋๋ค."
exit 0
fi
- -z : ๋ณ์๊ฐ ๋น์ด์์ ๊ฒฝ์ฐ๋ฅผ ์๋ฏธํ๋ค.
- -x : ํ์ผ ์คํ ๊ถํ์ด ์๋ ๊ฒฝ์ฐ๋ฅผ ์๋ฏธํ๋ค.
- chmod +x : .sh ํ์ผ ์คํ ๊ถํ์ ์ถ๊ฐํ๋ค.
- exit 0 : ์ ์ ์ข ๋ฃ์์ ๋ช ์ํ๋ค.
- ์ฌ๊ธฐ์ ์์์ ์ธ๊ธํ ๋ถ๊ธฐ๊ฐ ์์๋๋ค.
- ํ์ฌ ํฌํธ๊ฐ ๋น์ด์๋ค๋ฉด blue, green ๋ชจ๋ ์คํ๋๊ณ ์์ง ์๋ค๋ ์๋ฏธ์ด๋ฏ๋ก DEFAULT_SWITCH_SCRIPT๋ฅผ ์คํ์ํค๊ณ ๋ฐฐํฌ ์คํฌ๋ฆฝํธ๋ฅผ ์ข ๋ฃํ๋ค.
echo "-> ๐คผ NGINX ํฌํธ ์ค์์นญ ์คํฌ๋ฆฝํธ ์์"
if [ ! -x "$SWITCH_SCRIPT" ]
then
chmod +x "$SWITCH_SCRIPT"
# .sh ํ์ผ ์คํ ๊ถํ์ ํ์ธํ๊ณ ์์ผ๋ฉด ์ถ๊ฐ
fi
"$SWITCH_SCRIPT"
echo "-> ๐ซ ๊ธฐ์กด ํฌํธ์ธ $CURRENT_SERVER_PROFILE ํฌํธ : $CURRENT_SERVER_PORT KILL"
CURRENT_PORT_PID=$(netstat -anv | grep $CURRENT_SERVER_PORT | head -n 1 | awk '{print $9}')
echo "-> ๐ฆ $CURRENT_SERVER_PROFILE ์ PID : $CURRENT_PORT_PID"
kill $CURRENT_PORT_PID >/dev/null 2>&1
echo "->๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์ฑ๊ณต> ์คํฌ๋ฆฝํธ๋ฅผ ์ข ๋ฃํฉ๋๋ค."
if [ ! -x "$SWITCH_SCRIPT" ]
then
chmod +x "$SWITCH_SCRIPT"
# .sh ํ์ผ ์คํ ๊ถํ์ ํ์ธํ๊ณ ์์ผ๋ฉด ์ถ๊ฐ
fi
"$SWITCH_SCRIPT"
echo "-> ๐ซ ๊ธฐ์กด ํฌํธ์ธ $CURRENT_SERVER_PROFILE ํฌํธ : $CURRENT_SERVER_PORT KILL"
CURRENT_PORT_PID=$(netstat -anv | grep $CURRENT_SERVER_PORT | head -n 1 | awk '{print $9}')
echo "-> ๐ฆ $CURRENT_SERVER_PROFILE ์ PID : $CURRENT_PORT_PID"
kill $CURRENT_PORT_PID >/dev/null 2>&1
echo "->๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์ฑ๊ณต> ์คํฌ๋ฆฝํธ๋ฅผ ์ข ๋ฃํฉ๋๋ค."
- ๋ง์ฝ ์์ if ๋ฌธ์์ ํด๋น์ด ์ ๋๋ค๋ฉด(blue or green ์ค ํ๋๊ฐ ์ด์์์) ํฌํธ ์ค์์นญ์ ์ํด ์ด if ๋ฌธ์ผ๋ก ๋ด๋ ค์จ๋ค.
- SWITCH_ SCRIPT๋ฅผ ์คํํ์ฌ nginx์ ํ๋ก์ ๋ฐฉํฅ์ ๋ณ๊ฒฝํ๊ณ ๊ธฐ์กด์ ์คํ๋๊ณ ์๋ ํ๋กํ์ ํฌํธ๋ฅผ ์ฃฝ์ธ๋ค.
- ๋ง์ง๋ง์ผ๋ก ์คํฌ๋ฆฝํธ ์ข ๋ฃ๋ฅผ ์๋ฆฌ๋ ์ถ๋ ฅ๋ฌธ์ ์ถ๋ ฅํ๊ณ ๋ฐฐํฌ ์คํฌ๋ฆฝํธ ํ์ผ์ ์ต์ข ์ ์ผ๋ก ์ข ๋ฃ๋๋ค.
๐ deploy.sh ์คํฌ๋ฆฝํธ ์์ฑ๋ณธ
#!/bin/bash
SWITCH_SCRIPT="./switch.sh"
DEFAULT_SWITCH_SCRIPT="./default_switch.sh"
SERVER_HOME=์ ํจ์ค ์ํฌ์คํ์ด์ค ๋ด ๋น๋ ํ
์คํธ ๊ฒฝ๋ก
echo "-> ๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์์>"
CURRENT_SERVER_PROFILE=$(curl -s http://localhost:28080/profile | head -n 1)
if [ "$CURRENT_SERVER_PROFILE" == blue ]
then
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Profile : $CURRENT_SERVER_PROFILE"
CURRENT_SERVER_PORT=8081
SWITCH_SERVER_PROFILE=green
SWITCH_SERVER_PORT=8082
elif [ "$CURRENT_SERVER_PROFILE" == green ]
then
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Profile : $CURRENT_SERVER_PROFILE"
CURRENT_SERVER_PORT=8082
SWITCH_SERVER_PROFILE=blue
SWITCH_SERVER_PORT=8081
else
echo "-> โ ๏ธ profile not found : ์ผ์นํ๋ Profile์ด ์์ต๋๋ค."
echo "-> โ ๏ธ set default profile : ๊ธฐ๋ณธ Profile์ธ blue๋ฅผ ํ ๋นํฉ๋๋ค."
echo "-> โ ๏ธ ๋ธ๋ฃจ, ๊ทธ๋ฆฐ์ด ๋ชจ๋ ๋์์ค์ด ์๋์์ผ๋ฏ๋ก, blue๋ง ๋ฐฐํฌํ๊ณ ์คํฌ๋ฆฝํธ๋ฅผ ์ข
๋ฃํฉ๋๋ค."
SWITCH_SERVER_PROFILE=blue
SWITCH_SERVER_PORT=8081
fi
echo "-> ๐ $SWITCH_SERVER_PROFILE ๋ฐฐํฌ"
SWITCH_PORT_PID=$(netstat -anv | grep $SWITCH_SERVER_PORT | head -n 1 | awk '{print $9}')
if [ ! -z "$SWITCH_PORT_PID" ]
then
echo "-> ๐ฆ $SWITCH_SERVER_PROFILE ์ PID : $SWITCH_PORT_PID"
kill $SWITCH_PORT_PID >/dev/null 2>&1
fi
echo "-> ๐ nohup java -jar $SERVER_HOME/forJenkins.jar --spring.profiles.active=$SWITCH_SERVER_PROFILE > /dev/null &"
nohup java -jar $SERVER_HOME/forJenkins.jar --spring.profiles.active=$SWITCH_SERVER_PROFILE > /dev/null &
echo "-> ๐ $SWITCH_SERVER_PROFILE : 10์ด ํ Health Check Start"
echo "-> ๐ curl -s http://localhost:$SWITCH_SERVER_PORT/actuator/health"
sleep 10
for retry_count in {1..10}
do
response=$(curl -s http://localhost:$SWITCH_SERVER_PORT/actuator/health)
count=$(echo $response | grep "UP" | wc -l)
if [ $count -ge 1 ]
then
echo "-> ๐จ [$retry_count ๋ฒ์งธ Health Check]"
echo "-> โ
Health Check ์ฑ๊ณต!!!"
break
else
echo "-> โ Health Check ์คํจ, ์๋ฒ๋ก๋ถํฐ ์๋ต์ ๋ฐ์ง ๋ชปํ์ต๋๋ค."
echo "-> ๐ฃ ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต : $response"
fi
if [ $retry_count -eq 10 ]
then
echo "-> ๐ต๐ซ Health Check $retry_count ํ ์๋ ๋ชจ๋ ์คํจ..."
echo "-> ๐ซ ๋ฐฐํฌ ์ข
๋ฃ"
exit 1
fi
echo "-> ๐ค Health Check ์คํจ, 5์ด ํ ์ฌ์๋..."
sleep 5
done
if [ -z "$CURRENT_SERVER_PORT" ]
then
echo "-> ๐ NGINX : Default Port๋ก ํ๋ก์ ์ค์ "
if [ ! -x "$DEFAULT_SWITCH_SCRIPT" ]
then
chmod +x "$DEFAULT_SWITCH_SCRIPT"
fi
"$DEFAULT_SWITCH_SCRIPT"
echo "->๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์ฑ๊ณต> ์คํฌ๋ฆฝํธ๋ฅผ ์ข
๋ฃํฉ๋๋ค."
exit 0
fi
echo "-> ๐คผ NGINX ํฌํธ ์ค์์นญ ์คํฌ๋ฆฝํธ ์์"
if [ ! -x "$SWITCH_SCRIPT" ]
then
chmod +x "$SWITCH_SCRIPT"
fi
"$SWITCH_SCRIPT"
echo "-> ๐ซ ๊ธฐ์กด ํฌํธ์ธ $CURRENT_SERVER_PROFILE ํฌํธ : $CURRENT_SERVER_PORT KILL"
CURRENT_PORT_PID=$(netstat -anv | grep $CURRENT_SERVER_PORT | head -n 1 | awk '{print $9}')
echo "-> ๐ฆ $CURRENT_SERVER_PROFILE ์ PID : $CURRENT_PORT_PID"
kill $CURRENT_PORT_PID >/dev/null 2>&1
echo "->๐จ๐ป๐ผ<Jenkins ๋ฐฐํฌ ์ฑ๊ณต> ์คํฌ๋ฆฝํธ๋ฅผ ์ข
๋ฃํฉ๋๋ค."
๐ ๏ธ NGINX ์ค์
server {
listen 28080;
server_name localhost;
include /usr/local/etc/nginx/conf.d/service-url.inc;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass $service_url;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
- nginx๋ฅผ ๋ค์ด๋ก๋ํ์ฌ ์ค์นํ๋ค.
- ๋ณดํต nginx ํด๋์ ๊ฒฝ๋ก๋ ๋ ๊ฐ์ง์ธ ๊ฒ ๊ฐ๋ค.
- /usr/local/etc/nginx
- /opt/homebrew/etc/nginx
- nginx.conf์ ๋ค์ด๊ฐ ๋ณด๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ ํ ๋ด์ฉ์ด ์์ ํ ๋ฐ ์ ๋ถ ๋ฌด์ํ๊ณ http {} ์์ ์์ ๊ฐ์ด ์ ๋๋ค.
- ์ฌ๊ธฐ์ ํต์ฌ์ ์๋์ ๊ฐ๋ค.
๐ก listen
- ํฌํธํฌ์๋ฉํ ํฌํธ์ด๋ค.
- nginx์ ์์ ๊ฐ์ด 28080์ด๋ผ ์ ๋๋ค๋ฉด ํฌํธ ํฌ์๋ฉ๋์ด nginx ํฌํธ๋ก ์ ์ํ ๋ 28080 -> 8081 or 28080 -> 8082๋ก ์คํ๋ง๋ถํธ ์๋ฒ์ ์ฐ๊ฒฐ๋ ๊ฒ์ด๋ค.
๐ก include /usr/local/etc/nginx/conf.d/service-url.inc;
- include๋ก ํด๋น ๊ฒฝ๋ก์ service-url.inc๋ฅผ nginx.conf์ ๊ฐ์ ธ์จ๋ค๋ ๋ป์ด๋ค.
- nginx ํด๋์ conf.d๊ฐ ์๋ค๋ฉด ๋ง๋ค์ด๋ ๋ฌด๋ฐฉํ๋ค. ์ด ํด๋ ์์ vi service-url.inc ๋ช ๋ น์ด๋ก ํ์ผ์ ๋ง๋ค์ด๋๋ค.
- ์ด๊ธฐ๊ฐ์ผ๋ก set \$service_url http://127.0.0.1:8081๊ณผ ๊ฐ์ด ๋ธ๋ฃจ or ๊ทธ๋ฆฐ ํฌํธ ํ๋๋ฅผ ์ง์ ํด ๋๋๋ค.(ํ ์คํธ๋ฅผ ์ํด blue๋ก ํด๋์.)
๐ก location / {proxy_pass $service_url}
- ์์ service-url.inc์ service_url ๋ณ์ซ๊ฐ์ ๊ฐ์ ธ์ ์ฌ์ฉํ๋ค.
โจ๏ธ NGINX ํ ์คํธ
- ๋ก์ปฌ์์ ์คํ๋ง๋ถํธ ์๋ฒ๋ฅผ blue๋ก ์คํ์์ผ ๋๊ณ localhost:28080์ผ๋ก ์ ์ํ๋ฉด 280280 -> 8081๋ก ์ฐ๊ฒฐ๋์ด ์คํ๋ง๋ถํธ์ ์ถ๋ ฅ๋ฌธ์ด ํ๋ฉด์ ๋์ฌ ๊ฒ์ด๋ค.
๐ DEFAULT_SWITCH_SCRIPT
#!/bin/bash
SWITCH_PROXY_PORT=8081
echo "-> โก๏ธ set default port : ๊ธฐ๋ณธ ํฌํธ์ธ $SWITCH_PROXY_PORT ๋ฅผ ํ ๋นํฉ๋๋ค."
echo "-> ๐ NGINX ๋ด์ ํ๋ก์ ๋ฐฉํฅ ์ค์ "
echo "-> ๐ฅณ set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"
echo "set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"| tee /usr/local/etc/nginx/conf.d/service-url.inc >/dev/null 2>&1
echo "-> ๐ NGINX ์ฌ์์"
/usr/local/bin/nginx -s reload
SWITCH_PROXY_PORT=8081
echo "-> โก๏ธ set default port : ๊ธฐ๋ณธ ํฌํธ์ธ $SWITCH_PROXY_PORT ๋ฅผ ํ ๋นํฉ๋๋ค."
echo "-> ๐ NGINX ๋ด์ ํ๋ก์ ๋ฐฉํฅ ์ค์ "
echo "-> ๐ฅณ set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"
echo "set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"| tee /usr/local/etc/nginx/conf.d/service-url.inc >/dev/null 2>&1
echo "-> ๐ NGINX ์ฌ์์"
/usr/local/bin/nginx -s reload
- set a b : a ๋ณ์์ b๋ฅผ ํ ๋นํ๋ค.
- tee : ์์์ ์ถ๋ ฅํ ๋ฌธ์์ด์ ํด๋น ๊ฒฝ๋ก์ ๋ฎ์ด์ด๋ค.
- nginx ์ค์ ์ ์๋ฃํ๋ค๋ฉด ์ด์ ์ด ์คํฌ๋ฆฝํธ๊ฐ ์ดํด๊ฐ ๋ ๊ฒ์ด๋ค.
- ๋ง์ฝ blue, green์ด ๋ชจ๋ ์คํ ์ค์ด ์๋๋ผ๋ฉด ๋ํดํธ ํ๋กํ์ธ blue๊ฐ ์คํํ๋๋ก ํ๋ ๊ฒ์ด๋ค.
- SWITCH ํ ํฌํธ๋ฅผ 8081๋ก ์ค์ ํ์ฌ nginx.conf์ proxy_pass์ ์ฐ๊ฒฐํ ํฌํธ๋ฅผ 8081๋ก ์ค์ ํ๋ ๊ฒ์ด๋ค.
- ๋ง์ง๋ง์๋ nginx ํด๋๋ก ๊ฒฝ๋ก๋ฅผ ์ค์ ํ๊ณ reload๋ฅผ ์คํํ๋ค.
- ์ด๋ ๊ฒ ํด์ฃผ๋ ์ด์ ๋ ์ ํจ์ค๊ฐ ๋ก์ปฌ ์๋ฒ์ ์คํฌ๋ฆฝํธ ํ์ผ์ ์คํ์ํฌ ๋ nginx ํด๋๋ฅผ ์ฐพ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ด๋ค.
๐ SWITCH_SCRIPT
#!/bin/bash
CURRENT_PROFILE=$(curl -s http://localhost:28080/profile)
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Spring Boot Port : $CURRENT_PROFILE"
if [ $CURRENT_PROFILE == blue ]
then
CURRENT_PROXY_PORT=8081
SWITCH_PROXY_PORT=8082
elif [ $CURRENT_PROFILE == green ]
then
CURRENT_PROXY_PORT=8082
SWITCH_PROXY_PORT=8081
else
echo "-> โ ๏ธ profile not found : $CURRENT_PROFILE -> ์ผ์นํ๋ Profile์ด ์์ต๋๋ค."
echo "-> โ ๏ธ set default port : ๊ธฐ๋ณธ ํฌํธ์ธ 8081์ ํ ๋นํฉ๋๋ค."
SWITCH_PROXY_PORT=8081
fi
echo "-> โก๏ธ ํ์ฌ NGINX ํ๋ก์ ํฌํธ : $CURRENT_PROXY_PORT"
echo "-> โก๏ธ ๋ณ๊ฒฝํ NGINX ํ๋ก์ ํฌํธ : $SWITCH_PROXY_PORT"
echo "-> ๐ NGINX ๋ด์ ํ๋ก์ ๋ฐฉํฅ ๋ณ๊ฒฝ"
echo "-> ๐ฅณ set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"
echo "set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"| tee /usr/local/etc/nginx/conf.d/service-url.inc >/dev/null 2>&1
echo "-> โ NGINX ์ฌ์์"
/usr/local/bin/nginx -s reload
CURRENT_PROFILE=$(curl -s http://localhost:28080/profile)
echo "-> ๐ก ํ์ฌ ๊ตฌ๋์ค์ธ Spring Boot Port : $CURRENT_PROFILE"
if [ $CURRENT_PROFILE == blue ]
then
CURRENT_PROXY_PORT=8081
SWITCH_PROXY_PORT=8082
elif [ $CURRENT_PROFILE == green ]
then
CURRENT_PROXY_PORT=8082
SWITCH_PROXY_PORT=8081
else
echo "-> โ ๏ธ profile not found : $CURRENT_PROFILE -> ์ผ์นํ๋ Profile์ด ์์ต๋๋ค."
echo "-> โ ๏ธ set default port : ๊ธฐ๋ณธ ํฌํธ์ธ 8081์ ํ ๋นํฉ๋๋ค."
SWITCH_PROXY_PORT=8081
fi
echo "-> โก๏ธ ํ์ฌ NGINX ํ๋ก์ ํฌํธ : $CURRENT_PROXY_PORT"
echo "-> โก๏ธ ๋ณ๊ฒฝํ NGINX ํ๋ก์ ํฌํธ : $SWITCH_PROXY_PORT"
echo "-> ๐ NGINX ๋ด์ ํ๋ก์ ๋ฐฉํฅ ๋ณ๊ฒฝ"
echo "-> ๐ฅณ set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"
echo "set \$service_url http://127.0.0.1:${SWITCH_PROXY_PORT};"| tee /usr/local/etc/nginx/conf.d/service-url.inc >/dev/null 2>&1
echo "-> โ NGINX ์ฌ์์"
/usr/local/bin/nginx -s reload
- SWITCH ์คํฌ๋ฆฝํธ์ ๊ฒฝ์ฐ ๋ณ๋์ ์คํฌ๋ฆฝํธ ํ์ผ์ด๊ธฐ ๋๋ฌธ์ ๋ค์ ํ๋ฒ ํ์ฌ ํฌํธ์ ํ๋กํ์ ํ์ธํ๋ค.
- ์ดํ blue๋ผ๋ฉด green์ผ๋ก, green์ด๋ผ๋ฉด blue๋ก ์ค์์นญํ SWITCH ํฌํธ ๋ณ์์ ๊ฐ์ ์ฑ์์ค๋ค.
- ํ๋ก์ ๋ฐฉํฅ์ ๋ณ๊ฒฝํ๊ณ ๋์๋ ์์ ๋ง์ฐฌ๊ฐ์ง๋ก nginx๋ฅผ reload ํ๋ค.
๐ค nginx reload ํ ์คํธ
- ์ด ๊ทธ๋ฆผ์ ๋ค์ ๋ณด์.
- ์์ SWITCH_SCRIPT๋ ์ด ๋ถ๋ถ์ ํด๋น๋๋ ๊ฒ์ธ๋ฐ ๋ฌธ๋ ์๋ฌธ์ด ๋ค์๋ค.
- ์ง๊ธ ๋ด๊ฐ ํ๋ ๋ฐฐํฌ ๋ฐฉ์์ ๊ณผ์ฐ 100% ๋ฌด์ค๋จ ๋ฐฐํฌ๋ผ๊ณ ํ ์ ์์๊น?
- blue์์ green์ผ๋ก, green์์ blue๋ก nginx ํ๋ก์ ๋ฐฉํฅ์ ๋ณ๊ฒฝํ๊ณ nginx reload ํ๋ ์๊ฐ nginx๋ ์ฐฐ๋์ ์๊ฐ์ด์ง๋ง ๋ถ๋ช ์ค๋จ๋ ๊ฒ์ด๋ค.
- ์ฆ, reload ๋๋ ์๊ฐ ์์ฒญ์ด ๋ค์ด์จ๋ค๋ฉด ๊ทธ ์์ฒญ์ ์ฒ๋ฆฌ ๋ชปํ๋ ๊ฒ ์๋๊น?
- ๊ทธ๋์ ์คํํด ๋ดค๋ค.
- ์คํ๋ง๋ถํธ ํ๋ก์ ํธ ๋ด์ nginx_test.sh์ count_line์ด๋ผ๋ ํ์ผ์ ์์ฑํ๋ค.
๐ nginx_test.sh
count = 0
while true
do
count=$((count+1))
profile=$(curl -s http://localhost:28080/profile | head -n 1)
current_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "Profile : $profile | Count: $count | Time: $current_time"
done
while true
do
count=$((count+1))
profile=$(curl -s http://localhost:28080/profile | head -n 1)
current_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "Profile : $profile | Count: $count | Time: $current_time"
done
- ์์ ๊ฐ์ด ์์ฑํ๋ค.
- ํ
์คํธ ๋ฐฉ์์ ๊ฐ๋จํ๋ค.
- ๋งค ๋ฐ๋ณต๋ง๋ค ํ์ฌ Nginx๊ฐ ๋ฐ๋ผ๋ณด๊ณ ์๋ ์คํ๋ง๋ถํธ ์๋ฒ์ ํ๋กํ์ด ์ถ๋ ฅํ๊ณ , nginx๊ฐ reload ๋๊ณ blue๋ก ๋ฐ๋๋ ์๊ฐ ์ผ๋ง๋ ์ค๋จ์ด ์ง์๋๋์ง๋ฅผ ํ ์คํธํ๋ ๊ฒ์ด๋ค.
- ๋ฐ๋ณต๋ฌธ ์กฐ๊ฑด์ true๋ก ์ค์ ํ์ฌ ๋ฌดํ ๋ฐ๋ณตํ๋ค.
- ๋ฐ๋ณต๋ฌธ์ curl ๋ช ๋ น์ด๋ก ๋ด์ profile์ ๊ฐ์ง๊ณ ์๋ค๊ฐ "ํ๋กํ | ๋ฐ๋ณต๋ฌธ ํ์ | ์ด๋จ์ ํ์ฌ์๊ฐ์ ๋งค๋ฒ ์ถ๋ ฅํ ๊ฒ์ด๋ค.
- deploy.sh ํ์ผ์ ๋ก์ปฌ์์ ์คํํ๋ค.
- nginx ๋ฐฐํฌ ์คํฌ๋ฆฝํธ๊ฐ ์ข ๋ฃ๋์ด ํ๋ก์ ๋ฐฉํฅ์ด ๋ณ๊ฒฝ๋๋ฉด ctrl + c๋ก nginx_test.sh๋ฅผ ์ข ๋ฃ์ํจ๋ค.
- ์ดํ ์ถ๋ ฅ๋ ๋ด์ฉ์์ 1์ด ์์ ๋ณด๋ด์ง ์์ฒญ์ ์๋ผ count_line.txt์ ๋ถ์ฌ ๋ฃ๊ณ 1์ด ์์ ๋ช ๋ฒ ์ค๋จ๋๋์ง๋ฅผ ํ์ธํ๋ค.
์คํ ๊ฒฐ๊ณผ
Profile : blue | Count: 442 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 443 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 444 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 445 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 446 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 447 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 448 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 449 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 450 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 451 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 452 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 453 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 454 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 455 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 456 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 457 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 458 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 459 | Time: 2023-11-22 21:39:30
Profile : <!DOCTYPE html> | Count: 460 | Time: 2023-11-22 21:39:30
Profile : green | Count: 461 | Time: 2023-11-22 21:39:30
Profile : green | Count: 462 | Time: 2023-11-22 21:39:30
Profile : green | Count: 463 | Time: 2023-11-22 21:39:30
Profile : green | Count: 464 | Time: 2023-11-22 21:39:30
Profile : green | Count: 465 | Time: 2023-11-22 21:39:30
Profile : green | Count: 466 | Time: 2023-11-22 21:39:30
Profile : green | Count: 467 | Time: 2023-11-22 21:39:30
Profile : green | Count: 468 | Time: 2023-11-22 21:39:30
Profile : green | Count: 469 | Time: 2023-11-22 21:39:30
Profile : green | Count: 470 | Time: 2023-11-22 21:39:30
Profile : green | Count: 471 | Time: 2023-11-22 21:39:30
Profile : green | Count: 472 | Time: 2023-11-22 21:39:30
Profile : green | Count: 473 | Time: 2023-11-22 21:39:30
Profile : green | Count: 474 | Time: 2023-11-22 21:39:30
Profile : green | Count: 475 | Time: 2023-11-22 21:39:30
Profile : green | Count: 476 | Time: 2023-11-22 21:39:30
Profile : green | Count: 477 | Time: 2023-11-22 21:39:30
Profile : green | Count: 478 | Time: 2023-11-22 21:39:30
Profile : green | Count: 479 | Time: 2023-11-22 21:39:30
Profile : green | Count: 480 | Time: 2023-11-22 21:39:30
Profile : green | Count: 481 | Time: 2023-11-22 21:39:30
Profile : green | Count: 482 | Time: 2023-11-22 21:39:30
Profile : green | Count: 483 | Time: 2023-11-22 21:39:30
Profile : green | Count: 484 | Time: 2023-11-22 21:39:30
Profile : green | Count: 485 | Time: 2023-11-22 21:39:30
Profile : green | Count: 486 | Time: 2023-11-22 21:39:30
Profile : green | Count: 487 | Time: 2023-11-22 21:39:30
Profile : green | Count: 488 | Time: 2023-11-22 21:39:30
Profile : green | Count: 489 | Time: 2023-11-22 21:39:30
Profile : green | Count: 490 | Time: 2023-11-22 21:39:30
Profile : green | Count: 491 | Time: 2023-11-22 21:39:30
Profile : green | Count: 492 | Time: 2023-11-22 21:39:30
Profile : green | Count: 493 | Time: 2023-11-22 21:39:30
Profile : green | Count: 494 | Time: 2023-11-22 21:39:30
Profile : green | Count: 495 | Time: 2023-11-22 21:39:30
Profile : green | Count: 496 | Time: 2023-11-22 21:39:30
Profile : green | Count: 497 | Time: 2023-11-22 21:39:30
Profile : green | Count: 498 | Time: 2023-11-22 21:39:30
Profile : green | Count: 499 | Time: 2023-11-22 21:39:30
Profile : green | Count: 500 | Time: 2023-11-22 21:39:30
Profile : green | Count: 501 | Time: 2023-11-22 21:39:30
Profile : green | Count: 502 | Time: 2023-11-22 21:39:30
Profile : green | Count: 503 | Time: 2023-11-22 21:39:30
Profile : green | Count: 504 | Time: 2023-11-22 21:39:30
Profile : green | Count: 505 | Time: 2023-11-22 21:39:30
Profile : green | Count: 506 | Time: 2023-11-22 21:39:30
Profile : green | Count: 507 | Time: 2023-11-22 21:39:30
Profile : green | Count: 508 | Time: 2023-11-22 21:39:30
Profile : green | Count: 509 | Time: 2023-11-22 21:39:30
Profile : green | Count: 510 | Time: 2023-11-22 21:39:30
Profile : green | Count: 511 | Time: 2023-11-22 21:39:30
Profile : green | Count: 512 | Time: 2023-11-22 21:39:30
Profile : green | Count: 513 | Time: 2023-11-22 21:39:30
Profile : green | Count: 514 | Time: 2023-11-22 21:39:30
Profile : green | Count: 515 | Time: 2023-11-22 21:39:30
Profile : green | Count: 516 | Time: 2023-11-22 21:39:30
Profile : green | Count: 517 | Time: 2023-11-22 21:39:30
Profile : green | Count: 518 | Time: 2023-11-22 21:39:30
Profile : green | Count: 519 | Time: 2023-11-22 21:39:30
Profile : green | Count: 520 | Time: 2023-11-22 21:39:30
Profile : green | Count: 521 | Time: 2023-11-22 21:39:30
Profile : green | Count: 522 | Time: 2023-11-22 21:39:30
Profile : green | Count: 523 | Time: 2023-11-22 21:39:30
Profile : green | Count: 524 | Time: 2023-11-22 21:39:30
Profile : green | Count: 525 | Time: 2023-11-22 21:39:30
Profile : green | Count: 526 | Time: 2023-11-22 21:39:30
Profile : green | Count: 527 | Time: 2023-11-22 21:39:30
Profile : green | Count: 528 | Time: 2023-11-22 21:39:30
Profile : green | Count: 529 | Time: 2023-11-22 21:39:30
Profile : green | Count: 530 | Time: 2023-11-22 21:39:30
Profile : green | Count: 531 | Time: 2023-11-22 21:39:30
Profile : green | Count: 532 | Time: 2023-11-22 21:39:30
Profile : green | Count: 533 | Time: 2023-11-22 21:39:30
Profile : green | Count: 534 | Time: 2023-11-22 21:39:30
Profile : green | Count: 535 | Time: 2023-11-22 21:39:30
Profile : green | Count: 536 | Time: 2023-11-22 21:39:30
Profile : green | Count: 537 | Time: 2023-11-22 21:39:30
Profile : green | Count: 538 | Time: 2023-11-22 21:39:30
Profile : green | Count: 539 | Time: 2023-11-22 21:39:30
Profile : green | Count: 540 | Time: 2023-11-22 21:39:30
Profile : green | Count: 541 | Time: 2023-11-22 21:39:30
Profile : green | Count: 542 | Time: 2023-11-22 21:39:30
Profile : green | Count: 543 | Time: 2023-11-22 21:39:30
Profile : green | Count: 544 | Time: 2023-11-22 21:39:30
Profile : green | Count: 545 | Time: 2023-11-22 21:39:30
Profile : green | Count: 546 | Time: 2023-11-22 21:39:30
Profile : green | Count: 547 | Time: 2023-11-22 21:39:30
Profile : green | Count: 548 | Time: 2023-11-22 21:39:30
Profile : green | Count: 549 | Time: 2023-11-22 21:39:30
Profile : green | Count: 550 | Time: 2023-11-22 21:39:30
Profile : green | Count: 551 | Time: 2023-11-22 21:39:30
Profile : green | Count: 552 | Time: 2023-11-22 21:39:30
Profile : green | Count: 553 | Time: 2023-11-22 21:39:30
Profile : green | Count: 554 | Time: 2023-11-22 21:39:30
Profile : green | Count: 555 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 443 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 444 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 445 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 446 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 447 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 448 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 449 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 450 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 451 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 452 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 453 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 454 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 455 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 456 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 457 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 458 | Time: 2023-11-22 21:39:30
Profile : blue | Count: 459 | Time: 2023-11-22 21:39:30
Profile : <!DOCTYPE html> | Count: 460 | Time: 2023-11-22 21:39:30
Profile : green | Count: 461 | Time: 2023-11-22 21:39:30
Profile : green | Count: 462 | Time: 2023-11-22 21:39:30
Profile : green | Count: 463 | Time: 2023-11-22 21:39:30
Profile : green | Count: 464 | Time: 2023-11-22 21:39:30
Profile : green | Count: 465 | Time: 2023-11-22 21:39:30
Profile : green | Count: 466 | Time: 2023-11-22 21:39:30
Profile : green | Count: 467 | Time: 2023-11-22 21:39:30
Profile : green | Count: 468 | Time: 2023-11-22 21:39:30
Profile : green | Count: 469 | Time: 2023-11-22 21:39:30
Profile : green | Count: 470 | Time: 2023-11-22 21:39:30
Profile : green | Count: 471 | Time: 2023-11-22 21:39:30
Profile : green | Count: 472 | Time: 2023-11-22 21:39:30
Profile : green | Count: 473 | Time: 2023-11-22 21:39:30
Profile : green | Count: 474 | Time: 2023-11-22 21:39:30
Profile : green | Count: 475 | Time: 2023-11-22 21:39:30
Profile : green | Count: 476 | Time: 2023-11-22 21:39:30
Profile : green | Count: 477 | Time: 2023-11-22 21:39:30
Profile : green | Count: 478 | Time: 2023-11-22 21:39:30
Profile : green | Count: 479 | Time: 2023-11-22 21:39:30
Profile : green | Count: 480 | Time: 2023-11-22 21:39:30
Profile : green | Count: 481 | Time: 2023-11-22 21:39:30
Profile : green | Count: 482 | Time: 2023-11-22 21:39:30
Profile : green | Count: 483 | Time: 2023-11-22 21:39:30
Profile : green | Count: 484 | Time: 2023-11-22 21:39:30
Profile : green | Count: 485 | Time: 2023-11-22 21:39:30
Profile : green | Count: 486 | Time: 2023-11-22 21:39:30
Profile : green | Count: 487 | Time: 2023-11-22 21:39:30
Profile : green | Count: 488 | Time: 2023-11-22 21:39:30
Profile : green | Count: 489 | Time: 2023-11-22 21:39:30
Profile : green | Count: 490 | Time: 2023-11-22 21:39:30
Profile : green | Count: 491 | Time: 2023-11-22 21:39:30
Profile : green | Count: 492 | Time: 2023-11-22 21:39:30
Profile : green | Count: 493 | Time: 2023-11-22 21:39:30
Profile : green | Count: 494 | Time: 2023-11-22 21:39:30
Profile : green | Count: 495 | Time: 2023-11-22 21:39:30
Profile : green | Count: 496 | Time: 2023-11-22 21:39:30
Profile : green | Count: 497 | Time: 2023-11-22 21:39:30
Profile : green | Count: 498 | Time: 2023-11-22 21:39:30
Profile : green | Count: 499 | Time: 2023-11-22 21:39:30
Profile : green | Count: 500 | Time: 2023-11-22 21:39:30
Profile : green | Count: 501 | Time: 2023-11-22 21:39:30
Profile : green | Count: 502 | Time: 2023-11-22 21:39:30
Profile : green | Count: 503 | Time: 2023-11-22 21:39:30
Profile : green | Count: 504 | Time: 2023-11-22 21:39:30
Profile : green | Count: 505 | Time: 2023-11-22 21:39:30
Profile : green | Count: 506 | Time: 2023-11-22 21:39:30
Profile : green | Count: 507 | Time: 2023-11-22 21:39:30
Profile : green | Count: 508 | Time: 2023-11-22 21:39:30
Profile : green | Count: 509 | Time: 2023-11-22 21:39:30
Profile : green | Count: 510 | Time: 2023-11-22 21:39:30
Profile : green | Count: 511 | Time: 2023-11-22 21:39:30
Profile : green | Count: 512 | Time: 2023-11-22 21:39:30
Profile : green | Count: 513 | Time: 2023-11-22 21:39:30
Profile : green | Count: 514 | Time: 2023-11-22 21:39:30
Profile : green | Count: 515 | Time: 2023-11-22 21:39:30
Profile : green | Count: 516 | Time: 2023-11-22 21:39:30
Profile : green | Count: 517 | Time: 2023-11-22 21:39:30
Profile : green | Count: 518 | Time: 2023-11-22 21:39:30
Profile : green | Count: 519 | Time: 2023-11-22 21:39:30
Profile : green | Count: 520 | Time: 2023-11-22 21:39:30
Profile : green | Count: 521 | Time: 2023-11-22 21:39:30
Profile : green | Count: 522 | Time: 2023-11-22 21:39:30
Profile : green | Count: 523 | Time: 2023-11-22 21:39:30
Profile : green | Count: 524 | Time: 2023-11-22 21:39:30
Profile : green | Count: 525 | Time: 2023-11-22 21:39:30
Profile : green | Count: 526 | Time: 2023-11-22 21:39:30
Profile : green | Count: 527 | Time: 2023-11-22 21:39:30
Profile : green | Count: 528 | Time: 2023-11-22 21:39:30
Profile : green | Count: 529 | Time: 2023-11-22 21:39:30
Profile : green | Count: 530 | Time: 2023-11-22 21:39:30
Profile : green | Count: 531 | Time: 2023-11-22 21:39:30
Profile : green | Count: 532 | Time: 2023-11-22 21:39:30
Profile : green | Count: 533 | Time: 2023-11-22 21:39:30
Profile : green | Count: 534 | Time: 2023-11-22 21:39:30
Profile : green | Count: 535 | Time: 2023-11-22 21:39:30
Profile : green | Count: 536 | Time: 2023-11-22 21:39:30
Profile : green | Count: 537 | Time: 2023-11-22 21:39:30
Profile : green | Count: 538 | Time: 2023-11-22 21:39:30
Profile : green | Count: 539 | Time: 2023-11-22 21:39:30
Profile : green | Count: 540 | Time: 2023-11-22 21:39:30
Profile : green | Count: 541 | Time: 2023-11-22 21:39:30
Profile : green | Count: 542 | Time: 2023-11-22 21:39:30
Profile : green | Count: 543 | Time: 2023-11-22 21:39:30
Profile : green | Count: 544 | Time: 2023-11-22 21:39:30
Profile : green | Count: 545 | Time: 2023-11-22 21:39:30
Profile : green | Count: 546 | Time: 2023-11-22 21:39:30
Profile : green | Count: 547 | Time: 2023-11-22 21:39:30
Profile : green | Count: 548 | Time: 2023-11-22 21:39:30
Profile : green | Count: 549 | Time: 2023-11-22 21:39:30
Profile : green | Count: 550 | Time: 2023-11-22 21:39:30
Profile : green | Count: 551 | Time: 2023-11-22 21:39:30
Profile : green | Count: 552 | Time: 2023-11-22 21:39:30
Profile : green | Count: 553 | Time: 2023-11-22 21:39:30
Profile : green | Count: 554 | Time: 2023-11-22 21:39:30
Profile : green | Count: 555 | Time: 2023-11-22 21:39:30
- 21์ 39๋ถ 30์ด 00์์ 31์ด๊ฐ ๋๊ธฐ ์ ๊น์ง ์ด 114๋ฒ์ curl ๋ช ๋ น์ด๋ฅผ ์คํ์ ํ๊ณ ๊ทธ์ค์ ๋ฑ ํ ๋ฒ ์ค๋จ์ด ๋์๋ค.
- ์ค๋จ๋๋ ์๊ฐ์ 0.01์ด๋ ์ ๋๋ ๊ฒ ๊ฐ๋ค.
- 100% ๋ฌด์ค๋จ์ ์๋์ง๋ง ๊ฑฐ์ ๋ฌด์ค๋จ์ด๋ผ๊ณ ๋ณผ ์ ์์ง ์์๊น?
- ์ด๋ง์ ๋ ์๋ฒฝํ ํ๊ณ ์ถ๋ค๋ฉด nginx๊ฐ ์ค๋จ๋ ๋ ์์ฒญ์ ๊ฐ์ง๊ณ ์๋ค๊ฐ ์ฐ๊ฒฐ๋๋ฉด ๋ณด๋ธ๋ค๋ ์ค์ ์ nginx์ ์ถ๊ฐํ๋ ๊ธฐ๋ฅ์ด ์์ง ์์๊น ์๊ฐํ๋ค. ์ด๊ฑด ๋์ค์ nginx๋ฅผ ์ ๋๋ก ๊ณต๋ถํ ๋ ์์๋ด์ผ๊ฒ ๋ค.
๐ ์ ๋ฆฌ
- ๋ฐฐํฌ ์คํฌ๋ฆฝํธ ํ์ผ์ด ์์ฑ์ด ๋๋ฌ๋ค.
- ์คํฌ๋ฆฝํธ ํ์ผ์ ์์ฑํ๋ฉฐ ๋ค์๋ "์๋น์ค๊ฐ ์ค๋จ๋๋ ์๊ฐ์ด ์ด๋ ์ ๋์ผ๊น?"์ ๋ํ ์๋ฌธ๋ ๊ฐ๋จํ ํ ์คํธ๋ก ์ด๋์ ๋ ํด๊ฒฐ๋์๋ค.
- ์ด์ ๊ฐ์ฅ ์์ํ ์์ ์ด์๋ Groovy๋ฅผ ์ฌ์ฉํ์ฌ ์ ํจ์ค ํ์ผ์ ์์ฑํ๋ ๋จ๊ณ๋ง ๋จ์๋ค.
- ๋ค์ ๊ธ์ ๊ณ์๋๋ค.
์ฐธ๊ณ
๋คผํผ
https://hudi.blog/zero-downtime-deployment-with-jenkins-and-nginx/
https://hyunminh.github.io/nonstop-deploy/
728x90