Step‑by‑Step: Deploy a Go Guessing Game on Kubernetes
This guide walks through building a Go number‑guessing game, containerizing it with Docker, pushing the image, and deploying the application to a Kubernetes cluster using a Deployment manifest, then verifying and running the game inside the pod.
Build and push a Docker image
Write a Dockerfile that copies the compiled Go binary into an Alpine base image. Build the image and push it to a registry:
docker build -t my-image:1.0 .
docker push my-image:1.0Go guessing‑game program
package main
import (
"fmt"
"math/rand"
"time"
)
const maxGuesses = 10
func main() {
rand.Seed(time.Now().UnixNano())
number := rand.Intn(100) + 1
fmt.Println("I'm thinking of a number between 1 and 100.")
fmt.Printf("You have %d guesses.
", maxGuesses)
for guesses := 1; guesses <= maxGuesses; guesses++ {
fmt.Printf("Guess #%d: ", guesses)
var guess int
_, err := fmt.Scanln(&guess)
if err != nil {
fmt.Println("Invalid input. Please enter an integer.")
continue
}
if guess < 1 || guess > 100 {
fmt.Println("Invalid input. Please enter a number between 1 and 100.")
continue
}
if guess < number {
fmt.Println("Too low.")
} else if guess > number {
fmt.Println("Too high.")
} else {
fmt.Printf("Correct! You guessed the number in %d guesses.
", guesses)
return
}
}
fmt.Printf("Sorry, you did not guess the number. It was %d.
", number)
}Prepare the Go project
# Initialise a Go module
go mod init test
# (optional) set a proxy for faster dependency download
go env -w GOPROXY=https://goproxy.cn,direct
# Resolve dependencies
go mod tidy
# Build a static Linux binary
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-demo main.goCreate a Docker image for the binary
FROM alpine
WORKDIR /app
COPY k8s-demo /app
CMD ["/app/k8s-demo"]Build the image, export it as a tarball, copy it to each cluster node, and import it with containerd:
# Build the image
docker build -t docker.io/library/k8sgame:v1 .
# Export the image
docker save -o k8sgame.tar.gz docker.io/library/k8sgame:v1
# Transfer to nodes (example nodes node1 and node2)
scp k8sgame.tar.gz node1:/root/
scp k8sgame.tar.gz node2:/root/
# Import on each node
ctr -n=k8s.io images import k8sgame.tar.gzDeploy to Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: guess-game
spec:
replicas: 1
selector:
matchLabels:
app: guess-game
template:
metadata:
labels:
app: guess-game
spec:
containers:
- name: guess-game
image: docker.io/library/k8sgame:v1
imagePullPolicy: IfNotPresent
# Keep the container alive; the game will be started manually
command: ["/bin/sh","-c","sleep 3600"]Apply the manifest and verify the pod status:
# Apply the Deployment
kubectl apply -f deployment.yaml
# List pods
kubectl get pods -o wideEnter the running pod and start the game binary:
# Replace <code>guess-game-xxxx</code> with the actual pod name shown by <code>kubectl get pods</code>
kubectl exec -it guess-game-xxxx -- /bin/sh
# Inside the container
/app/k8s-demoThe program prompts for guesses, confirming that the Go application runs correctly inside the Kubernetes pod.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
