2017-06-23 19:23:36 +00:00
---
2017-07-21 18:52:38 +00:00
title: "Example: Deploying PHP Guestbook application with Redis"
2018-02-18 20:13:37 +00:00
reviewers:
2017-08-19 00:19:11 +00:00
- ahmetb
2018-05-05 16:00:51 +00:00
content_template: templates/tutorial
2018-05-15 22:29:27 +00:00
weight: 20
2019-02-21 16:24:20 +00:00
card:
name: tutorials
weight: 30
title: "Stateless Example: PHP Guestbook with Redis"
2017-06-23 19:23:36 +00:00
---
2018-05-05 16:00:51 +00:00
{{% capture overview %}}
2017-08-07 22:56:20 +00:00
This tutorial shows you how to build and deploy a simple, multi-tier web application using Kubernetes and [Docker ](https://www.docker.com/ ). This example consists of the following components:
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
* A single-instance [Redis ](https://redis.io/ ) master to store guestbook entries
2018-07-31 00:40:38 +00:00
* Multiple [replicated Redis ](https://redis.io/topics/replication ) instances to serve reads
2017-08-07 22:56:20 +00:00
* Multiple web frontend instances
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% /capture %}}
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% capture objectives %}}
2017-08-07 22:56:20 +00:00
* Start up a Redis master.
* Start up Redis slaves.
* Start up the guestbook frontend.
* Expose and view the Frontend Service.
* Clean up.
2018-05-05 16:00:51 +00:00
{{% /capture %}}
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% capture prerequisites %}}
2017-06-23 19:23:36 +00:00
2018-07-17 06:00:38 +00:00
{{< include " task-tutorial-prereqs . md " > }}
2017-06-23 19:23:36 +00:00
2018-07-17 06:00:38 +00:00
{{< version-check > }}
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% /capture %}}
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% capture lessoncontent %}}
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
## Start up the Redis Master
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
The guestbook application uses Redis to store its data. It writes its data to a Redis master instance and reads data from multiple Redis slave instances.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
### Creating the Redis Master Deployment
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
The manifest file, included below, specifies a Deployment controller that runs a single replica Redis master Pod.
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
{{< codenew file = "application/guestbook/redis-master-deployment.yaml" > }}
2017-08-07 22:56:20 +00:00
1. Launch a terminal window in the directory you downloaded the manifest files.
2018-07-02 20:06:18 +00:00
1. Apply the Redis Master Deployment from the `redis-master-deployment.yaml` file:
2018-07-17 06:00:38 +00:00
```shell
2018-07-02 20:06:18 +00:00
kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master-deployment.yaml
```
1. Query the list of Pods to verify that the Redis Master Pod is running:
```shell
kubectl get pods
```
The response should be similar to this:
```shell
NAME READY STATUS RESTARTS AGE
redis-master-1068406935-3lswp 1/1 Running 0 28s
```
1. Run the following command to view the logs from the Redis Master Pod:
```shell
kubectl logs -f POD-NAME
```
2018-05-05 16:00:51 +00:00
{{< note > }}
2018-11-06 19:33:04 +00:00
Replace POD-NAME with the name of your Pod.
2018-05-05 16:00:51 +00:00
{{< / note > }}
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
### Creating the Redis Master Service
2017-06-23 19:23:36 +00:00
2017-08-19 00:19:11 +00:00
The guestbook applications needs to communicate to the Redis master to write its data. You need to apply a [Service ](/docs/concepts/services-networking/service/ ) to proxy the traffic to the Redis master Pod. A Service defines a policy to access the Pods.
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
{{< codenew file = "application/guestbook/redis-master-service.yaml" > }}
2017-08-07 22:56:20 +00:00
1. Apply the Redis Master Service from the following `redis-master-service.yaml` file:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master-service.yaml
```
1. Query the list of Services to verify that the Redis Master Service is running:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get service
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
The response should be similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
2018-08-16 23:30:08 +00:00
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 < none > 443/TCP 1m
redis-master ClusterIP 10.0.0.151 < none > 6379/TCP 8s
2018-07-02 20:06:18 +00:00
```
{{< note > }}
2018-11-06 19:33:04 +00:00
This manifest file creates a Service named `redis-master` with a set of labels that match the labels previously defined, so the Service routes network traffic to the Redis master Pod.
2018-07-02 20:06:18 +00:00
{{< / note > }}
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
## Start up the Redis Slaves
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
Although the Redis master is a single pod, you can make it highly available to meet traffic demands by adding replica Redis slaves.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
### Creating the Redis Slave Deployment
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
Deployments scale based off of the configurations set in the manifest file. In this case, the Deployment object specifies two replicas.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
If there are not any replicas running, this Deployment would start the two replicas on your container cluster. Conversely, if there are more than two replicas are running, it would scale down until two replicas are running.
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
{{< codenew file = "application/guestbook/redis-slave-deployment.yaml" > }}
2017-08-07 22:56:20 +00:00
1. Apply the Redis Slave Deployment from the `redis-slave-deployment.yaml` file:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl apply -f https://k8s.io/examples/application/guestbook/redis-slave-deployment.yaml
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Query the list of Pods to verify that the Redis Slave Pods are running:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get pods
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
The response should be similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
NAME READY STATUS RESTARTS AGE
redis-master-1068406935-3lswp 1/1 Running 0 1m
redis-slave-2005841000-fpvqc 0/1 ContainerCreating 0 6s
redis-slave-2005841000-phfv9 0/1 ContainerCreating 0 6s
```
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
### Creating the Redis Slave Service
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
The guestbook application needs to communicate to Redis slaves to read data. To make the Redis slaves discoverable, you need to set up a Service. A Service provides transparent load balancing to a set of Pods.
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
{{< codenew file = "application/guestbook/redis-slave-service.yaml" > }}
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Apply the Redis Slave Service from the following `redis-slave-service.yaml` file:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl apply -f https://k8s.io/examples/application/guestbook/redis-slave-service.yaml
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Query the list of Services to verify that the Redis slave service is running:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get services
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
The response should be similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```
2018-08-16 23:30:08 +00:00
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 < none > 443/TCP 2m
redis-master ClusterIP 10.0.0.151 < none > 6379/TCP 1m
redis-slave ClusterIP 10.0.0.223 < none > 6379/TCP 6s
2018-07-02 20:06:18 +00:00
```
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
## Set up and Expose the Guestbook Frontend
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
The guestbook application has a web frontend serving the HTTP requests written in PHP. It is configured to connect to the `redis-master` Service for write requests and the `redis-slave` service for Read requests.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
### Creating the Guestbook Frontend Deployment
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
{{< codenew file = "application/guestbook/frontend-deployment.yaml" > }}
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Apply the frontend Deployment from the `frontend-deployment.yaml` file:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Query the list of Pods to verify that the three frontend replicas are running:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get pods -l app=guestbook -l tier=frontend
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
The response should be similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```
NAME READY STATUS RESTARTS AGE
frontend-3823415956-dsvc5 1/1 Running 0 54s
frontend-3823415956-k22zn 1/1 Running 0 54s
frontend-3823415956-w9gbt 1/1 Running 0 54s
```
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
### Creating the Frontend Service
2017-06-23 19:23:36 +00:00
2017-08-19 00:19:11 +00:00
The `redis-slave` and `redis-master` Services you applied are only accessible within the container cluster because the default type for a Service is [ClusterIP ](/docs/concepts/services-networking/service/#publishing-services---service-types ). `ClusterIP` provides a single IP address for the set of Pods the Service is pointing to. This IP address is accessible only within the cluster.
2017-08-07 22:56:20 +00:00
If you want guests to be able to access your guestbook, you must configure the frontend Service to be externally visible, so a client can request the Service from outside the container cluster. Minikube can only expose Services through `NodePort` .
2018-05-05 16:00:51 +00:00
{{< note > }}
2018-11-06 19:33:04 +00:00
Some cloud providers, like Google Compute Engine or Google Kubernetes Engine, support external load balancers. If your cloud provider supports load balancers and you want to use it, simply delete or comment out `type: NodePort` , and uncomment `type: LoadBalancer` .
2018-05-05 16:00:51 +00:00
{{< / note > }}
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
{{< codenew file = "application/guestbook/frontend-service.yaml" > }}
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
1. Apply the frontend Service from the `frontend-service.yaml` file:
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml
```
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
1. Query the list of Services to verify that the frontend Service is running:
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get services
```
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
The response should be similar to this:
```
2018-08-16 23:30:08 +00:00
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
2019-03-19 18:36:21 +00:00
frontend NodePort 10.0.0.112 < none > 80:31323/TCP 6s
2018-08-16 23:30:08 +00:00
kubernetes ClusterIP 10.0.0.1 < none > 443/TCP 4m
redis-master ClusterIP 10.0.0.151 < none > 6379/TCP 2m
redis-slave ClusterIP 10.0.0.223 < none > 6379/TCP 1m
2018-07-02 20:06:18 +00:00
```
2017-08-07 22:56:20 +00:00
### Viewing the Frontend Service via `NodePort`
If you deployed this application to Minikube or a local cluster, you need to find the IP address to view your Guestbook.
1. Run the following command to get the IP address for the frontend Service.
2018-07-02 20:06:18 +00:00
```shell
minikube service frontend --url
```
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
The response should be similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```
http://192.168.99.100:31323
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Copy the IP address, and load the page in your browser to view your guestbook.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
### Viewing the Frontend Service via `LoadBalancer`
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
If you deployed the `frontend-service.yaml` manifest with type: `LoadBalancer` you need to find the IP address to view your Guestbook.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
1. Run the following command to get the IP address for the frontend Service.
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get service frontend
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
The response should be similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```
2018-08-16 23:30:08 +00:00
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend ClusterIP 10.51.242.136 109.197.92.229 80:32372/TCP 1m
2018-07-02 20:06:18 +00:00
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Copy the external IP address, and load the page in your browser to view your guestbook.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
## Scale the Web Frontend
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
Scaling up or down is easy because your servers are defined as a Service that uses a Deployment controller.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
1. Run the following command to scale up the number of frontend Pods:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl scale deployment frontend --replicas=5
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Query the list of Pods to verify the number of frontend Pods running:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get pods
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
The response should look similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```
NAME READY STATUS RESTARTS AGE
frontend-3823415956-70qj5 1/1 Running 0 5s
frontend-3823415956-dsvc5 1/1 Running 0 54m
frontend-3823415956-k22zn 1/1 Running 0 54m
frontend-3823415956-w9gbt 1/1 Running 0 54m
frontend-3823415956-x2pld 1/1 Running 0 5s
redis-master-1068406935-3lswp 1/1 Running 0 56m
redis-slave-2005841000-fpvqc 1/1 Running 0 55m
redis-slave-2005841000-phfv9 1/1 Running 0 55m
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Run the following command to scale down the number of frontend Pods:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl scale deployment frontend --replicas=2
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
1. Query the list of Pods to verify the number of frontend Pods running:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get pods
```
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
The response should look similar to this:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```
NAME READY STATUS RESTARTS AGE
frontend-3823415956-k22zn 1/1 Running 0 1h
frontend-3823415956-w9gbt 1/1 Running 0 1h
redis-master-1068406935-3lswp 1/1 Running 0 1h
redis-slave-2005841000-fpvqc 1/1 Running 0 1h
redis-slave-2005841000-phfv9 1/1 Running 0 1h
```
2017-08-07 22:56:20 +00:00
2018-05-05 16:00:51 +00:00
{{% /capture %}}
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% capture cleanup %}}
2017-08-07 22:56:20 +00:00
Deleting the Deployments and Services also deletes any running Pods. Use labels to delete multiple resources with one command.
2017-06-23 19:23:36 +00:00
2017-08-07 22:56:20 +00:00
1. Run the following commands to delete all Pods, Deployments, and Services.
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl delete deployment -l app=redis
kubectl delete service -l app=redis
kubectl delete deployment -l app=guestbook
kubectl delete service -l app=guestbook
```
The responses should be:
```
2018-08-16 23:30:08 +00:00
deployment.apps "redis-master" deleted
deployment.apps "redis-slave" deleted
2018-07-02 20:06:18 +00:00
service "redis-master" deleted
service "redis-slave" deleted
2018-08-16 23:30:08 +00:00
deployment.apps "frontend" deleted
2018-07-02 20:06:18 +00:00
service "frontend" deleted
```
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
1. Query the list of Pods to verify that no Pods are running:
2017-06-23 19:23:36 +00:00
2018-07-02 20:06:18 +00:00
```shell
kubectl get pods
```
The response should be this:
```
No resources found.
```
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% /capture %}}
2017-06-23 19:23:36 +00:00
2018-05-05 16:00:51 +00:00
{{% capture whatsnext %}}
2017-08-19 00:19:11 +00:00
* Complete the [Kubernetes Basics ](/docs/tutorials/kubernetes-basics/ ) Interactive Tutorials
2017-10-12 07:47:35 +00:00
* Use Kubernetes to create a blog using [Persistent Volumes for MySQL and Wordpress ](/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/#visit-your-new-wordpress-blog )
2017-08-19 00:19:11 +00:00
* Read more about [connecting applications ](/docs/concepts/services-networking/connect-applications-service/ )
* Read more about [Managing Resources ](/docs/concepts/cluster-administration/manage-deployment/#using-labels-effectively )
2018-05-05 16:00:51 +00:00
{{% /capture %}}