How to configure different path for different target in <HTTPTargetConnection>

In my project we are using k8 platform and moving to eks. We want to do throttling between k8 and eks for atleast 2 weeks to fully migrate to eks. 

Now the path for eks and k8 are different. Like below

 

<TargetEndpoint name="default">
 
<HTTPTargetConnection>
   
<LoadBalancer>
<Algorithm>Weighted<Algorithm>
     
<Server name="eks">
<Weight>1</Weight>
</Server>
     
<Server name="k8">
<Weight>2</Weight>
</Server>
   
</LoadBalancer>
   
<Path>/eks</Path> or <Path>/k8</Path>
 
</HTTPTargetConnection>
</TargetEndpoint>

How we can achieve so that both request can be served with different path. Please help

 

1 5 329
5 REPLIES 5

The LoadBalancer is intended to balance traffic between identical origins, it does not support dynamic changes to the path. In my experience the best approach to this type of conditional routing is accomplished using javascript to set a variable for RouteRules and by having distinct targets defined. The code would do the following:

Grab a JSON object from KVM that defines your meta data for targets and amount of traffic to send - say something like:

 

{
 "eks": .3, // 30% of traffic goes to EKS
 "aks": .1, //10% of traffic goes to AKS
 "gke": 1 //the balance goes to GKE
}

 

Generate a random number between 0 and 1 to select the target. You can be as formal as you like in this logic, though it is very easy to make this harder than needs to be. This might look something like this:

 

var targets=context.getVariable("flow.weights"), //get value previously read from KVM
    randomNumber=Math.random(), //generates a random number between 0-1
    targetK8s="gke";

for (const [key, value] of Object.entries(targets)) {
  if(targets[key])<=randomNumber) {
    targetK8s=key;
    break; //break out of the for loop
  } 
}

context.setVariable("targetK8s", targetK8s);

 

The flow variable targetK8s then can be used to route to the appropriate target server.

Updates to KVM then allow you to redirect traffic as needed without redeploying proxies achieving essentially the same outcome as you would like to achieve using LoadBalancer features. 

Just wanted to add to this (already excellent) answer, Dino has an nice, detailed write up and example of Blue/Green routing in Apigee here, which you may also find useful.

My project is also in similar situation. For us, we don't have Apigee access to modify the weightage. We are controlling the weightage through the <HttpTargetConnection> and asked to deploy for every change in the weightage. The weightage is defined at the <loadBalancer> configuration, but the path needs to be different. If the path needs to be set dynamically using { } in the path tag, we need to assign the path in Javascript based on a condition. In this case, how to define a condition ? Based on the flow between two target server, the path differs. In this case, how to achieve using the <loadBalancer> configuration ?

If the path is required to be different then the loadbalancer tag is not going to work. When you say you don't have Apigee access, I presume you mean you can not change KVM values. Is that correct? If that is the case, then you could potentially utilize a configuration object in a shared flow, redeploying that shared flow would then allow you to effect changes to the weights without a full redploy of the proxy or changing anything in KVM. 

One way to do it is....  use the same path, but .... introduce an API Proxy facade in front of one of the two endpoints. 

So the model is, HttpTargetConnection with a LoadBalancer with two servers: server1 and server2.   Server1 points directly to /k8.  server2 points to... an Apigee API Proxy.  That proxy does this: changes the path, and proxies to /eks.