Local Shell

After installing Cloud SDK, install the kubectl command-line tool by
running the following command:

gcloud components install kubectl

Configuring default settings for gcloud

Before getting started, you should use gcloud to configure two default
settings: your default project and compute zone.

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 an approximate regional location in which your clusters
and their resources live. For example, us-west1-a is a zone in the us-west
region.

Configuring these default settings makes it easier to run gcloud commands,
since gcloud requires that you specify the project and compute zone in which
you wish to work. You can also specify these settings or override default
settings by passing operational flags, such as --project, --zone, and
--cluster, to gcloud commands.

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

Note:gcloud might return an error if these settings are either not configured
or not specified.

Setting a default project

To set a default project, run the following command from Cloud Shell:

gcloud config set project [PROJECT_ID]

Replace [PROJECT_ID] with your project ID.

Setting a default compute zone

To set a default compute zone, run the following command:

gcloud config set compute/zone [COMPUTE_ZONE]

where [COMPUTE_ZONE] is the desired geographical compute
zone, such as us-west1-a.

Creating a GKE cluster

A cluster consists of at least one cluster master 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 a cluster, run the following command:

gcloud container clusters create [CLUSTER_NAME]

where [CLUSTER_NAME] is the name you choose for the
cluster.

Note: 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.

To authenticate for the cluster, run the following command:

gcloud container clusters get-credentials [CLUSTER_NAME]

This command configures kubectl to use the cluster you created.

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

Creating the Deployment

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

In this command:

--image specifies a container image to deploy. In this case, the
command pulls the example image from a Google Container Registry
bucket, gcr.io/google-samples/hello-app. :1.0
indicates the specific image version to pull. If a version is not
specified, the latest version is used.

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

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 --target-port routes the traffic to port 8080 of the application.

Optional: hello-app code review

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

hello-app is packaged as Docker container image.
Container images are stored in any Docker image registry, such as
Google Container Registry. We host hello-app in a Container Registry bucket
named gcr.io/google-samples/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.

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. To learn more about how this file work, refer to
Dockerfile reference in the Docker
documentation.