This project is a simple example of how to deploy a node backend and react frontend on GKE using terraform.
The infrastructure consists of the following
- GKE cluster running auto-pilot
- Cloud Storage for assets
- Cloud SQL Postgress
- Backend and frontend workloads
- Ingress for both workloads
Simple node backend using TypeScript. It expose a GraphQL API using Apollo which use Cloud SQL (and TypeORM) for persistence.
Build your docker image. The terraform will define the URL pattern for the image in the container registry (google_container_registry_image -> stooks-gke-${var.env}) which wil lbe output as the project name.
docker build . -t gcr.io/${PROJECT_ID}/{project_name}:v1
gcloud builds submit --tag gcr.io/${PROJECT_ID}/{project_name}:v1 .
If you wish to run it locally, then go to: Read more here. Requirements is to have postgres database running locally.
Simple react frontend using TypeScript. It communicates with the backend using GraphQL. As with the backend, the terraform defines will output the image url to use.
docker build . -t gcr.io/${PROJECT_ID}/{project_name}-frontend:v1
gcloud builds submit --tag gcr.io/${PROJECT_ID}/{project_name}-frontend:v1 .
If you wish to run it locally, then go to: Read more here.
- Create a Google Cloud Project
- Enable all the necessary Google Cloud APIs such as compute, container registry, cloud sql and storage. Google will let you know if you forgot anything so don't worry.
In the project directory, you can run:
- Install all necessary tools: terraform, gcloud and kubectl
- Authenticate with Google Cloud: gcloud auth application-default login
- Create the infrastructure with terraform:
cd terraform/dev && terraform init && terraform apply
- Follow the guides for Frontend and Backend above to build and push docker images.
cd apps/frontend and apps/backend
docker build -t gcr.io/${PROJECT_ID}/.....:v1 .
gcloud docker -- push gcr.io/${PROJECT_ID}/......
- Authenticate kubectl:
gcloud container clusters get-credentials $(terraform output cluster_name) --zone=$(terraform output cluster_zone)
- Render Kubernetes config template:
terraform output k8s_rendered_template_backend > backendk8s.yml
- Update Kubernetes resources:
kubectl apply -f backendk8s.yml
- Get direct ip address with:
kubectl get ingress stooks-gke-dev-ingress
- Render Kubernetes config template:
terraform output k8s_rendered_template_frontend > frontendk8s.yml
- Update Kubernetes resources:
kubectl apply -f frontendk8s.yml
- Get direct ip address with:
kubectl get ingress stooks-gke-dev-frontend-ingress
To get direct IP addresses:
kubectl get ingress stooks-gke-dev-ingress
kubectl get ingress stooks-gke-frontend-dev
Inspired by examples https://github.com/hashicorp/learn-terraform-provision-gke-cluster and https://github.com/epiphone/gke-terraform-example.