IRule Creation help

Hi Guys,

our marketing ask to to check if there is a possbility to create an IRule that said that 2% of the traffic that coming to specific virtual server will go a specfic pool.

For example:

I got a virtual server with the IP address of 1.1.1.1 which have a default pool named x.

I need a rule that say that once he got a traffic so 2% of the traffic will go to some other pool which is not the default pool of the virtual server.

Is it possible?

Thanks

Ido

I’m sure that some other folks will have ideas here too, but this particular issue is a little hard to solve directly. After all, how much of your traffic is 2%? You won’t really know what 2% was until your day is done and you’re counting requests in your server logs. But you can approximate it using something like below.

**Note: This uses the (totally great) table command, which requires 10.1 or higher. Also, this rule is an example only and it may not work for your requirements. That said, it will send 2% of HTTP requests to a specific place.

when HTTP_REQUEST {

    set requestNum [table incr "mkt_count" ]

    if { $requestNum >= 99 }{
        table set "mkt_count" 0
    }

    if { $requestNum == 1 or $requestNum == 2 } {
        log local0. "Got a 2% request. Counter is at $requestNum"
        pool marketing-pool
    } else {
       log local0. "Got a normal request. Counter is at $requestNum "
        pool default-pool
        }
}

Give this a whirl in your test environment and see if it will work for you. Obviously, change your pool names to match. Also, if any of the smart folks out there have ideas to bulletproof this approach please chime in! This is a very cool use case.

HTH,

-Matt

Hi Ido,

incidentally you can find information about the table command here

http://devcentral.f5.com/wiki/default.aspx/iRules/table.html

I hope this helps

Bhattman

Thanks a lot, I will try it on our test env.

Ido

when HTTP_REQUEST { 
  if { rand() > 0.02 } { 
    pool default-pool 
  } else { 
    pool marketing-pool 
  } 
}

Regards

Kevin

Hi Guys,

I have a similar requirement from my marketing team but they would like to send 90% of traffice to a default pool and then 5% to pool x and 5% to pool y . Is this achievable?

Thanks,

Martin

Sure, you could use effectively the same logic as above just move some numbers around. Send anything that’s 1-5 to poolA, 6-10 to poolB, and the rest to the main pool.

Colin

If you have any issues or questions, feel free to ask and I’m happy to help with the code.

Colin

Hi Colin,

How could I set the range from 1-5 for example using $requestNum or is there something I could do using the rand command above?

Thanks,

Martin

Hi Martin,

I think this is what Colin was suggesting:

when HTTP_REQUEST {
   Save a random number between 0 and 1
  set rand [expr { rand() }]

  if { $rand < .90 } { 
    pool poolA
  } elseif { $rand < .95 }{
    pool poolB
  } else {
    pool poolC
  }
}

Aaron

Thanks Aaron this seems to work well. How could I prove (to marketing) that the pools are being load balanced as requested. Do I need to create a stats profile?

Thanks,

Martin

You could look in the BIG-IP stats information in the GUI and show that each of the pools is receiving a given amount of traffic. Or you could build any kind of custom reporting you want with a stats profile or snmp info or the like.

Colin

So I assume that cookie insert persistence will not work with this irule. Would this still work if the irule directed to virtual servers (rather than pools ) and then apply a persistence profile to each vs?

Thanks,

Martin

Interesting, the problem is once you introduce persistence you have more of a chance of not distributing the traffic as planned..

I remember reading about a similar issue earlier in the year… Here is the post dug up.

http://devcentral.f5.com/Community/GroupDetails/tabid/1082223/asg/50/aft/1176748/showtab/groupforums/Default.aspx

Read through the post, you could try enabling a persistence profile and using “match across”.. but I’m not sure that is going to work for you and your cookie persistence requirements..

Also, the post makes reference to “ratio” load balancing.. Could you possibly use one pool and use Ratio for a LB metric?

Otherwise you could probably go at it a few ways.. One idea, what if you set a unique cookie for each pool during the random logic, then before the random logic you could check if either cookie exists and send it to the appropriate pool..If no cookie, then you start the random logic. Problem is now we would need to figure out how to take the existing persistent traffic into your % metric or you’re not going to be meeting your original goals..

What do you want the user persisted to? Do you want to ensure that once they go to poolA they always go to pool A? If so you could use your own cookie to do this:

when HTTP_REQUEST {
    Check if there is a pool selector cookie in the request
    Use a switch statement to ensure only valid pool names are present
    instead of accepting any pool name from the user supplied cookie
   switch [HTTP::cookie pool_cookie] {
      poolA -
      poolB -
      poolC {
          Select the pool from the cookie
         pool [HTTP::cookie pool_cookie]
         set selected ""
      }
      default {
          No pool selector cookie, so randomaly choose a pool
          Save a random number between 0 and 1
         set rand [expr { rand() }]
         if { $rand < .90 } { 
            pool poolA
            set selected poolA
         } elseif { $rand < .95 }{
            pool poolB
            set selected poolB
         } else {
            pool poolC
            set selected poolC
         }
      }
   }
}
when HTTP_RESPONSE {
    Set a pool selector cookie if a pool was selected for this request
   if {$selected ne ""}{
      HTTP::cookie insert name pool_cookie value $selected path "/"
   }
}

If you need persistence within the pool members you could use a default cookie insert persistence profile. This will “just work” even when using multiple pools on the same virtual server.

Aaron

Hello,

Is it possible once I implement the below iRule(25% percent of production web traffic being routed to staging_web pool) on our production web virtual server to then route all traffic from the staging_web pool to our staging_app pool of servers via the production app virtual server? The dev team does not want to change DNS entries once we do a deploy to staging then production. Could I apply an iRule on the production app virtual server routing any traffic from the staging web servers to the staging_app pool?

when HTTP_REQUEST { if { rand() > 0.25 } { pool default-pool } else { staging_web } }

Thank you,