Deploying a golang Application with Kubernetes on Google Cloud
In this playbook, you’ll walk through how to:
- Containerize a Go application by authoring a Dockerfile with an official Go base image and best practices for layering and security. (docs.docker.com, docs.docker.com, snyk.io)
- Build and test the container locally in detached mode with port mapping. (docs.docker.com, docs.docker.com)
- Push the image to Google’s Artifact Registry by creating a regional repository, configuring Docker authentication, and retagging and pushing the image. (cloud.google.com, cloud.google.com)
- Deploy and expose the application on a GKE cluster via Kubernetes manifests (Deployment and Service), then verify via CLI and the GKE console. (cloud.google.com, kubernetes.io)
Each section explains both the commands and the underlying concepts, so you can reproduce this workflow in your own environments.
- Google Cloud SDK & gcloud CLI installed and authenticated to your project, with the Artifact Registry API enabled. (cloud.google.com)
- Docker CLI installed locally to build and run containers.
- kubectl installed and configured to manage your GKE clusters. (stackoverflow.com)
In your project root (valkyrie-app/), create a file named Dockerfile with these contents:
FROM golang:1.10
WORKDIR /go/src/app
COPY source .
RUN go install -v
ENTRYPOINT ["app","-single=true","-port=8080"]FROM golang:1.10selects the official Go base image, which provides a minimal Debian environment with Go 1.10 pre-installed for compiling your app. (docs.docker.com)WORKDIR /go/src/appsets and creates the working directory inside the container for all subsequent instructions. (docs.docker.com)COPY source .transfers your application source code into the image, forming a new read-write layer on top of the base image. (docs.docker.com)RUN go install -vcompiles and installs your Go application, placing the resulting binary into$GOPATH/bin. (docs.docker.com)ENTRYPOINT [...]defines the default command and flags executed when the container starts, ensuring consistent invocation of your app. (docs.docker.com)
Security platforms recommend minimizing layers and excluding unnecessary files via a .dockerignore to reduce image bloat and surface area for vulnerabilities. (snyk.io)
Community guides also recommend multi-stage builds and pinning base image versions to improve build performance and security. (medium.com)
From the valkyrie-app/ directory, run:
docker build -t valkyrie-prod:v0.0.2 .-t valkyrie-prod:v0.0.2tags the resulting image, so you can reference it easily.- Docker processes each Dockerfile instruction in order, creating immutable layers and leveraging build cache for fast rebuilds. (docs.docker.com, docs.docker.com)
Launch the container in the background on port 8080:
docker run -d -p 8080:8080 valkyrie-prod:v0.0.2 &-d(or--detach) runs the container as a daemon, freeing your terminal. (docs.docker.com)-p 8080:8080maps host port 8080 to container port 8080, allowing you to access the web server vialocalhost:8080. (docs.docker.com)&pushes the command itself into the shell’s background, so you can continue issuing commands immediately.
Verify it’s running:
docker psLook for valkyrie-prod:v0.0.2 with 0.0.0.0:8080->8080/tcp in the PORTS column. (docs.docker.com)
gcloud artifacts repositories create valkyrie-docker \
--repository-format=docker \
--location=us-central1 \
--description="Docker repo for Valkyrie app"This command provisions a Docker-format repository in the us-central1 region under your project. (cloud.google.com)
gcloud auth configure-docker us-central1-docker.pkg.devThis updates your ~/.docker/config.json to use the gcloud credential helper for the us-central1-docker.pkg.dev domain, securely delegating access via IAM. (cloud.google.com)
Tutorials like those on GeeksforGeeks walk through the same process to help beginners avoid authentication pitfalls. (geeksforgeeks.org)
Retag for Artifact Registry:
docker tag valkyrie-prod:v0.0.2 \
us-central1-docker.pkg.dev/PROJECT_ID/valkyrie-docker/valkyrie-prod:v0.0.2Replace PROJECT_ID with your Google Cloud project ID. (cloud.google.com)
Push to Artifact Registry:
docker push us-central1-docker.pkg.dev/PROJECT_ID/valkyrie-docker/valkyrie-prod:v0.0.2Docker uploads image layers and manifest to Artifact Registry, making the image available for GKE and other consumers. (cloud.google.com)
gcloud container clusters get-credentials valkyrie-dev \
--zone us-central1-cThis fetches cluster credentials and updates your local kubeconfig, so kubectl commands target the valkyrie-dev GKE cluster. (cloud.google.com)
Open valkyrie-app/k8s/deployment.yaml and service.yaml, then set the image to:
us-central1-docker.pkg.dev/PROJECT_ID/valkyrie-docker/valkyrie-prod:v0.0.2
This ensures Kubernetes pulls your Artifact Registry image when creating Pods. (cloud.google.com)
kubectl apply -f valkyrie-app/k8s/deployment.yaml
kubectl apply -f valkyrie-app/k8s/service.yaml- The Deployment manages ReplicaSets and ensures your desired number of Pods are running.
- The Service of type
LoadBalancerprovisions a cloud load balancer, assigning an external IP to route traffic to your Pods. (kubernetes.io) (kubernetes.io)
Check rollout status:
kubectl rollout status deployment/valkyrie-devEnsure all replicas are up and available. (cloud.google.com)
List Pods:
kubectl get podsConfirm each Pod shows READY 1/1 and STATUS Running. (cloud.google.com)
Retrieve the Service’s external IP:
kubectl get service valkyrie-devNavigate to that IP in your browser; you should see the running application.
Alternatively, in the Cloud Console go to Kubernetes Engine › Gateways, Services & Ingress and click the Load Balancer’s external IP to inspect service health and access logs. (kubernetes.io)
With this playbook in your README.md, any developer can follow the end-to-end workflow—from Dockerfile authoring to GKE deployment—while understanding the key concepts behind each command.