From b2f76391db56ccd8b432b23f275188f483aba414 Mon Sep 17 00:00:00 2001 From: Ameya Shenoy Date: Fri, 12 Feb 2021 03:26:27 +0530 Subject: [PATCH] feat: working prod deploy with duplicate code Signed-off-by: Ameya Shenoy --- backend/Dockerfile | 2 +- backend/backend/settings.py | 3 +- backend/prod-entrypoint.sh | 2 +- frontend/Dockerfile | 2 + frontend/src/axios-api.js | 2 +- k8s/app/backend.yaml | 76 +++++++++++++++++++ .../frontend.yaml} | 17 +++++ k8s/app/postgres.yaml | 61 +++++++++++++++ k8s/app/redis.yaml | 43 +++++++++++ k8s/app/rqscheduler.yaml | 63 +++++++++++++++ k8s/app/rqworker.yaml | 63 +++++++++++++++ k8s/frontend-service.yaml | 18 ----- k8s/{ => ingress}/ingress-https.yaml | 9 ++- 13 files changed, 338 insertions(+), 23 deletions(-) create mode 100644 k8s/app/backend.yaml rename k8s/{frontend-deployment.yaml => app/frontend.yaml} (65%) create mode 100644 k8s/app/postgres.yaml create mode 100644 k8s/app/redis.yaml create mode 100644 k8s/app/rqscheduler.yaml create mode 100644 k8s/app/rqworker.yaml delete mode 100644 k8s/frontend-service.yaml rename k8s/{ => ingress}/ingress-https.yaml (69%) diff --git a/backend/Dockerfile b/backend/Dockerfile index 99f27b6..a6620ea 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -35,5 +35,5 @@ ENV UWSGI_WORKERS=2 UWSGI_THREADS=4 ENV UWSGI_STATIC_MAP="/static/=/code/static/" UWSGI_STATIC_EXPIRES_URI="/static/.*\.[a-f0-9]{12,}\.(css|js|png|jpg|jpeg|gif|ico|woff|ttf|otf|svg|scss|map|txt) 315360000" USER ${APP_USER}:${APP_USER} -ENTRYPOINT ["sh", "/code/prod-entrypoint.sh"] +CMD sh /code/prod-entrypoint.sh diff --git a/backend/backend/settings.py b/backend/backend/settings.py index 3452b2f..61eeaa9 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -25,7 +25,8 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = os.environ.get('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = bool(os.environ.get('DEBUG')) +DEBUG = os.environ.get('DEBUG') +DEBUG = False if not DEBUG or DEBUG.lower() == 'false' else True ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS') if ALLOWED_HOSTS: diff --git a/backend/prod-entrypoint.sh b/backend/prod-entrypoint.sh index 4bf6624..1953ca7 100644 --- a/backend/prod-entrypoint.sh +++ b/backend/prod-entrypoint.sh @@ -3,7 +3,7 @@ # Apply database migrations echo "Apply database migrations" -python manage.py migrate collectstatic --noinput +python manage.py migrate # Start server echo "Starting server" diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 04a6a6d..bbc7f38 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -11,6 +11,8 @@ RUN set -ex \ && npm i COPY src /code/src +# hack: for prod api +RUN sed -i "s,http://localhost:8000,https://api.bullish.codingcoffee.me,g" /code/src/axios-api.js RUN NODE_ENV=production npm run build FROM nginx:1.19.6-alpine diff --git a/frontend/src/axios-api.js b/frontend/src/axios-api.js index b695be1..ccd1726 100644 --- a/frontend/src/axios-api.js +++ b/frontend/src/axios-api.js @@ -2,7 +2,7 @@ import axios from 'axios' // TODO: fix baseurl as per builds const getAPI = axios.create({ - baseURL: 'http://127.0.0.1:8000', + baseURL: 'http://localhost:8000', timeout: 60000, }) diff --git a/k8s/app/backend.yaml b/k8s/app/backend.yaml new file mode 100644 index 0000000..2818a47 --- /dev/null +++ b/k8s/app/backend.yaml @@ -0,0 +1,76 @@ +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bullish-backend +spec: + selector: + matchLabels: + app: bullish-backend + replicas: 1 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + template: + metadata: + labels: + app: bullish-backend + spec: + containers: + - name: bullish-backend + image: codingcoffee/bullish-backend + env: + - name: POSTGRES_USER + value: bullish + - name: POSTGRES_PASSWORD + value: password + - name: POSTGRES_DB + value: bullish + - name: PGDATA + value: /var/lib/postgresql/data/pgdata + - name: SECRET_KEY + value: sample + - name: ALLOWED_HOSTS + value: api.bullish.codingcoffee.me + - name: CORS_ALLOWED_ORIGINS + value: https://bullish.codingcoffee.me + - name: REDIS_HOST + value: bullish-redis + - name: REDIS_PORT + value: "6379" + - name: REDIS_CACHE_DB + value: "1" + - name: REDIS_QUEUE_DB + value: "0" + - name: POSTGRES_SERVER + value: bullish-database + - name: POSTGRES_USER + value: bullish + - name: POSTGRES_PASSWORD + value: password + - name: POSTGRES_DB + value: bullish + - name: POSTGRES_PORT + value: "5432" + +--- + +apiVersion: v1 +kind: Service +metadata: + name: bullish-backend + labels: + app: bullish-backend +spec: + type: ClusterIP + selector: + app: bullish-backend + ports: + - port: 8000 + targetPort: 8000 + protocol: TCP + +... diff --git a/k8s/frontend-deployment.yaml b/k8s/app/frontend.yaml similarity index 65% rename from k8s/frontend-deployment.yaml rename to k8s/app/frontend.yaml index d6805c7..a9e5f35 100644 --- a/k8s/frontend-deployment.yaml +++ b/k8s/app/frontend.yaml @@ -23,4 +23,21 @@ spec: - name: bullish-frontend image: codingcoffee/bullish-frontend +--- + +apiVersion: v1 +kind: Service +metadata: + name: bullish-frontend + labels: + app: bullish-frontend +spec: + type: ClusterIP + selector: + app: bullish-frontend + ports: + - port: 80 + targetPort: 80 + protocol: TCP + ... diff --git a/k8s/app/postgres.yaml b/k8s/app/postgres.yaml new file mode 100644 index 0000000..d593152 --- /dev/null +++ b/k8s/app/postgres.yaml @@ -0,0 +1,61 @@ +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bullish-database +spec: + selector: + matchLabels: + app: bullish-database + replicas: 1 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + template: + metadata: + labels: + app: bullish-database + spec: + volumes: + - name: postgres-data + hostPath: + path: "/var/k8s-pv/bullish/postgres" + type: DirectoryOrCreate + + containers: + - name: postgres + image: postgres:13.1-alpine + env: + - name: POSTGRES_USER + value: bullish + - name: POSTGRES_PASSWORD + value: password + - name: POSTGRES_DB + value: bullish + - name: PGDATA + value: /var/lib/postgresql/data/pgdata + volumeMounts: + - name: postgres-data + mountPath: /var/lib/postgresql/data/pgdata + +--- + +apiVersion: v1 +kind: Service +metadata: + name: bullish-database + labels: + app: bullish-database +spec: + type: ClusterIP + selector: + app: bullish-database + ports: + - port: 5432 + targetPort: 5432 + protocol: TCP + +... diff --git a/k8s/app/redis.yaml b/k8s/app/redis.yaml new file mode 100644 index 0000000..a47a2f9 --- /dev/null +++ b/k8s/app/redis.yaml @@ -0,0 +1,43 @@ +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bullish-redis +spec: + selector: + matchLabels: + app: bullish-redis + replicas: 1 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + template: + metadata: + labels: + app: bullish-redis + spec: + containers: + - name: redis + image: redis:6.0.10-alpine3.13 + +--- + +apiVersion: v1 +kind: Service +metadata: + name: bullish-redis + labels: + app: bullish-redis +spec: + type: ClusterIP + selector: + app: bullish-redis + ports: + - port: 6379 + targetPort: 6379 + protocol: TCP + +... diff --git a/k8s/app/rqscheduler.yaml b/k8s/app/rqscheduler.yaml new file mode 100644 index 0000000..8b5bdcd --- /dev/null +++ b/k8s/app/rqscheduler.yaml @@ -0,0 +1,63 @@ +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bullish-scheduler +spec: + selector: + matchLabels: + app: bullish-scheduler + replicas: 1 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + template: + metadata: + labels: + app: bullish-scheduler + spec: + containers: + - name: bullish-backend + image: codingcoffee/bullish-backend + command: + - "/bin/sh" + - "-c" + - "python manage.py rqscheduler" + env: + - name: POSTGRES_USER + value: bullish + - name: POSTGRES_PASSWORD + value: password + - name: POSTGRES_DB + value: bullish + - name: PGDATA + value: /var/lib/postgresql/data/pgdata + - name: SECRET_KEY + value: sample + - name: ALLOWED_HOSTS + value: api.bullish.codingcoffee.me + - name: CORS_ALLOWED_ORIGINS + value: https://bullish.codingcoffee.me + - name: REDIS_HOST + value: bullish-redis + - name: REDIS_PORT + value: "6379" + - name: REDIS_CACHE_DB + value: "1" + - name: REDIS_QUEUE_DB + value: "0" + - name: POSTGRES_SERVER + value: bullish-database + - name: POSTGRES_USER + value: bullish + - name: POSTGRES_PASSWORD + value: password + - name: POSTGRES_DB + value: bullish + - name: POSTGRES_PORT + value: "5432" + +... diff --git a/k8s/app/rqworker.yaml b/k8s/app/rqworker.yaml new file mode 100644 index 0000000..d4f329f --- /dev/null +++ b/k8s/app/rqworker.yaml @@ -0,0 +1,63 @@ +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: bullish-worker +spec: + selector: + matchLabels: + app: bullish-worker + replicas: 1 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + template: + metadata: + labels: + app: bullish-worker + spec: + containers: + - name: bullish-backend + image: codingcoffee/bullish-backend + command: + - "/bin/sh" + - "-c" + - "python manage.py rqworker default" + env: + - name: POSTGRES_USER + value: bullish + - name: POSTGRES_PASSWORD + value: password + - name: POSTGRES_DB + value: bullish + - name: PGDATA + value: /var/lib/postgresql/data/pgdata + - name: SECRET_KEY + value: sample + - name: ALLOWED_HOSTS + value: api.bullish.codingcoffee.me + - name: CORS_ALLOWED_ORIGINS + value: https://bullish.codingcoffee.me + - name: REDIS_HOST + value: bullish-redis + - name: REDIS_PORT + value: "6379" + - name: REDIS_CACHE_DB + value: "1" + - name: REDIS_QUEUE_DB + value: "0" + - name: POSTGRES_SERVER + value: bullish-database + - name: POSTGRES_USER + value: bullish + - name: POSTGRES_PASSWORD + value: password + - name: POSTGRES_DB + value: bullish + - name: POSTGRES_PORT + value: "5432" + +... diff --git a/k8s/frontend-service.yaml b/k8s/frontend-service.yaml deleted file mode 100644 index ebb6eff..0000000 --- a/k8s/frontend-service.yaml +++ /dev/null @@ -1,18 +0,0 @@ ---- - -apiVersion: v1 -kind: Service -metadata: - name: bullish-frontend - labels: - app: bullish-frontend -spec: - type: ClusterIP - selector: - app: bullish-frontend - ports: - - port: 80 - targetPort: 80 - protocol: TCP - -... diff --git a/k8s/ingress-https.yaml b/k8s/ingress/ingress-https.yaml similarity index 69% rename from k8s/ingress-https.yaml rename to k8s/ingress/ingress-https.yaml index 3271160..f9e35a6 100644 --- a/k8s/ingress-https.yaml +++ b/k8s/ingress/ingress-https.yaml @@ -1,6 +1,6 @@ --- -apiVersion: extensions/v1beta1 +apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: production-https-ingress @@ -18,10 +18,17 @@ spec: - backend: serviceName: bullish-frontend servicePort: 80 + - host: api.bullish.codingcoffee.me + http: + paths: + - backend: + serviceName: bullish-backend + servicePort: 8000 tls: - hosts: - bullish.codingcoffee.me + - api.bullish.codingcoffee.me secretName: production-tls-cert ...