You want to make automatic deployment whenever the developer push some new commit in the application source code repository.


  • You have Google Cloud account login
  • You have Google Cloud SDK installed locally
  • Enable billing for your project
  • You must activate Kubernetes Engine API, Cloud Build API, Repository API
  • Sample project provided at sample


You want to make automatic deployment whenever the developer push some new commit in the application source code repository.

Creating New Kubernetes Cluster

Create new cluster with f1-micro machine type. For this trial purpose we will use preemptible machine to keep the cost down. Preemptible means we are using spare capacity available from GCP to create node. This node will be restart by Google every 24 hours.

Create new project in GCP and configure default zone

$ gcloud projects create atambua
$ gcloud config set project atambua
$ gcloud config set compute/zone asia-southeast1-a
$ gcloud config list
region = asia-southeast1
zone = asia-southeast1-a
account = [email protected]
disable_usage_reporting = True
project = atambua

Check available machine type in your zone

$ gcloud compute machine-types list --filter=zone=asia-southeast1-a

Create new Kubernetes cluster

$ gcloud container clusters create my-cluster --machine-type=f1-micro --preemptible --zone=asia-southeast1-a

Verify that nodes are being created.

$ kubectl get nodes
NAME                                        STATUS   ROLES    AGE   VERSION
gke-my-cluster-default-pool-8d0c1fcb-gc08   Ready    <none>   19s   v1.13.11-gke.14
gke-my-cluster-default-pool-8d0c1fcb-hjjh   Ready    <none>   18s   v1.13.11-gke.14
gke-my-cluster-default-pool-8d0c1fcb-n9b8   Ready    <none>   20s   v1.13.11-gke.14

Creating Google Cloud Build trigger for automatic deployment

  • Go to Google Cloud Build and choose to create new trigger
  • Connect your source repository either in Github or Google Repository to cloud build trigger
  • Edit your trigger configuration and create cloudbuild.yaml file

Connect repository and create trigger


Connect repo trigger

Preparing cloudbuild.yaml file

  • Create your own cloudbuild.yaml file (sample available in the Github)
  • To learn the basic of cloud build configuration open here

Make sure you already set all default config to proper value to ensure Google Cloud Build step will not failed. Set your cluster to [cluster-name]

$ gcloud config set container/cluster my-cluster

Create cloudbuild.yaml file and put in the the root directory of your project. Cloud Build will running code inside this file whenever you push new commit in the repo and automatically deploy application in the Kubernetes Engine.

cloudbuild.yaml file content:

- id: 'build image'
  name: ''
    - 'build'
    - '-t'
    - '${PROJECT_ID}/myreact-app:$SHORT_SHA'
    - '.'
    - '-f'
    - './Dockerfile'
  timeout: 1000s
- id: 'push to container registry'
  name: ''
    - 'push'
    - '${PROJECT_ID}/myreact-app:$SHORT_SHA'
- id: 'deploy to gke'
  name: ''
    - 'KUBECONFIG=/.kube/config'
  entrypoint: 'bash'
    - '-c'
    - |
          gcloud container clusters get-credentials your-k8s-cluster-name --project your-project-id --zone your-compute-zone
          sed -i 's||$PROJECT_ID/myreact-app:$SHORT_SHA|' ./deployment.yaml
          kubectl apply -f ./deployment.yaml
timeout: 2000s

Create your Kubernetes deployment

This file will be apply in the cloud builder step id “deploy to gke”. File deployment.yaml will contains Kubernetes instruction to deploy container image to Kubernetes cluster:

apiVersion: extensions/v1beta1
kind: Deployment
    app: myreact-app
  name: myreact-app
  replicas: 1
      app: myreact-app
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
      creationTimestamp: null
        app: myreact-app
      - image:
        imagePullPolicy: Always
        name: myreact-app

Evaluate the trigger result in Cloud Build history

Check in the cloud build history if the trigger was run successfully into completion.

Verify that pods are running in the cluster

$ kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
myreact-app-d5f85b778-k76dz   1/1     Running   0          26m

Exposing your deployment/pods

Before you can access the pods from outside you need to create Kubernetes service to expose the pods

$ kubectl expose deployment myreact-app --port=80 --target-port=80 --type=LoadBalancer

Verify service creation

$ kubectl get svc
NAME          TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)        AGE
kubernetes    ClusterIP     <none>         443/TCP        33m
myreact-app   LoadBalancer   80:31119/TCP   21m

Check your application

Try open external ip with your browser, in this case


You successfully deploy application in Kubernetes cluster.

Dengan pengalaman PT Quorum Nusa Prestasi yang luas dalam industri Teknologi Informasi, kami mendorong klien kami untuk membuka bisnis mereka terhadap peluang baru dan mewujudkan impian. Perusahaan perangkat lunak ada untuk memecahkan masalah dan membantu kehidupan, dan ini adalah prioritas utama kami.

Continue Reading