2017-06-23 19:23:36 +00:00
---
2020-08-03 19:50:11 +00:00
title: "Example: Deploying PHP Guestbook application with MongoDB"
2018-02-18 20:13:37 +00:00
reviewers:
2017-08-19 00:19:11 +00:00
- ahmetb
2020-05-30 19:10:23 +00:00
content_type: tutorial
2018-05-15 22:29:27 +00:00
weight: 20
2019-02-21 16:24:20 +00:00
card:
name: tutorials
weight: 30
2020-08-03 19:50:11 +00:00
title: "Stateless Example: PHP Guestbook with MongoDB"
min-kubernetes-server-version: v1.14
2017-06-23 19:23:36 +00:00
---
2020-05-30 19:10:23 +00:00
<!-- overview -->
2020-08-03 19:50:11 +00:00
This tutorial shows you how to build and deploy a simple _(not production ready)_ , 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
2020-08-03 19:50:11 +00:00
* A single-instance [MongoDB ](https://www.mongodb.com/ ) to store guestbook entries
2017-08-07 22:56:20 +00:00
* Multiple web frontend instances
2017-06-23 19:23:36 +00:00
2020-05-30 19:10:23 +00:00
## {{% heading "objectives" %}}
2020-08-03 19:50:11 +00:00
* Start up a Mongo database.
2017-08-07 22:56:20 +00:00
* Start up the guestbook frontend.
* Expose and view the Frontend Service.
* Clean up.
2017-06-23 19:23:36 +00:00
2020-05-30 19:10:23 +00:00
## {{% heading "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
2020-05-30 19:10:23 +00:00
<!-- lessoncontent -->
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
## Start up the Mongo Database
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
The guestbook application uses MongoDB to store its data.
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
### Creating the Mongo Deployment
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
The manifest file, included below, specifies a Deployment controller that runs a single replica MongoDB Pod.
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
{{< codenew file = "application/guestbook/mongo-deployment.yaml" > }}
2018-07-02 20:06:18 +00:00
2017-08-07 22:56:20 +00:00
1. Launch a terminal window in the directory you downloaded the manifest files.
2020-08-03 19:50:11 +00:00
1. Apply the MongoDB Deployment from the `mongo-deployment.yaml` file:
2018-07-02 20:06:18 +00:00
2021-04-01 03:42:41 +00:00
<!-- -
for local testing of the content via relative file path
kubectl apply -f ./content/en/examples/application/guestbook/mongo-deployment.yaml
-->
2018-07-17 06:00:38 +00:00
```shell
2020-08-03 19:50:11 +00:00
kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-deployment.yaml
2018-07-02 20:06:18 +00:00
```
2020-08-03 19:50:11 +00:00
1. Query the list of Pods to verify that the MongoDB Pod is running:
2018-07-02 20:06:18 +00:00
```shell
kubectl get pods
```
The response should be similar to this:
```shell
NAME READY STATUS RESTARTS AGE
2020-08-03 19:50:11 +00:00
mongo-5cfd459dd4-lrcjb 1/1 Running 0 28s
2018-07-02 20:06:18 +00:00
```
2020-08-03 19:50:11 +00:00
1. Run the following command to view the logs from the MongoDB Deployment:
2018-07-02 20:06:18 +00:00
```shell
2020-08-03 19:50:11 +00:00
kubectl logs -f deployment/mongo
2018-07-02 20:06:18 +00:00
```
2020-08-03 19:50:11 +00:00
### Creating the MongoDB Service
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
The guestbook application needs to communicate to the MongoDB to write its data. You need to apply a [Service ](/docs/concepts/services-networking/service/ ) to proxy the traffic to the MongoDB Pod. A Service defines a policy to access the Pods.
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
{{< codenew file = "application/guestbook/mongo-service.yaml" > }}
2018-07-02 20:06:18 +00:00
2020-08-03 19:50:11 +00:00
1. Apply the MongoDB Service from the following `mongo-service.yaml` file:
2017-06-23 19:23:36 +00:00
2021-04-01 03:42:41 +00:00
<!-- -
for local testing of the content via relative file path
kubectl apply -f ./content/en/examples/application/guestbook/mongo-service.yaml
-->
2018-07-02 20:06:18 +00:00
```shell
2020-08-03 19:50:11 +00:00
kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-service.yaml
2018-07-02 20:06:18 +00:00
```
2020-08-03 19:50:11 +00:00
1. Query the list of Services to verify that the MongoDB 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
2021-03-11 16:22:49 +00:00
mongo ClusterIP 10.0.0.151 < none > 27017/TCP 8s
2018-07-02 20:06:18 +00:00
```
{{< note > }}
2020-08-03 19:50:11 +00:00
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.
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
## Set up and Expose the Guestbook Frontend
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
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.
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
2021-04-01 03:42:41 +00:00
<!-- -
for local testing of the content via relative file path
kubectl apply -f ./content/en/examples/application/guestbook/frontend-deployment.yaml
-->
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
2021-02-18 16:13:01 +00:00
kubectl get pods -l app.kubernetes.io/name=guestbook -l app.kubernetes.io/component=frontend
2018-07-02 20:06:18 +00:00
```
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
2021-02-27 13:24:45 +00:00
The `mongo` Services you applied is only accessible within the Kubernetes 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
2020-08-03 19:50:11 +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 Kubernetes cluster. However a Kubernetes user you can use `kubectl port-forward` to access the service even though it uses a `ClusterIP` .
2017-08-07 22:56:20 +00:00
2018-05-05 16:00:51 +00:00
{{< note > }}
2021-02-04 21:41:29 +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, 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
2021-04-01 03:42:41 +00:00
<!-- -
for local testing of the content via relative file path
kubectl apply -f ./content/en/examples/application/guestbook/frontend-service.yaml
-->
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
2019-04-30 19:38:56 +00:00
kubectl get services
2018-07-02 20:06:18 +00:00
```
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
2021-04-01 03:42:41 +00:00
frontend ClusterIP 10.0.0.112 < none > 80/TCP 6s
2018-08-16 23:30:08 +00:00
kubernetes ClusterIP 10.0.0.1 < none > 443/TCP 4m
2020-08-03 19:50:11 +00:00
mongo ClusterIP 10.0.0.151 < none > 6379/TCP 2m
2018-07-02 20:06:18 +00:00
```
2017-08-07 22:56:20 +00:00
2020-08-03 19:50:11 +00:00
### Viewing the Frontend Service via `kubectl port-forward`
2017-08-07 22:56:20 +00:00
2020-08-03 19:50:11 +00:00
1. Run the following command to forward port `8080` on your local machine to port `80` on the service.
2017-08-07 22:56:20 +00:00
2018-07-02 20:06:18 +00:00
```shell
2020-08-03 19:50:11 +00:00
kubectl port-forward svc/frontend 8080:80
2018-07-02 20:06:18 +00:00
```
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
```
2020-08-03 19:50:11 +00:00
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
2018-07-02 20:06:18 +00:00
```
2017-06-23 19:23:36 +00:00
2020-08-03 19:50:11 +00:00
1. load the page [http://localhost:8080 ](http://localhost:8080 ) 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
```
2021-04-01 03:42:41 +00:00
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend LoadBalancer 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
2019-04-30 19:38:56 +00:00
## Scale the Web Frontend
2017-06-23 19:23:36 +00:00
2021-02-03 19:28:28 +00:00
You can scale up or down as needed 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
2019-04-30 19:38:56 +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
2021-04-01 03:42:41 +00:00
mongo-1068406935-3lswp 1/1 Running 0 56m
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. 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
2021-04-01 03:42:41 +00:00
mongo-1068406935-3lswp 1/1 Running 0 1h
2018-07-02 20:06:18 +00:00
```
2019-04-30 19:38:56 +00:00
2017-06-23 19:23:36 +00:00
2020-05-30 19:10:23 +00:00
## {{% heading "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
2020-08-03 19:50:11 +00:00
kubectl delete deployment -l app.kubernetes.io/name=mongo
kubectl delete service -l app.kubernetes.io/name=mongo
kubectl delete deployment -l app.kubernetes.io/name=guestbook
kubectl delete service -l app.kubernetes.io/name=guestbook
2018-07-02 20:06:18 +00:00
```
The responses should be:
```
2020-08-03 19:50:11 +00:00
deployment.apps "mongo" deleted
service "mongo" deleted
deployment.apps "frontend" deleted
2018-07-02 20:06:18 +00:00
service "frontend" deleted
```
2019-04-30 19:38:56 +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
```
2019-04-30 19:38:56 +00:00
The response should be this:
2018-07-02 20:06:18 +00:00
```
No resources found.
```
2017-06-23 19:23:36 +00:00
2020-05-30 19:10:23 +00:00
## {{% heading "whatsnext" %}}
2017-08-19 00:19:11 +00:00
* Complete the [Kubernetes Basics ](/docs/tutorials/kubernetes-basics/ ) Interactive Tutorials
2019-04-30 19:38:56 +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 )