You are browsing documentation for an older version. See the latest documentation here.
Multiple Backends
HTTPRoute supports adding multiple Services under its BackendRefs
field. When you add multiple Services,
requests through the HTTPRoute are distributed across the Services. This guide walks through creating an HTTPRoute with multiple backend Services.
Before you begin ensure that you have Installed Kong Ingress Controller with Gateway API support in your Kubernetes cluster and are able to connect to Kong.
Before you begin ensure that you have Installed Kong Ingress Controller with Gateway API support in your Kubernetes cluster and are able to connect to Kong.
Prerequisites
Install the Gateway APIs
-
Install the Gateway API CRDs before installing Kong Ingress Controller.
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.1.0/standard-install.yaml
-
Create a
Gateway
andGatewayClass
instance to use.echo " --- apiVersion: gateway.networking.k8s.io/v1 kind: GatewayClass metadata: name: kong annotations: konghq.com/gatewayclass-unmanaged: 'true' spec: controllerName: konghq.com/kic-gateway-controller --- apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: kong spec: gatewayClassName: kong listeners: - name: proxy port: 80 protocol: HTTP " | kubectl apply -f -
The results should look like this:
gatewayclass.gateway.networking.k8s.io/kong created gateway.gateway.networking.k8s.io/kong created
Install Kong
You can install Kong in your Kubernetes cluster using Helm.
-
Add the Kong Helm charts:
helm repo add kong https://charts.konghq.com helm repo update
-
Install Kong Ingress Controller and Kong Gateway with Helm:
helm install kong kong/ingress -n kong --create-namespace
Test connectivity to Kong
Kubernetes exposes the proxy through a Kubernetes service. Run the following commands to store the load balancer IP address in a variable named PROXY_IP
:
-
Populate
$PROXY_IP
for future commands:export PROXY_IP=$(kubectl get svc --namespace kong kong-gateway-proxy -o jsonpath='{.status.loadBalancer.ingress[0].ip}') echo $PROXY_IP
-
Ensure that you can call the proxy IP:
curl -i $PROXY_IP
The results should look like this:
HTTP/1.1 404 Not Found Content-Type: application/json; charset=utf-8 Connection: keep-alive Content-Length: 48 X-Kong-Response-Latency: 0 Server: kong/3.0.0 {"message":"no Route matched with those values"}
Deploy multiple Services with HTTPRoute
- Deploy two echo Services so that you have multiple
BackendRef
s to use for traffic splitting:kubectl apply -f https://docs.konghq.com/assets/kubernetes-ingress-controller/examples/echo-services.yaml
The results should look like this:
service/echo created deployment.apps/echo created service/echo2 created deployment.apps/echo2 created
-
Deploy a HTTPRoute that sends traffic to both the services. By default, traffic is distributed evenly across all services:
echo 'apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: echo 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 -
The results should look like this:
httproute.gateway.networking.k8s.io/echo created
- Send multiple requests through this route and tabulating 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 --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 } ]'
The results should look like this:
httproute.gateway.networking.k8s.io/echo patched
-
Send the same requests and roughly 1/3 of the requests go to
echo2
and 2/3 going toecho
:curl -s "$PROXY_IP/echo/hostname?iteration="{1..200} -w "\n" | sort | uniq -c
The results should look like this:
67 echo2-7cb798f47-gv6hs 133 echo-658c5ff5ff-tv275