Introduction: Why Go and Kubernetes are the Perfect Pair
Go (Golang) and Kubernetes are the modern standard for building and deploying microservice architectures. Go compiles into a static binary, requires no dependencies on the target machine, starts quickly, and uses memory efficiently. Kubernetes, in turn, provides automation for deployment, scaling, and container management.
In this guide, we will walk through the complete deployment cycle of a simple Go application on Kubernetes. You will learn how to write a Dockerfile, create Kubernetes manifests (Deployment, Service, ConfigMap), and run the application in a cluster. All code examples can be used immediately in your own projects.
1. Preparing the Go Application for Containerization
First, let's create a simple HTTP server in Go that will respond to requests and display a greeting. It is important that the application listens on a port that we specify via an environment variable. This is a standard practice for Kubernetes.
Create the file main.go:
package main
import ( "fmt" "log" "net/http" "os")
func main() { port := os.Getenv("PORT") if port == "" { port = "8080" }
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { name := os.Getenv("APP_NAME") if name == "" { name = "Go App" } fmt.Fprintf(w, "Hello from %s! Running on port %s", name, port) })
log.Printf("Server started on port %s", port) log.Fatal(http.ListenAndServe(":"+port, nil))}This code reads the PORT and APP_NAME environment variables, which will allow us to flexibly configure the application via ConfigMap in Kubernetes.
2. Creating a Dockerfile for the Go Application
For deployment on Kubernetes, we need a Docker image. We will use a multi-stage build — this will reduce the image size and improve security.
Create the file Dockerfile:
# Build stageFROM golang:1.22-alpine AS builder
WORKDIR /appCOPY go.mod go.sum ./RUN go mod downloadCOPY . .RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server .
# Final imageFROM alpine:latestRUN apk --no-cache add ca-certificatesWORKDIR /root/COPY --from=builder /app/server .EXPOSE 8080CMD ["./server"]Build the image and push it to a registry (e.g., Docker Hub):
docker build -t yourusername/go-k8s-app:latest .docker push yourusername/go-k8s-app:latest3. Kubernetes Manifests: Deployment, Service, ConfigMap
Now let's create manifests for deploying the application in the cluster. It is recommended to store them in the k8s/ folder.
3.1. ConfigMap for Configuration
Let's externalize the application settings into a ConfigMap — this will allow changing them without rebuilding the image.
File k8s/configmap.yaml:
apiVersion: v1kind: ConfigMapmetadata: name: go-app-configdata: APP_NAME: "My Go Application" PORT: "8080"3.2. Deployment — Pod Description
The Deployment manages pod replicas and ensures updates without downtime.
File k8s/deployment.yaml:
apiVersion: apps/v1kind: Deploymentmetadata: name: go-app-deployment labels: app: go-appspec: replicas: 2 selector: matchLabels: app: go-app template: metadata: labels: app: go-app spec: containers: - name: go-app image: yourusername/go-k8s-app:latest ports: - containerPort: 8080 envFrom: - configMapRef: name: go-app-config livenessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 5 periodSeconds: 10\