|
1 | 1 | --- |
2 | | -title: "gRPC server - Go" |
| 2 | +title: "gRPC Server - Go" |
3 | 3 | #linkTitle: "" |
4 | 4 | weight: 1 |
5 | 5 | type: "docs" |
6 | 6 | --- |
7 | 7 |
|
8 | | -A simple gRPC server written in Go that you can use for testing. |
| 8 | +A [gRPC](https://grpc.io) server written in Go. |
| 9 | + |
| 10 | +This sample can be used to try out gRPC, HTTP/2, and custom port configuration |
| 11 | +in a knative service. |
| 12 | + |
| 13 | +The container image is built with two binaries: the server and the client. |
| 14 | +This is done for ease of testing and is not a recommended practice |
| 15 | +for production containers. |
9 | 16 |
|
10 | 17 | ## Prerequisites |
11 | 18 |
|
12 | 19 | - [Install the latest version of Knative Serving](../../../install/README.md). |
13 | 20 |
|
14 | 21 | - Install [docker](https://www.docker.com/). |
15 | 22 |
|
16 | | -- Download a copy of the code: |
| 23 | +- A [Docker Hub account](https://hub.docker.com) to which you can upload the sample's container image. |
| 24 | + |
| 25 | +## Build and Deploy the sample code |
| 26 | + |
| 27 | +1. Download a copy of the code: |
17 | 28 |
|
18 | 29 | ```shell |
19 | 30 | git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs |
20 | 31 | cd knative-docs/docs/serving/samples/grpc-ping-go |
21 | 32 | ``` |
22 | 33 |
|
23 | | -## Build and run the gRPC server |
| 34 | +2. Use Docker to build a container image for this service and push to Docker Hub. |
24 | 35 |
|
25 | | -First, build and publish the gRPC server to DockerHub (replacing `{username}`): |
| 36 | + Replace `{username}` with your Docker Hub username then run the commands: |
26 | 37 |
|
27 | | -```shell |
28 | | -# Build and publish the container, run from the root directory. |
29 | | -docker build \ |
30 | | - --tag "docker.io/{username}/grpc-ping-go" \ |
31 | | - --file=docs/serving/samples/grpc-ping-go/Dockerfile . |
32 | | -docker push "docker.io/{username}/grpc-ping-go" |
33 | | -``` |
| 38 | + ```shell |
| 39 | + # Build the container on your local machine. |
| 40 | + docker build --tag "{username}/grpc-ping-go" . |
| 41 | + |
| 42 | + # Push the container to docker registry. |
| 43 | + docker push "{username}/grpc-ping-go" |
| 44 | + ``` |
| 45 | + |
| 46 | +3. Update the `service.yaml` file in the project to reference the published image from step 1. |
| 47 | + |
| 48 | + Replace `{username}` in `service.yaml` with your Docker Hub user name: |
| 49 | + |
| 50 | + |
| 51 | + ```yaml |
| 52 | + apiVersion: serving.knative.dev/v1alpha1 |
| 53 | + kind: Service |
| 54 | + metadata: |
| 55 | + name: grpc-ping |
| 56 | + namespace: default |
| 57 | + spec: |
| 58 | + template: |
| 59 | + spec: |
| 60 | + containers: |
| 61 | + - image: docker.io/{username}/grpc-ping-go |
| 62 | + ports: |
| 63 | + - name: h2c |
| 64 | + containerPort: 8080 |
| 65 | + ``` |
| 66 | +
|
| 67 | +4. Use `kubectl` to deploy the service. |
| 68 | + |
| 69 | + ```shell |
| 70 | + kubectl apply --filename service.yaml |
| 71 | + ``` |
34 | 72 |
|
35 | | -Next, replace `{username}` in `sample.yaml` with your DockerHub username, and |
36 | | -apply the yaml. |
| 73 | + Response: |
| 74 | + |
| 75 | + ```shell |
| 76 | + service "grpc-ping" created |
| 77 | + ``` |
| 78 | + |
| 79 | +## Exploring |
| 80 | + |
| 81 | +Once deployed, you can inspect the created resources with `kubectl` commands: |
37 | 82 |
|
38 | 83 | ```shell |
39 | | -kubectl apply --filename docs/serving/samples/grpc-ping-go/sample.yaml |
| 84 | +# This will show the Knative service that we created: |
| 85 | +kubectl get ksvc --output yaml |
| 86 | +
|
| 87 | +# This will show the Route, created by the service: |
| 88 | +kubectl get route --output yaml |
| 89 | +
|
| 90 | +# This will show the Configuration, created by the service: |
| 91 | +kubectl get configurations --output yaml |
| 92 | +
|
| 93 | +# This will show the Revision, created by the Configuration: |
| 94 | +kubectl get revisions --output yaml |
40 | 95 | ``` |
41 | 96 |
|
42 | | -## Use the client to stream messages to the gRPC server |
| 97 | +## Testing the service |
| 98 | + |
| 99 | +Testing the gRPC service requires using a gRPC client built from the same |
| 100 | +protobuf definition used by the server. |
43 | 101 |
|
44 | 102 | 1. Fetch the created ingress hostname and IP. |
45 | 103 |
|
46 | 104 | ```shell |
47 | 105 | # Put the ingress IP into an environment variable. |
48 | | - export SERVICE_IP=`kubectl get svc istio-ingressgateway --namespace istio-system --output jsonpath="{.status.loadBalancer.ingress[*].ip}"` |
| 106 | + export SERVICE_IP=$(kubectl get svc istio-ingressgateway --namespace istio-system --output jsonpath="{.status.loadBalancer.ingress[*].ip}") |
49 | 107 | ``` |
50 | 108 |
|
51 | | -1. Use the client to send message streams to the gRPC server (replacing |
52 | | - `{username}`) |
| 109 | +1. Use the gRPC client to send message streams to the gRPC server. |
| 110 | + |
| 111 | + The Dockerfile builds the client binary. To run the client you will use the |
| 112 | + same container image deployed for the server with an override to the |
| 113 | + entrypoint command to use the client binary instead of the server binary. |
| 114 | + |
| 115 | + Replace `{username}` with your Docker Hub user name and run the command: |
53 | 116 |
|
54 | 117 | ```shell |
55 | | - docker run -ti --entrypoint=/client docker.io/{username}/grpc-ping-go \ |
| 118 | + docker run --rm {username}/grpc-ping-go \ |
| 119 | + /client \ |
56 | 120 | -server_addr="${SERVICE_IP}:80" \ |
57 | 121 | -server_host_override="grpc-ping.default.example.com" \ |
58 | 122 | -insecure |
59 | 123 | ``` |
| 124 | + |
| 125 | + The arguments after the container tag `{username}/grpc-ping-go` are used |
| 126 | + instead of the entrypoint command defined in the Dockerfile `CMD` statement. |
| 127 | + |
0 commit comments