From 161710d8e18826ff641f194c537145abc39f2f1a Mon Sep 17 00:00:00 2001 From: Youta EGUSA Date: Wed, 18 Sep 2019 21:13:48 +0900 Subject: [PATCH 1/2] =?UTF-8?q?kubernates=E3=81=B8=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 5 ++ .gitignore | 3 +- Makefile | 25 +++++++++ isucon/portal/docker_settings.py | 4 +- kubernates/00-secret.yml | 18 +++++++ kubernates/01-redis.yml | 33 ++++++++++++ kubernates/02-postgres.yml | 47 ++++++++++++++++ kubernates/03-volume.yml | 57 ++++++++++++++++++++ kubernates/04-app.yml | 93 ++++++++++++++++++++++++++++++++ kubernates/05-nginx.yml | 56 +++++++++++++++++++ nginx/Dockerfile | 4 ++ nginx/conf.d/default.conf | 2 +- 12 files changed, 343 insertions(+), 4 deletions(-) create mode 100644 Makefile create mode 100644 kubernates/00-secret.yml create mode 100644 kubernates/01-redis.yml create mode 100644 kubernates/02-postgres.yml create mode 100644 kubernates/03-volume.yml create mode 100644 kubernates/04-app.yml create mode 100644 kubernates/05-nginx.yml create mode 100644 nginx/Dockerfile diff --git a/.dockerignore b/.dockerignore index d383911..16011d7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,3 +2,8 @@ README.md .gitignore .git/ media/icons/* +docs/ +kubernates/ +nginx/ +logs/ +data/ diff --git a/.gitignore b/.gitignore index ff9fefd..f01e776 100644 --- a/.gitignore +++ b/.gitignore @@ -103,4 +103,5 @@ venv.bak/ # mypy .mypy_cache/ -log/ +logs/ +data/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1e5d802 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +all: nginx app + +.PHONY: nginx app apply delete + +nginx: nginx/Dockerfile + cd nginx && docker build -t isucon/portal-nginx . + +app: Dockerfile + docker build -t isucon/portal-app . + +apply: + kubectl apply -f kubernates/00-*.yml + kubectl apply -f kubernates/01-*.yml + kubectl apply -f kubernates/02-*.yml + kubectl apply -f kubernates/03-*.yml + kubectl apply -f kubernates/04-*.yml + kubectl apply -f kubernates/05-*.yml + +delete: + kubectl delete -f kubernates/05-*.yml + kubectl delete -f kubernates/04-*.yml + kubectl delete -f kubernates/03-*.yml + kubectl delete -f kubernates/02-*.yml + kubectl delete -f kubernates/01-*.yml + kubectl delete -f kubernates/00-*.yml diff --git a/isucon/portal/docker_settings.py b/isucon/portal/docker_settings.py index 6028dd5..8f26395 100644 --- a/isucon/portal/docker_settings.py +++ b/isucon/portal/docker_settings.py @@ -38,7 +38,7 @@ 'NAME': 'isucon', 'USER': 'isucon', 'PASSWORD': 'password', - 'HOST': 'postgres', + 'HOST': os.environ.get('POSTGRES_HOST', "postgres"), 'PORT': '5432', 'ATOMIC_REQUESTS': True, } @@ -54,7 +54,7 @@ else: raise ValueError("Invalid DJANGO_DATABASE_TYPE '{}'".format(DATABASE_TYPE)) -REDIS_HOST = 'redis' +REDIS_HOST = os.environ.get('REDIS_HOST', "redis") REDIS_RANKING_TOPN = os.getenv('REDIS_RANKING_TOPN', 30) SOCIAL_AUTH_GITHUB_KEY = os.environ.get("GITHUB_KEY", "") diff --git a/kubernates/00-secret.yml b/kubernates/00-secret.yml new file mode 100644 index 0000000..8c2c346 --- /dev/null +++ b/kubernates/00-secret.yml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: Secret +metadata: + name: portal-secret +type: Opaque +data: + github_key: NjUwYjFjODVkMTVjOWJlMzgzNmU= + # 650b1c85d15c9be3836e + github_secret: YmY4OWE0MTBmNDk5YzY3NGI0ZTllOTljZjE0Nzg2ZWU3MDhjM2YzZQ== + # bf89a410f499c674b4e9e99cf14786ee708c3f3e + django_allowed_host: cG9ydGFsLmlzdWNvbi5uZXQ= + # portal.isucon.net + slack_endpoint_url: aHR0cHM6Ly9ob29rcy5zbGFjay5jb20vc2VydmljZXMvVDA1MDZWOEpLL0JMOEE1MFJBNy96M0xXR0t0TVdFMHVONlluYm5CaTFGbHA= + # https://hooks.slack.com/services/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 + alibaba_access_key_id: TFRBSXpjVGptU3h3bzhwSg== + # LTAIzcTjmSxwo8pJ + alibaba_access_key_secret: ZFE4dmkwYnBpNzE0aFlLblZmQ2YySzNwb2h2Sjhn + # dQ8vi0bpi714hYKnVfCf2K3pohvJ8g diff --git a/kubernates/01-redis.yml b/kubernates/01-redis.yml new file mode 100644 index 0000000..8ddd827 --- /dev/null +++ b/kubernates/01-redis.yml @@ -0,0 +1,33 @@ +apiVersion: v1 +kind: Service +metadata: + name: portal-redis +spec: + ports: + - port: 6379 + selector: + app: portal-redis + +--- + +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: portal-redis +spec: + selector: + matchLabels: + app: portal-redis + strategy: + type: Recreate + template: + metadata: + labels: + app: portal-redis + spec: + containers: + - image: redis:5.0-alpine + name: portal-redis + ports: + - containerPort: 6379 + name: portal-redis diff --git a/kubernates/02-postgres.yml b/kubernates/02-postgres.yml new file mode 100644 index 0000000..9d78c0b --- /dev/null +++ b/kubernates/02-postgres.yml @@ -0,0 +1,47 @@ +apiVersion: v1 +kind: Service +metadata: + name: portal-db +spec: + ports: + - port: 5432 + selector: + app: portal-db + +--- + +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: portal-db +spec: + selector: + matchLabels: + app: portal-db + strategy: + type: Recreate + template: + metadata: + labels: + app: portal-db + spec: + containers: + - image: postgres:12-alpine + name: portal-db + env: + - name: POSTGRES_DB + value: isucon + - name: POSTGRES_USER + value: isucon + - name: POSTGRES_PASSWORD + value: password + ports: + - containerPort: 5432 + name: portal-db + volumeMounts: + - name: postgress-volume + mountPath: /var/lib/postgresql/data + volumes: + - name: postgress-volume + hostPath: + path: /opt/portal/data/postgresql diff --git a/kubernates/03-volume.yml b/kubernates/03-volume.yml new file mode 100644 index 0000000..3a535b0 --- /dev/null +++ b/kubernates/03-volume.yml @@ -0,0 +1,57 @@ +kind: PersistentVolume +apiVersion: v1 +metadata: + name: portal-static-volume +spec: + storageClassName: manual + capacity: + storage: 100Gi + accessModes: + - ReadWriteOnce + hostPath: + path: /opt/portal/data/static + +--- + +kind: PersistentVolume +apiVersion: v1 +metadata: + name: portal-static-nginx-volume +spec: + storageClassName: manual + capacity: + storage: 100Gi + accessModes: + - ReadWriteOnce + hostPath: + path: /opt/portal/data/static + +--- + +kind: PersistentVolume +apiVersion: v1 +metadata: + name: portal-media-volume +spec: + storageClassName: manual + capacity: + storage: 100Gi + accessModes: + - ReadWriteOnce + hostPath: + path: /opt/portal/data/media + +--- + +kind: PersistentVolume +apiVersion: v1 +metadata: + name: portal-media-nginx-volume +spec: + storageClassName: manual + capacity: + storage: 100Gi + accessModes: + - ReadWriteOnce + hostPath: + path: /opt/portal/data/media diff --git a/kubernates/04-app.yml b/kubernates/04-app.yml new file mode 100644 index 0000000..f9b4b24 --- /dev/null +++ b/kubernates/04-app.yml @@ -0,0 +1,93 @@ +apiVersion: v1 +kind: Service +metadata: + name: portal-app +spec: + ports: + - port: 5000 + selector: + app: portal-app + +--- + +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: portal-app +spec: + selector: + matchLabels: + app: portal-app + strategy: + type: Recreate + template: + metadata: + labels: + app: portal-app + spec: + containers: + - image: isucon/portal-app:latest + imagePullPolicy: IfNotPresent + name: portal-app + env: + - name: REDIS_HOST + value: portal-redis + - name: DJANGO_DATABASE_TYPE + value: postgres + - name: POSTGRES_HOST + value: portal-db + - name: DJANGO_DEBUG + value: "false" + - name: CONTEST + value: "false" + - name: GITHUB_KEY + valueFrom: + secretKeyRef: + name: portal-secret + key: github_key + - name: GITHUB_SECRET + valueFrom: + secretKeyRef: + name: portal-secret + key: github_secret + - name: DJANGO_ALLOWED_HOST + valueFrom: + secretKeyRef: + name: portal-secret + key: django_allowed_host + - name: SLACK_ENDPOINT_URL + valueFrom: + secretKeyRef: + name: portal-secret + key: slack_endpoint_url + - name: ALIBABA_ACCESS_KEY_ID + valueFrom: + secretKeyRef: + name: portal-secret + key: alibaba_access_key_id + - name: ALIBABA_ACCESS_KEY_SECRET + valueFrom: + secretKeyRef: + name: portal-secret + key: alibaba_access_key_secret + ports: + - containerPort: 5000 + name: portal-app + volumeMounts: + - name: static-volume + mountPath: /opt/app/static + - name: media-volume + mountPath: /opt/app/media + - name: log-volume + mountPath: /var/log/django/ + + volumes: + - name: static-volume + hostPath: + path: /opt/portal/data/static + - name: media-volume + hostPath: + path: /opt/portal/data/media + - name: log-volume + hostPath: + path: /opt/portal/logs/django diff --git a/kubernates/05-nginx.yml b/kubernates/05-nginx.yml new file mode 100644 index 0000000..0c6639d --- /dev/null +++ b/kubernates/05-nginx.yml @@ -0,0 +1,56 @@ +apiVersion: v1 +kind: Service +metadata: + name: portal-nginx +spec: + type: NodePort + ports: + - name: "http-port" + port: 80 + targetPort: 80 + nodePort: 30080 + selector: + app: portal-nginx + +--- + +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: portal-nginx +spec: + selector: + matchLabels: + app: portal-nginx + strategy: + type: Recreate + template: + metadata: + labels: + app: portal-nginx + spec: + containers: + - image: isucon/portal-nginx:latest + imagePullPolicy: IfNotPresent + name: portal-nginx + ports: + - containerPort: 80 + name: portal-nginx + volumeMounts: + - name: static-volume + mountPath: /opt/app/static + - name: media-volume + mountPath: /opt/app/media + - name: log-volume + mountPath: /var/log/nginx/ + + volumes: + - name: static-volume + hostPath: + path: /opt/portal/data/static + - name: media-volume + hostPath: + path: /opt/portal/data/media + - name: log-volume + hostPath: + path: /opt/portal/logs/nginx diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..4b43060 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:latest + +ADD nginx.conf /etc/nginx/ +ADD conf.d/ /etc/nginx/conf.d/ diff --git a/nginx/conf.d/default.conf b/nginx/conf.d/default.conf index b902603..bd6db28 100644 --- a/nginx/conf.d/default.conf +++ b/nginx/conf.d/default.conf @@ -1,5 +1,5 @@ upstream portal { - server django:5000; + server portal-app:5000; keepalive 128; } From fe3b26fcbba184fbed1626e00ffb01808f9c6228 Mon Sep 17 00:00:00 2001 From: Youta EGUSA Date: Wed, 18 Sep 2019 21:59:14 +0900 Subject: [PATCH 2/2] README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 303dd4e..2ac3f9c 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,13 @@ python manage.py runserver ``` +## kubernates (minikube) + +```bash +make +make apply +``` + ## docker-compose ```bash