Play in the Google Cloud Kubernetes cluster
Kubernetes is a container orchestration system. It allows for easy deployment, management and scaling of containerized applications. Its key feature is the ability to create clusters of identical nodes of a single application. This allows for easy scalability even between multiple hosts.
Configuring PlayFramework applications
In order to run your Play server in Kubernetes Cluster you need to containerize it. The instruction on how to create basic Docker image of your application can be found in this article.
Since we will be using Kubernetes Stateless Pods, all the configuration for your application should be stored in
application.conf inside your server’s image (not in volume).
Pushing Docker image to container repository
In order to create a Kubernetes Cluster in Google Cloud you need to upload your image to a container repository on Docker Hub, Google Container Registry or something similar.
After you’ve chosen your Container Repository provider, you need to connect your docker client to the repository (the authentication method may vary) and push your local application image to it, using this command:
docker push REPOSITORY_URL/IMAGE_NAME:TAG
Configuring default settings for gcloud
There are two ways you can configure Google Cloud. First one is by using Cloud Shell which is pre configured with Google Cloud CLI and
kubectl available via Google Console. The second one is by installing Google Cloud CLI and
kubectl on your local machine.
First thing that you need to configure is your project. You have to set its name using the command presented below, where
PROJECT_NAME is the name of your project.
gcloud config set project PROJECT_NAME
Next thing to do is setting the compute zone and region using the commands below. First one sets the zone to
COMPUTE_ZONE. Second one sets the region to
COMPUTE_REGION. You should substitute both of these names with your zone and region.
gcloud config set compute/zone COMPUTE_ZONE
gcloud config set compute/region COMPUTE_REGION
Creating Kubernetes cluster
After configuring your project in gclound, the next step is creating a cluster. For now it will consist of 1 Node. To achieve this, you need to run the command below, where CLUSTER_NAME is your cluster’s name.
gcloud container clusters create CLUSTER_NAME --num-nodes=1
Deploying your application
Now that you created your Kubernetes Cluster, you need to configure
kubectl to a previously user created cluster. To do this, you will run this command:
gcloud container clusters get-credentials CLUSTER_NAME
Next step is creating a deployment. Deployment is a Kubernetes object representing a stateless application consisting of multiple identical Pods. You will create one by running the following command, where
DEPLOYMENT_NAME stands for the name of your deployment (e.g. play-server) and
IMAGE_URL is a url to the repository with your image and its specified tag.
kubectl create deployment DEPLOYMENT_NAME --image=IMAGE_URL
The last step is making your deployment accessible for other users. To achieve this you need to expose your deployment to the internet, by running:
kubectl expose deployment DEPLOYMENT_NAME --type LoadBalancer --port 80 --target-port 9000
This command will expose deployment using Google Cloud Load Balancer on port 80 – default http port. We need to set
target-port, it will route traffic incoming on port 80 to port 9000 of the application – which is a default port for Play applications.
In order to check if your application is running, you should run:
kubectl get pods
You should see one
DEPLOYMENT_NAME Pod running.
To access your application you need its external IP address. You should find it by running:
kubectl get service DEPLOYMENT_NAME
Now you can copy and paste it to the URL bar in your browser. If everything was configured correctly you should see your Play app.
All commands that use
kubectl are general Kubernetes commands and should work with any other Kubernetes providers like AWS, Digital Ocean or locally hosted ones like minikube.
Commands that use
gcloud are specific to Google Cloud.