Deploying a Go Web Application with Kubernetes

Table of Contents

  1. Overview
  2. Prerequisites
  3. Setup
  4. Creating a Go Web Application
  5. Dockerizing the Go Application
  6. Deploying the Go Application with Kubernetes
  7. Conclusion

Overview

In this tutorial, we will learn how to deploy a Go web application with Kubernetes. Kubernetes is an open-source container orchestration platform that helps automate the deployment, scaling, and management of applications. By the end of this tutorial, you will be able to deploy your Go web application on a Kubernetes cluster and take advantage of its powerful features like automatic scaling, load balancing, and fault tolerance.

Prerequisites

Before starting this tutorial, you should have the following prerequisites:

  • Basic knowledge of Go programming language
  • Familiarity with Docker and container concepts
  • Access to a Kubernetes cluster (either locally or in the cloud)

Setup

To follow along with this tutorial, make sure you have Go and Docker installed on your machine. You can download and install Go from the official website (https://golang.org/) and Docker from the Docker website (https://www.docker.com/). Additionally, ensure you have a Kubernetes cluster set up and configured. If you don’t have a cluster, you can use Minikube (https://kubernetes.io/docs/tutorials/hello-minikube/) to set up a local cluster for testing purposes.

Creating a Go Web Application

First, let’s create a simple Go web application. Open a text editor and create a new file called main.go. Add the following code to the file:

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 defines a basic HTTP server that listens on port 8080 and responds with “Hello, World!” for any request made to the root endpoint (“/”).

Save the file and open a terminal. Navigate to the directory where you saved the main.go file and run the following command to build and run the Go application:

go run main.go

You should see the following output:

Listening on :8080...

Access http://localhost:8080 in your web browser, and you should see the message “Hello, World!” displayed on the page.

Dockerizing the Go Application

Next, we’ll create a Docker image for our Go application. Docker allows us to package our application along with its dependencies into a container image, making it portable and easy to deploy.

Create a new file called Dockerfile in the same directory as your main.go file. Add the following code to the Dockerfile:

FROM golang:1.16-alpine

WORKDIR /app

COPY . .

RUN go build -o main .

EXPOSE 8080

CMD ["./main"]

This Dockerfile sets the base image as the official Go Alpine image, creates a working directory inside the container, copies the application code into the container, builds the Go application, exposes port 8080, and specifies the command to run the application.

Save the Dockerfile, open a terminal, and navigate to the directory where your main.go and Dockerfile files are located.

Build the Docker image using the following command:

docker build -t go-web-app .

This command builds the Docker image with the given tag -t go-web-app, using the current directory (.) as the build context.

Once the build process completes, you can verify that the image was created successfully by running:

docker images

You should see the go-web-app image listed.

Deploying the Go Application with Kubernetes

Now that we have a Docker image for our Go application, let’s deploy it with Kubernetes.

Create a new file called deployment.yaml and add the following code:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-web-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: go-web-app
  template:
    metadata:
      labels:
        app: go-web-app
    spec:
      containers:
        - name: go-web-app
          image: go-web-app
          ports:
            - containerPort: 8080

This YAML code defines a Kubernetes Deployment resource, which specifies the desired state for our application. It creates three replicas of the Go application, labels them with app: go-web-app, and exposes port 8080.

Save the deployment.yaml file and open a terminal. Make sure your kubectl command is properly configured to connect to your Kubernetes cluster.

Deploy the application by running the following command:

kubectl apply -f deployment.yaml

This command applies the configuration defined in the deployment.yaml file to the Kubernetes cluster. The cluster will create the necessary resources to deploy and manage the Go application.

To verify that the deployment was successful, run:

kubectl get pods

You should see three pods listed, each representing a replica of the Go application.

To expose the deployed application to the outside world, we can create a Kubernetes Service resource. Create a new file called service.yaml and add the following code:

apiVersion: v1
kind: Service
metadata:
  name: go-web-app-service
spec:
  selector:
    app: go-web-app
  ports:
    - name: http
      port: 8080
  type: LoadBalancer

Save the service.yaml file and apply the configuration to the cluster:

kubectl apply -f service.yaml

This command creates a Service resource that selects the Go application pods based on the app: go-web-app label and exposes the application via a LoadBalancer service on port 8080.

To access the deployed application, run:

kubectl get service go-web-app-service

You should see an external IP address listed under the EXTERNAL-IP column. Access this IP address in your web browser, and you should see the “Hello, World!” message once again.

Congratulations! You have successfully deployed a Go web application with Kubernetes.

Conclusion

In this tutorial, we learned how to deploy a Go web application with Kubernetes. We started by creating a simple Go web application, dockerized it to create a container image, and then deployed it to a Kubernetes cluster. We explored the steps required to create a Docker image for the Go application, deploy it using a Kubernetes Deployment resource, and access it through a Service with a LoadBalancer. With this knowledge, you can now leverage the power of Kubernetes to scale, manage, and deploy your Go web applications in a production environment.

Remember to clean up your resources after completing the tutorial to avoid unnecessary costs or resource usage:

kubectl delete deployment go-web-app-deployment
kubectl delete service go-web-app-service

Feel free to explore additional Kubernetes features and experiment with different configurations to further enhance your deployment setup. Happy coding!