Example: Deploying PHP Guestbook application with MongoDB

This tutorial 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 MongoDB to store guestbook entries
  • Multiple web frontend instances

Objectives

  • Start up a Mongo database.
  • 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:

Your Kubernetes server must be at or later than version v1.14. To check the version, enter kubectl version.

Start up the Mongo Database

The guestbook application uses MongoDB to store its data.

Creating the Mongo Deployment

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

application/guestbook/mongo-deployment.yaml Example: Deploying PHP Guestbook application with MongoDB - 图1

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: mongo
  5. labels:
  6. app.kubernetes.io/name: mongo
  7. app.kubernetes.io/component: backend
  8. spec:
  9. selector:
  10. matchLabels:
  11. app.kubernetes.io/name: mongo
  12. app.kubernetes.io/component: backend
  13. replicas: 1
  14. template:
  15. metadata:
  16. labels:
  17. app.kubernetes.io/name: mongo
  18. app.kubernetes.io/component: backend
  19. spec:
  20. containers:
  21. - name: mongo
  22. image: mongo:4.2
  23. args:
  24. - --bind_ip
  25. - 0.0.0.0
  26. resources:
  27. requests:
  28. cpu: 100m
  29. memory: 100Mi
  30. ports:
  31. - containerPort: 27017
  1. Launch a terminal window in the directory you downloaded the manifest files.

  2. Apply the MongoDB Deployment from the mongo-deployment.yaml file:

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

    1. kubectl get pods

    The response should be similar to this:

    1. NAME READY STATUS RESTARTS AGE
    2. mongo-5cfd459dd4-lrcjb 1/1 Running 0 28s
  4. Run the following command to view the logs from the MongoDB Deployment:

    1. kubectl logs -f deployment/mongo

Creating the MongoDB Service

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

application/guestbook/mongo-service.yaml Example: Deploying PHP Guestbook application with MongoDB - 图2

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: mongo
  5. labels:
  6. app.kubernetes.io/name: mongo
  7. app.kubernetes.io/component: backend
  8. spec:
  9. ports:
  10. - port: 27017
  11. targetPort: 27017
  12. selector:
  13. app.kubernetes.io/name: mongo
  14. app.kubernetes.io/component: backend
  1. Apply the MongoDB Service from the following mongo-service.yaml file:

    1. kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-service.yaml
  2. Query the list of Services to verify that the MongoDB 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. mongo ClusterIP 10.0.0.151 <none> 27017/TCP 8s

Note: This manifest file creates a Service named mongo with a set of labels that match the labels previously defined, so the Service routes network traffic to the MongoDB Pod.

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 mongo Service to store Guestbook entries.

Creating the Guestbook Frontend Deployment

application/guestbook/frontend-deployment.yaml Example: Deploying PHP Guestbook application with MongoDB - 图3

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: frontend
  5. labels:
  6. app.kubernetes.io/name: guestbook
  7. app.kubernetes.io/component: frontend
  8. spec:
  9. selector:
  10. matchLabels:
  11. app.kubernetes.io/name: guestbook
  12. app.kubernetes.io/component: frontend
  13. replicas: 3
  14. template:
  15. metadata:
  16. labels:
  17. app.kubernetes.io/name: guestbook
  18. app.kubernetes.io/component: frontend
  19. spec:
  20. containers:
  21. - name: guestbook
  22. image: paulczar/gb-frontend:v5
  23. # image: gcr.io/google-samples/gb-frontend:v4
  24. resources:
  25. requests:
  26. cpu: 100m
  27. memory: 100Mi
  28. env:
  29. - name: GET_HOSTS_FROM
  30. value: dns
  31. ports:
  32. - 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.kubernetes.io/name=guestbook -l app.kubernetes.io/component=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 mongo 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 you can use kubectl port-forward to access the service even though it uses a ClusterIP.

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, uncomment type: LoadBalancer.

application/guestbook/frontend-service.yaml Example: Deploying PHP Guestbook application with MongoDB - 图4

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: frontend
  5. labels:
  6. app.kubernetes.io/name: guestbook
  7. app.kubernetes.io/component: frontend
  8. spec:
  9. # if your cluster supports it, uncomment the following to automatically create
  10. # an external load-balanced IP for the frontend service.
  11. # type: LoadBalancer
  12. ports:
  13. - port: 80
  14. selector:
  15. app.kubernetes.io/name: guestbook
  16. app.kubernetes.io/component: 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 ClusterIP 10.0.0.112 <none> 80/TCP 6s
    3. kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 4m
    4. mongo ClusterIP 10.0.0.151 <none> 6379/TCP 2m

Viewing the Frontend Service via kubectl port-forward

  1. Run the following command to forward port 8080 on your local machine to port 80 on the service.

    1. kubectl port-forward svc/frontend 8080:80

    The response should be similar to this:

    1. Forwarding from 127.0.0.1:8080 -> 80
    2. Forwarding from [::1]:8080 -> 80
  2. load the page http://localhost:8080 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 LoadBalancer 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

You can scale up or down as needed 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. mongo-1068406935-3lswp 1/1 Running 0 56m
  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. mongo-1068406935-3lswp 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.kubernetes.io/name=mongo
    2. kubectl delete service -l app.kubernetes.io/name=mongo
    3. kubectl delete deployment -l app.kubernetes.io/name=guestbook
    4. kubectl delete service -l app.kubernetes.io/name=guestbook

    The responses should be:

    1. deployment.apps "mongo" deleted
    2. service "mongo" deleted
    3. deployment.apps "frontend" deleted
    4. 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