Deploying a Golang Microservice to Kubernetes

With the rise of cloud computing, containerization, and microservices architecture, developers are adopting new approaches to building and deploying applications that are more scalable and resilient. Microservices architecture, in particular, has gained significant popularity due to its ability to break down monolithic applications into smaller, independent services.

Go is a great language choice for microservices because Golang applications have the ability to handle multiple requests concurrently using goroutines and channels. To deploy a Go application as a microservice, you have to containerize it with a tool like Docker and then deploy it with a container orchestration platform like Kubernetes.

In this tutorial, you’ll learn how to build a Golang microservice, package it as a Docker container, deploy it on Kubernetes, and monitor the deployment using Komodor.

Setting Up a Kubernetes Cluster

You need the following prerequisites to follow along in this tutorial:

  1. Go version 1.7 or higher
  2. Docker
  3. Dockerhub account
  4. Kubernetes

If you don’t already have a Kubernetes cluster set up, check out Komodor’s Guide to Getting Started with Kubernetes for step-by-step instructions.

Since you’ll be building the Docker image from code, you can obtain the code from this GitHub repo. Run the command below to clone the repo:

Preparing the Golang Microservice for Deployment

The microservice we’ll be working with in this tutorial is a shopping cart API application with six routes:

#HTTP MethodEndpointRequest bodyDescription
1GET/nilBase route
2GET/itemsnilReturns a list of cart items
3POST/items{ name: <item name>}Creates a new cart item
4GET/item/{id}nilReturns an item with the given id
5PUT/item/{id}{ name: <item name>}Updates an item with the given id
6DELETE/item/{id}nilRemoves an item from the cart with the given id

If you change-directory into the application’s folder cd shopping-cart-microservice and run the go run main.go file, the app will be available on port 8080. You can interact with the microservice via a tool like Postman to perform CRUD operations on cart items.

What comes after building your application? Deployment!

To prepare the Golang microservice for deployment, you need to build a Docker image from the application code and push this image to a container registry. You can achieve this using the docker build and docker push commands. It’ll be helpful to tag your image for easy versioning. For this tutorial, I’m assuming you’re pushing to Docker Hub, which means you need to tag your image with your username. For example, if your username is johndoe, tag your image as johndoe/shopping-cart-microservice.

Run the command below to build the image:

After building your image, test it locally on port 4000 by running the command below:

If you navigate to http://localhost:4000, you should see Shopping Cart Microservice displayed on the screen.

To push the image, first authenticate the Docker CLI by running:

After completing the authentication, run:

Deploying the Golang Microservice in Kubernetes

To deploy your microservice to Kubernetes, you need to create a deployment and a service. The deployment specifies how many replicas of your service are provisioned at a time, and the service exposes your deployment set to your local network.

In the repo, you’ll find a shopping-cart.yaml file that contains the deployment and service specifications. You can also find the file content in the snippet below:

The manifest above specifies the deployment and service for the shopping cart. The deployment defines a container built from the vicradon/shopping-cart-microservice:latest image. The container has three replicas, which you could increase or decrease to scale the app. The shopping-cart-service service forwards traffic directed at port 80 to port 8080. The nodePort field specifies that the service is exposed on a static port on each worker node in the cluster (in this case, port 30000). You can access the service from outside the cluster using the node’s IP address and port 30000.

Remember to replace the vicradon/shopping-cart-microservice:latest with the name of the image you pushed to Docker Hub.

Then run the kubectl command to apply the manifest to your Kubernetes cluster.

The previous command should give the following output:

If you want to scale the deployment manually using kubectl, all you need to do is edit the manifest and reapply it. Assuming your shopping cart app hasn’t gotten much traction and you want to scale down, you can edit the file to the following:

Then run: 

kubectl apply -f shopping-cart.yaml

Monitoring the Application in Kubernetes

Now that your application has been deployed to your Kubernetes cluster, you can sit back and see it working for your users. However, you should still monitor your app so that you can correct downtimes and address issues that may affect your application’s health.

There are several options for monitoring Kubernetes services. One that’s easy to set up and get moving quickly with is Komodor, a dev-first platform for monitoring the health of services on Kubernetes.

You can install the Komodor suite on your Kubernetes cluster using Helm, or you can install it directly on Mac or Linux. This tutorial installs Komodor using Helm.

Install Helm, then apply the Helm chart to your cluster. Sign up for a Komodor account, if you haven’t already, to allow Komodor to show you the metrics of your Kubernetes environment.

As part of your sign-up process, you can either input a team name of your choice or choose one of the defaults.

Choosing a team name

When you complete sign-up, you’ll be required to set up Komodor locally. Since you’re using Helm, copy the command to install via Helm and paste it in a terminal that has access to your Kubernetes cluster.

Note that Helm Dash, Komodor’s open-source dashboard for Helm, is now part of Komodor’s commercial suite. Its chart visualization is going to make your next leadership meeting presentation super easy.

Komodor setup
cluster setup successful

You should see a shopping-cart-deployment service on your dashboard:

Kubernetes services view on Komodo dashboard

Click on the service, and you’ll see a dashboard with different views into the deployment.

Shopping-cart-deployment view

Notice the Scale button at the top of the Deployments page. Say your app doesn’t have much traffic right now, and you want to reduce the number of instances from 3 to 1. Click Scale and reduce the replica amount.

scale a deployment on Komodor

The scale command registers a new event, which should appear on your dashboard.

Conclusion

Thanks to its lightweight, portability, and concurrency capabilities, Go is a popular language for containerized applications (Kubernetes itself was in fact built with Golang). Packaging your Golang microservices into containers and deploying them using Kubernetes means you can take advantage of its self-healing, auto-scaling, and load-balancing features. But deploying Go applications to Kubernetes is just your first move toward leveraging this massive container orchestration system effectively—monitoring your Kubernetes services is an important next step.

Komodor is a great tool for monitoring, operating, and troubleshooting the health of your services on Kubernetes. Installed in your Kubernetes environment, Komodor collects logs and metrics from the Kubernetes API and displays them in an easy-to-use dashboard. Try Komodor for free to quickly identify and resolve issues before they impact your users, or join our Slack Kommunity to learn more.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 5

No votes so far! Be the first to rate this post.