Deployment of containerized application to GKE using CircleCI

Wilson Tan
4 min readAug 9, 2020

Previously, we have looked into some CI/CD tools like Jenkins, GitHub Actions and TravisCI and in this article, we will be looking at automating deployment of containerized Go application to Google Kubernetes Engine (GKE) using another CI/CD tool, CircleCI.

Like some of the other CI/CD tools that we have covered earlier, CircleCI provides free plan for open source repository hosted on GitHub or Bitbucket which is ideal for us to experiment with.

CircleCI

Prerequisite

  1. Open source repository hosted on GitHub / Bitbucket
  2. Google Cloud Console account
  3. Dockerfile and Docker Hub account
  4. kubectl installed on local machine
  5. CircleCI account

We will first deploy the containerized application onto GKE and to do this, you have to first enable billing on your account and create a project. Note that you may be charged a small amount of money for using the infrastructure provided on Google Cloud.

Deploying containerized application manually

It is assumed that you have Dockerfile script ready and you have built and published the Docker image for your application to Docker repository. Here, we will be looking at the public Docker repository, Docker Hub. On top of that, you should have kubectl already installed in your local machine, if you have not, head on to https://kubernetes.io/docs/tasks/tools/install-kubectl/ for the installation guide.

Let’s get started on Google Cloud Console.

  1. Create a new project and ensure that you have enabled billing account
  2. Create a new cluster via the Kubernetes Engine > Clusters on the side panel.
Google Kubernetes Engine (GKE)

3. Google Cloud Console provides a very simple setup guide to quickly setup a new cluster and that is sufficient for us to do what we want. Click on ‘My first cluster’ and create a new cluster after reviewing through the specs.

Setup wizard to create a new cluster

4. Connect to our cluster from our local machine. Click on the ‘Connect’ button on your cluster once it is ready and you will be prompted with a popup as below. Copy the command and run in your local machine. What it will do is that it will create a new context in the config file in $HOME/.kube directory and set the current context to connect to your cluster.

Connecting to your cluster on GKE

5. Verify that you have connected to your cluster by running kubectl config current-context .

6. Once connected, we can deploy our application to the cluster by running kubectl apply -f <deployment YAML file>. Below is an example of the deployment YAML file to GKE. In the YAML files, the following are specified:

a. A Namespace called sandbox

b. Service called hello-world that listens to our deployment on port 8080

c. Deployment called hello-world that pulls wilsontanwm/gotest:latest Docker image from Docker hub (alternatively, you can use other Docker images) and it runs on port 8080 which is also where the Go application runs on

d. Ingress called hello-world-routing that will direct external http traffic that matches the path /* to our service hello-world

YAML file for deploying application on Kubernetes

Your application is now up and running, you can verify that by visiting the IP address listed on the Ingress.

Automating deployment to GKE using CircleCI

Now that we have done the deployment to GKE in manual way, we will do it the automatic way using CircleCI. CircleCI has this thing called Orbs which are reusable package of YAML configuration where the repetitive codes are condensed into single line of code. You can check out their registry at https://circleci.com/orbs/registry/ so that you can use the Orbs and not write long piece of configuration codes in CircleCI.

Do note that we have to add a configuration file called config.yml in .circleci directory so that CircleCI will pick up. Below is an example of CircleCI configuration file where we have defined 3 jobs, namely build, dockerize and deploy.

Configuration file for CircleCI

In the workflows section (starting from line 49), you can see that we have set the jobs to run only on specific branch like master or develop. The requires tag tells CircleCI that the job will only be executed when the jobs it requires succeed, as such the jobs can be run sequentially.

In build job, we have to specify which Docker image to use and it is using circleci/golang:1.14 since it’s a Go application. Then we can specify the steps like checking out the code, running build and test.

In dockerize job, we can use circleci/docker@1.4.0 to build the Docker image and then push the image to Docker repository. Do note that some of the steps require some environment variables to be setup in CircleCI, for example DOCKER_PASSWORD and DOCKER_LOGIN in the docker/check step. You can refer to https://circleci.com/orbs/registry/orb/circleci/docker for more commands.

In deploy job, we will be using circleci/gcp-gcr@0.8.0 and circleci/gcp-gke@1.1.0 for authentication and deployment to GKE. Note that you need to create a service account on Google Cloud Console to give permission CircleCI to perform deployment to GKE.

With the configuration setup, we can push the files to our repo and setup the project in CircleCI to start the build and whenever there’s update to the branches (master or develop or any branch that you specified in the CircleCI configuration file), the workflow will be initiated.

That’s all for automating the deployment to GKE using CircleCI.

Till next time! Cheers!

--

--