Skip to content

Commit de96643

Browse files
infra: blue-green 무중단 배포 로직 추가
* feat: ec2 t3.medium으로 변경 * feat: 무중단 배포 로직 추가 * feat: 무중단 배포 로직(deploy)
1 parent e24ac4c commit de96643

3 files changed

Lines changed: 97 additions & 20 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,53 @@ jobs:
116116
working-directory: /
117117
comment: Deploy
118118
command: |
119-
docker pull ghcr.io/${{ needs.buildImageAndPush.outputs.owner_lc }}/${{ needs.buildImageAndPush.outputs.image_name }}:latest && \
120-
docker stop app1 2>/dev/null || true && \
121-
docker rm app1 2>/dev/null || true && \
122-
docker run -d --name app1 --network common -p 8080:8080 \
119+
set -e
120+
IMAGE="ghcr.io/${{ needs.buildImageAndPush.outputs.owner_lc }}/waitfair:latest"
121+
122+
# 현재 활성 서버 확인 (설정 파일에서)
123+
if grep -q "server app1 app1:8080 check.*weight 100" /dockerProjects/haproxy/haproxy.cfg; then
124+
ACTIVE=app1
125+
STANDBY=app2
126+
else
127+
ACTIVE=app2
128+
STANDBY=app1
129+
fi
130+
131+
docker pull $IMAGE
132+
docker stop $STANDBY 2>/dev/null || true
133+
docker rm $STANDBY 2>/dev/null || true
134+
135+
docker run -d \
136+
--name $STANDBY \
137+
--network common \
123138
-e DOPPLER_TOKEN=${{ secrets.DOPPLER_TOKEN }} \
124-
ghcr.io/${{ needs.buildImageAndPush.outputs.owner_lc }}/${{ needs.buildImageAndPush.outputs.image_name }}:latest && \
125-
docker image prune -f
139+
$IMAGE
140+
141+
# Health Check
142+
for i in {1..30}; do
143+
if docker exec $STANDBY curl -sf http://localhost:8080/actuator/health > /dev/null 2>&1; then
144+
break
145+
fi
146+
if [ $i -eq 30 ]; then
147+
docker logs $STANDBY --tail 50
148+
docker stop $STANDBY
149+
docker rm $STANDBY
150+
exit 1
151+
fi
152+
sleep 2
153+
done
154+
155+
# HAProxy 설정 변경 (weight 전환)
156+
if [ "$STANDBY" = "app1" ]; then
157+
sed -i 's/server app1 app1:8080 check.*weight 0/server app1 app1:8080 check inter 2s rise 2 fall 3 weight 100/' /dockerProjects/haproxy/haproxy.cfg
158+
sed -i 's/server app2 app2:8080 check.*weight 100/server app2 app2:8080 check inter 2s rise 2 fall 3 weight 0/' /dockerProjects/haproxy/haproxy.cfg
159+
else
160+
sed -i 's/server app2 app2:8080 check.*weight 0/server app2 app2:8080 check inter 2s rise 2 fall 3 weight 100/' /dockerProjects/haproxy/haproxy.cfg
161+
sed -i 's/server app1 app1:8080 check.*weight 100/server app1 app1:8080 check inter 2s rise 2 fall 3 weight 0/' /dockerProjects/haproxy/haproxy.cfg
162+
fi
163+
164+
# HAProxy graceful reload
165+
docker kill -s HUP haproxy
166+
167+
sleep 10
168+
docker image prune -f

infra/main.tf

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ resource "aws_security_group" "sg_1" {
117117
cidr_blocks = ["0.0.0.0/0"]
118118
}
119119

120-
ingress {
121-
from_port = 8080
122-
to_port = 8080
123-
protocol = "tcp"
124-
cidr_blocks = ["0.0.0.0/0"]
125-
}
120+
# //삭제 고려
121+
# ingress {
122+
# from_port = 8080
123+
# to_port = 8080
124+
# protocol = "tcp"
125+
# cidr_blocks = ["0.0.0.0/0"]
126+
# }
126127

127128
# Prometheus
128129
ingress {
@@ -278,7 +279,7 @@ systemctl enable docker
278279
systemctl start docker
279280
280281
# 도커 네트워크 생성
281-
docker network create common
282+
docker network create common || true
282283
283284
# nginx 설치
284285
docker run -d \
@@ -293,6 +294,45 @@ docker run -d \
293294
-v /dockerProjects/npm_1/volumes/etc/letsencrypt:/etc/letsencrypt \
294295
jc21/nginx-proxy-manager:latest
295296
297+
# haproxy 설치
298+
mkdir -p /dockerProjects/haproxy
299+
cat > /dockerProjects/haproxy/haproxy.cfg <<'HAPROXY'
300+
global
301+
maxconn 4096
302+
log stdout format raw local0
303+
304+
defaults
305+
mode http
306+
log global
307+
option httplog
308+
option dontlognull
309+
option forwardfor
310+
timeout connect 5s
311+
timeout client 60s
312+
timeout server 60s
313+
timeout check 2s
314+
retries 3
315+
316+
frontend http_front
317+
bind *:80
318+
default_backend app_backend
319+
320+
backend app_backend
321+
balance roundrobin
322+
option httpchk GET /actuator/health
323+
http-check expect status 200
324+
server app1 app1:8080 check inter 2s rise 2 fall 3 weight 100
325+
server app2 app2:8080 check inter 2s rise 2 fall 3 weight 0
326+
HAPROXY
327+
328+
# HAProxy 실행
329+
docker run -d \
330+
--name haproxy \
331+
--restart unless-stopped \
332+
--network common \
333+
-e TZ=Asia/Seoul \
334+
-v /dockerProjects/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro \
335+
haproxy:2.9-alpine
296336
297337
# redis 설치
298338
docker run -d \

infra/variables.tf

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,11 @@ variable "team_tag" {
2020
variable "instance_type" {
2121
description = "EC2 instance type"
2222
type = string
23-
default = "t3.small"
23+
default = "t3.medium"
2424
}
2525

2626
variable "root_volume_size" {
2727
description = "Root volume size in GB"
2828
type = number
2929
default = 20
3030
}
31-
32-
33-
# variable "prefix" {
34-
# description = "Prefix for all resources"
35-
# default = "dev"
36-
# }

0 commit comments

Comments
 (0)