Quickstart for Endpoints on Container Engine

This page shows you how to configure, deploy, and send requests against a sample API running on an instance in Google Container Engine.

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 Cloud Platform project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Note the project ID, because you'll need it later.
  5. Install cURL for testing purposes.
  6. Download the Google Cloud SDK.
  7. Use the gcloud command-line tool to install the kubectl command-line tool:
    gcloud components install kubectl
  8. Update the Cloud SDK, install the Endpoints components, authenticate and set the default project to your project ID by invoking the following commands:
    gcloud components update
    gcloud auth login
    gcloud auth application-default login
    gcloud config set project [YOUR_PROJECT_ID]
  9. Note: gcloud auth application-default login is run to authorize kubectl.

Getting the sample code

Java

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/java-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd java-docs-samples/endpoints/getting-started
Python

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/python-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd python-docs-samples/endpoints/getting-started
Go

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    go get -u -d github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
  2. Change to the directory that contains the sample code:
    cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
PHP

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/php-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd php-docs-samples/endpoints/getting-started
Ruby

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd ruby-docs-samples/endpoints/getting-started
NodeJS

To clone or download the sample API:

  1. Clone the sample app repository to your local machine:
    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:
    cd nodejs-docs-samples/endpoints/getting-started

Configuring Endpoints

Open the openapi.yaml configuration file and replace YOUR-PROJECT-ID with your own project ID.

Note: The configuration sample displays the lines that need to be edited. To run Cloud Endpoints, the complete configuration file is required.

Deploying the API configuration

To deploy the sample API configuration to Service Management:

  1. Make sure you are in the java-docs-samples/endpoints/getting-started directory.

  2. Invoke the following command:

    gcloud service-management deploy openapi.yaml
    

    This will create a new Cloud Endpoints service with the name echo-api.endpoints.YOUR-PROJECT_ID.cloud.goog if it does not exist, and then update that service's configuration to your Open API specification.

    The output of this gcloud command will contain the Service Configuration identifier, similar to [2016-12-14r1] and the service name. Note the identifier and service name as they will be used as SERVICE_CONFIG_ID and SERVICE_NAME respectively later.

    You can find the service configuration ID later by invoking the command gcloud service-management configs list --service=echo-api.endpoints.[PROJECT-ID].cloud.goog, replacing [PROJECT-ID] with your project ID.

  3. Make a note of the service name and the service configuration because you'll need them later when you configure the container cluster for the API.

Creating a container cluster

To create a container cluster for the sample API:

  1. Visit the Container clusters page.
    Create a Container cluster

  2. Click Create a container cluster.

  3. Accept the defaults and click Create.

  4. Note the cluster name and zone; you'll need it later.

Authenticating kubectl to the container cluster

In order to use kubectl to create and manage cluster resources, you need to get cluster credentials and make them available to kubectl.

To authenticate kubectl:

  1. Invoke the following command:

    gcloud container clusters get-credentials NAME [--zone ZONE]
    

    Replace NAME with the cluster name and ZONE with the cluster zone.

Deploying the containers to the cluster

To deploy to the cluster:

  1. Edit the Kubernetes configuration file /endpoints/getting-started/container-engine.yaml, replacing SERVICE_NAME and SERVICE_CONFIG_ID shown in the snippet below with the values returned when you deployed the API:
  1. Start the service using the kubectl create command:

    kubectl create -f container-engine.yaml
    

Get the cluster's external IP address

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:

  1. Invoke the command

    kubectl get service
    
  2. Note the value for EXTERNAL-IP; you'll need it to send requests to the API.

Sending a request to the sample API

After the sample API is running in the container cluster, you can send requests to the API.

To send a request to the API

  1. Create an API key in the API credentials page.

    Create an API key.

    1. Click Create credentials, then select API key.
    2. Copy the key, then paste it into the following export statement:

      export ENDPOINTS_KEY=AIza...
      
  2. Send an HTTP request using curl, as follows:

    curl -d '{"message":"hello world"}' -H "content-type:application/json" "http://[IP_ADDRESS]:80/echo?key=${ENDPOINTS_KEY}"
    

    Replace [IP_ADDRESS] with the external IP address of your instance.

  3. 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.

  4. 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!

Clean up

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

See Deleting an API and API Instances for information on stopping the services used by this quickstart.

What's next

Send feedback about...

Cloud Endpoints