Use the following high-level task list as you work through the tutorial. All tasks are required to successfully send requests to the API.
- Set up a Cloud Platform project, and download required software. See Before you begin.
- Copy and configure files from the bookstore-grpc sample. See Configuring Endpoints.
- Deploy the Endpoints configuration to create a Cloud Endpoints service. See Deploying the Endpoints configuration.
- Create a backend to serve the API and deploy the API. See Deploying the API backend.
- Get the service's external IP address: See Getting the service's external IP address.
- Send a request to the API. See Sending a request to the API.
- Avoid incurring charges to your Google Cloud Platform account. See Clean up.
Before you begin
Sign in to your Google account.
If you don't already have one, sign up for a new account.
- Select or create a Cloud Platform project.
- Enable billing for your project.
- Note the project ID, because you'll need it later.
- Install and initialize the Cloud SDK.
- Update the Cloud SDK and install the Endpoints components.
gcloud components update
- Make sure that Cloud SDK (
gcloud) is authorized to access your data and services on Google Cloud Platform:
gcloud auth loginA new browser tab opens and you are prompted to choose an account.
- Set the default project to your project ID.
gcloud config set project [YOUR-PROJECT-ID]
[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
gcloudto manage them, see Managing Cloud SDK Configurations.
gcloud components install kubectl
- Acquire new user credentials to use for Application Default Credentials.
The user credentials are needed to authorize
gcloud auth application-default loginA new browser tab opens and you are prompted to choose an account.
protoc, the protocol buffers compiler.
The bookstore-grpc sample contains the files that you need to copy locally and configure.
- Create a self-contained protobuf descriptor file from your service
- Save a copy of
bookstore.protofrom our example repo. This file defines the Bookstore service's API.
- Create the descriptor file using the protocol buffers compiler,
protoc --include_imports --include_source_info bookstore.proto --descriptor_set_out out.pb
You may need to provide additional flags (
-I) for the root directories for the imported
- Save a copy of
- Create a gRPC API Configuration YAML file:
- Save a copy of
api_config.yaml. This file defines the gRPC API configuration for the Bookstore service.
- Replace <MY_PROJECT_ID> in your
api_config.yamlfile with your Cloud Platform Console project ID.
Note that the
apis: namevalue in this file exactly matches the fully-qualified API name from the
.protofile; otherwise deployment won't work. The
Bookstoreservice is defined in
endpoints.examples.bookstore. Its fully-qualified API name is
endpoints.examples.bookstore.Bookstore, just as it appears in
apis: - name: endpoints.examples.bookstore.Bookstore
- Save a copy of
Deploying the Endpoints Configuration
To deploy the Endpoints configuration, you use Google Service Management, an infrastructure service of Google Cloud Platform that manages other APIs and services, including services created using Cloud Endpoints.
- Deploy the proto descriptor file and the configuration file using the
gcloud service-management deploy out.pb api_config.yaml
- Wait for this operation to complete. Once it is done, take a note of the
service name and the service config ID in the command output, you'll need
this when deploying your backend:
Service Configuration [SERVICE_CONFIG_ID] uploaded for service [SERVICE_NAME]
When you copy the service name and service configuration ID, do not include the square brackets.
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 creating a Container Engine cluster to host the API backend and deploying the API.
Creating a container cluster
To create a container cluster for our example:
- Go to the console's container clusters page: Go to the container clusters page.
- Click Create cluster.
- Accept the default settings and click Create. Note the cluster name and zone, as you'll need them later in this tutorial.
Authenticating kubectl to the container cluster
kubectl to create and manager cluster resources, you need to get cluster credentials and make them available to
kubectl. To do this, invoke the following command, replacing
[NAME] with your new cluster name and
[ZONE] with its cluster zone. Do not include the square brackets.
gcloud container clusters get-credentials [NAME] --zone [ZONE]
Deploying the sample API and ESP to the cluster
To deploy our sample gRPC service to the cluster so that clients can use it:
- Save and edit a copy of the Kubernetes configuration file,
SERVICE_CONFIG_IDshown in the snippet below with the values returned when you deployed the API configuration:
spec: containers: - name: esp image: gcr.io/endpoints-release/endpoints-runtime:1 args: [ "-P", "9000", "-s", "SERVICE_NAME", "-v", "SERVICE_CONFIG_ID", "-a", "grpc://127.0.0.1:8000" ] ports: - containerPort: 9000 - name: bookstore image: gcr.io/endpointsv2/python-grpc-bookstore-server:1 ports: - containerPort: 8000
In this configuration file, the following arguments specify how you want to run the Extensible Service Proxy container:
* `-s` (or `--service`): specifies ths name of your Endpoints service * `-v` (or `--version`): specifies the service config ID of the Endpoints service * `-P` (or `--http2_port`): specifies the port that accepts HTTP2 connections * `-a` (or `--backend`): specifies the application backend to which the ESP proxies requests. In this example, the `grpc://` prefix indicates that the backend accepts gRPC traffic.
spec: containers: - name: esp image: gcr.io/endpoints-release/endpoints-runtime:1 args: [ "-P", "9000", "-s", "bookstore.endpoints.example-project.cloud.goog", "-v", "2016-12-14r1", "-a", "grpc://127.0.0.1:8000" ]
- Start the service:
kubectl create -f grpc-bookstore.yaml
Getting the service's external IP address
You'll need the service's external IP address to send requests to the sample API. It can take a few minutes after you start your service in the container before the external IP address is ready.
To view the external IP address:
Invoke the command:
kubectl get service
Note the value for
EXTERNAL-IPand save it into a
SERVER_IPenvironment variable. We’ll use it when sending requests to the sample API.
Sending a request to the API
We use a sample gRPC client written in Python to send requests to the sample API.
Clone the git repo where the gRPC client code is hosted:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Change your working directory:
virtualenv env source env/bin/activate python -m pip install -r requirements.txt
Send a request to the sample API
python bookstore_client.py --host $SERVER_IP --port 80
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.
- Look at the request logs for your API in the Logs Viewer page.
View Endpoints request logs
You just deployed and tested an API in Cloud Endpoints!
To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:
Delete the API:
gcloud service-management delete [SERVICE_NAME]
Replace [SERVICE_NAME] with the name of your API. Do not include the square brackets.
Delete the Kubernetes cluster:
gcloud container clusters delete [NAME] --zone [ZONE]
[ZONE]with the name and zone of your Kubernetes cluster. Do not include the square brackets.