In my previous post we discussed about creating a GKE cluster to deploy your apps. In this post I am planning to walk you through how to deploy apps to your GKE cluster, and we will automate the entire process.

Architecture

Architecture My app is a simple index.html which prints “Hello World”. I am building this app using nginx docker images and I am using Google Cloud Build to build the image and push it to the container registry. So my ideas is when I push to git master branch Cloud Build will be triggered and build the images with the commit hash (docker tag) and then it will update the kubernetes deployment with the newly built image.

Folder Structure Folder structure Dockerfile

    FROM nginx:stable-alpine
    COPY code /usr/share/nginx/html

cloudbuild.yaml

    steps:
    - id: Step 1 - Build The Image
      name: 'gcr.io/cloud-builders/docker'
      args: [ 'build', '-t', 'us.gcr.io/${PROJECT_ID}/my-app:${SHORT_SHA}', './my-app' ]
    - id: Push the image to GCR
      name: 'gcr.io/cloud-builders/docker'
      args: [ 'push',  'us.gcr.io/${PROJECT_ID}/my-app:${SHORT_SHA}' ]
    - name: 'gcr.io/cloud-builders/gcloud'
      id: Generate manifest
      entrypoint: /bin/sh
      args:
      - '-c'
      - |
        sed "s/GOOGLE_CLOUD_PROJECT/${PROJECT_ID}/g" k8s/deployment.yaml.tpl | \
        sed "s/COMMIT_SHA/${SHORT_SHA}/g" > k8s/deployment.yaml        
    - name: 'gcr.io/cloud-builders/gke-deploy:stable'
      id: Generate manifest
      args:
      - 'apply'
      - '--filename=k8s'
      - '--location=<gke-cluster-region>'
      - '--cluster=<gke-cluster-name>'

Step 1

In step one the docker image will be build using the given Dockerfile. {PROJECT_ID} and {SHORT_SHA} values will get assigned automatically.

Step 2

Pushing the image to your Google Container Registry

Step 3

Replace the GOOGLE_CLOUD_PROJECT and COMMIT_SHA in the deployment.yaml.tpl with the actual values, and write new file into k8s/depliyment.yaml

Step 4

Deploy the manifest created in the above step. Make sure you replace and before your push the code into the git.

Create Cloud Build Trigger

I am maintaining my code at bitbucket, and I have mirrored my bitbucket repository with Google Cloud Build.

  1. First connect your bitbucket repository with cloud Build Cloud Build
  1. Select bitbicket and click continue Cloud Build

  2. Select your repository from the list and clickconnect repository. Cloud Build

  3. The we are going to add a trigger. Cloud Build

  4. Give a name for your trigger. Select trigger type as Branch and type master. SelectCloud Build configuration file** as** Build Configuration **and five the path as *my-app/cloudbuild.yaml. *And click create trigger Cloud Build

  5. Your trigger will be created and you should see that its trigger when you click history. Cloud Build 7.You can see that my build is failed. Lets dive deep and see why it has been failed. When you click on the build no you can see details of your build steps like below. Cloud Build You can see that my Step 1 - 3 are success and Step 4 has failed. This is failed because I did not give my gke cluster name and region in the code. So when you deploying update those two values with your gke cluster name and region.

  6. So now I am going to fix and push my changes to dev branch, and then I will merge my changes to master branch.

I have merged my dev branch to the master branch and note the commit id. (This commit id will be the tag of the docker image) Cloud Build 9. If you got to GCR and check you should be able to see the image is push with the relevant tag Cloud Build 10. All steps of our build is success and we should be able to see the app is deployed to our gke cluster Cloud Build 11. When you go to workloads in your gke cluster you should be able to see your my-appdeployment. Cloud Build You can find my code base for this post in here..

In this post we discussed about how to deploy your app to gke cluster when you push your code to your git repository. In order to achieve this we used Google Cloud Build, Google Container Registry and Google Kubernetes Engine. In the part 3 I am planning to discuss about how to access your application from the internet without spending money on Google Load Balancers.