|
| 1 | +# Advanced Routing |
| 2 | + |
| 3 | +In this example we will deploy NGINX Kubernetes Gateway and configure advanced routing rules for a simple cafe application. |
| 4 | +We will use `HTTPRoute` resources to route traffic to the cafe application based on a combination of the request method, headers, and query parameters. |
| 5 | + |
| 6 | +## Running the Example |
| 7 | + |
| 8 | +## 1. Deploy NGINX Kubernetes Gateway |
| 9 | + |
| 10 | +1. Follow the [installation instructions](https://github.com/nginxinc/nginx-kubernetes-gateway/blob/main/README.md#run-nginx-gateway) to deploy NGINX Gateway. |
| 11 | + |
| 12 | +1. Save the public IP address of NGINX Kubernetes Gateway into a shell variable: |
| 13 | + |
| 14 | + ``` |
| 15 | + GW_IP=XXX.YYY.ZZZ.III |
| 16 | + ``` |
| 17 | + |
| 18 | +1. Save the port of NGINX Kubernetes Gateway: |
| 19 | + |
| 20 | + ``` |
| 21 | + GW_PORT=<port number> |
| 22 | + ``` |
| 23 | + |
| 24 | +## 2. Deploy the Cafe Application |
| 25 | + |
| 26 | +1. Create the coffee and the tea deployments and services: |
| 27 | + |
| 28 | + ``` |
| 29 | + kubectl apply -f cafe.yaml |
| 30 | + ``` |
| 31 | + |
| 32 | +1. Check that the Pods are running in the `default` namespace: |
| 33 | + |
| 34 | + ``` |
| 35 | + kubectl -n default get pods |
| 36 | + NAME READY STATUS RESTARTS AGE |
| 37 | + coffee-6f4b79b975-2sb28 1/1 Running 0 12s |
| 38 | + tea-6fb46d899f-fm7zr 1/1 Running 0 12s |
| 39 | + ``` |
| 40 | + |
| 41 | +## 3. Configure Routing |
| 42 | + |
| 43 | +1. Create the `HTTPRoute` resources: |
| 44 | + |
| 45 | + ``` |
| 46 | + kubectl apply -f cafe-routes.yaml |
| 47 | + ``` |
| 48 | + |
| 49 | +## 4. Test the Application |
| 50 | + |
| 51 | +We will use `curl` to send requests to the `coffee` and `tea` services. |
| 52 | + |
| 53 | +### 4.1 Access coffee |
| 54 | + |
| 55 | +Send a `POST` request to the path `/coffee` with the headers `X-Demo-Header:Demo-X1` and `version:v1`: |
| 56 | + |
| 57 | +``` |
| 58 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee -X POST -H "X-Demo-Header:Demo-X1" -H "version:v1" |
| 59 | +Server address: 10.12.0.18:80 |
| 60 | +Server name: coffee-7586895968-r26zn |
| 61 | +``` |
| 62 | + |
| 63 | +Header keys are case-insensitive, so we can also access coffee with the following request: |
| 64 | + |
| 65 | +``` |
| 66 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee -X POST -H "X-DEMO-HEADER:Demo-X1" -H "Version:v1" |
| 67 | +Server address: 10.12.0.18:80 |
| 68 | +Server name: coffee-7586895968-r26zn |
| 69 | +``` |
| 70 | + |
| 71 | +Only `POST` requests to the path `/coffee` with the headers `X-Demo-Header:Demo-X1` and `version:v1` will be able to access coffee. |
| 72 | +For example, try sending the following `GET` request: |
| 73 | +``` |
| 74 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee -H "X-Demo-Header:Demo-X1" -H "version:v1" |
| 75 | +``` |
| 76 | + |
| 77 | +NGINX Kubernetes Gateway returns a 405 since the request method does not match the method defined in the routing rule for `/coffee`. |
| 78 | + |
| 79 | +### 4.2 Access tea |
| 80 | + |
| 81 | +Send a request to the path `/tea` with the query parameter `Great=Example`: |
| 82 | + |
| 83 | +``` |
| 84 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea?Great=Example |
| 85 | +Server address: 10.12.0.19:80 |
| 86 | +Server name: tea-7cd44fcb4d-xfw2x |
| 87 | +``` |
| 88 | + |
| 89 | +Query parameters are case-sensitive, so the case must match what you specify in the `HTTPRoute` resource. |
| 90 | + |
| 91 | +Only requests to the path `/tea` with the query parameter `Great=Example` will be able to access tea. |
| 92 | +For example, try sending the following request: |
| 93 | + |
| 94 | +``` |
| 95 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea |
| 96 | +``` |
| 97 | + |
| 98 | +NGINX Kubernetes Gateway returns a 404 since the request does not satisfy the routing rule configured for `/tea`. |
0 commit comments