Skip to content

Commit

Permalink
chore(build): add switch deployment strategy from cloud run to Kubern…
Browse files Browse the repository at this point in the history
…etes (#68)

* feat: switch deployment strategy from cloud run to Kubernetes

- replacement of  task in cloudbuild file with
- addition of  file with the relevant kubernetes configurations

switch idr-server deployment strategy

* refactor: modularize k8ts services for flexibility of changes

* chore: increase pod replicas
Increase django service replicas from 1 to 2
Increase PgBouncer service replicas from 1 to 2
  • Loading branch information
saladgg committed Aug 30, 2023
1 parent 8bb04ce commit eb3bb59
Show file tree
Hide file tree
Showing 18 changed files with 596 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
max-parallel: 4
matrix:
python-version: ["3.11"]

# Service containers to run with `container-job`
services:
postgres:
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
exclude: ^deploy/
- id: check-added-large-files

- repo: https://github.com/psf/black
Expand Down
120 changes: 91 additions & 29 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ steps:
[
"build",
"-t",
"europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
"${_IMAGE_NAME}:$COMMIT_SHA",
".",
]

Expand All @@ -16,15 +16,15 @@ steps:
args:
[
"push",
"europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA"
"${_IMAGE_NAME}:$COMMIT_SHA"
]

# Apply the latest migrations
- id: "apply migrations"
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i", "europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
"-i", "${_IMAGE_NAME}:$COMMIT_SHA",
"-s", "${_CLOUDSQL_INSTANCE_CONNECTION_NAME}",
"-e", "DJANGO_SETTINGS_MODULE=config.settings.production",
"-e", "GOOGLE_CLOUD_PROJECT=$PROJECT_ID",
Expand All @@ -37,7 +37,7 @@ steps:
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i", "europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
"-i", "${_IMAGE_NAME}:$COMMIT_SHA",
"-s", "${_CLOUDSQL_INSTANCE_CONNECTION_NAME}",
"-e", "DJANGO_SETTINGS_MODULE=config.settings.production",
"-e", "GOOGLE_CLOUD_PROJECT=$PROJECT_ID",
Expand All @@ -50,51 +50,113 @@ steps:
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i", "europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
"-i", "${_IMAGE_NAME}:$COMMIT_SHA",
"-s", "${_CLOUDSQL_INSTANCE_CONNECTION_NAME}",
"-e", "DJANGO_SETTINGS_MODULE=config.settings.production",
"-e", "GOOGLE_CLOUD_PROJECT=$PROJECT_ID",
"-e", "SETTINGS_NAME=${_SETTINGS_NAME}",
"--", "python", "/app/manage.py", "collectstatic", "--noinput"
]

# Collect static files
# Compress static assets
- id: "compress static assets"
name: "gcr.io/google-appengine/exec-wrapper"
args:
[
"-i", "europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
"-i", "${_IMAGE_NAME}:$COMMIT_SHA",
"-s", "${_CLOUDSQL_INSTANCE_CONNECTION_NAME}",
"-e", "DJANGO_SETTINGS_MODULE=config.settings.production",
"-e", "GOOGLE_CLOUD_PROJECT=$PROJECT_ID",
"-e", "SETTINGS_NAME=${_SETTINGS_NAME}",
"--", "python", "/app/manage.py", "compress"
]

# Deploy an image from Container Registry to Cloud Run
- id: "deploy to cloud run"
name: "gcr.io/cloud-builders/gcloud"
args: [
"beta",
"run",
"deploy",
"${_SERVICE_NAME}",
"--image", "europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA",
"--region", "europe-west1",
"--platform", "managed",
"--allow-unauthenticated",
"--add-cloudsql-instances", "${_CLOUDSQL_INSTANCE_CONNECTION_NAME}",
"--set-env-vars", "GOOGLE_CLOUD_PROJECT=$PROJECT_ID,SETTINGS_NAME=${_SETTINGS_NAME},DJANGO_SETTINGS_MODULE=config.settings.production,ENV_PATH=/tmp/secrets/.env",
"--min-instances", "1",
"--max-instances", "8",
"--memory", "512M",
"--cpu", "1",
"--set-secrets", "/tmp/secrets/.env=${_SETTINGS_NAME}:latest",
"--timeout", "59m59s"
]
# Get credentials for the cluster
- id: "get credentials for the cluster"
name: "gcr.io/cloud-builders/kubectl"
env:
- CLOUDSDK_CORE_PROJECT=$PROJECT_ID
- CLOUDSDK_COMPUTE_ZONE=${_GKE_COMPUTE_ZONE}
- CLOUDSDK_CONTAINER_CLUSTER=${_GKE_CLUSTER}
- KUBECONFIG=/workspace/.kube/config
args:
- cluster-info

# Show kubernetes manifest with values substituted
- id: "show kubernetes manifest"
dir: "deploy"
name: "gcr.io/$PROJECT_ID/helm:3.7.0"
env:
- KUBECONFIG=/workspace/.kube/config
args:
- template
- ${_APP_NAME}-${_DEPLOYMENT_TYPE}
- --namespace=${_NAMESPACE}
- --values
- values.yaml
- --set
- namespace=${_NAMESPACE}
- --set
- django.image.repository=${_IMAGE_NAME}
- --set
- django.image.tag=$COMMIT_SHA
- --set
- ingress.networking.domain=${_DOMAIN_NAME}
- --set
- ingress.networking.issuer.name=${_LETSENCRYPT_SERVER_TYPE}
- --set
- ingress.networking.static_ip_name=${_STATIC_IP_NAME}
- --set
- pg_bouncer.env.db_name=${_PG_NAME}
- --set
- pg_bouncer.env.db_user=${_PG_USER}
- --set
- pg_bouncer.env.db_password=${_PG_PASSWORD}
- --set
- cloud_sql.env.cloudsql_connection_instance=${_CLOUDSQL_INSTANCE_CONNECTION_NAME}
- --set
- django.configmap.config_name=${_CONFIGMAP_FILE}
- .

# Update essential Variables and Deploy to cluster
- id: "update essential variables and start deployment to cluster"
dir: "deploy"
name: "gcr.io/$PROJECT_ID/helm:3.7.0"
env:
- KUBECONFIG=/workspace/.kube/config
args:
- upgrade
- --install
- ${_APP_NAME}-${_DEPLOYMENT_TYPE}
- --namespace=${_NAMESPACE}
- --values
- values.yaml
- --set
- namespace=${_NAMESPACE}
- --set
- django.image.repository=${_IMAGE_NAME}
- --set
- django.image.tag=$COMMIT_SHA
- --set
- ingress.networking.domain=${_DOMAIN_NAME}
- --set
- ingress.networking.issuer.name=${_LETSENCRYPT_SERVER_TYPE}
- --set
- ingress.networking.static_ip_name=${_STATIC_IP_NAME}
- --set
- pg_bouncer.env.db_name=${_PG_NAME}
- --set
- pg_bouncer.env.db_user=${_PG_USER}
- --set
- pg_bouncer.env.db_password=${_PG_PASSWORD}
- --set
- cloud_sql.env.cloudsql_connection_instance=${_CLOUDSQL_INSTANCE_CONNECTION_NAME}
- --set
- django.configmap.config_name=${_CONFIGMAP_FILE}
- .

images:
- "europe-west1-docker.pkg.dev/$PROJECT_ID/fyj/idr-server-${_DEPLOYMENT_TYPE}:$COMMIT_SHA"
- "${_IMAGE_NAME}:$COMMIT_SHA"

timeout: 1200s
queueTtl: 3600s
Loading

0 comments on commit eb3bb59

Please sign in to comment.