In this example we will deploy NGINX Kubernetes Gateway and configure advanced routing rules for a simple cafe application.
We will use HTTPRoute
resources to route traffic to the cafe application based on a combination of the request method, headers, and query parameters.
The cafe application consists of four Services: coffee-v1-svc
, coffee-v2-svc
, tea-svc
, and tea-post-svc
. In the next section we will create the following routing rules for the cafe application:
- For the path
/coffee
route requests with the headerversion
set tov2
or with the query paramTEST
set tov2
tocoffee-v2-svc
, and all other requests tocoffee-v1-svc
. - For the path
/tea
route POST requests totea-post-svc
, and all other requests, such asGET
requests, totea-svc
.
-
Follow the installation instructions to deploy NGINX Gateway.
-
Save the public IP address of NGINX Kubernetes Gateway into a shell variable:
GW_IP=XXX.YYY.ZZZ.III
-
Save the port of NGINX Kubernetes Gateway:
GW_PORT=<port number>
-
Create the coffee and the tea Deployments and Services:
kubectl apply -f cafe.yaml
-
Check that the Pods are running in the
default
namespace:kubectl -n default get pods NAME READY STATUS RESTARTS AGE coffee-v1-75869cf7ff-vlfpq 1/1 Running 0 17m coffee-v2-67499ff985-2k6cc 1/1 Running 0 17m tea-6fb46d899f-hjzwr 1/1 Running 0 17m tea-post-648dfcdd6c-2rlqb 1/1 Running 0 17m
-
Create the
Gateway
:kubectl apply -f gateway.yaml
-
Create the
HTTPRoute
resources:kubectl apply -f cafe-routes.yaml
We will use curl
to send requests to the /coffee
and /tea
endpoints of the cafe application.
Send a request with the header version:v2
and confirm that the response comes from coffee-v2-svc
:
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee -H "version:v2"
Server address: 10.116.2.67:8080
Server name: coffee-v2-67499ff985-gw6vt
...
Send a request with the query parameter TEST=v2
and confirm that the response comes from coffee-v2-svc
:
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee?TEST=v2
Server address: 10.116.2.67:8080
Server name: coffee-v2-67499ff985-gw6vt
...
Send a request without the header or the query parameter and confirm the response comes from coffee-v1-svc
:
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee
Server address: 10.116.2.70:8080
Server name: coffee-v1-75869cf7ff-vlfpq
...
Send a POST request and confirm that the response comes from tea-post-svc
:
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea -X POST
Server address: 10.116.2.72:8080
Server name: tea-post-648dfcdd6c-2rlqb
...
Send a GET request and confirm that the response comes from tea-svc
:
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea
Server address: 10.116.3.30:8080
Server name: tea-6fb46d899f-hjzwr
...
The /tea
endpoint has routing rules configured for GET and POST requests. If you send a request with a different method, NGINX Kubernetes Gateway will return a 404.
Send a PUT request and confirm the 404 Not Found response:
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea -X PUT
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.3</center>
</body>
</html>