Getting Started with gRPC on Compute Engine

This page shows you how to deploy a simple example gRPC service with the Google Cloud Endpoints Extensible Server Proxy (ESP) in a Docker container in Google Compute Engine.

This page uses the Python version of the bookstore-grpc sample. See the What's next section for gRPC samples in other languages.

For an overview of Cloud Endpoints, see About Cloud Endpoints and Cloud Endpoints Architecture.

Task List

Use the following high-level task list as you work through the tutorial. All tasks are required to successfully send requests to the API.

  1. Set up a Cloud Platform project, and download required software. See Before you begin.
  2. Create a Compute Engine VM instance. See Creating a Compute Engine instance.
  3. Copy and configure files from the bookstore-grpc sample. See Configuring Endpoints.
  4. Deploy the Endpoints configuration to create a Cloud Endpoints service. See Deploying the Endpoints configuration.
  5. Deploy the API and ESP on the Compute Engine VM. See Deploying the API backend.
  6. Send a request to the API. See Sending a request to the API.
  7. Avoid incurring charges to your Google Cloud Platform account. See Clean up.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Note the project ID, because you'll need it later.
  5. Install and initialize the Cloud SDK.
  6. Update the Cloud SDK and install the Endpoints components:
    gcloud components update
  7. Make sure that Cloud SDK (gcloud) is authorized to access your data and services on Google Cloud Platform:
    gcloud auth login
    A new browser tab opens and you are prompted to choose an account.
  8. Set the default project to your project ID.
    gcloud config set project [YOUR_PROJECT_ID]

    Replace [YOUR_PROJECT_ID] with your project ID. Do not include the square brackets. If you have other Cloud Platform projects, and you want to use gcloud to manage them, see Managing Cloud SDK Configurations.

  9. Follow the steps in the gRPC Python Quickstart to install gRPC and the gRPC tools.

Creating a Compute Engine instance

    To create a Compute Engine instance:

  1. In the GCP Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click Create instance.
  3. In the Firewall section, select Allow HTTP traffic and Allow HTTPS traffic.
  4. Click Create to create the instance.
  5. Screenshot of the VM instance creation window with the required options set

    Allow a short time for the instance to start up. Once ready, it will be listed on the VM Instances page with a green status icon.

  6. Make sure you that you can connect to your VM instance.
    1. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
    2. You can now use the terminal to run Linux commands on your Debian instance.
    3. Enter exit to disconnect from the instance.
  7. Note the instance Name, Zone, and External IP address because you'll need them later.

Configuring Endpoints

Clone the bookstore-grpc sample repository from GitHub.

To configure Endpoints:

  1. Create a self-contained protobuf descriptor file from your service .proto file:
    1. Save a copy of bookstore.proto from the example repo. This file defines the Bookstore service's API.
    2. Create the following directory: mkdir generated_pb2
    3. Create the descriptor file, api_descriptor.pb, using the protoc protocol buffers compiler. Run the following command in the directory where you saved bookstore.proto:
      python -m grpc_tools.protoc \
          --include_imports \
          --include_source_info \
          --proto_path=. \
          --descriptor_set_out=api_descriptor.pb \
          --python_out=generated_pb2 \
          --grpc_python_out=generated_pb2 \
          bookstore.proto
      

      In the above command, --proto_path is set to the current working directory. In your gRPC build environment, if you use a different directory for .proto input files, change --proto_path so the compiler searches the directory where you saved bookstore.proto.

  2. Create a gRPC API Configuration YAML file:
    1. Save a copy of api_config.yaml. This file defines the gRPC API configuration for the Bookstore service.
    2. Replace <MY_PROJECT_ID> in your api_config.yaml file with your GCP project ID. For example:
      #
      # Name of the service configuration.
      #
      name: bookstore.endpoints.example-project-12345.cloud.goog
      

      Note that the apis.name field value in this file exactly matches the fully-qualified API name from the .proto file; otherwise deployment won't work. The Bookstore service is defined in bookstore.proto inside package endpoints.examples.bookstore. Its fully-qualified API name is endpoints.examples.bookstore.Bookstore, just as it appears in api_config.yaml.

      apis:
        - name: endpoints.examples.bookstore.Bookstore
      

See Configuring Endpoints for more information.

Deploying the Endpoints Configuration

To deploy the Endpoints configuration, you use the gcloud endpoints services deploy command. This command uses Service Infrastructure, Google’s foundational services platform, used by Cloud Endpoints and other services to create and manage APIs and services.

  1. Make sure you are in the directory where api_descriptor.pb and api_config.yaml are located.
  2. Deploy the proto descriptor file and the configuration file using the gcloud command-line tool:
    gcloud endpoints services deploy api_descriptor.pb api_config.yaml
    

    As it is creating and configuring the service, Service Management outputs a great deal of information to the terminal. On successful completion, you will see a line like the following that displays the service configuration ID and the service name:

    Service Configuration [2017-02-13r0] uploaded for service [bookstore.endpoints.example-project.cloud.goog]
    

    In the above example, 2017-02-13r0 is the service configuration ID and bookstore.endpoints.example-project.cloud.goog is the service name. The service configuration ID consists of a date stamp followed by a revision number. If you deploy the Endpoints configuration again on the same day, the revision number is incremented in the service configuration ID.

If you get an error message, see Troubleshooting Endpoints Configuration Deployment.

See Deploying the Endpoints Configuration for additional information.

Deploying the API backend

So far you have deployed the API configuration to Service Management, but you have not yet deployed the code that will serve the API backend. This section walks you through getting Docker set up on your VM instance and running the API backend code and the Extensible Service Proxy in a Docker container.

Install Docker on the VM Instance

To install Docker on the VM instance:

  1. Set the zone for your project by invoking the command:
    gcloud config set compute/zone [YOUR_INSTANCE_ZONE]
    

    Replace [YOUR_INSTANCE_ZONE] with the zone where your instance is running. Do not include the square brackets.

  2. Connect to your instance using the following command:
    gcloud compute ssh [INSTANCE_NAME]
    

    Replace [INSTANCE_NAME] with your VM instance name. Do not include the square brackets.

  3. See the Docker documentation to set up the Docker repository. Make sure to follow the steps that match the version and architecture of your VM instance:
    • Jessie or newer
    • x86_64 / amd64

Running the sample API and ESP in a Docker container

To run the sample gRPC service with ESP in a Docker container so that clients can use it:

  1. On the VM instance, create your own container network called esp_net.
    sudo docker network create --driver bridge esp_net
    
  2. Run the sample Bookstore server that serves the sample API:
    sudo docker run \
        --detach \
        --name=bookstore \
        --net=esp_net \
        gcr.io/endpointsv2/python-grpc-bookstore-server:1
    
  3. Run the pre-packaged Extensible Server Proxy Docker Container. In the ESP startup options, replace [SERVICE_NAME] with the name of your service. This is the same name that you configured in the name field in the api_config.yaml file. For example: bookstore.endpoints.example-project-12345.cloud.goog
    sudo docker run \
        --detach \
        --name=esp \
        --publish=80:9000 \
        --net=esp_net \
        gcr.io/endpoints-release/endpoints-runtime:1 \
        --service=[SERVICE_NAME] \
        --rollout_strategy=managed \
        --http2_port=9000 \
        --backend=grpc://bookstore:8000
    

    The --rollout_strategy=managed" option configures ESP to use the latest deployed service configuration. When you specify this option, within a minute after you deploy a new service configuration, ESP detects the change and automatically begins using it. We recommend that you specify this option instead of a specific configuration ID for ESP to use. For mode details on the ESP arguments, see ESP Startup Options.

If you have Transcoding enabled, make sure to configure a port for HTTP1.1 or SSL traffic.

If you get an error message, see Troubleshooting Cloud Endpoints on Compute Engine.

Sending a request to the API

To send requests to the sample API, you can use a sample gRPC client written in Python.

  1. Clone the git repo where the gRPC client code is hosted:

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    
  2. Change your working directory:

    cd python-docs-samples/endpoints/bookstore-grpc/
    
  3. Install dependencies:

    pip install virtualenv
    virtualenv env
    source env/bin/activate
    python -m pip install -r requirements.txt
    
  4. Send a request to the sample API

    python bookstore_client.py --host $SERVER_IP --port 80
    
  5. Look at the activity graphs for your API in the Endpoints page.
    View Endpoints activity graphs
    It may take a few moments for the request to be reflected in the graphs.

  6. Look at the request logs for your API in the Logs Viewer page.
    View Endpoints request logs

If you’re sending the request from the same instance in which the Docker containers are running, you can replace $SERVER_IP with localhost. Otherwise replace $SERVER_IP with the external IP of the instance. The external IP can be found by executing

gcloud compute instances list

If you do not get a successful response, see Troubleshooting Response Errors.

You just deployed and tested an API in Cloud Endpoints!

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Delete the API:
    gcloud endpoints services delete [SERVICE_NAME]
    

    Replace [SERVICE_NAME] with the name of your service.

  2. In the GCP Console, go to the VM Instances page.

    Go to the VM Instances page

  3. Click the checkbox next to the instance you want to delete.
  4. Click the Delete button at the top of the page to delete the instance.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Endpoints with gRPC
Need help? Visit our support page.