|
| 1 | +# Endpoints Getting Started with gRPC & Python Quickstart |
| 2 | + |
| 3 | +It is assumed that you have a working Python environment and a Google |
| 4 | +Cloud account and [SDK](https://cloud.google.com/sdk/) configured. |
| 5 | + |
| 6 | +1. Install dependencies using virtualenv: |
| 7 | + |
| 8 | + ```bash |
| 9 | + virtualenv -p python3 env |
| 10 | + source env/bin/activate |
| 11 | + pip install -r requirements.txt |
| 12 | + ``` |
| 13 | + |
| 14 | +1. Test running the code, optional: |
| 15 | + |
| 16 | + ```bash |
| 17 | + # Run the server: |
| 18 | + python greeter_server.py |
| 19 | +
|
| 20 | + # Open another command line tab and enter the virtual environment: |
| 21 | + source env/bin/activate |
| 22 | +
|
| 23 | + # In the new command line tab, run the client: |
| 24 | + python greeter_client.py |
| 25 | + ``` |
| 26 | + |
| 27 | +1. The gRPC Services have already been generated. If you change the proto, or |
| 28 | + just wish to regenerate these files, run: |
| 29 | + |
| 30 | + ```bash |
| 31 | + python -m grpc_tools.protoc -I protos --python_out=. --grpc_python_out=. protos/helloworld.proto |
| 32 | + ``` |
| 33 | + |
| 34 | +1. Generate the `out.pb` from the proto file: |
| 35 | + |
| 36 | + ```bash |
| 37 | + python -m grpc_tools.protoc --include_imports --include_source_info -I protos protos/helloworld.proto --descriptor_set_out out.pb |
| 38 | + ``` |
| 39 | + |
| 40 | +1. Edit, `api_config.yaml`. Replace `MY_PROJECT_ID` with your project id. |
| 41 | + |
| 42 | +1. Deploy your service config to Service Management: |
| 43 | + |
| 44 | + ```bash |
| 45 | + gcloud service-management deploy out.pb api_config.yaml |
| 46 | + # The Config ID should be printed out, looks like: 2017-02-01r0, remember this |
| 47 | +
|
| 48 | + # Set your project ID as a variable to make commands easier: |
| 49 | + GCLOUD_PROJECT=<Your Project ID> |
| 50 | +
|
| 51 | + # Print out your Config ID again, in case you missed it: |
| 52 | + gcloud service-management configs list --service hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog |
| 53 | + ``` |
| 54 | + |
| 55 | +1. Also get an API key from the Console's API Manager for use in the |
| 56 | + client later. (https://console.cloud.google.com/apis/credentials) |
| 57 | +
|
| 58 | +1. Enable the Cloud Build API: |
| 59 | +
|
| 60 | + ```bash |
| 61 | + gcloud service-management enable cloudbuild.googleapis.com |
| 62 | + ``` |
| 63 | +
|
| 64 | +1. Build a docker image for your gRPC server, and store it in your Registry: |
| 65 | +
|
| 66 | + ```bash |
| 67 | + gcloud container builds submit --tag gcr.io/${GCLOUD_PROJECT}/python-grpc-hello:1.0 . |
| 68 | + ``` |
| 69 | +
|
| 70 | +1. Either deploy to GCE (below) or GKE (further down). |
| 71 | +
|
| 72 | +### GCE |
| 73 | +
|
| 74 | +1. Enable the Compute Engine API: |
| 75 | +
|
| 76 | + ```bash |
| 77 | + gcloud service-management enable compute-component.googleapis.com |
| 78 | + ``` |
| 79 | +
|
| 80 | +1. Create your instance and ssh in: |
| 81 | +
|
| 82 | + ```bash |
| 83 | + gcloud compute instances create grpc-host --image-family gci-stable --image-project google-containers --tags=http-server |
| 84 | + gcloud compute ssh grpc-host |
| 85 | + ``` |
| 86 | +
|
| 87 | +1. Set some variables to make commands easier: |
| 88 | +
|
| 89 | + ```bash |
| 90 | + GCLOUD_PROJECT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google") |
| 91 | + SERVICE_NAME=hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog |
| 92 | + SERVICE_CONFIG_ID=<Your Config ID> |
| 93 | + ``` |
| 94 | +
|
| 95 | +1. Pull your credentials to access Container Registry, and run your gRPC server |
| 96 | + container: |
| 97 | +
|
| 98 | + ```bash |
| 99 | + /usr/share/google/dockercfg_update.sh |
| 100 | + docker run -d --name=grpc-hello gcr.io/${GCLOUD_PROJECT}/python-grpc-hello:1.0 |
| 101 | + ``` |
| 102 | +
|
| 103 | +1. Run the Endpoints proxy: |
| 104 | +
|
| 105 | + ```bash |
| 106 | + docker run --detach --name=esp \ |
| 107 | + -p 80:9000 \ |
| 108 | + --link=grpc-hello:grpc-hello \ |
| 109 | + gcr.io/endpoints-release/endpoints-runtime:1 \ |
| 110 | + -s ${SERVICE_NAME} \ |
| 111 | + -v ${SERVICE_CONFIG_ID} \ |
| 112 | + -P 9000 \ |
| 113 | + -a grpc://grpc-hello:50051 |
| 114 | + ``` |
| 115 | +
|
| 116 | +1. Back on your local machine, get the external IP of your GCE instance: |
| 117 | +
|
| 118 | + ```bash |
| 119 | + gcloud compute instances list |
| 120 | + ``` |
| 121 | +
|
| 122 | +1. Run the client: |
| 123 | +
|
| 124 | + ```bash |
| 125 | + python greeter_client.py --host=<IP of GCE Instance>:80 --api_key=<API Key from Console> |
| 126 | + ``` |
| 127 | +
|
| 128 | +1. Cleanup: |
| 129 | +
|
| 130 | + ```bash |
| 131 | + gcloud compute instances delete grpc-host |
| 132 | + ``` |
| 133 | +
|
| 134 | +### GKE |
| 135 | +
|
| 136 | +1. Create a cluster. You can specify a different zone than us-central1-a if you |
| 137 | + want: |
| 138 | +
|
| 139 | + ```bash |
| 140 | + gcloud container clusters create my-cluster --zone=us-central1-a |
| 141 | + ``` |
| 142 | +
|
| 143 | +1. Edit `container-engine.yaml`. Replace `SERVICE_NAME`, `SERVICE_CONFIG_ID`, |
| 144 | + and `GCLOUD_PROJECT` with your values: |
| 145 | +
|
| 146 | + `SERVICE_NAME` is equal to hellogrpc.endpoints.GCLOUD_PROJECT.cloud.goog, |
| 147 | + replacing GCLOUD_PROJECT with your project ID. |
| 148 | +
|
| 149 | + `SERVICE_CONFIG_ID` can be found by running the following command, replacing |
| 150 | + GCLOUD_PROJECT with your project ID. |
| 151 | +
|
| 152 | + ```bash |
| 153 | + gcloud service-management configs list --service hellogrpc.endpoints.GCLOUD_PROJECT.cloud.goog |
| 154 | + ``` |
| 155 | +
|
| 156 | +1. Deploy to GKE: |
| 157 | +
|
| 158 | + ```bash |
| 159 | + kubectl create -f ./container-engine.yaml |
| 160 | + ``` |
| 161 | +
|
| 162 | +1. Get IP of load balancer, run until you see an External IP: |
| 163 | +
|
| 164 | + ```bash |
| 165 | + kubectl get svc grpc-hello |
| 166 | + ``` |
| 167 | +
|
| 168 | +1. Run the client: |
| 169 | +
|
| 170 | + ```bash |
| 171 | + python greeter_client.py --host=<IP of GKE LoadBalancer>:80 --api_key=<API Key from Console> |
| 172 | + ``` |
| 173 | +
|
| 174 | +1. Cleanup: |
| 175 | +
|
| 176 | + ```bash |
| 177 | + gcloud container clusters delete my-cluster --zone=us-central1-a |
| 178 | + ``` |
0 commit comments