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.
- Open source repository hosted on GitHub / Bitbucket
- Google Cloud Console account
- Dockerfile and Docker Hub account
- kubectl installed on local machine
- 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.
- Create a new project and ensure that you have enabled billing account
- Create a new cluster via the Kubernetes Engine > Clusters on the side panel.
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.
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.
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
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
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
.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.
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.
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.
dockerize job, we can use
email@example.com 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.
deploy job, we will be using
firstname.lastname@example.org 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!