Deploy a PlayFramework App that uses Redis onto Google Compute Engine Instance
In this article, we will deploy a simple app built with PlayFramework it has just three routes, it uses Redis an in-memory data structure store, used as a database, cache and message broker. Will deploy it onto a Google Cloud Compute instance tagged with a firewall rule that allows ingress traffic to port 9000 which is the default port for a play server.
Lets first create a Compute Engine Instance, when on GCP console select a project you wish to use or can create a new project. Then on the navigation menu select compute Engine, then create Instance. Give it a name of your choice, will leave the default Zone, then for the boot disk will select Ubuntu 16.04, then for firewall rule: select an option for Allow HTTP traffic, save the a few seconds the VM will be created with an External IP which will append the port(9000) for play to be able to access the app.
yur VM is now running, SSH into it and will need to install the following: Scala language, sbt, Redis. Make sure all are installed, for Redis will need to keep the server running using:
redis-cli&
Then will need to create a firewall rule. On the navigation menu, select Firewall Rules, give it a name of your choice for, source IP put 0.0.0.0/0 which allows incoming traffic from any network, for protocols and ports select tcp then fill in port 9000.
Lets now prepare our code…
Hope your PlayApplication run well and ready for deployment. Run
sbt stage
to build your app for production, as the code is running the target directory is recreated containing the other directories: universal > stage > ( lib, bin, config ). In the lib & config is where the file to deploy
In the root folder, we create file and name it: deploy , this is where we place just simple lines of code that defines the path of the code to deploy and path of the VM on google cloud.
we use gcloud compute scp command: gcloud compute scp — copy files to and from Google Compute Engine virtual machines via scp
gcloud compute scp [directory to Local files to deploy] [path to your cloud VM]
Mine looks like this:
gcloud compute scp --recurse /home/trevor/Work/my-app/target/universal/stage/lib/[file-to-deploy]* root@my-vm:/home/trevor/lib
In my command will deploy all * files from the lib directory , then to root referring to my GCP account, then my VM name / directory where I would like to push the files to. Remember to create directories in your VM instance. In the VM console use pwd command to get the path to your vm.
Lets now deploy…
In your IDE run command:
./deploy
sbt will first compile the code, you will see a success message, then files will be pushed to cloud with 100% output message. Upon checking in the directoris created in the VM you will see the files.
Remember we left the redis-cli running our app should be up and running. or you can re run the redis-cli& to be sure.