Deploying a Go-swagger service using Google Kubernetes Engine (GKE)

Shashank Vivek
5 min readSep 14, 2020

There are several ways to deploy a Go app on Google Cloud. You can use App Engine, GCE or GKE. In this tutorial, we’ll be focusing on GKE to deploy our go-swagger project. A basic Go project can easily be deployed using the examples available on GCP documentation . But in case of go-swagger, even a basic project requires lot of dependencies along with below command to run it as server:

go run cmd\e-food-server\main.go --scheme http --port=8080

In this tutorial, I will cover on the steps to deploy my github project on GKE.

To start with, you need to have a GCP project configured along with billing details. Once we have that checked, we can jump to Cloud Shell and setup GCP project ID using command:

gcloud set config set project $YOUR_PROJECT_ID

Let’s break it down as steps which will actually be self explanatory with the help of images.

Step 1: Export all variables which will to be used later:

Step 2. Create GKE cluster

To deploy our service, we need to start by building GKE cluster. It can be achieved using below command:

gcloud container clusters create my-demo-gke \ 
— num-nodes 1 \
— enable-basic-auth \
— issue-client-certificate \
— zone $my_zone

Once executed, you can see the cluster by Navigating to Kubernetes → Cluster from GCP menu.

Step 3: Clone the target repository and download all dependencies

Now that we have our cluster created, we need to clone the github repo in the cloud shell. We’ll be using below command to get the project which I have created

git clone https://github.com/shashankvivek/demo-go-service.git

Later navigate to the project directory & download all dependencies using:

go get ./...

Step 4: Create binaries

Once we have our dependencies downloaded, we need create the binary file which we’ll be later using for deploying with the help of Docker image and pushing it to GCP Container Registry.

To build the binary, use below command:

env CGO_ENABLED=0 GOOS=linux go build -a -o ./main ./cmd/demo-server/...

Note: You’ll see that we setting up few values and then doing go build . The reason for that can be explained in this stackoverflow post.

Step 5: Create docker image

Once the binaries are created, we need to create a docker image which can be pushed to Container Registry. Here we are using latest tag but in real world, it can be combination of branchName-buildNumber to make all images unique.

docker build -f "./Dockerfile" -t gcr.io/$proj_id/my-demo-gke:latest .

The given Dockerfile has 6 steps which basically downloads an alpine image, copies the binary file, expose 8080 port of Docker image and the runs the binary.

To test the docker file, we can even run it on cloud shell using below command:

docker run -it gcr.io/$proj_id/my-demo-gke:latest sh

Step 6: Push docker image to GCP

After successfully building the docker image, the next step is to push it to Container Registry.

gcloud docker -- push gcr.io/$proj_id/my-demo-gke:latest

We can cross check the image by getting into Side Panel → Cloud Registry

Step 7: Create deployment

Now that we have our cluster and image ready. The next step is to deploy the image. To do that, I have a deployment.yaml file which takes care of that. Please make sure to replace PROJECT_ID in deployment.yaml file.

To make the deployment , we just need to run

kubectl apply -f deployment.yaml

This deployment will create one POD and add respective selectors and labels to it.

We can confirm the deployment status by going to Workloads section of GKE.

Step 8: Expose the deployment using GKE service

Just deploying the code is not enough, we need to provide a port and IP so that we can access it. To do that , we’ll be using GKE service of type LoadBalander .

To create the LoadBalancer service, we need to run below command:

kubectl apply -f service.yaml

Once created, we can cross check the status of service under Service & Ingress . IT may take upto 60 secs to get the service up and running.

Step 9: Accessing the Endpoint

Now that we have created a load balancer serive, We can go to externally exposed IP which can be found under External endpoints section of it. Refer below screenshot.

At last, we can see the deployed code on an external endpoint as below:

We can compare the output to local code to confirm.

I hope this article will be helpful to people who are new to GKE as well as go-swagger. I had my own learning curve so I decided to collect all the information in this one article. Cheers 🍹

If you like this article, please 👏 clap 👏 few times to encourage me 🐼 to write more.

--

--