This tutorial demonstrates how to build a multi-tier web application using Google Kubernetes Engine (GKE). The tutorial application is a guestbook that allows visitors to enter text in a log and see the last few logged entries.
The tutorial shows how to set up the guestbook web service on an external IP address with a load balancer and how to run a Redis cluster with a single master and multiple workers.
GCP organises resources into projects. This allows you to collect all of the related resources for a single application in one place.
Begin by creating a new project or selecting an existing project for this tutorial.
Select a project, or
Navigate to Kubernetes Engine
Open the in the upper-left corner of the console, then select Kubernetes Engine.
Create a Kubernetes cluster
A cluster consists of at least one cluster master machine and multiple worker machines called nodes. You deploy applications to clusters, and the applications run on the nodes.
-
Click the button.
-
Enter a for this cluster.
-
Choose a for this cluster.
-
Click to create the cluster.
Get the sample application code
Cloud Shell is a built-in command-line tool for the console. In this section, you'll start Cloud Shell and use it to get the sample application code. Later, you'll use Cloud Shell to run the example app using a prebuilt container image.
Open Cloud Shell
Open Cloud Shell by clicking the button in the navigation bar in the upper-right corner of the console.
Get the sample code
Clone the sample code:
Set up a Redis master
In this section, you'll deploy a Redis master and verify that it is running.
Set up gcloud and kubectl credentials
Exploring the controller
View the configuration file for the controller:
Deploy the master controller
Deploy the Redis master:
View the running pod
Verify that the Redis master pod is running:
Create the redis-master service
In this section, you'll create a service to proxy the traffic to the Redis master pod.
View your service configuration:
This manifest file defines a service named redis-master
with a set of label selectors. These labels match the set of labels that are deployed in the previous step.
Create the service:
Verify that the service has been created:
Create the Redis worker service
The guestbook application needs to communicate to Redis workers to read data. To make the Redis workers discoverable, you need to set up a service. A service provides transparent load balancing to a set of pods.
View the configuration file that defines the worker service:
This file defines a service named redis-slave
running on port 6379. Note that the selector
field of the service matches the Redis worker pods created in the previous step.
Create the service:
Set up the guestbook web front-end
Now that you have the Redis storage of your guestbook up and running, start the guestbook web servers. Like the Redis workers, this is a replicated application managed by a deployment.
This tutorial uses a simple PHP front-end. It is configured to talk to either the Redis worker or master services, depending on whether the request is a read or a write. It exposes a simple JSON interface and serves a user experience based on jQuery and Ajax.
Create the front-end deployment
Expose the front-end on an external IP address
The services that you created in the previous steps are only accessible within the container cluster, because the default type for a service does not expose it to the Internet.
To make the guestbook web front-end service externally visible, you need to specify the type LoadBalancer
in the service configuration.
Use the following command to replace NodePort
with LoadBalancer
in the type
specification in the frontend-service.yaml
configuration file:
Create the service
Create the service:
Cleanup
With a GKE cluster running, you can create and delete resources with thekubectl
command-line client.
To remove your cluster, select thenextto the cluster name and click thebutton.