|
| 1 | +# Example |
| 2 | + |
| 3 | +In this example we will deploy NGINX Kubernetes Gateway and configure traffic splitting for a simple cafe application. |
| 4 | +We will use `HTTPRoute` resources to split traffic between two versions of the application -- `coffee-v1` and `coffee-v2`. |
| 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 Coffee Application |
| 25 | + |
| 26 | +1. Create the Cafe 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-v1-7c57c576b-rfjsh 1/1 Running 0 21m |
| 38 | + coffee-v2-698f66dc46-vcb6r 1/1 Running 0 21m |
| 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-route.yaml |
| 53 | + ``` |
| 54 | + |
| 55 | +This `HTTPRoute` resource defines a route for the path `/coffee` that sends 80% of the requests to `coffee-v1` and 20% to `coffee-v2`. |
| 56 | +In this example, we use 80 and 20; however, the weights are calculated proportionally and do not need to sum to 100. |
| 57 | +For example, the weights of 8 and 2, 16 and 4, or 32 and 8 all evaluate to the same relative proportions. |
| 58 | + |
| 59 | +## 4. Test the Application |
| 60 | + |
| 61 | +To access the application, we will use `curl` to send requests to `/coffee`: |
| 62 | + |
| 63 | +``` |
| 64 | +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee |
| 65 | +``` |
| 66 | + |
| 67 | +80% of the responses will come from `coffee-v1`: |
| 68 | + |
| 69 | +``` |
| 70 | +Server address: 10.12.0.18:80 |
| 71 | +Server name: coffee-v1-7c57c576b-rfjsh |
| 72 | +``` |
| 73 | + |
| 74 | +20% of the responses will come from `coffee-v2`: |
| 75 | + |
| 76 | +``` |
| 77 | +Server address: 10.12.0.19:80 |
| 78 | +Server name: coffee-v2-698f66dc46-vcb6r |
| 79 | +``` |
| 80 | + |
| 81 | +### 5. Modify the Traffic Split Configuration |
| 82 | + |
| 83 | +Let's shift more of the traffic to `coffee-v2`. To do this we will update the `HTTPRoute` resource and change the weight |
| 84 | +of the `coffee-v2` backend to 80. Backends with equal weights will receive an equal share of traffic. |
| 85 | + |
| 86 | +1. Apply the updated `HTTPRoute` resource: |
| 87 | + |
| 88 | + ``` |
| 89 | + kubectl apply -f cafe-route-equal-weight.yaml |
| 90 | + ``` |
| 91 | + |
| 92 | +2. Test the application again: |
| 93 | + |
| 94 | + ``` |
| 95 | + curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee |
| 96 | + ``` |
| 97 | + |
| 98 | +The responses will now be split evenly between `coffee-v1` and `coffee-v2`. |
| 99 | + |
| 100 | +We can continue modifying the weights of the backends to shift more and more traffic to `coffee-v2`. If there's an issue |
| 101 | +with `coffee-v2`, we can quickly shift traffic back to `coffee-v1`. |
0 commit comments