[DOCKER] Deploy Django + React from scratch to GKE (4) Create cluster and container PUSH

Deploy Django + React from scratch to GKE (4) Create cluster and container PUSH

This is a continuation of (3) Creating a GCP project.

Thing you want to do

I want to develop an application with Djagno + React configuration and deploy it to Google Kubernetes Engine I wrote it because it was unlikely that there was a cohesive tutorial.

However, ** I think there are some points that are not perfect yet **, but I think that if you have some experience, you can use it immediately.

Caution

This is a record of an inexperienced hobby engineer struggling to deploy to create a portfolio. If there are any deficiencies, please point them out. ..

Aiming figure

environment

$ node --version
v12.14.1

$ npm --version
6.13.7

$ python --version
Python 3.7.4

$ docker --version
Docker version 19.03.8

OS windows10 pro

Deploy

Creating a Kubernetes cluster

Create a cluster and deploy the container. It can be accessed from the outside by setting Service and Ingress.

Create a cluster from the console.

Cluster name: [K8S_CLUSTER]
Location type:zone:[K8S_CLUSTER_ZONE]
Master version: 1.14.10-gke.27(Default)

Get contexts

Get the contexts to use the created cluster from the local kubectl.

$\gke-django-tutorial\gcloud container clusters get-credentials [K8S_CLUSTER] --zone="[K8S_CLUSTER_ZONE]"
Fetching cluster endpoint and auth data.
kubeconfig entry generated for [K8S_CLUSTER].

#Check if the context is applied
$\gke-django-tutorial\manifests\kubectl config current-context

Secrets

Environment variables that should be kept secret, such as database user names and passwords, have been managed in .env, but Kubernetes registers them in the Secrets resource and uses them.

Cloud SQL

By using Secrets, you can safely use CloudSQL user name and password as environment variables. To use an instance of Cloud SQL from GKE, you need to create Secrets for instance-level and database access.

Create Secrets for instance-level access.

$\gke-django-tutorial\manifests\kubectl create secret generic cloudsql-oauth-credentials --from-file=credentials.json=".\secrets\cloudsql\ZZZZZZZZZZZZZZZ.json"

secret/cloudsql-oauth-credentials created

Create Secrets for accessing the database.

$\gke-django-tutorial\manifests\kubectl create secret generic cloudsql --from-literal=username="[DATABASE_USER]" --from-literal=password="[DATABASE_PASSWORD]"

SECRET_KEY

Let's add the remaining SECRET_KEY from the .env file to Secrets. Leave DEBUG in backend / config / settings.py as False.

$\gke-django-tutorial\manifests\kubectl create secret generic secret-key --from-literal=SECRET_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

The relevant parts of backend / config / settings.py are as follows.

# backend/config/settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.basename(BASE_DIR)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY')  #Change

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

DATABASES = {
    'default': {
        # If you are using Cloud SQL for MySQL rather than PostgreSQL, set
        # 'ENGINE': 'django.db.backends.mysql' instead of the following.
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'web-db',
        'USER': os.environ.get('DATABASE_USER'),
        'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Build container image

Build the image locally and upload it to Google Cloud Registry. The image name must be in the format gcr.io/${PROJECT}/${IMAGENAME}:${TAGNAME}.

#Confirmation of project name
$\gke-django-tutorial\gcloud config get-value project
Your active configuration is: [YOUR_PROJECT]
gke-django-tutorial

#Build image
# web-back(Django)
$\gke-django-tutorial\docker image build --no-cache -t gcr.io/[YOUR_PROJECT]/web-back:latest ./backend/web-back/.

# nginx-back
$\gke-django-tutorial\docker image build --no-cache -t gcr.io/[YOUR_PROJECT]/nginx-back:latest ./backed/nginx/.

# web-front(React)
$\gke-django-tutorial\docker image build --no-cache -t gcr.io/[YOUR_PROJECT]/web-front:latest ./frontend/web-front/.

# nginx-back
$\gke-django-tutorial\docker image build --no-cache -t gcr.io/[YOUR_PROJECT]/nginx-front:latest ./frontend/nginx/.

Upload container image

Upload the created 4 Docker images to Google Container Registry (GCR).

# backend
$\gke-django-tutorial\gcloud docker -- push gcr.io/[YOUR_PROJECT]/web-back:latest
$\gke-django-tutorial\gcloud docker -- push gcr.io/[YOUR_PROJECT]/nginx-back:latest

# frontend
$\gke-django-tutorial\gcloud docker -- push gcr.io/[YOUR_PROJECT]/web-front:latest
$\gke-django-tutorial\gcloud docker -- push gcr.io/[YOUR_PROJECT]/nginx-front:latest

I was able to create backend and frontend container images and push them to GCR.

(5)Deployment

Recommended Posts

Deploy Django + React from scratch to GKE (4) Create cluster and container PUSH
Deploy Django + React from scratch to GKE (3) Create a GCP project
Deploy Django + React from scratch to GKE (1) Backend development --Nginx + Django
Deploy Django + React from scratch to GKE: Table of Contents
Django memo # 1 from scratch
Python regular expression basics and tips to learn from scratch
Django starting from scratch (part: 2)
Django starting from scratch (part: 1)
Shell to create django project
React and Flask to GCP
Create and list Django models
Deploy django project to heroku
[Introduction to pytorch-lightning] Autoencoder of MNIST and Cifar10 made from scratch ♬
Create custom Django commands and run them from the command line