Weight traffic to specific backends
Create an HTTPRoute
resource, and specify a weight
property under spec.rules[*].backendRefs[*].weight
to route traffic to specific backends.
Prerequisites
Kong Konnect
If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.
- The following Konnect items are required to complete this tutorial:
- Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
-
Set the personal access token as an environment variable:
export KONNECT_TOKEN='YOUR KONNECT TOKEN'
Copied to clipboard!
Deploy demo Services
This how-to deploys multiple Services to your Kubernetes cluster to simulate a production environment.
Deploy the Services and create routing resources:
kubectl apply -f https://developer.konghq.com/manifests/kic/echo-services.yaml -n kong
Create an HTTPRoute
To route HTTP traffic, create an HTTPRoute
resource pointing at your Kubernetes Service
:
echo 'apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: echo
namespace: kong
annotations:
konghq.com/strip-path: "true"
spec:
parentRefs:
- name: kong
rules:
- matches:
- path:
type: PathPrefix
value: /echo
backendRefs:
- name: echo
kind: Service
port: 80
- name: echo2
kind: Service
port: 80
' | kubectl apply -f -
Test your deployment
Send multiple requests through this Route and tabulate the results to check an even distribution of requests across the Services:
curl -s "$PROXY_IP/echo/hostname?iteration="{1..200} -w "\n" | sort | uniq -c
The results should look like this:
100 echo2-7cb798f47-gv6hs
100 echo-658c5ff5ff-tv275
Add Service weights
The weight
field overrides the default distribution of requests across Services. Each Service instead receives weight / sum(all Service weights)
percent of the requests.
-
Add weights to the Services in the HTTPRoute’s backend list:
kubectl patch -n kong --type json httproute echo -p='[ { "op":"add", "path":"/spec/rules/0/backendRefs/0/weight", "value":200 }, { "op":"add", "path":"/spec/rules/0/backendRefs/1/weight", "value":100 } ]'
Copied to clipboard! -
Send the same requests again. This time, roughly 1/3 of the requests go to
echo2
and 2/3 go toecho
:curl -s "$PROXY_IP/echo/hostname?iteration="{1..200} -w "\n" | sort | uniq -c
Copied to clipboard!The results should look like this:
133 echo-658c5ff5ff-tv275 67 echo2-7cb798f47-gv6hs
Copied to clipboard!
Cleanup
Uninstall KIC from your cluster
helm uninstall kong -n kong