This tutorial shows how to get started with Compute Engine. Follow this tutorial by deploying a Hello World Ruby web app to Compute Engine. For help getting started with App Engine, see the App Engine standard environment.
- Use Cloud Shell to download and deploy a Hello World sample app.
- Deploy a Hello World sample app to a single Compute Engine instance.
This tutorial uses the following billable components of Google Cloud:
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Compute Engine API.
In the Google Cloud Console, open the app in Cloud Shell.
Cloud Shell provides command-line access to your cloud resources directly from the browser. Open Cloud Shell in your browser and click Proceed to download the sample code and change into the app directory.
In Cloud Shell, configure the
gcloudtool to use your new Google Cloud project:
# Configure gcloud for your project gcloud config set project YOUR_PROJECT_ID
Running the app in Cloud Shell
In Cloud Shell, install your app dependencies using
Run the Sinatra app:
bundle exec ruby app.rb -p 8080
In Cloud Shell, click Web preview , and select Preview on port 8080. This opens a new window with your running app.
To stop the local web server, press Control+C.
Deploying to a single instance
This section walks you through running a single instance of your app on Compute Engine.
From Cloud Shell, you can deploy to a single Compute Engine instance virtual machine (VM) which runs your app.
Use a startup script to initialize an instance
You need a way to instruct your instance to download and run your code. An instance can have a startup script that runs whenever the instance is started or restarted.
# Install Stackdriver logging agent curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh bash install-logging-agent.sh # Install dependencies apt-get update && apt-get -y upgrade && apt-get install -y autoconf bison \ build-essential git libssl-dev libyaml-dev libreadline6-dev zlib1g-dev \ libncurses5-dev libffi-dev libgdbm3 libgdbm-dev nginx supervisor # Account to own server process useradd -m -d /home/rubyapp rubyapp # Install Ruby and Bundler mkdir /home/rubyapp/.ruby git clone https://github.com/rbenv/ruby-build.git /home/rubyapp/.ruby-build /home/rubyapp/.ruby-build/bin/ruby-build 2.6.5 /home/rubyapp/.ruby chown -R rubyapp:rubyapp /home/rubyapp cat >/home/rubyapp/.profile << EOF export PATH="/home/rubyapp/.ruby/bin:$PATH" EOF su -l rubyapp -c "gem install bundler" # Fetch source code git clone https://github.com/GoogleCloudPlatform/getting-started-ruby.git /opt/app # Set ownership to newly created account chown -R rubyapp:rubyapp /opt/app # Install ruby dependencies su -l rubyapp -c "cd /opt/app/gce && bundle install" # Disable the default NGINX configuration rm /etc/nginx/sites-enabled/default # Enable our NGINX configuration cp /opt/app/gce/rubyapp.conf /etc/nginx/sites-available/rubyapp.conf ln -s /etc/nginx/sites-available/rubyapp.conf /etc/nginx/sites-enabled/rubyapp.conf # Start NGINX systemctl restart nginx.service # Configure supervisor to run the ruby app cat >/etc/supervisor/conf.d/rubyapp.conf << EOF [program:rubyapp] directory=/opt/app/gce command=bash -lc "bundle exec ruby app.rb" autostart=true autorestart=true user=rubyapp environment=HOME="/home/rubyapp",USER="rubyapp" stdout_logfile=syslog stderr_logfile=syslog EOF supervisorctl reread supervisorctl update # Application should now be running under supervisor
The startup script performs these tasks:
Installs the Cloud Logging agent. The agent automatically collects logs from syslog.
Installs Ruby 2.6.5, Bundler, and their system dependencies.
Clones the app's source code from the Cloud Source Repositories and installs dependencies.
Install and configures Nginx.
Installs Supervisor to run the app as a daemon.
Configures Supervisor to run the app. Supervisor makes sure the app is restarted if it exits unexpectedly or is terminated by an admin or other process. It also sends the app's
stderrto syslog to be collected by the Logging agent.
Create and configure a Compute Engine instance
Create a Compute Engine instance:
MY_INSTANCE_NAME="my-app-instance" ZONE=us-central1-a gcloud compute instances create $MY_INSTANCE_NAME \ --image-family=debian-9 \ --image-project=debian-cloud \ --machine-type=g1-small \ --scopes userinfo-email,cloud-platform \ --metadata-from-file startup-script=startup-script.sh \ --zone $ZONE \ --tags http-server
gcloud compute instances create my-app-instance ^ --image-family=debian-9 ^ --image-project=debian-cloud ^ --machine-type=g1-small ^ --scopes userinfo-email,cloud-platform ^ --metadata-from-file startup-script=gce/startup-script.sh ^ --zone us-central1-a ^ --tags http-server
Replace the zone with a development zone, for example
us-central1-a. For more information on regions and zones, see Geography and regions.
This creates a new instance, allows it to access Google Cloud services, and runs your startup script. The instance name is
Check the progress of the instance creation:
gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
YOUR_ZONEwith the zone you deployed your instance to.
When the startup script is complete, you see the following message:
startup-script: INFO Finished running startup scripts.
Create a firewall rule to allow traffic to your instance:
gcloud compute firewall-rules create default-allow-http-80 \ --allow tcp:80 \ --source-ranges 0.0.0.0/0 \ --target-tags http-server \ --description "Allow port 80 access to http-server"
Get the external IP address of your instance:
gcloud compute instances list
To see your app running, enter this URL in your browser:
YOUR_INSTANCE_IPwith the external IP address of your instance.
Manage and monitor an instance
You can use the Cloud Console to monitor and manage your instance.
- In the Cloud Console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
- To view all of the logs generated by your Compute Engine resources,
go to the Logs viewer page.
Go to Logs viewer
Cloud Logging is automatically configured to gather logs from various common services, including
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete the project
- In the Cloud Console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete the individual resources
gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all gcloud compute firewall-rules delete default-allow-http-80