Pacman + NGINX + PHP + MongoDB Application
This is an example Kubernetes application that hosts an HTML5 Pacman game with NGINX as the web server and PHP backend to read and write data to a MongoDB database.
Pacman Game Architecture Overview
NGINX + PHP FPM
PHP FPM is used for the PHP API to receive read and write requests from clients and perform database operations. The PHP MongoDB driver extension contains the minimal API for core driver functionality. In addition, the PHP library for MongoDB provides the higher level APIs. Both are needed.
MongoDB is used as the backend database to store the Pacman game's high score user data.
Clone This Repository
Clone this repo which contains the Kubernetes configs and Dockerfile resources.
git clone https://github.com/font/k8s-example-apps.git cd k8s-example-apps/pacman-nginx-app
Create Application Container Image
The Dockerfile performs the following steps:
- It is based on Ubuntu 16.04 Xenial and installs NGINX, PHP FPM, PHP MongoDB, and Composer (for the PHP MongoDB lib).
- It then creates a basic NGINX configuration that includes enabling PHP FastCGI.
- Clones the Pacman game into the configured root directory of the NGINX web server.
- Replaces the host 'localhost' with 'mongo' in the PHP backend API to match the host DNS given to the MongoDB Kubernetes service.
- Exposes port 80 for the web server.
php7.0-fpm, and runs a forever command to keep the container running.
To build the image run:
cd docker docker build -t <user>/pacman-nginx-app . cd ..
You can test the image by running:
docker run -p 8000:80 <user>/pacman-nginx-app
And going to
http://localhost:8000/ to see if you get the Pacman game.
Install Google Cloud SDK
To test on a Kubernetes cluster, make sure you have the Google Cloud SDK installed. You can quickly do this on Linux/Mac with:
curl https://sdk.cloud.google.com | bash
Once installed, log in and update it:
gcloud auth login gcloud components update
Create a Google Cloud Project
You can either create a new project or use an existing one. See the Google Cloud Docs for more details.
Push container to Google Cloud Container Registry
You'll want to tag your previously created Docker image to use the Google Cloud Container Registry URL and then push it:
docker tag <user>/pacman-nginx-app gcr.io/YOUR_PROJECT_ID/pacman-nginx-app gcloud docker push gcr.io/YOUR_PROJECT_ID/pacman-nginx-app
Once you've pushed your image, you'll need to update the Kubernetes resources to point to your image before you continue with the rest of the guides.
sed -i 's/ifontlabs/YOUR_PROJECT_ID/' controllers/web-controller.yaml replicasets/pacman-replicaset.yaml
Set Up Kubernetes Cluster(s)
You'll need to create 1 or at least 3 Kubernetes cluster(s) depending on whether you want to try out the Pac-Man app on 1 cluster, or try it out on a federated cluster. Below are links to the two choices that will guide you through it: