- Before
zero-downtime-deployment_before_trim.mp4
In the process of Deploy, there was about 10 ~ 15 secs of downtime
.
- After
zero-downtime-deployment_after_trim.mp4
By applying deploy.sh
file and Nginx Web Server's proxy_pass
, we managed to decrease the downtime to 0 sec
.
- deploy.sh
IS_GREEN=$(sudo docker ps | grep beadyeyes-spring-green) # Check the current running container
DEFAULT_CONF=" /etc/nginx/nginx.conf"
if [ -z $IS_GREEN ];then # blue라면
echo "### BLUE => GREEN ###"
echo "1. tag green image"
sudo docker tag beadyeyes-spring beadyeyes-spring-green # tag the image to green
echo "2. green container up"
sudo docker-compose up -d beadyeyes-spring-green # execute green container
while [ 1 = 1 ]; do
echo "3. green health check..."
sleep 3
REQUEST=$(curl ${ green-server }) # request to green server
if [ -n "$REQUEST" ]; then # if the service is available, then cancel health check
echo "health check success"
break ;
fi
done;
echo "4. reload nginx"
sudo cp /etc/nginx/nginx.green.conf /etc/nginx/nginx.conf
sudo nginx -s reload
echo "5. blue container down"
sudo docker-compose stop beadyeyes-spring-blue
else
echo "### GREEN => BLUE ###"
echo "1. tag blue image"
sudo docker tag beadyeyes-spring beadyeyes-spring-blue
echo "2. blue container up"
sudo docker-compose up -d beadyeyes-spring-blue
else
echo "### GREEN => BLUE ###"
echo "1. tag blue image"
sudo docker tag beadyeyes-spring beadyeyes-spring-blue
echo "2. blue container up"
sudo docker-compose up -d beadyeyes-spring-blue
while [ 1 = 1 ]; do
echo "3. blue health check..."
sleep 3
REQUEST=$(curl { blue-server }) # request to blue server
if [ -n "$REQUEST" ]; then
echo "health check success"
break ;
fi
done;
echo "4. reload nginx"
sudo cp /etc/nginx/nginx.blue.conf /etc/nginx/nginx.conf
sudo nginx -s reload
echo "5. green container down"
sudo docker-compose stop beadyeyes-spring-green
fi
- Check which conatiner is running currently.
- If the blue container is running, then pull the green image from Dockerhub and tag the image to green.
- Health check through port 8080, since the green-server is using this port.(blue-server is using port 8081)
- Modify the nginx.conf to the required conf, in this case, nginx.green.conf.
- Stop the blue container.
- Use SpringBoot Framework and open the cloned repository
- Then, click the green start button on the right above area, or select the 'Run Pointer Application' option in the Run Menu.
-
Make sure you have installed docker and DockerHub repository on your local area. Also you must have docker and docker-compose in your server area.
-
Create docker image of the project. From the project that was build, make a docker image and push it to your own DockerHub repository.
docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
docker buildx build --push --platform linux/amd64 -t ${{ secrets.DOCKERHUB_REPOSITORY }}:${GITHUB_SHA::7} .
- Pull the docker image and use docker-compose to deploy.
Inside the vm instance ssh console, use these process to deploy the image via docker-compose.
Make sure to tag the correct image name, so that the docker-compose.yml file can detect it.
Also, deploy.sh file with Nginx feature allows the project to be deployed with zero downtime.
sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
sudo docker pull ${{ secrets.DOCKERHUB_REPOSITORY }}:${GITHUB_SHA::7}
sudo docker tag ${{ secrets.DOCKERHUB_REPOSITORY }}:${GITHUB_SHA::7} beadyeyes-spring
sudo chmod 777 ./deploy.sh
./deploy.sh
sudo docker image prune -f