Databotsgcp describes the deployment of a simple app. Our app is to be deployed on Google’s Kubernetes Engine using Cloud Build for continuous integration & delivery, and terraform to codify the provisioning of our infrastructure.
These are the prerequisites to get started
- A GCP account
- Terraform installed
- Google SDK installed
- Github Account
Terraform allows us express our infrastructure as code. It also attempts to sync our desired state of infrastructure (as declared in code) with the live state of our Infrastructure. Minor configurations can also be made with it.
We set the project properties for our gcloud cli using this command;
gcloud init
After filling in our configurations we enable the following APIs using this command;
gcloud services enable compute.googleapis.com
gcloud services enable cloudresourcemanager.googleapis.com
gcloud services enable container.googleapis.com
gcloud services enable servicenetworking.googleapis.com
gcloud services enable cloudbuild.googleapis.com
After enabling our APIs, we create a bucket to host our Terraform state remotely using this command;
gsutil mb gs://<BUCKET_NAME>
Next we will give some authority to terraform to manage resources using this command;
gcloud auth application-default login
Note; This is for testing purposes only. In production, we will create a service account, assign fine permissions to it and store the key file in a secrets manager. We will then reference those credentials in our terraform configurations.
We initialize terraform using this command;
terraform init
Finally, we will provision our infra by running this command in the infra directory;
terraform apply
This creates a VPC, a subnet, a regional GKE cluster and stores the state in a bucket.
Cloud Build is a serverless service which executes builds on GCP as a series of steps as defined in a configuration file.
The builds are executed by Cloud Builders, which are themselves container images that allow you run commands.
We will define the following build steps in our build config file
- Step one clones our remote repository using the git builder
- Step two builds our container image using the docker builder
- Step three pushes our container image to container register using docker builder
- The final step applies our Kubernetes deployment manifest to our cluster using gke-deploy builder
Next we will give some authority to Cloud Build to manage our Kubernetes cluster using the following command;
gcloud projects add-iam-policy-binding <PROJECT> -<SERVICE_ACCOUNT>@cloudbuild.gserviceaccount.com --role=roles/container.developer
Cloud Build uses Triggers to automate CI/CD. GitHub Triggers automate builds in repositories using events such as pushes or pulls. After connecting our repository, we will create our Trigger using Push to Branch
as the event trigger. This means that pushing any changes to our connected repository will trigger a build.
Finally we push our app to the repository and let Cloud Build do its thing.
Since our app is internet facing and exposed with a load balancer, we can reach it by using the Load Balancer IP or from our Services.
Fin