Introduction: Why Go and Kubernetes Are a Perfect Pair
Go (Golang) and Kubernetes are two key tools in modern development. Go, created at Google, is a compiled, statically typed language that is ideal for building high-performance microservices. Kubernetes, in turn, is the de facto standard for container orchestration. Their combination provides a powerful ecosystem: you write lightweight, fast code in Go, package it into a Docker image, and Kubernetes manages its deployment, scaling, and recovery.
In this guide, we will deploy a simple Go application on Kubernetes step by step. You will learn how to write a Dockerfile, create Deployment and Service manifests, and apply them in a cluster. The article is intended for developers who are already familiar with the basics of Go and have a basic understanding of Docker.
Step 1: Creating and Containerizing a Go Application
Let's start with a simple HTTP server in Go. Create a file main.go:
package main
import ( "fmt" "net/http" "os")
func main() { port := os.Getenv("PORT") if port == "" { port = "8080" } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello from Go on Kubernetes!") }) http.ListenAndServe(":"+port, nil)}This code listens on the port specified by the PORT environment variable (default 8080) and returns a simple message.
Building the Docker Image
Now let's create a Dockerfile for a multi-stage build — this will reduce the size of the final image:
# Build stageFROM golang:1.21-alpine AS builderWORKDIR /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:v1 .docker push yourusername/go-k8s-app:v1Step 2: Deployment Manifest
Kubernetes uses YAML manifests to describe the desired state of the cluster. Create a file deployment.yaml:
apiVersion: apps/v1kind: Deploymentmetadata: name: go-app-deployment labels: app: go-appspec: replicas: 3 selector: matchLabels: app: go-app template: metadata: labels: app: go-app spec: containers: - name: go-app image: yourusername/go-k8s-app:v1 ports: - containerPort: 8080 env: - name: PORT value: "8080" readinessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: httpGet: path: / port: 8080 initialDelaySeconds: 15 periodSeconds: 20Key fields breakdown:
replicas: 3— Kubernetes will maintain 3 running instances (pods) of the application.readinessProbe— checks if the pod is ready to accept traffic (requests to /).livenessProbe— checks if the pod is alive; if not, Kubernetes will restart it.
Step 3: Service Manifest
To make the application accessible inside the cluster or from the outside, a Service is needed. Create service.yaml:
apiVersion: v1kind: Servicemetadata: name: go-app-servicespec: selector: app: go-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancerselector— links the service to pods that have the label