Deploying a containerized web application

This tutorial shows you how to package a web application in a Docker container
image, and run that container image on a Google Kubernetes Engine cluster as a
load-balanced set of replicas that can scale to the needs of your users.

Option A: Use Google Cloud Shell

You can follow this tutorial using Google Cloud Shell, which comes
preinstalled with the gcloud, docker, and kubectl command-line tools used
in this tutorial. If you use Cloud Shell, you don’t need to install these
command-line tools on your workstation.

Set the PROJECT_ID environment variable in your shell by retrieving the pre-
configured project ID on gcloud by running the command below:

export PROJECT_ID="$(gcloud config get-value project -q)"

The value of PROJECT_ID will be used to tag the container image for pushing it
to your private Container Registry.

To build the container image of this application and tag it for uploading, run
the following command:

docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .

This command instructs Docker to build the image using the Dockerfile in the
current directory and tag it with a name, such as gcr.io/my-project/hello-app:v1.
The gcr.io prefix refers to Google Container Registry, where
the image will be hosted. Running this command does not upload the image yet.

You can run docker images command to verify that the build was successful:

Step 2: Upload the container image

You need to upload the container image to a registry so that GKE
can download and run it.

First, configure Docker command-line tool to authenticate to Container
Registry (you need to run this only once):

gcloud auth configure-docker

You can now use the Docker command-line tool to upload the image to your
Container Registry:

docker push gcr.io/${PROJECT_ID}/hello-app:v1

Step 3: Run your container locally (optional)

To test your container image using your local Docker engine, run the following
command:

docker run --rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-app:v1

If you're on Cloud Shell, you can can click "Web preview" button on the top
right to see your application running in a browser tab. Otherwise, open a new
terminal window (or a Cloud Shell tab) and run to verify if the container works
and responds to requests with "Hello, World!":

curl http://localhost:8080

Once you've seen a successful response, you can shut down the container by
pressing Ctrl+C in the tab where docker run command is running.

Note: If you are using an existing Google Kubernetes Engine cluster or if you have
created a cluster through Google Cloud Platform Console, you need to run the following command to retrieve
cluster credentials and configure kubectl command-line tool with them:

gcloud container clusters get-credentials hello-cluster

If you have already created a cluster with the gcloud container clusters create
command listed above, this step is not necessary.

Step 5: Deploy your application

To deploy and manage applications on a GKE cluster, you must
communicate with the Kubernetes cluster management system. You typically do this
by using the kubectl command-line tool.

Kubernetes represents applications as Pods, which are
units that represent a container (or group of tightly-coupled containers). The
Pod is the smallest deployable unit in Kubernetes. In this tutorial, each Pod
contains only your hello-app container.

The kubectl run command below causes Kubernetes to create a
Deployment named hello-web on your cluster.
The Deployment manages multiple copies of your application, called replicas, and
schedules them to run on the individual nodes in your cluster. In this case, the
Deployment will be running only one Pod of your application.

Run the following command to deploy your application, listening on port 8080:

Step 6: Expose your application to the Internet

By default, the containers you run on GKE are not accessible from
the Internet, because they do not have external IP addresses. You must
explicitly expose your application to traffic from the Internet, run the
following command:

The kubectl expose command above creates a Service
resource, which provides networking and IP support to your application's Pods.
GKE creates an external IP and a Load Balancer (subject to
billing) for your application.

The --port flag specifies the port number configured on the Load Balancer, and
the --target-port flag specifies the port number that is used by the Pod
created by the kubectl run command from the previous step.

Note: GKE assigns the external IP address to the Service
resource—not the Deployment. If you want to find out the external IP that
GKE provisioned for your application, you can inspect the Service
with the kubectl get service command:

Now, you have multiple instances of your application running independently of
each other and you can use the kubectl scale command to adjust capacity of
your application.

The load balancer you provisioned in the previous step will start routing
traffic to these new replicas automatically.

Step 8: Deploy a new version of your app

GKE's rolling update mechanism ensures that your application
remains up and available even as the system replaces instances of your old
container image with your new one across all the running replicas.

You can create an image for the v2 version of your application by building the
same source code and tagging it as v2 (or you can change the "Hello, World!"
string to "Hello, GKE!" before building the image):

docker build -t gcr.io/${PROJECT_ID}/hello-app:v2 .

Then push the image to the Google Container Registry:

docker push gcr.io/${PROJECT_ID}/hello-app:v2

Now, apply a rolling update to the existing deployment with an image update: