Tutorial: Deploy to GCP

In this tutorial, we will deploy an existing Go CDK application called Guestbook to Google Cloud Platform (GCP).

Guestbook is a sample application that records visitors’ messages and displays a cloud banner with an administrative message. The main business logic is written in a cloud-agnostic manner using MySQL, the generic blob API, and the generic runtimevar API. All platform-specific code is set up by Wire.

Prerequisites🔗

You will need to install the following software for this tutorial:

Prerequisite: Cloning the Guestbook Sample🔗

Before you can do anything else, you need to clone the Go CDK repository. The repository contains the Guestbook sample.

git clone https://github.com/google/go-cloud.git
cd go-cloud/samples/guestbook

Building🔗

Run the following in the samples/guestbook directory:

go generate && go build

Running Locally🔗

You will need to run a local MySQL database server and create a local message of the day. localdb/main.go runs the local MySQL database server using Docker:

go get ./localdb/... # Get package dependencies.
go run localdb/main.go

In another terminal, run the guestbook application:

# Set a local Message of the Day.
echo 'Hello, World!' > motd.txt

# Run the server.
# For blob, it uses fileblob, pointing at the local directory ./blobs.
# For runtimevar, it uses filevar, pointing at the local file ./motd.txt.
#   You can update the ./motd.txt while the server is running, refresh
#   the page, and see it change.
./guestbook -env=local -bucket=blobs -motd_var=motd.txt

Your server is now running on http://localhost:8080/.

You can stop the MySQL database server with Ctrl-\. MySQL ignores Ctrl-C (SIGINT).

Running on Google Cloud Platform (GCP)🔗

If you want to run this sample on GCP, you need to create a project, download the gcloud SDK, install kubectl and log in.

# Install kubectl.
gcloud components install kubectl

# Opens a browser to log you into GCP.
gcloud auth login

You can then use Terraform, a tool for initializing cloud resources, to set up your project. Finally, this sample provides a script for building the Guestbook binary and deploying it to the Kubernetes cluster created by Terraform.

gcloud auth application-default login
cd gcp
terraform init

# Terraform will prompt you for your GCP project ID, desired region,
# and desired zone.
terraform apply

go run deploy/main.go

The deploy script will display the URL of your running service.

Cleanup🔗

To clean up the created resources, run terraform destroy inside the gcp directory using the same variables you entered during terraform apply.

Gophers🔗

The Go gopher was designed by Renee French and used under the Creative Commons 3.0 Attributions license.