Introduction

Google Kubernetes Engine (GKE) is a managed Kubernetes service provided by Google Cloud. In this guide, you'll learn how to use the Go programming language to build containerized applications and deploy them on GKE. We'll cover setting up your development environment, creating a Go application, Dockerizing it, deploying it on GKE, and provide sample code and detailed steps.


Prerequisites

Before getting started, make sure you have GoLang installed, access to a Google Cloud Platform (GCP) project with GKE enabled, the Google Cloud SDK (gcloud) installed on your system, and Docker for containerization. Familiarity with GoLang and basic web application development will be helpful.


Setting Up Your Development Environment

To begin building and deploying Go applications on GKE, follow these steps to set up your development environment:

  1. Install Go: If you haven't already, download and install Go from the official website.
  2. Set Up GCP Project: Create a GCP project or use an existing one, and enable GKE in the project settings.
  3. Install Google Cloud SDK: Install the Google Cloud SDK (gcloud) to interact with GCP and GKE.
  4. Install Docker: Install Docker on your local machine to containerize your applications.

Creating a Go Application

Develop your Go application as per your requirements. Ensure it's a web server that listens to the correct port and responds to HTTP requests. Here's a simple example of a Go web server:

                            package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, GKE!")
})
http.ListenAndServe(":8080", nil)
}

Containerizing Your Application

To deploy your Go application on GKE, you need to containerize it using Docker. Create a Dockerfile for your application like the one below:

                            # Use the official Golang image
FROM golang:1.16
# Set the working directory
WORKDIR /app
# Copy the local code to the container
COPY . .
# Build the Go application
RUN go build -o main
# Expose port 8080
EXPOSE 8080
# Command to run the executable
CMD ["./main"]

Deploying on Google Kubernetes Engine

Deploying your Go application on GKE involves creating a Docker image, pushing it to a container registry, defining Kubernetes deployment configurations, and deploying to GKE. Here are the general steps:

  1. Build the Docker Image: Use 'docker build' to build the Docker image for your application.
  2. Push to a Container Registry: Push the Docker image to a container registry like Google Container Registry (gcr.io).
  3. Define Kubernetes Deployment: Create a Kubernetes deployment YAML file to define your application's deployment.
  4. Deploy to GKE: Use 'kubectl apply' to deploy your application on GKE.

Sample Code

Here's a sample Go web server code and a simple Dockerfile for containerization. You can adapt this code to your application's requirements.

                            package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, GKE!")
})
http.ListenAndServe(":8080", nil)
}

Dockerfile:

                            # Use the official Golang image
FROM golang:1.16
# Set the working directory
WORKDIR /app
# Copy the local code to the container
COPY . .
# Build the Go application
RUN go build -o main
# Expose port 8080
EXPOSE 8080
# Command to run the executable
CMD ["./main"]

Conclusion

Using the Go programming language to build and deploy containerized applications on Google Kubernetes Engine (GKE) allows you to harness the power of container orchestration and scale your applications as needed. This guide covered setting up your development environment, creating a Go application, Dockerizing it, and deploying it on GKE. With this knowledge, you can effectively develop and deploy Go applications on the GKE platform.


Further Resources

To further explore GoLang development on Google Kubernetes Engine, consider the following resources: