Google Cloud Endpoints

From XennisWiki
Jump to: navigation, search

Service of the Google Cloud Platform to deploy and manage APIs.

Usage

(1) List, (2) enable, (3) deploy and (4) delete APIs

gcloud endpoints services list
gcloud endpoints services deploy my_descriptor.pb my_api.yaml
gcloud endpoints services delete <id>

Note: Not get confused with

gcloud services list
gcloud services enable <id>
gcloud endpoints disable <id>

List the configuration of a API to get all details.

gcloud endpoints services configs list --service <name>

Transcoding HTTP/JSON to gRPC

Further information: Google Cloud documentation page

Adjust proto file

Import the Google API annotations

Annotate the Google APIs import

import "google/api/annotations.proto";

Therefor download the Google APIs and set an environment variable to the path

export GOOGLEAPIS_DIR=~/path/to/googleapis
git clone https://github.com/googleapis/googleapis $GOOGLEAPIS_DIR

In the compile command use --proto_path=$(GOOGLEAPIS_DIR) to include the Google APIs directory.

Use the API HTTP annotation

In the my_api.proto file use the API HTTP annotation

[...]

import "google/api/annotations.proto";

[...]

service TelegramBotApi {
    rpc ReceiveUpdates (string) returns (int32) {
        option (google.api.http) = {
            post: "/updatesHook"
        };
    }
}

Configuration of the API

Exemplary api_config.yaml

type: google.api.Service
config_version: 3

# Name of the service configuration.
name: telegrambotapi.endpoints.<project-id>.cloud.goog

# API title to appear in the user interface (Google Cloud Console)
title: Telegram Bot API
apis:
- name: telegrambotapi.TelegramBotApi

# API usage restrictions.
usage:
  rules:
  - selector: "*"
    allow_unregistered_calls: true

Compile and deploy

Compile proto file and create the descriptor

protoc \
	--include_imports \
	--include_source_info \
	--proto_path=$(GOOGLEAPIS_DIR) \
	--proto_path=. \
	--descriptor_set_out=api_descriptor.pb \
	--go_out=plugins=grpc:. \
	my_api.proto

Deploy the proto descriptor and the configuration to Google Cloud Platform

gcloud endpoints services deploy api_descriptor.pb api_config.yaml

Configure Kubernetes service and deployment

In the service a TCP port is defined (here 443)

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
    - port: 443
      protocol: TCP
  selector:
    app: my-app
  type: LoadBalancer

In the deployment next to the container, that uses gRPC (here on port 50051) a Endpoints container is defined. In the example that container uses SSL. See Service with self-signed certificate for more information about how to create a service/deployment with a self-signed certificate.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: my-deployment
spec:
  template:
    metadata:
      labels:
        app: my-appy
    spec:
      containers:
      - name: my-app
        ports:
        - name: rpc-port
          containerPort: 50051
        [...]
      - name: esp
        image: gcr.io/endpoints-release/endpoints-runtime:1
        args: [
          "--ssl_port", "443",
          "--backend", "grpc://127.0.0.1:50051",
          "--service", "telegrambotapi.endpoints.<project-id>.cloud.goog",
          "--rollout_strategy", "managed",
        ]
        ports:
          - containerPort: 443
        volumeMounts:
          - mountPath: /etc/nginx/ssl
            name: nginx-ssl
            readOnly: true
      volumes:
      - name: nginx-ssl
        secret:
          secretName: nginx-ssl

After deploying the service and the deployment, get the IP of the service: kubectl get service <name>. Next use HTTP/JSON to communicate with the endpoint: curl -v <ip>/updatesHook.

Extensible Service Proxy (ESP)

Startup options

Further information: ESP Startup Options

SSL and gRPC example for Kubernetes:

image: gcr.io/endpoints-release/endpoints-runtime:1
args: [
   "--ssl_port", "443",
   "--backend", "grpc://127.0.0.1:50051",
   "--service", "<my-api>.endpoints.<project-id>.cloud.goog",
   "--rollout_strategy", "managed",
]

HTTP / gRPC

  • HTTP on port 8080: --backend 127.0.0.1:8080
  • gRPC on port 50051: --backend grpc://127.0.0.1:50051

SSL

For SSL set the --ssl_port 443 to a port, here 443

Tips

Cloud Endpoints Portal (Swagger-UI)

Create a Swagger-UI like documentation of the API can be archived by Cloud Endpoints Portal, see Getting Started Using Cloud Endpoints Portal

See also

External links