Deploy an app to a GKE cluster

In this quickstart, you deploy a simple web server containerized application to a Google Kubernetes Engine (GKE) cluster. You will learn how to create a cluster, and how to deploy the application to the cluster so that it can be accessed by users.

This quickstart assumes a basic understanding of Kubernetes.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install and initialize the Google Cloud CLI.
  3. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select a Cloud project:

      gcloud config set project PROJECT_ID
  4. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  5. Install and initialize the Google Cloud CLI.
  6. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select a Cloud project:

      gcloud config set project PROJECT_ID
  7. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  8. Install kubectl:

    gcloud components install kubectl

Set default settings for the gcloud CLI

Use the gcloud CLI to configure the following default settings: your default project, compute zone, and compute region.

Your project has a project ID, which is its unique identifier. When you first create a project, you can use the automatically generated project ID or you can create your own.

Your compute zone is a location in the region where your clusters and their resources live. For example, us-west1-a is a zone in the us-west region. Your compute region is the region where your clusters and their resources live (for example, us-west).

Configuring these default settings makes it easier to run gcloud commands, because gcloud requires that you specify the project and location in which you want to work. You can also specify these settings or override default settings with flags, such as --project, --zone, --region, and --cluster, in your gcloud commands.

When you create GKE resources after configuring your default project, zone, and region, the resources are automatically created in that project, zone, and region.

In your development environment, perform the following steps:

  1. Set the default project:

    gcloud config set project PROJECT_ID
    

    Replace PROJECT_ID with your project ID.

  2. Set the default zone:

    gcloud config set compute/zone COMPUTE_ZONE
    

    Replace COMPUTE_ZONE with your compute zone, such as us-west1-a.

  3. Set the default region:

    gcloud config set compute/region COMPUTE_REGION
    

    Replace COMPUTE_REGION with your compute region, such as us-west1.

Create a GKE cluster

A cluster consists of at least one cluster control plane machine and multiple worker machines called nodes. Nodes are Compute Engine virtual machine (VM) instances that run the Kubernetes processes necessary to make them part of the cluster. You deploy applications to clusters, and the applications run on the nodes.

To create clusters in GKE, you need to choose a mode of operation: Autopilot or Standard. If you use the Autopilot mode, your cluster is regional by default. If you use the Standard mode, this tutorial creates a zonal cluster.

Autopilot

Create an Autopilot cluster named hello-cluster:

gcloud container clusters create-auto hello-cluster \
    --region=COMPUTE_REGION

Replace COMPUTE_REGION with the Compute Engine region for the cluster.

Standard

Create a one-node Standard cluster named hello-cluster:

gcloud container clusters create hello-cluster \
    --num-nodes=1 \
    --zone=COMPUTE_ZONE

Replace COMPUTE_ZONE with the Compute Engine zone for the cluster.

It might take several minutes to finish creating the cluster.

Get authentication credentials for the cluster

After creating your cluster, you need to get authentication credentials to interact with the cluster:

gcloud container clusters get-credentials hello-cluster

This command configures kubectl to use the cluster you created.

Deploy an application to the cluster

Now that you have created a cluster, you can deploy a containerized application to it. For this quickstart, you can deploy our example web application, hello-app.

GKE uses Kubernetes objects to create and manage your cluster's resources. Kubernetes provides the Deployment object for deploying stateless applications like web servers. Service objects define rules and load balancing for accessing your application from the internet.

Create the Deployment

To run hello-app in your cluster, you need to deploy the application by running the following command:

kubectl create deployment hello-server \
    --image=us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0

This Kubernetes command, kubectl create deployment, creates a Deployment named hello-server. The Deployment's Pod runs the hello-app container image.

In this command:

  • --image specifies a container image to deploy. In this case, the command pulls the example image from an Artifact Registry repository, us-docker.pkg.dev/google-samples/containers/gke/hello-app. :1.0 indicates the specific image version to pull. If you don't specify a version, the image with the default tag latest is used.

Expose the Deployment

After deploying the application, you need to expose it to the internet so that users can access it. You can expose your application by creating a Service, a Kubernetes resource that exposes your application to external traffic.

To expose your application, run the following kubectl expose command:

kubectl expose deployment hello-server --type LoadBalancer --port 80 --target-port 8080

Passing in the --type LoadBalancer flag creates a Compute Engine load balancer for your container. The --port flag initializes public port 80 to the internet and the --target-port flag routes the traffic to port 8080 of the application.

Load balancers are billed per Compute Engine's load balancer pricing.

Inspect and view the application

  1. Inspect the running Pods by using kubectl get pods:

    kubectl get pods
    

    You should see one hello-server Pod running on your cluster.

  2. Inspect the hello-server Service by using kubectl get service:

    kubectl get service hello-server
    

    From this command's output, copy the Service's external IP address from the EXTERNAL-IP column.

  3. View the application from your web browser by using the external IP address with the exposed port:

    http://EXTERNAL_IP
    

You have just deployed a containerized web application to GKE.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Cloud project with the resources.

  1. Delete the application's Service by running kubectl delete:

    kubectl delete service hello-server
    

    This command deletes the Compute Engine load balancer that you created when you exposed the Deployment.

  2. Delete your cluster by running gcloud container clusters delete:

    gcloud container clusters delete hello-cluster
    

Optional: hello-app code review

hello-app is a simple web server application that consists of two files: main.go and a Dockerfile.

hello-app is packaged as a Docker container image. Container images are stored in any Docker image registry, such as Artifact Registry. We host hello-app in a Artifact Registry repository at us-docker.pkg.dev/google-samples/containers/gke/hello-app.

main.go

main.go is a web server implementation written in the 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() {
	// register hello function to handle all requests
	mux := http.NewServeMux()
	mux.HandleFunc("/", hello)

	// use PORT environment variable, or default to 8080
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	// start the web server on port and accept requests
	log.Printf("Server listening on port %s", port)
	log.Fatal(http.ListenAndServe(":"+port, mux))
}

// hello responds to the request with a plain-text "Hello, world" message.
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, "Version: 1.0.0\n")
	fmt.Fprintf(w, "Hostname: %s\n", host)
}

Dockerfile

Dockerfile describes the image you want Docker to build, including all of its resources and dependencies, and specifies which network port the app should expose. For more information about how this file works, see Dockerfile reference in the Docker documentation.

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"]

What's next

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how GKE performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Try GKE free