Set up weight-based routing between multiple apps.

Before you begin

  1. Follow the Get started guide to install Gloo Gateway.

  2. Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.

  3. Get the external address of the gateway and save it in an environment variable.

Deploy the Helloworld sample app

To demonstrate weighted routing for multiple apps, deploy 3 versions of the Helloworld sample app.

  1. Create the helloworld namespace.

      kubectl create namespace helloworld
      
  2. 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.yaml
      

    Example 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
      
  3. Verify that the Helloworld pods are up and running.

      kubectl -n default get pods -n helloworld
      

    Example 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

  1. Create an HTTPRoute resource for the traffic.split.example domain that routes 10% of the traffic to helloworld-v1, 10% to helloworld-v2, and 80% to helloworld-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
    EOF
      
    SettingDescription
    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 weight option to define the amount of traffic that you want to forward to each service.
  2. Verify that the HTTPRoute is applied successfully.

      kubectl get httproute/traffic-split -n helloworld -o yaml
      
  3. Send a few requests to the /hello path. Verify that you see responses from all 3 Helloworld apps, and that most responses are returned from helloworld-v3.

    Example 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.
  1. Remove the HTTP route resource.

      kubectl delete httproute traffic-split -n helloworld
      
  2. 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