Compute Engine

Create a VM then deploy your application to the VM.

Getting Started

Clone

cd $HOME
git clone https://github.com/saturnism/jvm-helloworld-by-example
cd jvm-helloworld-by-example/helloworld-springboot-tomcat

Build

./mvnw package

Create a VM

Enable API

gcloud services enable compute.googleapis.com

Create a VM

gcloud compute instances create helloworld \
  --scopes=cloud-platform

If you want to use a specific distribution, such as Debian 10, you can add additional parameters:

gcloud compute instances create helloworld \
  --image-family debian-10 --image-project debian-cloud

See Compute Engine Machine Types documentation for a list of Machine Types and the associated CPU/Memory resources.

Copy File to VM

If this is your first time connecting to the VM, it will automatically prompt you to generate a new SSH key.

SSH to VM

Install OpenJDK in the VM

Run in the VM

Expose

Firewall

By default, most ports on the Compute Engine are firewalled off. If you want to expose port 8080 in this case, you can first add a tag to the Compute Engine instance, and then add a firewall rule to allow inbound port 8080 traffic for any Compute Engine instance with a certain tag.

From outside of the VM (e.g., your computer, or Cloud Shell):

Add Tag

Add Firewall Rule

Connect

Find the external IP address of the Compute Engine VM instance:

You can now connect to the external IP on port 8080 of the application:

In production environments, you would most likely want to put a Load Balancer in front, either with a Network (L4) Load Balancer, or a HTTP (L7) Load Balancer.

Getting Started - Container in Compute Engine

Clone

Build

Containerize

Enable API

Enable Container Registry API to be able to push container images to the Container Registry.

Jib

Use Jib to containerize the application:

Learn different ways to containerize a Java application in the Container Image section.

Create a VM with Container Image

Enable API

Create a VM

This will automatically create a Container-Optimized VM, and start the container on VM startup.

Expose

Firewall

By default, most ports on a Compute Engine instance are firewalled off. If you want to expose port 8080 in this case, you can first add a tag to the Compute Engine instance, and then add a firewall rule to allow inbound port 8080 traffic for any Compute Engine instance with a certain tag.

Add a tag:

Add Firewall rule:

Connect

Find the external IP address of the Compute Engine VM instance:

You can now connect to the external IP on port 8080 of the application:

In production environments, you would most likely want to put a Load Balancer in front, either with a Network (L4) Load Balancer, or a HTTP (L7) Load Balancer.

To deploy a fleet of VMs, you can use Managed Instance Group to deploy a set of VMs running the same container image.

Last updated

Was this helpful?