Skip to content

Commit ea29845

Browse files
graysideknative-prow-robot
authored andcommitted
samples/grpc-ping-go: align with knative sample and README practices (#1753)
* samples/grpc-ping-go: align with knative sample and README practices * samples/grpc-ping-go: use 0.0.0.0 instead of localhost inside container * samples/grpc-ping-go: clarity & context improvements * serving/samples: remove unneeded -it flag from grpc-ping testing
1 parent 6ce457c commit ea29845

File tree

4 files changed

+109
-31
lines changed

4 files changed

+109
-31
lines changed

docs/serving/samples/grpc-ping-go/Dockerfile

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang AS builder
15+
# Use the offical Golang image to create a build artifact.
16+
# This is based on Debian and sets the GOPATH to /go.
17+
# https://hub.docker.com/_/golang
18+
FROM golang:1.12 as builder
1619

17-
# Get the dependencies from GitHub
20+
# Retrieve the dependencies.
1821
RUN go get google.golang.org/grpc
1922

20-
WORKDIR /go/src/github.com/knative/docs/
21-
ADD . /go/src/github.com/knative/docs/
23+
# Copy local code to the container image.
24+
WORKDIR /go/src/github.com/knative/docs/docs/serving/samples/grpc-ping-go
25+
COPY . ./
2226

23-
RUN CGO_ENABLED=0 go build -tags=grpcping ./docs/serving/samples/grpc-ping-go/
24-
RUN CGO_ENABLED=0 go build -tags=grpcping ./docs/serving/samples/grpc-ping-go/client
27+
# Build the command inside the container.
28+
# To facilitate gRPC testing, this container includes a client command.
29+
RUN CGO_ENABLED=0 go build -tags=grpcping -o ./ping-server
30+
RUN CGO_ENABLED=0 go build -tags=grpcping -o ./ping-client ./client
2531

32+
# Use a Docker multi-stage build to create a lean production image.
33+
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
34+
# https://github.com/GoogleContainerTools/distroless#readme
2635
FROM gcr.io/distroless/static
2736

28-
EXPOSE 8080
29-
COPY --from=builder /go/src/github.com/knative/docs/grpc-ping-go /server
30-
COPY --from=builder /go/src/github.com/knative/docs/client /client
37+
# Copy the binaries to the production image from the builder stage.
38+
COPY --from=builder /go/src/github.com/knative/docs/docs/serving/samples/grpc-ping-go/ping-server /server
39+
COPY --from=builder /go/src/github.com/knative/docs/docs/serving/samples/grpc-ping-go/ping-client /client
3140

32-
ENTRYPOINT ["/server"]
41+
# Run the service on container startup.
42+
CMD ["/server"]
Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,127 @@
11
---
2-
title: "gRPC server - Go"
2+
title: "gRPC Server - Go"
33
#linkTitle: ""
44
weight: 1
55
type: "docs"
66
---
77

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

1017
## Prerequisites
1118

1219
- [Install the latest version of Knative Serving](../../../install/README.md).
1320

1421
- Install [docker](https://www.docker.com/).
1522

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:
1728

1829
```shell
1930
git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs
2031
cd knative-docs/docs/serving/samples/grpc-ping-go
2132
```
2233

23-
## Build and run the gRPC server
34+
2. Use Docker to build a container image for this service and push to Docker Hub.
2435

25-
First, build and publish the gRPC server to DockerHub (replacing `{username}`):
36+
Replace `{username}` with your Docker Hub username then run the commands:
2637

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+
```
3472

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:
3782

3883
```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
4095
```
4196

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

44102
1. Fetch the created ingress hostname and IP.
45103

46104
```shell
47105
# 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}")
49107
```
50108

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:
53116

54117
```shell
55-
docker run -ti --entrypoint=/client docker.io/{username}/grpc-ping-go \
118+
docker run --rm {username}/grpc-ping-go \
119+
/client \
56120
-server_addr="${SERVICE_IP}:80" \
57121
-server_host_override="grpc-ping.default.example.com" \
58122
-insecure
59123
```
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+

docs/serving/samples/grpc-ping-go/grpc-ping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (p *pingServer) PingStream(stream ping.PingService_PingStreamServer) error
5151
}
5252

5353
func main() {
54-
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
54+
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
5555
if err != nil {
5656
log.Fatalf("failed to listen: %v", err)
5757
}

0 commit comments

Comments
 (0)