Skip to content

Commit 7dadd39

Browse files
committed
Add basic example
1 parent d3a4846 commit 7dadd39

File tree

6 files changed

+489
-0
lines changed

6 files changed

+489
-0
lines changed

examples/grpc-routing/README.md

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# gRPC Routing Example
2+
3+
In this example, we deploy NGINX Gateway Fabric, a simple gRPC web application, and then configure NGINX Gateway Fabric
4+
to route traffic to that application using GRPCRoute resources.
5+
6+
## Running the Example
7+
8+
## 1. Deploy NGINX Gateway Fabric
9+
10+
1. Follow the [installation instructions](https://docs.nginx.com/nginx-gateway-fabric/installation/) to deploy NGINX Gateway Fabric.
11+
NB: Ensure the Gateway APIs from the experimental channel are installed and that NGF is deployed with the Gateway experimental features enabled.
12+
13+
1. Save the public IP address of NGINX Gateway Fabric into a shell variable:
14+
15+
```text
16+
GW_IP=XXX.YYY.ZZZ.III
17+
```
18+
19+
1. Save the port of NGINX Gateway Fabric:
20+
21+
```text
22+
GW_PORT=<port number>
23+
```
24+
25+
## 2. Deploy the Helloworld Application
26+
27+
1. Create the two helloworld Deployments and Services:
28+
29+
```shell
30+
kubectl apply -f helloworld.yaml
31+
```
32+
33+
1. Check that the Pods are running in the `default` Namespace:
34+
35+
```shell
36+
kubectl -n default get pods
37+
```
38+
39+
```text
40+
NAME READY STATUS RESTARTS AGE
41+
grpc-infra-backend-v1-766c7d6788-rg92p 1/1 Running 0 12s
42+
grpc-infra-backend-v2-546f7c8d48-mjkkx 1/1 Running 0 12s
43+
```
44+
45+
Save these pod names into variables:
46+
47+
```text
48+
POD_V1=<grpc-infra-backend-v1-xxxxxxxxxx-xxxxx>
49+
POD_V2=<grpc-infra-backend-v2-xxxxxxxxxx-xxxxx>
50+
```
51+
52+
## 3. Configure Routing
53+
54+
There are 3 options to configure gRPC routing. To access the application and test the routing rules, we will use [grpcurl](https://github.com/fullstorydev/grpcurl?tab=readme-ov-file#installation).
55+
56+
### 3a. Configure exact method matching based routing
57+
58+
1. Create the Gateway and GRPCRoute resources:
59+
60+
```shell
61+
kubectl apply -f exact-method.yaml
62+
```
63+
64+
2. Test the Application:
65+
66+
```shell
67+
grpcurl -plaintext -proto grpc.proto -authority bar.com -d '{"name": "exact"}' ${GW_IP}:${GW_PORT} helloworld.Greeter/SayHello
68+
```
69+
70+
```text
71+
{
72+
"message": "Hello exact"
73+
}
74+
```
75+
76+
3. Clean up the Gateway and GRPCRoute resources:
77+
78+
```shell
79+
kubectl delete -f exact-method.yaml
80+
```
81+
82+
### 3b. Configure hostname based routing
83+
84+
1. Create the Gateway and GRPCRoute resources:
85+
86+
```shell
87+
kubectl apply -f hostname.yaml
88+
```
89+
90+
2. Test the Application:
91+
92+
```shell
93+
grpcurl -plaintext -proto grpc.proto -authority bar.com -d '{"name": "bar server"}' ${GW_IP}:${GW_PORT} helloworld.Greeter/SayHello
94+
```
95+
96+
```text
97+
{
98+
"message": "Hello bar server"
99+
}
100+
```
101+
102+
To make sure this came from the correct server, we can check the application server logs:
103+
104+
```shell
105+
kubectl logs ${POD_V1}
106+
```
107+
108+
```text
109+
2024/04/29 09:26:54 server listening at [::]:50051
110+
2024/04/29 09:28:54 Received: bar server
111+
```
112+
113+
Now we'll send a request to `foo.bar.com`
114+
115+
```shell
116+
grpcurl -plaintext -proto grpc.proto -authority foo.bar.com -d '{"name": "foo bar server"}' ${GW_IP}:${GW_PORT} helloworld.Greeter/SayHello
117+
```
118+
119+
```text
120+
{
121+
"message": "Hello foo bar server"
122+
}
123+
```
124+
125+
This time, we'll check the POD_V2 logs:
126+
127+
```shell
128+
kubectl logs ${POD_V2}
129+
```
130+
131+
```text
132+
2024/04/29 09:26:55 server listening at [::]:50051
133+
2024/04/29 09:29:46 Received: foo bar server
134+
```
135+
136+
3. Clean up the Gateway and GRPCRoute resources:
137+
138+
```shell
139+
kubectl delete -f hostname.yaml
140+
```
141+
142+
### 3c. Configure headers based routing
143+
144+
1. Create the Gateway and GRPCRoute resources:
145+
146+
```shell
147+
kubectl apply -f headers.yaml
148+
```
149+
150+
2. Test the Application:
151+
152+
```shell
153+
grpcurl -plaintext -proto grpc.proto -authority bar.com -d '{"name": "version one"}' -H 'version: one' ${GW_IP}:${GW_PORT} helloworld.Greeter/SayHello
154+
```
155+
156+
```text
157+
{
158+
"message": "Hello version one"
159+
}
160+
```
161+
162+
To make sure this came from the correct server, we can check the application server logs:
163+
164+
```shell
165+
kubectl logs ${POD_V1}
166+
```
167+
168+
```text
169+
<...>
170+
2024/04/29 09:30:27 Received: version one
171+
```
172+
173+
Now we'll send a request with the header `version: two`
174+
175+
```shell
176+
grpcurl -plaintext -proto grpc.proto -authority bar.com -d '{"name": "version two"}' -H 'version: two' ${GW_IP}:${GW_PORT} helloworld.Greeter/SayHello
177+
```
178+
179+
```text
180+
{
181+
"message": "Hello version two"
182+
}
183+
```
184+
185+
This time, we'll check the POD_V2 logs:
186+
187+
```shell
188+
kubectl logs ${POD_V2}
189+
```
190+
191+
```text
192+
<...>
193+
2024/04/29 09:32:46 Received: version two
194+
```
195+
196+
Finally, we'll send a request with the headers `version: two` and `color: orange`
197+
198+
```shell
199+
grpcurl -plaintext -proto grpc.proto -authority bar.com -d '{"name": "version two orange"}' -H 'version: two' -H 'color: orange' ${GW_IP}:${GW_PORT} helloworld.Greeter/SayHello
200+
```
201+
202+
```text
203+
{
204+
"message": "Hello version two orange"
205+
}
206+
```
207+
208+
Now check the POD_V1 logs again:
209+
210+
```shell
211+
kubectl logs ${POD_V1}
212+
```
213+
214+
```text
215+
<...>
216+
2024/04/29 09:30:27 Received: version one
217+
2024/04/29 09:33:26 Received: version two orange
218+
```
219+
220+
3. Clean up the Gateway and GRPCRoute resources:
221+
222+
```shell
223+
kubectl delete -f headers.yaml
224+
```
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apiVersion: gateway.networking.k8s.io/v1beta1
2+
kind: Gateway
3+
metadata:
4+
name: same-namespace
5+
spec:
6+
gatewayClassName: nginx
7+
listeners:
8+
- name: http
9+
port: 80
10+
protocol: HTTP
11+
allowedRoutes:
12+
namespaces:
13+
from: Same
14+
---
15+
apiVersion: gateway.networking.k8s.io/v1alpha2
16+
kind: GRPCRoute
17+
metadata:
18+
name: exact-matching
19+
spec:
20+
parentRefs:
21+
- name: same-namespace
22+
rules:
23+
- matches:
24+
- method:
25+
service: helloworld.Greeter
26+
method: SayHello
27+
backendRefs:
28+
- name: grpc-infra-backend-v1
29+
port: 8080

examples/grpc-routing/grpc.proto

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2015 gRPC authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
18+
option java_multiple_files = true;
19+
option java_package = "io.grpc.examples.helloworld";
20+
option java_outer_classname = "HelloWorldProto";
21+
22+
package helloworld;
23+
24+
// The greeting service definition.
25+
service Greeter {
26+
// Sends a greeting
27+
rpc SayHello (HelloRequest) returns (HelloReply) {}
28+
}
29+
30+
// The request message containing the user's name.
31+
message HelloRequest {
32+
string name = 1;
33+
}
34+
35+
// The response message containing the greetings
36+
message HelloReply {
37+
string message = 1;
38+
}

examples/grpc-routing/headers.yaml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
apiVersion: gateway.networking.k8s.io/v1beta1
2+
kind: Gateway
3+
metadata:
4+
name: same-namespace
5+
spec:
6+
gatewayClassName: nginx
7+
listeners:
8+
- name: http
9+
port: 80
10+
protocol: HTTP
11+
allowedRoutes:
12+
namespaces:
13+
from: Same
14+
---
15+
apiVersion: gateway.networking.k8s.io/v1alpha2
16+
kind: GRPCRoute
17+
metadata:
18+
name: grpc-header-matching
19+
spec:
20+
parentRefs:
21+
- name: same-namespace
22+
rules:
23+
# Matches "version: one"
24+
- matches:
25+
- headers:
26+
- name: version
27+
value: one
28+
backendRefs:
29+
- name: grpc-infra-backend-v1
30+
port: 8080
31+
# Matches "version: two"
32+
- matches:
33+
- headers:
34+
- name: version
35+
value: two
36+
backendRefs:
37+
- name: grpc-infra-backend-v2
38+
port: 8080
39+
# Matches "version: two" AND "color: orange"
40+
- matches:
41+
- headers:
42+
- name: version
43+
value: two
44+
- name: color
45+
value: orange
46+
backendRefs:
47+
- name: grpc-infra-backend-v1
48+
port: 8080
49+
# Matches "color: blue" OR "color: green"
50+
- matches:
51+
- headers:
52+
- name: color
53+
value: blue
54+
- headers:
55+
- name: color
56+
value: green
57+
backendRefs:
58+
- name: grpc-infra-backend-v1
59+
port: 8080
60+
# Matches "color: red" OR "color: yellow"
61+
- matches:
62+
- headers:
63+
- name: color
64+
value: red
65+
- headers:
66+
- name: color
67+
value: yellow
68+
backendRefs:
69+
- name: grpc-infra-backend-v2
70+
port: 8080

0 commit comments

Comments
 (0)