Can I set more than one failover Target?
Yes, Upstream supports multiple failover Targets.
Create a Service, a Route, and an Upstream with one of the latency
, least-connections
, or round-robin
load balancing strategies. Configure primary Targets on the Upstream with failover: false
and a failover Target with failover: true
.
This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.
Export your license to an environment variable:
export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
Run the quickstart script:
curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA
Once Kong Gateway is ready, you will see the following message:
Kong Gateway Ready
decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial, install decK version 1.43 or later.
This guide uses deck gateway apply
, which directly applies entity configuration to your Gateway instance.
We recommend upgrading your decK installation to take advantage of this tool.
You can check your current decK version with deck version
.
In this tutorial, we’re going to have two primary Targets and one failover Target. In this configuration, traffic will be routed to only the primary Targets if they are both healthy. If they are both unhealthy, traffic will be routed to the failover Target.
We’ll use three backends to represent our three Targets using Docker’s ultra-small hashicorp/http-echo
image:
docker run -d --rm --name primary1 -p 9001:5678 hashicorp/http-echo -text "PRIMARY-1"
docker run -d --rm --name primary2 -p 9002:5678 hashicorp/http-echo -text "PRIMARY-2"
docker run -d --rm --name failover -p 9003:5678 hashicorp/http-echo -text "FAILOVER"
Configure a Gateway Service and Route to point to the Upstream:
echo '
_format_version: "3.0"
upstreams:
- name: example-upstream
services:
- name: example-service
host: example-upstream
protocol: http
routes:
- name: example-route
paths:
- "/anything"
service:
name: example-service
' | deck gateway apply -
Now, you can configure Upstream with the two primary Targets failover: false
and one failover Target failover: true
.
Configure the first primary Target:
curl -X POST "http://localhost:8001/upstreams/example-upstream/targets/" \
-H "Accept: application/json" \
--json '{
"target": "host.docker.internal:9001",
"weight": 100,
"failover": false
}'
Configure the second primary Target:
curl -X POST "http://localhost:8001/upstreams/example-upstream/targets/" \
-H "Accept: application/json" \
--json '{
"target": "host.docker.internal:9002",
"weight": 100,
"failover": false
}'
Configure the failover Target:
curl -X POST "http://localhost:8001/upstreams/example-upstream/targets/" \
-H "Accept: application/json" \
--json '{
"target": "host.docker.internal:9003",
"weight": 50,
"failover": true
}'
Run the following to verify that only the primary Targets handle traffic because they are both healthy:
for i in {1..10}; do curl -sS http://localhost:8000/anything; echo; done
You’ll get an output like the following, where you can see the Targets cycling between PRIMARY-1
and PRIMARY-2
:
PRIMARY-2
PRIMARY-1
PRIMARY-1
PRIMARY-2
PRIMARY-2
PRIMARY-2
To validate that the failover Target works, let’s mark the primary Targets as unhealthy by shutting down the hosts. Run the following:
docker stop primary1 primary2
You can now validate that since the primary Targets are both unhealthy, only the failover Target routes traffic:
for i in {1..6}; do curl -s http://localhost:8000/anything; echo; done
This time, the response should show the FAILOVER
Target instead of PRIMARY
.
curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Can I set more than one failover Target?
Yes, Upstream supports multiple failover Targets.