Spring Code deployment in GCE vs GKE

Warun Kumar
Google Cloud - Community
8 min readMar 15, 2023

Let’s start with a bit of introduction and differences between GCE and GKE.

What is GCE: Google Compute Engine (GCE) is an infrastructure as a service (IaaS) offering.

In GCE we can build high performance , scalable compute nodes to run applications with fault tolerance.

Virtual Machines can be fully customized and pre-installed in GCE according to the application needs.

There are predefined machine types with predefined vCPU and memory disk to deploy applications quickly.

And with custom machine types users can tailor the machine according to their application requirements.

When I say tailor made machine , it means Users can choose OS , storage type and size , CPU , cores and others.

It provides a lot of benefits like on-demand provisioning , an easy integration with other GCP services, full control over the VMs with security , firewall rules , storage , s3 etc.

What is GKE : Google Kubernetes Engine (GKE) is GCP’s Container as a Service (CaaS) offering

Google Kubernetes Engine (GKE) is a managed Kubernetes service for containers and container clusters running on Google Cloud infrastructure. GKE is based on Kubernetes, an open source container management and orchestration platform developed by Google.

Users can easily manage kubernetes clusters without knowing the underlying infrastructure.

It gives a scalable and flexible platform for managing and running containerised workloads and applications.

GKE lets you deploy and manage your applications in a containerised environment.

Containers are a way of packaging your application code and dependencies so that they can be run on any machine. GKE provides a managed environment for running containers, including the Kubernetes cluster and the necessary infrastructure.

To deploy an application to GKE, you first need to create a Kubernetes cluster. Once the cluster is created, you can use the GKE web console or the kubectl command-line tool to deploy your application.

It provides several benefits over managing kubernetes clusters like it reduces operational costs by automating many tasks managing kubernetes clusters for scaling , monitoring etc. .

Also, like GCE it provides security , integration with other GCP services and others.

Some major differences :

We can run the application in both of them but, there are some major differences between GCE and GKE.

GCE VMs are completely managed by the user , once the user has provided an image all updates are managed by users only.

Whereas, in GKE for the master and node versions can be set to upgrade automatically.Users only need to choose which OS they want, the version managed by GKE.

Means if any security patches or updates comes for any node OS, it will get pushed automatically to the cluster (as long as you enable automatic upgrades)

GKE may cost you a little more than a stand-alone GCE VM, but it comes with more automation and management baked in.

I generally prefer GKE to create container clusters, upgrades , or for easy scalability purposes.

In GKE , the autoscaling features of kubernetes engine allows the application to work for peak time when the application demands more resources and also it scales down when there is less traffic.

But in GCE, it enables users to launch VMs on demand. VMs are usually launched from the standard images or custom images created by users and GCE is mainly managed through the CLI or web console.

Lets deep down for the demo , we will deploy a basic spring application in local , GCE and GKE.

Deploy Spring Code locally

Pre-requisites :

  • Basic understanding of Spring Application.
  • Maven version : 3.8.7
  • Java version : 11.0.18
  • Code Editor: Intellij
  • Operating system : Mac pro M1
  1. Let’s write a simple Spring Boot Code (if you have your code already you can use that):
  • Once the blank spring code downloaded , I created 3 major files: Controller class, Service class and Spring Application main class. Below is the code structure.
  • Let’s put some basic code , Controller class:
  • Service Class:
  • Spring Application main class:

Build Spring code :

  • Open terminal
  • Change directory to base directory , go to terminal write cd <base directory of spring code>
  • Build from maven , please run —
mvn clean install

Run spring code locally to test the code:

  • Run command —
mvn spring-boot:run

It will start the application on default port

  • If you set in application.properties — server.port=9999 , the application will start at port — 9999 and you can see logs something like this ,

open url : http://localhost:9999/demo/message

  • It should print “ok google” as per my service class implementation.

Deploy Spring Code in GCE

  • Prerequisites
  • Before deploying to GCE make sure to complete the above steps “Deploy Spring Code locally” .
  • Use the Cloud Console to create or choose a project. Remember the project ID; we use it in this demo.
  • Enable billing for your project.
  • Visit the API Library menu and enable the Compute Engine API to use GCE.
  • Also, install Cloud SDK if you do not already have it. Make sure you initialize the SDK. Use your project’s ID to set the default project for the gcloud command-line tool.
  • And, I am using java 11 , you can choose to use jdk 8 or higher
  • Create a bucket (choose any name , let’s say ‘sample-spring-gce-01’) . Run below command to create :
gsutil mb gs://sample-spring-gce-01

Build the code ,

  • In the project base directory , run “mvn clean install“ . After successful build there is a jar file inside the target folder.
  • Copy the jar file in the bucket created (“sample-spring-gce-01”)
gsutil cp target/SampleSpring-0.0.1-SNAPSHOT.jar gs://sample-spring-gce-01/SampleSpring.jar
  • Create a file called instance-startup.sh in your application’s root directory and copy the following content to it:
# Get the files we need
gsutil cp gs://${BUCKET}/SampleSpring.jar .

# Install dependencies
apt-get update
apt-get -y --force-yes install openjdk-11-jdk

# Make Java 11 default
update-alternatives --set java /usr/lib/jvm/java-11-openjdk-amd64/jre/bin/java

# Start server
java -jar SampleSpring.jar
  • The startup script does the following:
  • Downloads the JAR which is the Spring Boot application, previously built and uploaded to Cloud Storage.
  • Downloads and installs Java 11.
  • Runs the Spring Boot application.
  • Create an instance by running the following command, below command create an instance and deploy the jar as well(it runs start-up script) :
gcloud compute instances create spring-sample-instance \
--image-family debian-10 \
--image-project debian-cloud \
--machine-type g1-small \
--scopes "userinfo-email,cloud-platform" \
--metadata-from-file startup-script=instance-startup.sh \
--metadata BUCKET=sample-spring-gce-01 \
--zone us-east1-b \
--tags http-server
  • Check the logs of deployment.
gcloud compute instances get-serial-port-output spring-sample-instance   --zone us-east1-b
  • From the above logs ,we can observe the application is successfully deployed and running but, now we need to create a firewall rule to allow traffic to your instance (port=9999)
gcloud compute firewall-rules create default-allow-http-9999 \
--allow tcp:9999 \
--source-ranges 0.0.0.0/0 \
--target-tags http-server \
--description "Allow port 9999 access to http-server"
  • Check the external IP for the instance.
gcloud compute instances list
  • If you get the string “ok google” the application is successfully deployed and running in GCE.

Deploy Spring Code in GKE

  • Prerequisites:
  • Before deploying to GKE make sure to complete the above steps “Deploy Spring Code locally” .
  • Use the Cloud Console to create or choose a project. Remember the project ID; we use it in this demo.
  • Enable billing for your project.
  • Visit the API Library menu and enable the Kubernetes Engine API and Container Registry API to use GKE.
  • Also, install Cloud SDK if you do not already have it. Make sure you initialize the SDK. Use your project’s ID to set the default project for the gcloud command-line tool.
  • And, I am using java 11 , you can choose to use jdk 8 or higher .
  • Run below command in root directory of code
export GOOGLE_CLOUD_PROJECT=`gcloud config list --format="value(core.project)"`
./mvnw -DskipTests com.google.cloud.tools:jib-maven-plugin:build -Dimage=gcr.io/$GOOGLE_CLOUD_PROJECT/spring-sample:v1.0
gcloud components install kubectl
  • Now we are ready to create GKE cluster , but before make required api are enabled(compute.googleapis.com , container.googleapis.com), lets create a cluster with 2 n1-standard-1 nodes (below command will take some time)
gcloud container clusters create spring-sample-cluster \
--num-nodes 2 \
--machine-type n1-standard-1 \
--zone us-central1-c
  • Once the above command completed , it looks something like this
  • we can check the same from kubernetes UI page
  • So , our kubernetes cluster is ready and the image is ready . It’s time to deploy image in cluster , run below command in terminal
export GOOGLE_CLOUD_PROJECT=`gcloud config list --format="value(core.project)"`
kubectl create deployment spring-java --image=gcr.io/$GOOGLE_CLOUD_PROJECT/spring-sample:v1.0
  • This will deploy the image in cluster in 1 pods
  • Like GCE, we need to allow the external traffic , run below command for that
kubectl create service loadbalancer spring-java --tcp=9999:9999
  • check the external Ip
Kubectl get services
  • If we get the string “ok google” congrats , we successfully deploy the spring project in GKE.

--

--