Example: Deploying PHP Guestbook application with Redis

This tutorial shows you how to build and deploy a simple, multi-tier web application using Kubernetes and Docker. This example consists of the following components:

  • A single-instance Redis master to store guestbook entries
  • Multiple replicated Redis instances to serve reads
  • Multiple web frontend instances

Objectives

  • Start up a Redis master.
  • Start up Redis slaves.
  • Start up the guestbook frontend.
  • Expose and view the Frontend Service.
  • Clean up.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

Start up the Redis Master

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.

Creating the Redis Master Deployment

The manifest file, included below, specifies a Deployment controller that runs a single replica Redis master Pod.

application/guestbook/redis-master-deployment.yaml Example: Deploying PHP Guestbook application with Redis - 图1

  1. apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
  2. kind: Deployment
  3. metadata:
  4. name: redis-master
  5. labels:
  6. app: redis
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: redis
  11. role: master
  12. tier: backend
  13. replicas: 1
  14. template:
  15. metadata:
  16. labels:
  17. app: redis
  18. role: master
  19. tier: backend
  20. spec:
  21. containers:
  22. - name: master
  23. image: k8s.gcr.io/redis:e2e # or just image: redis
  24. resources:
  25. requests:
  26. cpu: 100m
  27. memory: 100Mi
  28. ports:
  29. - containerPort: 6379
  1. Launch a terminal window in the directory you downloaded the manifest files.

  2. Apply the Redis Master Deployment from the redis-master-deployment.yaml file:

    1. kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master-deployment.yaml
  3. Query the list of Pods to verify that the Redis Master Pod is running:

    1. kubectl get pods

    The response should be similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. redis-master-1068406935-3lswp 1/1 Running 0 28s
  4. Run the following command to view the logs from the Redis Master Pod:

    1. kubectl logs -f POD-NAME

Note: Replace POD-NAME with the name of your Pod.

Creating the Redis Master Service

The guestbook application needs to communicate to the Redis master to write its data. You need to apply a Service to proxy the traffic to the Redis master Pod. A Service defines a policy to access the Pods.

application/guestbook/redis-master-service.yaml Example: Deploying PHP Guestbook application with Redis - 图2

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: redis-master
  5. labels:
  6. app: redis
  7. role: master
  8. tier: backend
  9. spec:
  10. ports:
  11. - name: redis
  12. port: 6379
  13. targetPort: 6379
  14. selector:
  15. app: redis
  16. role: master
  17. tier: backend
  1. Apply the Redis Master Service from the following redis-master-service.yaml file:

    1. kubectl apply -f https://k8s.io/examples/application/guestbook/redis-master-service.yaml
  2. Query the list of Services to verify that the Redis Master Service is running:

    1. kubectl get service

    The response should be similar to this:

    1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    2. kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 1m
    3. redis-master ClusterIP 10.0.0.151 <none> 6379/TCP 8s

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

Start up the Redis Slaves

Although the Redis master is a single pod, you can make it highly available to meet traffic demands by adding replica Redis slaves.

Creating the Redis Slave Deployment

Deployments scale based off of the configurations set in the manifest file. In this case, the Deployment object specifies two replicas.

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 running, it would scale down until two replicas are running.

application/guestbook/redis-slave-deployment.yaml Example: Deploying PHP Guestbook application with Redis - 图3

  1. apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
  2. kind: Deployment
  3. metadata:
  4. name: redis-slave
  5. labels:
  6. app: redis
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: redis
  11. role: slave
  12. tier: backend
  13. replicas: 2
  14. template:
  15. metadata:
  16. labels:
  17. app: redis
  18. role: slave
  19. tier: backend
  20. spec:
  21. containers:
  22. - name: slave
  23. image: gcr.io/google_samples/gb-redisslave:v3
  24. resources:
  25. requests:
  26. cpu: 100m
  27. memory: 100Mi
  28. env:
  29. - name: GET_HOSTS_FROM
  30. value: dns
  31. # Using `GET_HOSTS_FROM=dns` requires your cluster to
  32. # provide a dns service. As of Kubernetes 1.3, DNS is a built-in
  33. # service launched automatically. However, if the cluster you are using
  34. # does not have a built-in DNS service, you can instead
  35. # access an environment variable to find the master
  36. # service's host. To do so, comment out the 'value: dns' line above, and
  37. # uncomment the line below:
  38. # value: env
  39. ports:
  40. - containerPort: 6379
  1. Apply the Redis Slave Deployment from the redis-slave-deployment.yaml file:

    1. kubectl apply -f https://k8s.io/examples/application/guestbook/redis-slave-deployment.yaml
  2. Query the list of Pods to verify that the Redis Slave Pods are running:

    1. kubectl get pods

    The response should be similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. redis-master-1068406935-3lswp 1/1 Running 0 1m
    3. redis-slave-2005841000-fpvqc 0/1 ContainerCreating 0 6s
    4. redis-slave-2005841000-phfv9 0/1 ContainerCreating 0 6s

Creating the Redis Slave Service

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.

application/guestbook/redis-slave-service.yaml Example: Deploying PHP Guestbook application with Redis - 图4

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: redis-slave
  5. labels:
  6. app: redis
  7. role: slave
  8. tier: backend
  9. spec:
  10. ports:
  11. - port: 6379
  12. selector:
  13. app: redis
  14. role: slave
  15. tier: backend
  1. Apply the Redis Slave Service from the following redis-slave-service.yaml file:

    1. kubectl apply -f https://k8s.io/examples/application/guestbook/redis-slave-service.yaml
  2. Query the list of Services to verify that the Redis slave service is running:

    1. kubectl get services

    The response should be similar to this:

    1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    2. kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2m
    3. redis-master ClusterIP 10.0.0.151 <none> 6379/TCP 1m
    4. redis-slave ClusterIP 10.0.0.223 <none> 6379/TCP 6s

Set up and Expose the Guestbook Frontend

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.

Creating the Guestbook Frontend Deployment

application/guestbook/frontend-deployment.yaml Example: Deploying PHP Guestbook application with Redis - 图5

  1. apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
  2. kind: Deployment
  3. metadata:
  4. name: frontend
  5. labels:
  6. app: guestbook
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: guestbook
  11. tier: frontend
  12. replicas: 3
  13. template:
  14. metadata:
  15. labels:
  16. app: guestbook
  17. tier: frontend
  18. spec:
  19. containers:
  20. - name: php-redis
  21. image: gcr.io/google-samples/gb-frontend:v4
  22. resources:
  23. requests:
  24. cpu: 100m
  25. memory: 100Mi
  26. env:
  27. - name: GET_HOSTS_FROM
  28. value: dns
  29. # Using `GET_HOSTS_FROM=dns` requires your cluster to
  30. # provide a dns service. As of Kubernetes 1.3, DNS is a built-in
  31. # service launched automatically. However, if the cluster you are using
  32. # does not have a built-in DNS service, you can instead
  33. # access an environment variable to find the master
  34. # service's host. To do so, comment out the 'value: dns' line above, and
  35. # uncomment the line below:
  36. # value: env
  37. ports:
  38. - containerPort: 80
  1. Apply the frontend Deployment from the frontend-deployment.yaml file:

    1. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml
  2. Query the list of Pods to verify that the three frontend replicas are running:

    1. kubectl get pods -l app=guestbook -l tier=frontend

    The response should be similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. frontend-3823415956-dsvc5 1/1 Running 0 54s
    3. frontend-3823415956-k22zn 1/1 Running 0 54s
    4. frontend-3823415956-w9gbt 1/1 Running 0 54s

Creating the Frontend Service

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

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.

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

application/guestbook/frontend-service.yaml Example: Deploying PHP Guestbook application with Redis - 图6

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: frontend
  5. labels:
  6. app: guestbook
  7. tier: frontend
  8. spec:
  9. # comment or delete the following line if you want to use a LoadBalancer
  10. type: NodePort
  11. # if your cluster supports it, uncomment the following to automatically create
  12. # an external load-balanced IP for the frontend service.
  13. # type: LoadBalancer
  14. ports:
  15. - port: 80
  16. selector:
  17. app: guestbook
  18. tier: frontend
  1. Apply the frontend Service from the frontend-service.yaml file:

    1. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml
  2. Query the list of Services to verify that the frontend Service is running:

    1. kubectl get services

    The response should be similar to this:

    1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    2. frontend NodePort 10.0.0.112 <none> 80:31323/TCP 6s
    3. kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 4m
    4. redis-master ClusterIP 10.0.0.151 <none> 6379/TCP 2m
    5. redis-slave ClusterIP 10.0.0.223 <none> 6379/TCP 1m

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.

    1. minikube service frontend --url

    The response should be similar to this:

    1. http://192.168.99.100:31323
  2. Copy the IP address, and load the page in your browser to view your guestbook.

Viewing the Frontend Service via LoadBalancer

If you deployed the frontend-service.yaml manifest with type: LoadBalancer 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.

    1. kubectl get service frontend

    The response should be similar to this:

    1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    2. frontend ClusterIP 10.51.242.136 109.197.92.229 80:32372/TCP 1m
  2. Copy the external IP address, and load the page in your browser to view your guestbook.

Scale the Web Frontend

Scaling up or down is easy because your servers are defined as a Service that uses a Deployment controller.

  1. Run the following command to scale up the number of frontend Pods:

    1. kubectl scale deployment frontend --replicas=5
  2. Query the list of Pods to verify the number of frontend Pods running:

    1. kubectl get pods

    The response should look similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. frontend-3823415956-70qj5 1/1 Running 0 5s
    3. frontend-3823415956-dsvc5 1/1 Running 0 54m
    4. frontend-3823415956-k22zn 1/1 Running 0 54m
    5. frontend-3823415956-w9gbt 1/1 Running 0 54m
    6. frontend-3823415956-x2pld 1/1 Running 0 5s
    7. redis-master-1068406935-3lswp 1/1 Running 0 56m
    8. redis-slave-2005841000-fpvqc 1/1 Running 0 55m
    9. redis-slave-2005841000-phfv9 1/1 Running 0 55m
  3. Run the following command to scale down the number of frontend Pods:

    1. kubectl scale deployment frontend --replicas=2
  4. Query the list of Pods to verify the number of frontend Pods running:

    1. kubectl get pods

    The response should look similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. frontend-3823415956-k22zn 1/1 Running 0 1h
    3. frontend-3823415956-w9gbt 1/1 Running 0 1h
    4. redis-master-1068406935-3lswp 1/1 Running 0 1h
    5. redis-slave-2005841000-fpvqc 1/1 Running 0 1h
    6. redis-slave-2005841000-phfv9 1/1 Running 0 1h

Cleaning up

Deleting the Deployments and Services also deletes any running Pods. Use labels to delete multiple resources with one command.

  1. Run the following commands to delete all Pods, Deployments, and Services.

    1. kubectl delete deployment -l app=redis
    2. kubectl delete service -l app=redis
    3. kubectl delete deployment -l app=guestbook
    4. kubectl delete service -l app=guestbook

    The responses should be:

    1. deployment.apps "redis-master" deleted
    2. deployment.apps "redis-slave" deleted
    3. service "redis-master" deleted
    4. service "redis-slave" deleted
    5. deployment.apps "frontend" deleted
    6. service "frontend" deleted
  2. Query the list of Pods to verify that no Pods are running:

    1. kubectl get pods

    The response should be this:

    1. No resources found.

What’s next