Skip to content

Latest commit

 

History

History

getting-started-grpc

Endpoints Getting Started with gRPC & Python Quickstart

Open in Cloud Shell

It is assumed that you have a working Python environment and a Google Cloud account and SDK configured.

  1. Install dependencies using virtualenv:

    virtualenv -p python3 env
    source env/bin/activate
    pip install -r requirements.txt
  2. Test running the code, optional:

    # Run the server:
    python greeter_server.py
    
    # Open another command line tab and enter the virtual environment:
    source env/bin/activate
    
    # In the new command line tab, run the client:
    python greeter_client.py
  3. The gRPC Services have already been generated. If you change the proto, or just wish to regenerate these files, run:

    python -m grpc.tools.protoc \
        --include_imports \
        --include_source_info \
        --proto_path=protos \
        --python_out=. \
        --grpc_python_out=. \
        --descriptor_set_out=api_descriptor.pb \
        helloworld.proto
  4. Edit, api_config.yaml. Replace MY_PROJECT_ID with your project id.

  5. Deploy your service config to Service Management:

    gcloud endpoints services deploy api_descriptor.pb api_config.yaml
    # The Config ID should be printed out, looks like: 2017-02-01r0, remember this
    
    # Set your project ID as a variable to make commands easier:
    GCLOUD_PROJECT=<Your Project ID>
    
    # Print out your Config ID again, in case you missed it:
    gcloud endpoints configs list --service hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog
  6. Also get an API key from the Console's API Manager for use in the client later. (https://console.cloud.google.com/apis/credentials)

  7. Enable the Cloud Build API:

    gcloud services enable cloudbuild.googleapis.com
  8. Build a docker image for your gRPC server, and store it in your Registry:

    gcloud container builds submit --tag gcr.io/${GCLOUD_PROJECT}/python-grpc-hello:1.0 .
  9. Either deploy to GCE (below) or GKE (further down).

GCE

  1. Enable the Compute Engine API:

    gcloud services enable compute-component.googleapis.com
  2. Create your instance and ssh in:

    gcloud compute instances create grpc-host --image-family gci-stable --image-project google-containers --tags=http-server
    gcloud compute ssh grpc-host
  3. Set some variables to make commands easier:

    GCLOUD_PROJECT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
    SERVICE_NAME=hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog
    SERVICE_CONFIG_ID=<Your Config ID>
  4. Pull your credentials to access Container Registry, and run your gRPC server container:

    /usr/share/google/dockercfg_update.sh
    docker run --detach --name=grpc-hello gcr.io/${GCLOUD_PROJECT}/python-grpc-hello:1.0
  5. Run the Endpoints proxy:

    docker run --detach --name=esp \
        --publish=80:9000 \
        --link=grpc-hello:grpc-hello \
        gcr.io/endpoints-release/endpoints-runtime:1 \
        --service=${SERVICE_NAME} \
        --version=${SERVICE_CONFIG_ID} \
        --http2_port=9000 \
        --backend=grpc://grpc-hello:50051
  6. Back on your local machine, get the external IP of your GCE instance:

    gcloud compute instances list
  7. Run the client:

    python greeter_client.py --host=<IP of GCE Instance>:80 --api_key=<API Key from Console>
  8. Cleanup:

    gcloud compute instances delete grpc-host

GKE

  1. Create a cluster. You can specify a different zone than us-central1-a if you want:

    gcloud container clusters create my-cluster --zone=us-central1-a
  2. Edit deployment.yaml. Replace SERVICE_NAME, SERVICE_CONFIG_ID, and GCLOUD_PROJECT with your values:

    SERVICE_NAME is equal to hellogrpc.endpoints.GCLOUD_PROJECT.cloud.goog, replacing GCLOUD_PROJECT with your project ID.

    SERVICE_CONFIG_ID can be found by running the following command, replacing GCLOUD_PROJECT with your project ID.

    gcloud endpoints configs list --service hellogrpc.endpoints.GCLOUD_PROJECT.cloud.goog
  3. Deploy to GKE:

    kubectl create -f ./deployment.yaml
  4. Get IP of load balancer, run until you see an External IP:

    kubectl get svc grpc-hello
  5. Run the client:

    python greeter_client.py --host=<IP of GKE LoadBalancer>:80 --api_key=<API Key from Console>
  6. Cleanup:

    gcloud container clusters delete my-cluster --zone=us-central1-a