|
| 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 | + ``` |
0 commit comments