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
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:
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.
apiVersion:apps/v1# for versions before 1.9.0 use apps/v1beta2kind:Deploymentmetadata:name:redis-masterlabels:app:redisspec:selector:matchLabels:app:redisrole:mastertier:backendreplicas:1template:metadata:labels:app:redisrole:mastertier:backendspec:containers:-name:masterimage:k8s.gcr.io/redis:e2e# or just image: redisresources:requests:cpu:100mmemory:100Miports:-containerPort:6379
Launch a terminal window in the directory you downloaded the manifest files.
Apply the Redis Master Deployment from the
redis-master-deployment.yaml
file:
Query the list of Pods to verify that the Redis Master Pod is running:
kubectl get pods
The response should be similar to this:
NAME READY STATUS RESTARTS AGE
redis-master-1068406935-3lswp 1/1 Running 0 28s
Run the following command to view the logs from the Redis Master Pod:
kubectl logs -f POD-NAME
Note:
Replace POD-NAME with the name of your Pod.
Creating the Redis Master Service
The guestbook applications 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.
Query the list of Services to verify that the Redis Master Service is running:
kubectl get service
The response should be similar to this:
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
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 are running, it would scale down until two replicas are running.
apiVersion:apps/v1# for versions before 1.9.0 use apps/v1beta2kind:Deploymentmetadata:name:redis-slavelabels:app:redisspec:selector:matchLabels:app:redisrole:slavetier:backendreplicas:2template:metadata:labels:app:redisrole:slavetier:backendspec:containers:-name:slaveimage:gcr.io/google_samples/gb-redisslave:v3resources:requests:cpu:100mmemory:100Mi-name:GET_HOSTS_FROMvalue:dns# Using `GET_HOSTS_FROM=dns` requires your cluster to# provide a dns service. As of Kubernetes 1.3, DNS is a built-in# service launched automatically. However, if the cluster you are using# does not have a built-in DNS service, you can instead# access an environment variable to find the master# service's host. To do so, comment out the 'value: dns' line above, and# uncomment the line below:# value: envports:-containerPort:6379
Apply the Redis Slave Deployment from the
redis-slave-deployment.yaml
file:
Query the list of Pods to verify that the Redis Slave Pods are running:
kubectl get pods
The response should be similar to this:
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
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.
Query the list of Services to verify that the Redis slave service is running:
kubectl get services
The response should be similar to this:
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
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.
apiVersion:apps/v1# for versions before 1.9.0 use apps/v1beta2kind:Deploymentmetadata:name:frontendlabels:app:guestbookspec:selector:matchLabels:app:guestbooktier:frontendreplicas:3template:metadata:labels:app:guestbooktier:frontendspec:containers:-name:php-redisimage:gcr.io/google-samples/gb-frontend:v4resources:requests:cpu:100mmemory:100Mi-name:GET_HOSTS_FROMvalue:dns# Using `GET_HOSTS_FROM=dns` requires your cluster to# provide a dns service. As of Kubernetes 1.3, DNS is a built-in# service launched automatically. However, if the cluster you are using# does not have a built-in DNS service, you can instead# access an environment variable to find the master# service's host. To do so, comment out the 'value: dns' line above, and# uncomment the line below:# value: envports:-containerPort:80
Apply the frontend Deployment from the
frontend-deployment.yaml
file:
Query the list of Pods to verify that the three frontend replicas are running:
kubectl get pods -l app=guestbook -l tier=frontend
The response should be similar to this:
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
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
.
apiVersion:v1kind:Servicemetadata:name:frontendlabels:app:guestbooktier:frontendspec:# comment or delete the following line if you want to use a LoadBalancertype:NodePort# if your cluster supports it, uncomment the following to automatically create# an external load-balanced IP for the frontend service.# type: LoadBalancerports:-port:80selector:app:guestbooktier:frontend
Apply the frontend Service from the
frontend-service.yaml
file: