Traffic splitting
Set up weight-based routing between multiple apps.
Before you begin
Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.
Deploy the Helloworld sample app
To demonstrate weighted routing for multiple apps, deploy 3 versions of the Helloworld sample app.
Create the helloworld namespace.
kubectl create namespace helloworldDeploy the Hellworld sample apps.
kubectl -n helloworld apply -f https://raw.githubusercontent.com/solo-io/gloo-edge-use-cases/main/docs/sample-apps/helloworld.yamlExample output:
service/helloworld-v1 created service/helloworld-v2 created service/helloworld-v3 created deployment.apps/helloworld-v1 created deployment.apps/helloworld-v2 created deployment.apps/helloworld-v3 createdVerify that the Helloworld pods are up and running.
kubectl -n default get pods -n helloworldExample output:
NAME READY STATUS RESTARTS AGE helloworld-v1-5c457458f-rfkc7 3/3 Running 0 30s helloworld-v2-6594c54f6b-8dvjp 3/3 Running 0 29s helloworld-v3-8576f76d87-czdll 3/3 Running 0 29s
Set up weighted routing
Create an HTTPRoute resource for the
traffic.split.exampledomain that routes 10% of the traffic tohelloworld-v1, 10% tohelloworld-v2, and 80% tohelloworld-v3.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: traffic-split namespace: helloworld spec: parentRefs: - name: http namespace: gloo-system hostnames: - traffic.split.example rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: helloworld-v1 port: 5000 weight: 10 - name: helloworld-v2 port: 5000 weight: 10 - name: helloworld-v3 port: 5000 weight: 80 EOFSetting Description spec.parentRefs.nameThe name and namespace of the gateway resource that serves the route. In this example, you use the gateway that you installed as part of the Get started guide. spec.hostnamesThe hostname for which you want to apply traffic splitting. spec.rules.matches.pathThe path prefix to match on. In this example, /is used.spec.rules.backendRefsA list of services you want to forward traffic to. Use the weightoption to define the amount of traffic that you want to forward to each service.Verify that the HTTPRoute is applied successfully.
kubectl get httproute/traffic-split -n helloworld -o yamlGet the external address of the gateway and save it in an environment variable.
Send a few requests to the
/hellopath. Verify that you see responses from all 3 Helloworld apps, and that most responses are returned fromhelloworld-v3.Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 200 OK HTTP/1.1 200 OK < server: envoy server: envoy < date: Mon, 20 Nov 2023 18:04:30 GMT date: Mon, 20 Nov 2023 18:04:30 GMT < content-type: text/html; charset=utf-8 content-type: text/html; charset=utf-8 < content-length: 60 content-length: 60 < x-envoy-upstream-service-time: 178 x-envoy-upstream-service-time: 178 < Hello version: v3, instance: helloworld-v3-8576f76d87-hhn4r
Cleanup
You can optionally remove the resources that you created as part of this guide.
Remove the HTTP route resource.
kubectl delete httproute traffic-split -n helloworldRemove the Helloworld apps.
kubectl delete -n helloworld -f https://raw.githubusercontent.com/solo-io/gloo-edge-use-cases/main/docs/sample-apps/helloworld.yaml