Skip to content

Instantly share code, notes, and snippets.

@mgiacomini
Last active June 20, 2018 00:06
Show Gist options
  • Save mgiacomini/d3e6d56e944a90472a31568efdc1c95c to your computer and use it in GitHub Desktop.
Save mgiacomini/d3e6d56e944a90472a31568efdc1c95c to your computer and use it in GitHub Desktop.
Deploy kubernetes in Google Cloud

Before you begin

  1. gcloud config set project [PROJECT_ID]
  2. gcloud config set compute/zone [COMPUTE_ZONE]
  3. gcloud components update

Cluster

Hire resources

  1. Create a container cluster: gcloud container clusters create [CLUSTER_NAME]
  2. Ensure was created: gcloud container clusters list

If you have multiple clusters

  1. You need to set a default cluster for the gcloud and kubectl command-line tools: gcloud config set container/cluster [CLUSTER_NAME]
  2. Pass the cluster's credentials to kubectl: gcloud container clusters get-credentials [CLUSTER_NAME]

Make sure your current context is set to the right cluster running the command

  1. kubectl config current-context must return gke_focus-sequencer-175820_us-east1-d_simulator-cluster

Create a new code repository for build images

  1. Link your github repo: https://console.cloud.google.com/code/develop/repo?project=focus-sequencer-175820&authuser=0
  2. Create a version trigger (Acionador de versão): https://console.cloud.google.com/gcr/triggers?project=focus-sequencer-175820&authuser=0

Additional resources

Database

Hire resources

  1. Cloud SQL - a postgres instance for staging and another for production (save the postgres user password when you create it)
  2. Enable the Google Cloud SQL API for your project
  3. Create a Cloud SQL service account with all privileges and download it.

Kubernetes configuration

Change the following vars in the k8s deployments files

  • project id - focus-sequencer-175820
  • cluster zone - us-east1-d
  • cluster id - simulator-cluster
  • sql instance id - simulator-stag-pg or simulator-prod-pg
  • endpoint - use your public domain or ip

Create kubernetes namespaces

  1. kubectl create namespace stag
  2. kubectl create namespace prod

Create pods secrets

To create files, can use the touch command. (man touch, for more details)

  1. Create a new file pg_username and execute echo "postgres" > pg_username
  2. Create a new file pg_password and execute echo "<SAVED-PWD>" > pg_password
  3. Generate a hash and save it with: mix phx.gen.secret
  4. Create a new file secret_key_base and execute echo "<SECRET>" > secret_key_base
  5. Let's create the kubernetes secrets from created files for prod and stag envs:
  • kubectl create secret generic simulator --from-file ./secret_key_base --from-file ./pg_username --from-file ./pg_password --namespace stag
  • kubectl create secret generic simulator --from-file ./secret_key_base --from-file ./pg_username --from-file ./pg_password --namespace prod
  1. After this, can remove them: rm -rf pg_username pg_password secret_key_base
  2. Create the secret cloudsql-keyfile with a file keyfile.json (which must be the keyfile for the cloudsql-prod service account previously created)
cp <path-to-cloudsql-prod-keyfile> ./keyfile.json
kubectl create secret generic cloudsql-keyfile --from-file ./keyfile.json --namespace stag
kubectl create secret generic cloudsql-keyfile --from-file ./keyfile.json --namespace prod
rm -rf keyfile.json

Create pod deployments and services

  1. kubectl create -f k8s/stag/simulator.web.deployment.yml --namespace stag
  2. kubectl create -f k8s/stag/simulator.api.deployment.yml --namespace stag
  3. kubectl create -f k8s/prod/simulator.web.deployment.yml --namespace prod
  4. kubectl create -f k8s/prod/simulator.api.deployment.yml --namespace prod
  5. kubectl create -f k8s/simulator.web.service.yml --namespace stag
  6. kubectl create -f k8s/simulator.api.service.yml --namespace stag
  7. kubectl create -f k8s/simulator.web.service.yml --namespace prod
  8. kubectl create -f k8s/simulator.api.service.yml --namespace prod

Notes: When create a new service there are differences between kubectl expose and kubectl create. See the kubernetes docs for more details.

Deploy

kubectl set image deployment/web-deployment web=gcr.io/focus-sequencer-175820/simulator.web:<commit-hash> --namespace <stag|prod>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment