Forum

Google Kubernetes E...
 
Notifications
Clear all

Google Kubernetes Engine quickstart - Create a guestbook with Redis and PHP

3 Posts
1 Users
0 Reactions
25 K Views
Posts: 108
Topic starter
(@taichi)
Member
Joined: 5 years ago

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.

  1. Click the  button.

  2. Enter a  for this cluster.

  3. Choose a  for this cluster.

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

 

Navigate to the directory containing the sample code:

cd examples/guestbook

 

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

gcloud container clusters \ get-credentials [cluster-name] \ --zone [cluster-zone]
 
Replace [cluster-name] and [cluster-zone] with the name and zone of the instance that you created.

Exploring the controller

View the configuration file for the controller:

cat redis-master-deployment.yaml
 
This file contains configuration to deploy a Redis master. The spec field defines the pod specification that the replication controller uses to create the Redis pod. The image tag refers to a Docker image to be pulled from a registry.

Deploy the master controller

Deploy the Redis master:

kubectl create -f \ redis-master-deployment.yaml
 
 
 

View the running pod

Verify that the Redis master pod is running:

kubectl get pods

 

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:

cat redis-master-service.yaml
 
 

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:

kubectl create -f \ redis-master-service.yaml
 
 

Verify that the service has been created:

kubectl get service

 

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:

cat redis-slave-service.yaml
 

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:

kubectl create -f \ redis-slave-service.yaml
 
 
Verify that the service has been created:
kubectl get 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

kubectl create -f \ frontend-deployment.yaml
 
 

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:

sed -i -e \ 's/NodePort/LoadBalancer/g' \ frontend-service.yaml
 
 

Create the service

Create the service:

kubectl create -f \ frontend-service.yaml
 
 
 

Visit the guestbook website

Your website is now running!

Find your external IP address

List the services:

kubectl get services --watch
 
 
 
 

Wait until you see an IP address in the External IP column for the frontend service. This may take a minute. To stop monitoring the services, press Ctrl+C.

Visit the application

Copy the IP address from the External IP column, and load the page in your browser.

 

 
 

Cleanup

With a GKE cluster running, you can create and delete resources with thekubectlcommand-line client.

To remove your cluster, select thenextto the cluster name and click thebutton.

 

Conclusion

Congratulations! You have deployed a multi-tier guestbook application using Google Kubernetes Engine.

Here are some suggestions for what you can do next:

 

2 Replies
Posts: 108
Topic starter
(@taichi)
Member
Joined: 5 years ago

jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-596696dd4-m875s 1/1 Running 0 7m1s
redis-slave-96685cfdb-jvnrm 0/1 ContainerCreating 0 4s
redis-slave-96685cfdb-qq5gs 0/1 ContainerCreating 0 4s
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ cat redis-slave-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
app: redis
role: slave
tier: backend
spec:
ports:
- port: 6379
selector:
app: redis
role: slave
tier: backend
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ kubectl create -f \
> redis-slave-service.yaml
service/redis-slave created
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.8.0.1 <none> 443/TCP 8m48s
redis-master ClusterIP 10.8.3.215 <none> 6379/TCP 7m19s
redis-slave ClusterIP 10.8.15.157 <none> 6379/TCP 4s
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ kubectl create -f \
> frontend-deployment.yaml
deployment.apps/frontend created
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ sed -i -e \
> 's/NodePort/LoadBalancer/g' \
> frontend-service.yaml
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ kubectl create -f \
> frontend-service.yaml
service/frontend created
jyan@cloudshell:~/examples/guestbook (jyan-51sec-org-test1)$ kubectl get services --watch
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend LoadBalancer 10.8.9.171 <pending> 80:32744/TCP 17s
kubernetes ClusterIP 10.8.0.1 <none> 443/TCP 9m41s
redis-master ClusterIP 10.8.3.215 <none> 6379/TCP 8m12s
redis-slave ClusterIP 10.8.15.157 <none> 6379/TCP 57s
NAME AGE
frontend 39s

Reply
Posts: 108
Topic starter
(@taichi)
Member
Joined: 5 years ago

rGb1sxK.png (1321×622)

Reply
Share: