Writing Your First Microservice in Golang and Deploying it in Minikube

Writing Your First Microservice in Golang and Deploying it in Minikube

Microservices are a popular architectural style for building complex applications. They allow developers to break down large applications into smaller, independent services that can be developed, deployed, and scaled independently. In this post, we will walk through how to write your first microservice in Golang and deploy it in Minikube.

What is Golang?

Golang, also known as Go, is an open-source programming language developed by Google. It is designed to be fast, efficient, and easy to use. Golang is particularly well-suited for building microservices because it is lightweight and has a small memory footprint.

What is Minikube?

Minikube is a tool that allows you to run a Kubernetes cluster on your local machine. Kubernetes is an open-source container orchestration platform that is widely used for deploying and managing microservices. Minikube makes it easy to test and develop Kubernetes applications locally.

Writing your first microservice in Golang

Step 1: Setting Up Your Development Environment

Ensure you have Golang installed and configured on your machine. Additionally, let's set up Minikube , a tool that enables you to run a single-node Kubernetes cluster locally. You also need to install kubectl

Step 2: Writing the Microservice

Our microservice will be a simple HTTP server that responds with "Hello World". In a new folder, Run

go mod init go-hello-world         

to initialize a Go module

Create main.go with below code:

package main

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

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

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

This code creates a new HTTP server using the?http?package in Golang. The?http.HandleFunc?function sets up a handler function that will be called when a request is made to the root URL. The handler function writes the "Hello, World!" message to the response writer. The?http.ListenAndServe?function starts the HTTP server and listens on port 8080. If there is an error starting the server, the?log.Fatal?function will print the error message and exit the program.

Deploying your microservice in Minikube

Start a new Minikube cluster by running the following command:

sh
minikube start        

This will start a new Kubernetes cluster in a virtual machine on your local machine. Once the cluster is running, you can deploy your microservice by creating a new Kubernetes deployment and service.First, create a new file called deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-hello-world
  template:
    metadata:
      labels:
        app: go-hello-world
    spec:
      containers:
      - name: go-hello-world
        image: golang:1.16
        ports:
        - containerPort: 8080
        command: ["go", "run", "main.go"]
        

This file defines a new Kubernetes deployment for your microservice. The deployment specifies that there should be one replica of the microservice running, and it sets up a container that runs the Golang image and executes the?go run main.go?command to start the microservice. Next, create a new file called service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: go-hello-world-service
spec:
  selector:
    app: go-hello-world
  ports:
  - name: http
    port: 80
    targetPort: 8080
  type: NodePort

        

This file defines a new Kubernetes service for your microservice. The service exposes port 80 and maps it to port 8080 on the container. It also sets the service type to NodePort, which allows you to access the service from outside the cluster.To deploy your microservice, run the following commands:

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

This will create a new Kubernetes deployment and service for your microservice. You can check the status of the deployment by running the following command:

kubectl get deployments
        

This will show you the status of the deployment, including the number of replicas that are running.To access your microservice, you can use the?minikube service?command to get the URL for the service:

minikube service go-hello-world-service --url
        

This will give you the URL for the service, which you can use to access your microservice. For example, if the URL is?https://localhost:32550, you can access your microservice by visiting that URL in your web browser.

Conclusion

In this post, we walked through how to write your first microservice in Golang and deploy it in Minikube. We covered the basics of Golang and Kubernetes, and we showed you how to create a simple HTTP server and deploy it in a Kubernetes cluster. With this knowledge, you can start building your own microservices and deploying them in Kubernetes.

Congratulations! You've just taken your first steps into the fascinating world of microservices and Kubernetes. Writing a microservice in Golang and deploying it in Minikube is a fantastic way to gain hands-on experience with modern development practices.

This journey doesn't end here, though. As you become more comfortable with these concepts, you can explore advanced topics like service discovery, load balancing, and more complex microservice architectures. So keep experimenting, learning, and building—there's an entire universe of possibilities waiting for you in the realm of microservices and Kubernetes! ????

Feel free to connect with me if you have any questions or if you'd like to share your experiences with microservices and Kubernetes. Happy coding! ???? ???? ??

#Microservices #Kubernetes #Golang #Minikube #DevOps #CodingJourney #TechCommunity #LearnAndGrow #deployment

???? Sven Hohlfeld

IT Consultant bei Sven Hohlfeld IT Consulting

1 年

Very nice and concise explanation. Thank you.

回复
Abhishek shukla

Attended FEROZE GANDHI INSTITUTE OF ENGG AND TECHNOLOGY , RAEBARELI

1 年

Very informative article !!

Rajaram J

Senior Vice President @ Qinecsa | Author

1 年

Great perspective!

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了