Forum Discussion

oscarnet_69487's avatar
oscarnet_69487
Icon for Nimbostratus rankNimbostratus
Feb 06, 2017

Irule to redirect round robin domain use datagroup ?

Hi:

 

how can i use data-group redirect domain

 

origin irule:

 

when RULE_INIT { set ::whichone 0 } when HTTP_REQUEST { switch $::whichone { 0{ HTTP::redirect "; } 1{ HTTP::redirect "; } 2{ HTTP::redirect "; } 3{ HTTP::redirect "; } 4{ HTTP::redirect "; } 5{ HTTP::redirect "; } } if { $::whichone == 5 } { set ::whichone 0 } else { incr ::whichone } }

 

=========================================== when i use data-group

 

ltm data-group internal /Common/redirect_dg { records { www00.abc.com { data 0 } www01.abc.com { data 1 } www02.abc.com { data 2 } www03.abc.com { data 3 } www04.abc.com { data 4 } www05.abc.com { data 5 } } type string }

 

irule + data-group

 

when RULE_INIT { set ::whichone 0 }

 

when HTTP_REQUEST { switch $::whichone { 0{ if { [class match $::whichone equals redirect_dg ] } { set REDIRECT [class match -value -- $::whichone equals redirect_dg] HTTP::redirect "http://$REDIRECT" } } 1{ if { [class match $::whichone equals redirect_dg ] } { set REDIRECT [class match -value -- $::whichone equals redirect_dg] HTTP::redirect "http://$REDIRECT" } } 2{ if { [class match $::whichone equals redirect_dg ] } { set REDIRECT [class match -value -- $::whichone equals redirect_dg] HTTP::redirect "http://$REDIRECT" } } 3{ if { [class match $::whichone equals redirect_dg ] } { set REDIRECT [class match -value -- $::whichone equals redirect_dg] HTTP::redirect "http://$REDIRECT" } } 4{ if { [class match $::whichone equals redirect_dg ] } { set REDIRECT [class match -value -- $::whichone equals redirect_dg] HTTP::redirect "http://$REDIRECT" } } 5{ if { [class match $::whichone equals redirect_dg ] } { set REDIRECT [class match -value -- $::whichone equals redirect_dg] HTTP::redirect "http://$REDIRECT" } } } if { $::whichone == 20 } { set ::whichone 0 } else { incr ::whichone }

 

}

but it is not work ? Does any help to me ? thank's.

 

4 Replies

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    I don't understand why this can't be done with the standard method of putting the 5 servers into a server pool and use the "round-robin" load balancing method?

     

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    Not a good idea to use a global variable, but would this work at all:

     

     

    when RULE_INIT {
        set ::whichone 0
    }
    
    when HTTP_REQUEST {
        set REDIRECT [class match -value -- $::whichone equals redirect_dg]
        if { $REDIRECT ne "" } {
            if { $::whichone == 20 } {
                set ::whichone 0
            } else {
                incr ::whichone
            }
            HTTP::redirect "http://$REDIRECT"
        }
    }
    

     

    ?

     

  • Hi Oscarnet,

    the use of global $::variables is since TMOS v10 not recommended and will degrade the CMP-operation mode of your Virtual Server to Single-CPU mode and may therefor cause a performance bottleneck.

    https://devcentral.f5.com/wiki/iRules.CMPCompatibility.ashx

    The recommended approach would be to migrate the usage of $::variables to $static::variables (aka. Static Namespace Variables). The required iRule code change is in your specific scenario pretty simple...

    ltm data-group internal /Common/redirect_dg {
        textrecords {
            0 {
                data www00.abc.com
            }
            1 {
                data www01.abc.com
            }
            2 {
                data www02.abc.com
            }
            3 {
                data www03.abc.com
            }
            4 {
                data www04.abc.com
            }
            5 {
                data www05.abc.com
            }
        }
        type string
    }
    
    when RULE_INIT {
        set static::whichone 0
    }
    
    when HTTP_REQUEST {
        set REDIRECT [class match -value -- $static::whichone equals redirect_dg]
        if { $REDIRECT ne "" } {
            if { $static::whichone == 5 } {
                set static::whichone 0
            } else {
                incr static::whichone
            }
            HTTP::redirect "http://$REDIRECT"
        }
    }

    Note: Keep in mind when CMP-operation is active, each CPU core will maintain an independent copy of the $static::variables value and will therefor have its own round robin decission. If this is a problem for you, then take a look to the [table] command to maintain a single counter across the individual CPU-cores.

    Beside of the impact on the CMP-operation, I think that using a rather simple "Random-Robin" algorythm instead of using a strict "Round-Robin" algorythm may be also a good idea. It will simplyfy the iRule logic pretty much for you...

    when RULE_INIT {
        set static::nodelist {
            www00.abc.com
            www01.abc.com
            www02.abc.com
            www03.abc.com
            www04.abc.com
            www05.abc.com
        }
    }
    when HTTP_REQUEST {
        HTTP::redirect "http://[lindex $static::nodelist [expr { int(rand()*[llength $static::nodelist])}]]"
    }

    Note: $static::nodelist defines the [list] of possible HOSTNAMES. The [lindex $static::nodelist ] command is used to parse the $static::nodelist and to extract a given a element by specifying a absolute position in the [list]. The [expr { int(rand()*[llength $static::nodelist])}] command generates a random number between 0 and the maximum number of entries in your $static::nodelist (e.g. = 0-5 in your case). The random value is then passed back to [lindex] to finally extract the value of the selected [list] item (aka. the HOSTNAME).

    Cheers, Kai