Deploying PHP Guestbook application with Redis
This Project shows you how to build and deploy a simple (not production ready), multi-tier web application using Kubernetes and Docker. This example consists of the following components:
- A single-instance Redis to store guestbook entries
- Multiple web frontend instances
Objectives
- Start up a Redis leader.
- Start up two Redis followers.
- 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. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:
Your Kubernetes server must be at or later than version v1.14. To check the version, enter kubectl version
.
Start up the Redis Database
The guestbook application uses Redis to store its data.
- Creating the Redis Deployment
- The manifest file, included below, specifies a Deployment controller that runs a single replica Redis Pod.
Create a file namedredis-leader-deployment.yaml
with below content# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook apiVersion: apps/v1 kind: Deployment metadata: name: redis-leader labels: app: redis role: leader tier: backend spec: replicas: 1 selector: matchLabels: app: redis template: metadata: labels: app: redis role: leader tier: backend spec: containers: - name: leader image: "docker.io/redis:6.0.5" resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 6379
- Apply the Redis Deployment from the
redis-leader-deployment.yaml
file:kubectl apply -f redis-leader-deployment.yaml
- Query the list of Pods to verify that the Redis Pod is running:
kubectl get pods
- Run the following command to view the logs from the Redis leader Pod:
kubectl logs -f deployment/redis-leader
- The manifest file, included below, specifies a Deployment controller that runs a single replica Redis Pod.
- Creating the Redis leader Service
- The guestbook application needs to communicate to the Redis to write its data. You need to apply a Service to proxy the traffic to the Redis Pod. A Service defines a policy to access the Pods.
Create a file namedredis-leader-service.yaml
with below content# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook apiVersion: v1 kind: Service metadata: name: redis-leader labels: app: redis role: leader tier: backend spec: ports: - port: 6379 targetPort: 6379 selector: app: redis role: leader tier: backend
- Apply the Redis Service from the following
redis-leader-service.yaml
file:kubectl apply -f redis-leader-service.yaml
- Query the list of Services to verify that the Redis Service is running:
kubectl get service
- The guestbook application needs to communicate to the Redis to write its data. You need to apply a Service to proxy the traffic to the Redis Pod. A Service defines a policy to access the Pods.
- Set up Redis followers
- Although the Redis leader is a single Pod, you can make it highly available and meet traffic demands by adding a few Redis followers, or replicas.
Create a file namedredis-follower-deployment.yaml
with below content# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook apiVersion: apps/v1 kind: Deployment metadata: name: redis-follower labels: app: redis role: follower tier: backend spec: replicas: 2 selector: matchLabels: app: redis template: metadata: labels: app: redis role: follower tier: backend spec: containers: - name: follower image: gcr.io/google_samples/gb-redis-follower:v2 resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 6379
- Apply the Redis Deployment from the following
redis-follower-deployment.yaml
file:kubectl apply -f
redis-follower-deployment.yaml
- Verify that the two Redis follower replicas are running by querying the list of Pods:
kubectl get pods
- Although the Redis leader is a single Pod, you can make it highly available and meet traffic demands by adding a few Redis followers, or replicas.
- Creating the Redis follower service
- The guestbook application needs to communicate with the Redis followers to read data. To make the Redis followers discoverable, you must set up another Service.
Create a file namedredis-follower-service.yaml
with below content# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook apiVersion: v1 kind: Service metadata: name: redis-follower labels: app: redis role: follower tier: backend spec: ports: # the port that this service should serve on - port: 6379 selector: app: redis role: follower tier: backend
-
Apply the Redis Service from the following
redis-follower-service.yaml
file:kubectl apply -f redis-follower-service.yaml
- Query the list of Services to verify that the Redis Service is running:
kubectl get service
- The guestbook application needs to communicate with the Redis followers to read data. To make the Redis followers discoverable, you must set up another Service.
Set up and Expose the Guestbook Frontend
Now that you have the Redis storage of your guestbook up and running, start the guestbook web servers. Like the Redis followers, the frontend is deployed using a Kubernetes Deployment.
The guestbook app uses a PHP frontend. It is configured to communicate with either the Redis follower or leader Services, depending on whether the request is a read or a write. The frontend exposes a JSON interface, and serves a jQuery-Ajax-based UX.
- Creating the Guestbook Frontend Deployment
- Create a file named
frontend-deployment.yaml
with below content# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook apiVersion: apps/v1 kind: Deployment metadata: name: frontend spec: replicas: 3 selector: matchLabels: app: guestbook tier: frontend template: metadata: labels: app: guestbook tier: frontend spec: containers: - name: php-redis image: gcr.io/google_samples/gb-frontend:v5 env: - name: GET_HOSTS_FROM value: "dns" resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 80
- Apply the frontend Deployment from the
frontend-deployment.yaml
file:kubectl apply -f frontend-deployment.yaml
- Query the list of Pods to verify that the three frontend replicas are running:
kubectl get pods -l app=guestbook -l tier=frontend
- Create a file named
- Creating the Frontend Service
- The Redis Services you applied is only accessible within the Kubernetes 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 Kubernetes cluster. However a Kubernetes user can use kubectl port-forward to access the service even though it uses a ClusterIP.
Create a file namedfrontend-service.yaml
with below content# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook apiVersion: v1 kind: Service metadata: name: frontend labels: app: guestbook tier: frontend spec: # if your cluster supports it, uncomment the following to automatically create # an external load-balanced IP for the frontend service. # type: LoadBalancer #type: LoadBalancer ports: # the port that this service should serve on - port: 80 selector: app: guestbook tier: frontend
- Apply the frontend Service from the
frontend-service.yaml
file:kubectl apply -f frontend-service.yaml
- Query the list of Services to verify that the frontend Service is running:
kubectl get services
- Copy the external IP address, and load the page in your browser to view your guestbook
- The Redis Services you applied is only accessible within the Kubernetes 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 Kubernetes cluster. However a Kubernetes user can use kubectl port-forward to access the service even though it uses a ClusterIP.
Scale the Web Frontend
- Run the following command to scale up the number of frontend Pods:
kubectl scale deployment frontend --replicas=5
- Query the list of Pods to verify the number of frontend Pods running:
kubectl get pods
Tag:Kubernetes