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-tomcatBuild
./mvnw packageCreate a VM
Enable API
gcloud services enable compute.googleapis.comCreate a VM
gcloud compute instances create helloworld \
--scopes=cloud-platformCopy File to VM
gcloud compute scp target/helloworld.jar helloworld:SSH to VM
gcloud compute ssh helloworldInstall OpenJDK in the VM
sudo apt-get update && sudo apt-get install -y openjdk-11-jdkRun in the VM
java -jar helloworld.jarExpose
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
gcloud compute instances add-tags helloworld --tags=webappAdd Firewall Rule
gcloud compute firewall-rules create webapp-rule \
--source-ranges=0.0.0.0/0 \
--target-tags=webapp \
--allow=tcp:8080Connect
Find the external IP address of the Compute Engine VM instance:
gcloud compute instances listYou can now connect to the external IP on port 8080 of the application:
EXTERNAL_IP=$(gcloud compute instances describe helloworld \
--format='value(networkInterfaces.accessConfigs[0].natIP)')
curl http://${EXTERNAL_IP}:8080Getting Started - Container in Compute Engine
Clone
git clone https://github.com/saturnism/jvm-helloworld-by-example
cd jvm-helloworld-by-example/helloworld-springboot-tomcatBuild
./mvnw packageContainerize
Enable API
Enable Container Registry API to be able to push container images to the Container Registry.
gcloud services enable containerregistry.googleapis.comJib
Use Jib to containerize the application:
PROJECT_ID=$(gcloud config get-value project)
./mvnw compile com.google.cloud.tools:jib-maven-plugin:2.4.0:build \
-Dimage=gcr.io/${PROJECT_ID}/helloworldCreate a VM with Container Image
Enable API
gcloud services enable compute.googleapis.comCreate a VM
PROJECT_ID=$(gcloud config get-value project)
gcloud compute instances create-with-container \
helloworld-with-container \
--container-image=gcr.io/${PROJECT_ID}/helloworld \
--scopes=cloud-platformExpose
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:
gcloud compute instances add-tags \
helloworld-with-container --tags=webappAdd Firewall rule:
gcloud compute firewall-rules create webapp-rule \
--source-ranges=0.0.0.0/0 \
--target-tags=webapp \
--allow=tcp:8080Connect
Find the external IP address of the Compute Engine VM instance:
gcloud compute instances listYou can now connect to the external IP on port 8080 of the application:
EXTERNAL_IP=$(gcloud compute instances describe helloworld-with-container \
--format='value(networkInterfaces.accessConfigs[0].natIP)')
curl http://${EXTERNAL_IP}:8080Last updated
Was this helpful?