Forum Discussion

eLeCtRoN's avatar
eLeCtRoN
Icon for Cirrus rankCirrus
Nov 06, 2019

Virtual Server as a Memeber in a Load Balancing Pool, Load Balancing (Round Robin) via iRule to another Virtual Server

Hello,

 

my Scenario above I know is not working and per default not allowed, but I know the iRule command "VIRTUAL" so I can send requests from a

virtual server to another virtual server, so my question is can I build a round robin algorithm inside the iRule ? So that I can balance requests from a virtual server to maybe 4 another virtual server without pool, because is is not allowed to put in a vertual server pool another virtual server as members ! When someone has a other idea feel free for a answer ! If I could get a example iRule for this scenario it would be nice ! My idea would be an iRule which balance all requests from a virtual server to 4 another virtual server with round robin, not with matching header, url and so on, just round robin.

 

best regards

Manuel

 

5 Replies

  • Hi,

    One option is do it storing the RR index in a table.

    It's just an idea but it works for me.

    Something here is blocking me to send you an example.

     

    Regards

     

     

     

     

  • Hello cjunior,

     

    hmm I m not a F5 newbie but I can do with you answer almost nothing, can you a bit more explain, or I always perfer some example iRule becaue I'm not a iRule professional ;) but I do my best. So in a short version what I want to do is just send requestes from a virtual server to 4 other virtual server with a pool and I want to load balance the traffic between the 4 virtual server with iRule, a ex F5 professional told me If you want balances with the iRule, you would have to script your own distribution algorithm. I saw something like this some years ago here at the dev central but I can't find it anymore !

     

    regards

    • cjunior's avatar
      cjunior
      Icon for Nacreous rankNacreous

      Hey man, sorry about that.

      I'm reaching devcentral support to attach my iRule sample.

      I wish to this could help you.

      Regards.

  • Here it is:

    when RULE_INIT {
        # Pool to probe VS addr wich have virtual server addresses
        set static::pool_vs_members "pool_probe_vs_loadbalance"
     
        # Map virtual server destination and name
        array set static::map_vs_name {"10.10.10.1 80" vs_name1}
        array set static::map_vs_name {"10.10.10.2 80" vs_name2}
        array set static::map_vs_name {"10.10.10.3 80" vs_name3}
        
        # Cookie name persistence
        set static::persist_cookiename "LBserverAddr_${static::pool_vs_members}"
     
        # Enable debug to write log
        set static::enable_debug 1
    }
    when HTTP_REQUEST {
        # Check if you are persisted
        if { [HTTP::cookie exists $static::persist_cookiename] } {
            set virtual_destination [string map {\" ""} [HTTP::cookie value $static::persist_cookiename]]
            if { $static::enable_debug } { log local0.debug "Persisted member: $virtual_destination" }
        } else {
            set virtual_destination "NONE"
        }
        
        # Get the active members from probe pool
        set member_list [active_members -list $static::pool_vs_members]
        if { $static::enable_debug } { log local0.debug "Active members in $static::pool_vs_members: $member_list" }
        
        # Check if there are members UP
        if { $member_list eq "" } {
            log local0.err "There is no active members in pool $static::pool_vs_members"
            HTTP::respond 200 content {<html><body><h2>We are unavailable :(</h2><p>Please try again...</body></html>} Connection close
            return
        }
        
        # Check if persisted member is UP 
        if { $static::enable_debug } { log local0.debug "Check if member $virtual_destination is UP" }
        set idx [lsearch $member_list "$virtual_destination"]
        if { $idx eq -1 } {
            #No previous member, select new one
            if { $static::enable_debug } { log local0.debug "Load balance round robin new member" }
            #Incr table
            set idx [table incr "$static::pool_vs_members"]
            #Check if index greater than list count
            if { $idx >= [llength $member_list] } {
                #Reset table and get first member on pool
                set idx 0
                table set "$static::pool_vs_members" $idx
            }
            if { $static::enable_debug } { log local0.debug "RR member index: $idx" }
            set virtual_destination [lindex $member_list $idx]
            set send_cookie 1
        } else {
            set send_cookie 0
        }
        
        if { $static::enable_debug } { log local0.debug "Selected virtual server: $virtual_destination / Name: $static::map_vs_name($virtual_destination)" }
     
        # Move it to virtual server
        virtual $static::map_vs_name($virtual_destination)
    }
    when HTTP_RESPONSE {
        if { $send_cookie } {
            if { $static::enable_debug } { log local0.debug "Persist address $virtual_destination" }
            HTTP::cookie insert name $static::persist_cookiename value $virtual_destination
        }
    }
  • I've been thinking about to use default pool and built-int load balance, but I don't know the real impact to connect and detach from vs.

    You may try this second option but take care and test it before apply on production.

    Regards.

    # Default pool: pool_probe_vs_loadbalance
    when HTTP_REQUEST {
        LB::connect
        set member "[LB::server addr]:[LB::server port]"
        LB::detach
     
        switch ${member} {
            "10.10.10.1:80" { 
                virtual vs_name1
            }
            "10.10.10.2:80" { 
                virtual vs_name2
            }
            "10.10.10.3:80" { 
                virtual vs_name3
            }
            default {
                log local0.err "FAIL to map vistual server"
            }
        }
    }