|
| 1 | +# Example |
| 2 | + |
| 3 | +In this example, we expand on the simple [cafe-example](../cafe-example) by using a ReferenceGrant to route to backends |
| 4 | +in a different namespace from our HTTPRoutes. |
| 5 | + |
| 6 | +## Running the Example |
| 7 | + |
| 8 | +## 1. Deploy NGINX Kubernetes Gateway |
| 9 | + |
| 10 | +1. Follow the [installation instructions](/docs/installation.md) 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 cafe namespace and cafe application: |
| 27 | + |
| 28 | + ``` |
| 29 | + kubectl apply -f cafe-ns-and-app.yaml |
| 30 | + ``` |
| 31 | + |
| 32 | +1. Check that the Pods are running in the `cafe` namespace: |
| 33 | + |
| 34 | + ``` |
| 35 | + kubectl -n cafe 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 `Gateway`: |
| 44 | + |
| 45 | + ``` |
| 46 | + kubectl apply -f gateway.yaml |
| 47 | + ``` |
| 48 | + |
| 49 | +1. Create the `HTTPRoute` resources: |
| 50 | + |
| 51 | + ``` |
| 52 | + kubectl apply -f cafe-routes.yaml |
| 53 | + ``` |
| 54 | +1. Create the `ReferenceGrant`: |
| 55 | + |
| 56 | + ``` |
| 57 | + kubectl apply -f reference-grant.yaml |
| 58 | + ``` |
| 59 | + This ReferenceGrant allows all HTTPRoutes in the `default` namespace to reference all Services in the `cafe` |
| 60 | + namespace. |
| 61 | + |
| 62 | +## 4. Test the Application |
| 63 | + |
| 64 | +To access the application, we will use `curl` to send requests to the `coffee` and `tea` Services. |
| 65 | + |
| 66 | +To get coffee: |
| 67 | + |
| 68 | +``` |
| 69 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee |
| 70 | +Server address: 10.12.0.18:80 |
| 71 | +Server name: coffee-7586895968-r26zn |
| 72 | +``` |
| 73 | + |
| 74 | +To get tea: |
| 75 | + |
| 76 | +``` |
| 77 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea |
| 78 | +Server address: 10.12.0.19:80 |
| 79 | +Server name: tea-7cd44fcb4d-xfw2x |
| 80 | +``` |
| 81 | + |
| 82 | +## 5. Remove the ReferenceGrant |
| 83 | + |
| 84 | +To restrict access to Services in the `cafe` Namespace, we can delete the ReferenceGrant we created in |
| 85 | +Step 3: |
| 86 | + |
| 87 | +``` |
| 88 | +kubectl delete -f reference-grant.yaml |
| 89 | +``` |
| 90 | + |
| 91 | +Now, if we try to access the application over HTTP, we will get an internal server error: |
| 92 | +``` |
| 93 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea |
| 94 | +
|
| 95 | +<html> |
| 96 | +<head><title>500 Internal Server Error</title></head> |
| 97 | +<body> |
| 98 | +<center><h1>500 Internal Server Error</h1></center> |
| 99 | +<hr><center>nginx/1.25.1</center> |
| 100 | +</body> |
| 101 | +</html> |
| 102 | +``` |
| 103 | + |
| 104 | +You can also check the conditions of the HTTPRoutes `coffee` and `tea` to verify that the reference is not permitted: |
| 105 | + |
| 106 | +``` |
| 107 | +kubectl describe httproute coffee |
| 108 | +
|
| 109 | +Condtions: |
| 110 | + Message: Backend ref to Service cafe/coffee not permitted by any ReferenceGrant |
| 111 | + Observed Generation: 1 |
| 112 | + Reason: RefNotPermitted |
| 113 | + Status: False |
| 114 | + Type: ResolvedRefs |
| 115 | + Controller Name: k8s-gateway.nginx.org/nginx-gateway-controller |
| 116 | +``` |
| 117 | + |
| 118 | +``` |
| 119 | +kubectl describe httproute tea |
| 120 | +
|
| 121 | +Condtions: |
| 122 | + Message: Backend ref to Service cafe/tea not permitted by any ReferenceGrant |
| 123 | + Observed Generation: 1 |
| 124 | + Reason: RefNotPermitted |
| 125 | + Status: False |
| 126 | + Type: ResolvedRefs |
| 127 | + Controller Name: k8s-gateway.nginx.org/nginx-gateway-controller |
| 128 | +``` |
0 commit comments