Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

valkyrie-app

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.


Prerequisites

  • 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)

1. Build and Test the Docker Image

1.1 Create the Dockerfile

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.10 selects 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/app sets 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 -v compiles 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)

1.2 Build the Image

From the valkyrie-app/ directory, run:

docker build -t valkyrie-prod:v0.0.2 .
  • -t valkyrie-prod:v0.0.2 tags 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)

1.3 Run the Container Locally

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:8080 maps host port 8080 to container port 8080, allowing you to access the web server via localhost: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 ps

Look for valkyrie-prod:v0.0.2 with 0.0.0.0:8080->8080/tcp in the PORTS column. (docs.docker.com)


2. Push the Image to Artifact Registry

2.1 Create the Artifact Registry Repository

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)

2.2 Configure Docker Authentication

gcloud auth configure-docker us-central1-docker.pkg.dev

This 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)

2.3 Tag and Push the Image

Retag for Artifact Registry:

docker tag valkyrie-prod:v0.0.2 \
  us-central1-docker.pkg.dev/PROJECT_ID/valkyrie-docker/valkyrie-prod:v0.0.2

Replace 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.2

Docker uploads image layers and manifest to Artifact Registry, making the image available for GKE and other consumers. (cloud.google.com)


3. Deploy to Kubernetes

3.1 Configure kubectl Context

gcloud container clusters get-credentials valkyrie-dev \
  --zone us-central1-c

This fetches cluster credentials and updates your local kubeconfig, so kubectl commands target the valkyrie-dev GKE cluster. (cloud.google.com)

3.2 Update Kubernetes Manifests

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)

3.3 Apply Deployment and Service

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 LoadBalancer provisions a cloud load balancer, assigning an external IP to route traffic to your Pods. (kubernetes.io) (kubernetes.io)

3.4 Verify the Deployment

Check rollout status:

kubectl rollout status deployment/valkyrie-dev

Ensure all replicas are up and available. (cloud.google.com)

List Pods:

kubectl get pods

Confirm each Pod shows READY 1/1 and STATUS Running. (cloud.google.com)

Retrieve the Service’s external IP:

kubectl get service valkyrie-dev

Navigate 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.

About

Deploying a golang Application with Kubernetes on Google Cloud

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages