Quickstart for Google Container Engine

This quickstart shows you how to use Google Cloud Shell to deploy a prebuilt Docker container image with a simple web application written in Go.

Before you begin

Take the following steps to enable the Google Container Engine API:
  1. Visit the Container Engine page in the Google Cloud Platform Console.
  2. Create or select a project.
  3. Wait for the API and related services to be enabled. This can take several minutes.
  4. Enable billing for your project.

    Enable billing

Install the following command-line tools used in this tutorial:

  • gcloud is used to create and delete Container Engine clusters gcloud is included in the Google Cloud SDK.
  • kubectl is used to manage Kubernetes, the cluster orchestration system used by Container Engine. You can install kubectl using gcloud:
    gcloud components install kubectl

Use Google Cloud Shell

To interact with kubectl immediately, use the Google Cloud Shell, which comes preinstalled with the gcloud and kubectl command-line tools. Follow these steps to launch the Google Cloud Shell:

  1. Go to Google Cloud Platform Console.
  2. Click the Activate Google Cloud Shell button at the top of the console window.

    Google Cloud Platform console

    A Cloud Shell session opens inside a new frame at the bottom of the console and displays a command-line prompt.

    Cloud Shell session

Set defaults for the gcloud command-line tool

Set a default compute zone. The following command sets the default zone as us-central1-b:

gcloud config set compute/zone us-central1-b

Run a container image

Follow these steps to create a cluster, then run the example "Hello App" web container using the prebuilt container image at Google Container Registry. If you'd like to see the source code for this example app, see Hello App code review.

  1. Create a cluster (this step can take a few minutes to complete).

    gcloud container clusters create example-cluster
    

  2. Run the container:

    kubectl run hello-web --image=gcr.io/google-samples/hello-app:1.0 --port=8080
    
  3. Expose the container. Note that the type="LoadBalancer" option in kubectl requests that Google Cloud Platform provision a load balancer for your container, which is billed per the regular Load Balancer pricing.

    kubectl expose deployment hello-web --type="LoadBalancer"
    
  4. Copy the external IP address for the hello-web app:

    kubectl get service hello-web
    
  5. View the app (replace EXTERNAL-IP with the external IP address you obtained in the previous step).

    http://EXTERNAL-IP:8080
    

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Delete the services you created to remove the load balancing resources that are provisioned to serve them:

    kubectl delete service hello-web
    
  2. Wait a few minutes for the service to spin down, then use the following command to delete the cluster you created:

    gcloud container clusters delete example-cluster
    

Optional: Hello App code review

Hello App is a simple web server application, consisting of just two files.

This section explains the source code for the application.

main.go

This file is a simple web server implementation written in Go programming language. The server responds to any HTTP request with a “Hello, world!” message.

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func main() {
	port := "8080"
	if fromEnv := os.Getenv("PORT"); fromEnv != "" {
		port = fromEnv
	}

	server := http.NewServeMux()
	server.HandleFunc("/", hello)
	log.Printf("Server listening on port %s", port)
	log.Fatal(http.ListenAndServe(":"+port, server))
}

func hello(w http.ResponseWriter, r *http.Request) {
	log.Printf("Serving request: %s", r.URL.Path)
	host, _ := os.Hostname()
	fmt.Fprintf(w, "Hello, world!\n")
	fmt.Fprintf(w, "Hostname: %s\n", host)
}

Dockerfile

The Dockerfile describes the image you want to build, and specifies which port the app should listen on.

FROM golang:1.8-alpine
ADD . /go/src/hello-app
RUN go install hello-app

FROM alpine:latest
COPY --from=0 /go/bin/hello-app .
ENV PORT 8080
CMD ["./hello-app"]

Optional: Run this tutorial locally with gcloud

As an alternative to Cloud Shell, you can install the gcloud command-line tool on your local computer. The Container Engine API uses the gcloud command-line tool for cluster operations and for managing credentials.

  1. Install the Google Cloud SDK, which includes the gcloud command-line tool.
  2. Use the gcloud command-line tool to install the kubectl command-line tool:

    gcloud components install kubectl
    

What's next

Try one of the following tutorials to build and deploy an application from start to finish:

If you'd like to get started on your own, your first step is to create a container cluster.

Container Engine uses Kubernetes for container management, deployment and scaling. More information on Kubernetes is available on the external Kubernetes site.

This quickstart uses kubectl, the command line interface for managing Kubernetes clusters. More information on kubectl is available in the kubectl reference.

Send feedback about...

Container Engine Documentation