Compute Engine
Create a VM then deploy your application to the VM.

Getting Started

Clone

1
cd $HOME
2
git clone https://github.com/saturnism/jvm-helloworld-by-example
3
cd jvm-helloworld-by-example/helloworld-springboot-tomcat
Copied!

Build

1
./mvnw package
Copied!

Create a VM

Enable API

1
gcloud services enable compute.googleapis.com
Copied!

Create a VM

1
gcloud compute instances create helloworld \
2
--scopes=cloud-platform
Copied!
If you want to use a specific distribution, such as Debian 10, you can add additional parameters:
1
gcloud compute instances create helloworld \
2
--image-family debian-10 --image-project debian-cloud
Copied!
See Compute Engine Machine Types documentation for a list of Machine Types and the associated CPU/Memory resources.

Copy File to VM

1
gcloud compute scp target/helloworld.jar helloworld:
Copied!
If this is your first time connecting to the VM, it will automatically prompt you to generate a new SSH key.

SSH to VM

1
gcloud compute ssh helloworld
Copied!

Install OpenJDK in the VM

1
sudo apt-get update && sudo apt-get install -y openjdk-11-jdk
Copied!

Run in the VM

1
java -jar helloworld.jar
Copied!

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

1
gcloud compute instances add-tags helloworld --tags=webapp
Copied!

Add Firewall Rule

1
gcloud compute firewall-rules create webapp-rule \
2
--source-ranges=0.0.0.0/0 \
3
--target-tags=webapp \
4
--allow=tcp:8080
Copied!

Connect

Find the external IP address of the Compute Engine VM instance:
1
gcloud compute instances list
Copied!
You can now connect to the external IP on port 8080 of the application:
1
EXTERNAL_IP=$(gcloud compute instances describe helloworld \
2
--format='value(networkInterfaces.accessConfigs[0].natIP)')
3
4
curl http://${EXTERNAL_IP}:8080
Copied!
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

1
git clone https://github.com/saturnism/jvm-helloworld-by-example
2
cd jvm-helloworld-by-example/helloworld-springboot-tomcat
Copied!

Build

1
./mvnw package
Copied!

Containerize

Enable API

Enable Container Registry API to be able to push container images to the Container Registry.
1
gcloud services enable containerregistry.googleapis.com
Copied!

Jib

Use Jib to containerize the application:
1
PROJECT_ID=$(gcloud config get-value project)
2
3
./mvnw compile com.google.cloud.tools:jib-maven-plugin:2.4.0:build \
4
-Dimage=gcr.io/${PROJECT_ID}/helloworld
Copied!
Learn different ways to containerize a Java application in the Container Image section.

Create a VM with Container Image

Enable API

1
gcloud services enable compute.googleapis.com
Copied!

Create a VM

1
PROJECT_ID=$(gcloud config get-value project)
2
gcloud compute instances create-with-container \
3
helloworld-with-container \
4
--container-image=gcr.io/${PROJECT_ID}/helloworld \
5
--scopes=cloud-platform
Copied!
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:
1
gcloud compute instances add-tags \
2
helloworld-with-container --tags=webapp
Copied!
Add Firewall rule:
1
gcloud compute firewall-rules create webapp-rule \
2
--source-ranges=0.0.0.0/0 \
3
--target-tags=webapp \
4
--allow=tcp:8080
Copied!

Connect

Find the external IP address of the Compute Engine VM instance:
1
gcloud compute instances list
Copied!
You can now connect to the external IP on port 8080 of the application:
1
EXTERNAL_IP=$(gcloud compute instances describe helloworld-with-container \
2
--format='value(networkInterfaces.accessConfigs[0].natIP)')
3
curl http://${EXTERNAL_IP}:8080
Copied!
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 modified 1yr ago