Skip to content

Traffic Splitting

This scenario demonstrates traffic splitting: how the weight field in an HttpRoute can be used to specify the distribution of traffic between two backend references, two services.

Context

The customers service should already be deployed in the default namespace (see Shared Gateway).

Instructions

  1. Deploy customers-v2 backing deployment:

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: customers-v2
      labels:
        app: customers
        version: v2
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: customers
          version: v2
      template:
        metadata:
          labels:
            app: customers
            version: v2
        spec:
          serviceAccountName: customers
          containers:
          - image: gcr.io/tetratelabs/customers:2.0.0
            imagePullPolicy: Always
            name: svc
            ports:
            - containerPort: 3000
    
    kubectl apply -f traffic-split/customers-v2.yaml
    
  2. Define services for each v1 and v2 subsets:

    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: customers-v1
    spec:
      selector:
        app: customers
        version: v1
      ports:
      - port: 80
        name: http
        targetPort: 3000
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: customers-v2
    spec:
      selector:
        app: customers
        version: v2
      ports:
      - port: 80
        name: http
        targetPort: 3000
    
    kubectl apply -f traffic-split/customers-subsets.yaml
    
  3. Define an HttpRoute that splits traffic 80/20 between v1 and v2:

    ---
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: customers
    spec:
      hostnames:
      - customers.esuez.org
      parentRefs:
      - name: eg
      rules:
      - backendRefs:
        - name: customers-v1
          port: 80
          weight: 80
        - name: customers-v2
          port: 80
          weight: 20
    
    kubectl apply -f traffic-split/customers-route.yaml
    

Test it

Send a number of test request to the customers route:

for i in {1..10}; do
  curl http://customers.esuez.org/ --resolve customers.esuez.org:80:$GATEWAY_IP
done

About 80% of requests should go to v1.

Responses from the v2 service can be distinguished from v1 by their payload: v2 returns customer names and cities, whereas v1 returns only customer names.