Traffic splitting
Set up weight-based routing between multiple apps.
Before you begin
-
Follow the Get started guide to install kgateway.
-
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
-
Get the external address of the gateway and save it in an environment variable.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESSkubectl port-forward deployment/http -n kgateway-system 8080:8080
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 helloworld -
Deploy 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 created -
Verify 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: kgateway-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 created when you set up the Sample app. 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 yaml -
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.for i in {1..20}; do curl -i http://$INGRESS_GW_ADDRESS:8080/hello \ -H "host: traffic.split.example:8080"; donefor i in {1..20}; do curl -i localhost:8080/hello \ -H "host: traffic.split.example"; doneExample output:
HTTP/1.1 200 OK server: envoy date: Wed, 12 Mar 2025 20:59:35 GMT content-type: text/html; charset=utf-8 content-length: 60 x-envoy-upstream-service-time: 110 Hello version: v3, instance: helloworld-v3-55bfdf76cf-nv545
Cleanup
You can remove the resources that you created in this guide.-
Remove the HTTP route resource.
kubectl delete httproute traffic-split -n helloworld -
Remove the Helloworld apps.
kubectl delete -n helloworld -f https://raw.githubusercontent.com/solo-io/gloo-edge-use-cases/main/docs/sample-apps/helloworld.yaml