Deploying a GoLang Microservice with Kubernetes

Introduction

Shantanu Bansal
2 min readJun 18, 2023

In the world of modern software development, microservices have gained significant popularity due to their scalability, flexibility, and maintainability. Kubernetes, on the other hand, has become the de facto standard for container orchestration, providing a robust platform for deploying, scaling, and managing microservices. In this blog, we will explore how to deploy a GoLang microservice using Kubernetes, leveraging its powerful features for seamless scalability and fault tolerance.

Prerequisites

Before diving into the deployment process, make sure you have the following:

  1. Docker: Install Docker on your local machine to build and containerize the GoLang microservice.
  2. Kubernetes: Set up a Kubernetes cluster or use a managed Kubernetes service like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Azure Kubernetes Service (AKS).
  3. kubectl: Install the kubectl command-line tool to interact with the Kubernetes cluster.

Step 1: Building and Containerizing the GoLang Microservice Let’s assume you have a GoLang microservice codebase ready. Here’s an example of a simple GoLang HTTP server:

package main

import (
"fmt"
"log"
"net/http"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is Shantanu!")
})

log.Fatal(http.ListenAndServe(":8080", nil))
}

To containerize this microservice, create a Dockerfile in the project directory:

FROM golang:1.16-alpine

WORKDIR /app
COPY . .

RUN go build -o app

EXPOSE 8080

CMD ["./app"]

Now, open a terminal, navigate to the project directory, and run the following commands:

docker build -t my-demo-app .
docker run -p 8080:8080 my-demo-app

Ensure that the microservice runs successfully locally by accessing http://localhost:8080 in your browser.

Step 2: Deploying the GoLang Microservice with Kubernetes. Now that we have a containerized microservice, let’s deploy it to Kubernetes.

Create a Kubernetes deployment YAML file named deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-demo-app
spec:
replicas: 3
selector:
matchLabels:
app: my-demo-app
template:
metadata:
labels:
app: my-demo-app
spec:
containers:
- name: my-demo-app
image: my-demo-app:latest
ports:
- containerPort: 8080

Create a Kubernetes service YAML file named service.yaml:

apiVersion: v1
kind: Service
metadata:
name: my-demo-app-service
spec:
selector:
app: my-demo-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer

Deploy the microservice to Kubernetes using the following commands:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Verify the deployment and service using:

kubectl get deployments
kubectl get services

You should see the deployed microservice and associated service listed.

Conclusion: Congratulations! You have successfully deployed a GoLang microservice using Kubernetes. Kubernetes provides an excellent platform for managing microservices, allowing easy scaling, fault tolerance, and load balancing. By leveraging the power of containerization and Kubernetes

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response