Forum Discussion

Paul_Brand_2026's avatar
Paul_Brand_2026
Icon for Nimbostratus rankNimbostratus
May 10, 2017

Inserting a Data Centre Cookie

Hi All,

Hoping someone can give a bit of guidance with an iRule.

I have a customer who has two data centres. With LTM active/standby pair in each. In addition to the F5 cookie insert for session persistence I need to also set another cookie to identify the data centre. This value is used by a cloud provider. The F5 does not need to take an action on the this cookie just set the value.

The logic for Data Centre 1 is

If cookie name Data_Centre is not present insert Data_Cente cookie with value of DC1

If cookie name Data_Centre is present and value is DC2. Remove the cookie and insert Data_Cente cookie with value of DC1

This my thinking on the iRule. Would be grateful for any feedback.

when HTTP_REQUEST {
 set the value for location in the cookie 
 set StrDC [HTTP::cookie "DtCntPrstc"] 
}
when HTTP_RESPONSE {  
 checks to see if there is a DtCnt2 or a cookie header with no value
 if { ($StrDC== "DtCnt2") or {$StrDC==""  } {
    HTTP::cookie remove "DtCntPrstc"
    HTTP::cookie insert name DtCntPrstc value "DtCnt1"

 }
}

Thanks

Paul.

  • Anesh's avatar
    Anesh
    Icon for Cirrostratus rankCirrostratus

    iRule would be part of shared configuration, so how would u identify datacenter based on this iRule, If the F5 fails over to standby device it would insert the same cookie as per the logic

     

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    Why not simply:

    For DC1:

    when HTTP_RESPONSE {
        HTTP::cookie insert name DtCntPrstc value "DtCnt1"
    }
    

    For DC2:

    when HTTP_RESPONSE {
        HTTP::cookie insert name DtCntPrstc value "DtCnt2"
    }
    

    for you don't seem to really need to care about an incoming cookie, right?

    Or is it that you need to provide a DC ID to the backend application servers in the very first client request?

  • Hi,

    You can try this irule

    when RULE_INIT {
        set static::local_dc "DtCnt1"
    }
    
    when HTTP_REQUEST {
        set setDCcookie 0
        if { [HTTP::cookie "DtCntPrstc"] ne $static::local_dc} {
            set setDCcookie 1
        }
    }
    when HTTP_RESPONSE {  
        if { $setDCcookie} {
            HTTP::cookie remove "DtCntPrstc"
            HTTP::cookie insert name DtCntPrstc value $static::local_dc
        }
    }