Deploying a Contanerized Web Appliation
Deploying a Contanerized Web Appliation
1. Visit the Kubernetes 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. Make sure that billing is enabled for your project.
A Cloud Shell session opens inside a new frame at the bottom of the console and
displays a command-line prompt.
If you prefer to follow this tutorial on your workstation, you need to install the following
tools:
1. Install the Google Cloud SDK, which includes the gcloud command-line tool.
2. Using the gcloud command line tool, install the Kubernetes command-line tool.
kubectl is used to communicate with Kubernetes, which is the cluster orchestration
system of GKE clusters:
For this tutorial, you will deploy a sample web application called hello-app, a web server
written in Go that responds to all requests with the message “Hello, World!” on port 80.
The application is packaged as a Docker image, using the Dockerfile that contains
instructions on how the image is built. You will use this file below to package the application
below.
Set the PROJECT_ID environment variable in your shell by retrieving the pre- configured
project ID on gcloud by running the command below:
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:
You can run docker images command to verify that the build was successful:
docker images
Output:
REPOSITORY TAG IMAGE ID
CREATED SIZE
gcr.io/my-project/hello-app v1 25cfadb1bf28 10
seconds ago 54 MB
First, configure Docker command-line tool to authenticate to Container Registry (you need to
run this only once):
You can now use the Docker command-line tool to upload the image to your Container
Registry:
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.
It may take several minutes for the cluster to be created. Once the command has completed,
run the following command and see the cluster’s three worker VM instances:
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:
To see the Pod created by the Deployment, 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:
kubectl get service
Output:
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-web 10.3.251.122 203.0.113.0 80:30877/TCP 3d
Once you've determined the external IP address for your application, copy the IP address.
Point your browser to this URL (such as http://203.0.113.0) to check if your application
is accessible.
You can see the new replicas running on your cluster by running the following commands:
The load balancer you provisioned in the previous step will start routing traffic to these new
replicas automatically.
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):
Now, apply a rolling update to the existing deployment with an image update:
Visit your application again at http://[EXTERNAL_IP], and observe the changes you made
take effect.
Cleaning up
To avoid incurring charges to your Google Cloud Platform account for the resources used in
this tutorial:
After completing this tutorial, follow these steps to remove the following resources to prevent
unwanted charges incurring on your account:
1. Delete the Service: This step will deallocate the Cloud Load Balancer created for
your Service:
2. Delete the container cluster: This step will delete the resources that make up the
container cluster, such as the compute instances, disks and network resources.