Skip to content

Commit

Permalink
hotfix: Fix db connection
Browse files Browse the repository at this point in the history
Redis 연결이 안되는 문제 해결
+ docker-compose의 links, depends_on으로 의존관계 명시

RDS에 정보가 입력이 안되는 문제
+ sh파일에 문제가 있어 env 파일로 해당 설정들을 이동
  • Loading branch information
Aqudi committed Dec 14, 2020
1 parent 567f60c commit 3550529
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 24 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,18 @@ ecs-cli compose \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-2:648240308375:targetgroup/target/e3086c4f494a30c4 \
--launch-type EC2
```

## 부딪힌 문제들

- Celery worker가 Redis를 인식 못하는 상황
- 로컬에서는 잘 됐지만 AWS에 배포하는 순간 작동을 하지 않아서 남감했다.
- docker-compose.yml의 links와 depends_on에 redis를 걸어주니까 해결됐다.
- RDS 연결
- 같은 VPC에 연결이 돼있어야 한다.
- entrypoint.sh에서 export 한 env는 환경에 적용이 되지 않았다.
- .django, .postgres 파일로 해당 설정들을 옮겨주니 DATABASE_URL 환경변수도 잘 적용됐다.
- 502 에러 (nginx, ELB)
- Target group을 잘 설정해야한다.
- Domain 문제
- 가비아에서 주문한 도메인을 Route53에 등록한 뒤 ACM을 발급받으면 된다.
- 이때 *.domain.com으로 해주면 편하다.
2 changes: 1 addition & 1 deletion compose/production/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

FROM judge0/compilers
FROM python:3.8-slim-buster

ENV PYTHONUNBUFFERED 1
Expand Down
11 changes: 4 additions & 7 deletions compose/production/django/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ set -o errexit
set -o pipefail
set -o nounset



# N.B. If only .env files supported variable expansion...
export CELERY_BROKER_URL="${REDIS_URL}"


if [ -z "${POSTGRES_USER}" ]; then
base_postgres_image_default_user='postgres'
export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

postgres_ready() {
python << END
python <<END
import sys
import psycopg2
Expand All @@ -37,9 +34,9 @@ sys.exit(0)
END
}
until postgres_ready; do
>&2 echo 'Waiting for PostgreSQL to become available...'
sleep 1
echo >&2 'Waiting for PostgreSQL to become available...'
sleep 1
done
>&2 echo 'PostgreSQL is available'
echo >&2 'PostgreSQL is available'

exec "$@"
6 changes: 2 additions & 4 deletions compose/production/django/start
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ set -o errexit
set -o pipefail
set -o nounset


python /app/manage.py collectstatic --noinput

python /app/manage.py collectstatic --noinput --settings=config.settings.production
python /app/manage.py migrate --noinput --settings=config.settings.production

/usr/local/bin/gunicorn config.asgi --bind 0.0.0.0:5000 --chdir=/app -k uvicorn.workers.UvicornWorker

1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = env.bool("DJANGO_DEBUG")
print(DEBUG)
# Local time zone. Choices are
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# though not all of them may be available with every OS.
Expand Down
4 changes: 2 additions & 2 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
SECRET_KEY = env("DJANGO_SECRET_KEY")
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["api.asports.kr"])
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["*.asports.kr"])

# DATABASES
# ------------------------------------------------------------------------------
DATABASES["default"] = env.db("DATABASE_URL") # noqa F405
DATABASES = {"default": env.db("DATABASE_URL")} # noqa F405
DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405

Expand Down
71 changes: 61 additions & 10 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

# 사용할 환경
env.target = DEV
env.compose = "local.yml"
env.compose = ["local.yml"]
env.settings = "--settings=config.settings.local"


# 커스텀 local 메소드
Expand All @@ -28,17 +29,20 @@ def local(string):
# ----------------------------------------------------------------
def dev():
env.target = DEV
env.compose = "local.yml"
env.compose = ["local.yml"]
env.settings = "--settings=config.settings.local"


def staging():
env.target = STAGING
env.compose = "staging.yml"
env.compose = ["staging.yml"]
env.settings = "--settings=config.settings.production"


def prod():
env.target = PRODUCTION
env.compose = "production.yml"
env.compose = ["staging.yml", "production.yml"]
env.settings = "--settings=config.settings.production"


# 장고 커맨드
Expand All @@ -49,17 +53,20 @@ def makemessages(locale):


def runserver():
local("python manage.py runserver")
local(f"python manage.py runserver {env.settings}")


def load_languages():
local(
"python manage.py loaddata algo_sports/codes/fixtures/programming_language.json"
f"python manage.py loaddata algo_sports/codes/fixtures/programming_language.json {env.settings}"
)


def shell():
local("python manage.py shell_plus")
if "local" in env.settings:
local(f"python manage.py shell_plus {env.settings}")
else:
local(f"python manage.py shell {env.settings}")


# Celery worker
Expand All @@ -71,12 +78,56 @@ def celery():
# Docker 커맨드
# ----------------------------------------------------------------
def build():
local(f"docker-compose -f {env.compose} build")
cmd = "docker-compose "
for compose in env.compose:
cmd += f"-f {compose} "
cmd += "build"
local(cmd)


def up(app="", option=""):
local(f"docker-compose -f {env.compose} up {option} {app}")
cmd = "docker-compose "
for compose in env.compose:
cmd += f"-f {compose} "
cmd += "up"
local(f"{cmd} {option} {app}")


def down(app="", option=""):
local(f"docker-compose -f {env.compose} down {option} {app}")
cmd = "docker-compose "
for compose in env.compose:
cmd += f"-f {compose} "
cmd += "down"
local(f"{cmd} {option} {app}")


def push():
cmd = "docker-compose "
for compose in env.compose:
cmd += f"-f {compose} "
cmd += "push"
local(cmd)


# Deploy 커맨드
# ----------------------------------------------------------------
def deploy():
local(
"""
CONFIG_NAME=algo-config
PROJECT_NAME=algo-service
ecs-cli compose \
--project-name $PROJECT_NAME \
--file staging.yml \
--file production.yml \
--ecs-params ./aws/ecs-params.yml \
service up \
--create-log-groups \
--cluster-config $CONFIG_NAME \
--container-name nginx \
--container-port 80 \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-2:648240308375:targetgroup/target/e3086c4f494a30c4 \
--launch-type EC2
"""
)
6 changes: 6 additions & 0 deletions production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ services:

redis:
image: redis:5.0
logging:
driver: awslogs
options:
awslogs-group: algo-sports
awslogs-region: ap-northeast-2
awslogs-stream-prefix: redis

celeryworker:
<<: *django
Expand Down
12 changes: 12 additions & 0 deletions staging.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: "3"

networks:
redis-network:

services:
django: &django
build:
Expand Down Expand Up @@ -27,10 +30,19 @@ services:
image: redis:5.0
ports:
- "6379:6379"
command: redis-server --appendonly yes
networks:
- redis-network

celeryworker:
<<: *django
command: /start-celeryworker
depends_on:
- redis
links:
- redis
networks:
- redis-network

flower:
<<: *django
Expand Down

0 comments on commit 3550529

Please sign in to comment.