Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
Jul 04, 2011

need help in forward uri to different pool via specific percentage

I have two pool: cjj1 ,cjj2

 

 

1:for uri in data group aaa, send 80% request to cjj1 and 20% to cjj2

 

2:if one client visit cjj1,all other requests from this client should be sent to cjj1,so I think we should do cookie persisit here

 

 

how to achieve this via irule.

 

we suppose the new cookie is ttttttt

 

thanks very much

 

7 Replies

  • Hi Jucao,

     

     

    You can create a string datagroup with the URIs and then use the class command (v10) or the matchclass command (v9) to do the lookup. You can use the rand function to do ratio load balancing between the pools. For requests sent to cjj pool, you can set a cookie and check for that cookie before selecting between the pools. Cookie insert persistence would not work for this scenario as default persistence ensures persistence within a single pool.

     

     

    Here are a few related links:

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/class

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/matchclass

     

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

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/http__cookie

     

     

    Aaron
  • Hi,dear hoolio

     

    can u give me an example for this,

     

    I don't understand this part:Cookie insert persistence would not work for this scenario as default persistence ensures persistence within a single pool.

     

     

    I want to insert a new cookie and persist it

     

     

    how to achieve this?
  • Hi Jucao,

    Cookie insert persistence can only be used to persist between members of one pool, not between different pools. Instead you can insert your own cookie and look for that in requests.

    Here's a rough, untested example of what I was thinking of. Note that I've used a session cookie. If the user closes their browser after receiving the cjj1 cookie, they'll get load balanced again and could end up going to the other pool. If you change that to a time-expired cookie to avoid that, over time, more than 20% of the users will get pinned to the cjj1 pool.

    when HTTP_REQUEST {
    
        Track whether to set a cookie to force use of the cjj1 pool
       set cjj1_cookie 0
    
        Check if request already has a cookie indicating 
        they should be sent to the cjj1 pool
       if {[HTTP::cookie value cjj1_cookie] == 1}{
    
           Select the cjj1 pool
          pool cjj1_pool
    
           Exit this event in this rule
          return
       }
    
        If we got here, request did not have a pool selector cookie
       if {[class match [HTTP::uri] starts_with cjj1_uri_class]}{
    
          if { rand() < 0.20 } { 
    
              Select the cjj1 pool
             pool cjj1_pool
    
      Track whether to set a cookie to force use of the cjj1 pool
             set cjj1_cookie 1
          }
           Default action if a pool was not selected above will be to use the VS default pool
       }
    }
    when HTTP_RESPONSE {
    
        Check if request was for a cjj1 URI
       if { $cjj1_cookie == 1 }{
    
           Insert a session cookie in the response
          HTTP::cookie insert cjj1_cookie value 1 path "/"
       }
    }
    

    Aaron
  • hI,I have worked out,but still need to confirm with u in some details

     

    Can u have a look at the irule below

     

     

    when HTTP_RESPONSE {

     

    if { $stubhubA_cookie == 1 }{

     

    HTTP::cookie insert name stubcjjA_cookie value 1 path "/"

     

    HTTP::cookie expires "stubcjjA_cookie" "25920000" relative

     

    }

     

    if { $stubhubB_cookie == 1 }{

     

    HTTP::cookie insert name stubhubB_cookie value 1 path "/"

     

    HTTP::cookie expires "stubhubB_cookie" "25920000" relative

     

    }

     

    }

     

    when HTTP_REQUEST {

     

     

    set stubcjjA_cookie 0

     

    set stubcjjB_cookie 0

     

     

     

    if {[class match $uri starts_with gen3abURLs_SRWD29]} {

     

     

    if {[HTTP::cookie value stubcjjA_cookie] == 1}{

     

    pool SRWD29-G3-BRX

     

    log local0. "BRX $stubcjjA_cookie PERSIST"

     

    return

     

    }

     

    if {[HTTP::cookie value stubcjjB_cookie] == 1}{

     

    pool SRWD29-BROWSE

     

    log local0. "BRX $stubcjjB_cookie PERSIST"

     

    return

     

    }

     

    if { rand() <0.2 } {

     

    pool SRWD29-G3-BRX

     

    set stubhubA_cookie 1

     

    log local0. "BRX stubhubA_cookie $stubcjj_cookie "

     

    } else {

     

    pool SRWD29-BROWSE

     

    set stubhubB_cookie 1

     

    log local0. "BRS stubhubB_cookie $stubcjjB_cookie "

     

    return

     

    }

     

    }

     

     

     

     

    questiones:

     

    1:when u mention session cookie ,it means when I close the broswer,the cookie will expire?

     

    how to set cookie as session cookie or permanent cookie

     

     

    2:in the HTTP_RESPONSE event,it will affect all the http response? if some traffic didn't have the cookie,what will irule do?

     

  • If you set an expire time of something other than 0 on the cookie it should be saved by the client on the filesystem and sent on each request until it expires. If you don't set an expire time or set it to 0, then the client should store it in memory and not send it after the browser is closed.

     

     

    If no cookie needs to be set in HTTP_RESPONSE the example I posted won't do anything.

     

     

    Aaron
  • If you set an expire time of something other than 0 on the cookie it should be saved by the client on the filesystem and sent on each request until it expires. If you don't set an expire time or set it to 0, then the client should store it in memory and not send it after the browser is closed.

     

     

    If no cookie needs to be set in HTTP_RESPONSE the example I posted won't do anything.

     

     

    Aaron
  • Hi,Hoolio

     

    you give me great help on this,I respect u

     

     

    have a good day