Forum Discussion
Robert_47833
Altostratus
Jul 29, 2011how to insert cookie and persist to it
Hi,dear irule
I have a pool cjj which has 10 members ,ration is the load blance method
I need to insert cookie and persist with cookie ,
for example client1 ----->VS---->pool member1 ,then insert one cookie(session cookie) to it, then persist this connection to the same pool member.
when HTTP::request {
if [HTTP::uri] starts_with "help"
pool cjj
persist cookie insert BIGIP_ABURL 0
}
when I want to apply it to the VS,it told me I need to add a persist profile
but I don't want to add a profile.because there are 10 pools under this vs and in this irule(don't want to use persist none in them,too many pools)
it will affect them
1:how to resolve this?
2:in the rule above,I don't set the cookie value,bigip will set it atuomatically?
pls help
12 Replies
- The_Bhattman
Nimbostratus
Hi Jucao,
Have you taken a look at the following Codeshare example
http://devcentral.f5.com/wiki/iRules.Manual_cookie_persistence.ashx
I don't believe it requires a persistance profile
Bhattman - Robert_47833
Altostratus
Hi,Bhattman
Thank u for your reply
but actually in my enviorment ,uri didn't end like this
set node_forced [findstr [HTTP::uri] "?node=" 6]
so I can't use this.
do u have another way to reslove this?
or do u know how to optimize rand()? - The_Bhattman
Nimbostratus
Hi Jucao,
Here is another way to persist without a profile
http://devcentral.f5.com/wiki/iRules.ElectionHashLoadBalancingAndPersistence.ashx
You should be able to make alterations so you can use Election Hash persistence based on the URI
Bhattman - Robert_47833
Altostratus
Hi,Bhattman
Thank u for your reply
sorry to not to tell u the whole story
what I am trying to acieve is:
for some specific uri,I want to send them to two pool with ration
for example :
pool cjj1 20% traffic
pool cjj2 80% traffic
and for the traffic hit pool cjj1,make them hit cjj1 always during the session,for pool cjj2,do the same
so I use:
1:when HTTP_REQUEST timing on {
if {[class match $uri starts_with gen3abURLs_SRWD29]} {
if {[HTTP::cookie valueA_cookie] == 1}{
pool cjj1
return
}
if {[HTTP::cookie value B_cookie] == 1}{
pool cjj2
return
}
if { rand() <0.2 } {
pool cjj1
set A_cookie 1
return
}
else {
pool cjj2
set B_cookie 1
return
}
when HTTP_RESPONSE {
if { $A_cookie == 1 }{
HTTP::cookie insert name A_cookie value 1 path "/"
}
if { $B_cookie == 1 }{
HTTP::cookie insert name B_cookie value 1 path "/"
}
this irule do meet my requirement,but I checked the performance,it is not good,I think it is rand() make the performance bad,but I don't know how to optimize it
that is why I am trying to work around this
2:so I want to put members in cjj1 and cjj2 to a single pool ,we suppose cjjall and use ratio method in pool
the next step is to persist the request to the same pool member without profile
the link http://devcentral.f5.com/wiki/iRules.ElectionHashLoadBalancingAndPersistence.ashx show us how to lb to pool members ,but not to ratio
do u have some metod to work around this? - The_Bhattman
Nimbostratus
Hi Jucao,
Thanks for explanation. This was extremely helpful in understanding your issue.
Let's first approach your first issue where you want to make the iRule more efficient and bring down the cycles, before moving into item 2
The following is the code re-written to use switch commands and expression commands.when HTTP_REQUEST timing on { if {[class match $uri starts_with gen3abURLs_SRWD29] } { log local0. "Entering testi" set rand [expr { rand() }] set value_cookie "" switch -glob [HTTP::cookie value_cookie] { "1" { pool cjj1 } "2" { Pool cjj2" } default { if { $rand <0.2 } { Pool cjj1 set value_cookie 1 } else { pool cjj2 set value_cookie 2 } } } } } when HTTP_RESPONSE { if {$value_cookie ne "" } { HTTP::cookie insert name value_cookie value $value_cookie path "/" } }
Hopefully that will drop the cycles down enough to be efficient for you.
Bhattman - Robert_47833
Altostratus
Hi,Bhattman
the performance has improved in my env
pls see
[jucao@srwd00lba001:/S1-green-P:Active] ~ b rule SRWD29-Main-rb1115test show all
RULE SRWD29-Main-rb1115test
+-> HTTP_REQUEST 182 total 0 fail 0 abort
| | Cycles (min, avg, max) = (0, 743327, 940739)
+-> HTTP_RESPONSE 182 total 0 fail 0 abort
| Cycles (min, avg, max) = (0, 0, 0)
[jucao@srwd00lba001:/S1-green-P:Active] ~ b rule SRWD29-Main-rb1112 show all
RULE SRWD29-Main-rb1112
+-> HTTP_REQUEST 64 total 0 fail 0 abort
| | Cycles (min, avg, max) = (0, 428880, 527337)
+-> HTTP_RESPONSE 64 total 0 fail 0 abort
| Cycles (min, avg, max) = (0, 0, 0)
hhahaha,
thanks very much
I need to tell u if I put set value_cookie "" under the if {[class match $uri starts_with gen3abURLs_SRWD29] },in bigip log,we will see error ,like we don't have value_cookie
so I put set value_cookie "" under the when HTTP_REQUEST timing on .
Is it right?
your script is so great to resolve this.
Do u have method to fix the item2? - The_Bhattman
Nimbostratus
Hi Jucao,
Good to hear that the cycles came down.
I don't have syntax for item 2 but I suppose it would be somewhat similar to item 1 code
The following is untested iRule.
when HTTP_REQUEST timing on {
set uri [string tolower [HTTP::uri]]
if {[class match $uri starts_with gen3abURLs_SRWD29] } {
if {[HTTP::cookie value_cookie] ne "" } {
pool cjjlb member $value_cookie
}
}
}
when LB_SELECTED {
set lbvalue "[LB::server_addr] [LB::server port]"
when HTTP_RESPONSE {
if {$lbvalue ne "" } {
HTTP::cookie insert name value_cookie value $lbvalue path "/"
}
}
I hope this helps
Bhattman - Robert_47833
Altostratus
Hi,Bhattman
1:can u tell me why item1 has improved
set rand [expr { rand() }]
I have changed it as
set BigipABurl_cookie ""
if {[class match $uri starts_with gen3abURLs_SRWD29]} {
set rand [expr { rand() }]
switch -glob [HTTP::cookie BigipABurl_cookie] {
"1" {
pool SRWD29-G3-BRX
return
}
"2" {
pool SRWD29-BROWSE
return
}
default {
if { $rand <0.2 } {
pool SRWD29-G3-BRX
set BigipABurl_cookie 1
return
}
else {
pool SRWD29-BROWSE
set BigipABurl_cookie 2
return
}
}
}
}
when HTTP_RESPONSE {
if {$BigipABurl_cookie ne "" } {
HTTP::cookie insert name BigipABurl_cookie value $BigipABurl_cookie path "/"
}
2:I have changed it as:
when HTTP_REQUEST timing on {
set uri [string tolower [HTTP::uri]]
if {[class match $uri starts_with gen3abURLs_SRWD29] } {
if {[HTTP::cookie value_cookie] ne "" } {
pool cjjlb member $value_cookie
}
else {
pool cjjb
}
}
when LB_SELECTED {
set lbvalue "[LB::server_addr] [LB::server port]"
when HTTP_RESPONSE {
if {$lbvalue ne "" } {
HTTP::cookie insert name value_cookie value $lbvalue path "/"
}
}
for this method,I have a question
we didn't define $value_cookie
also I want to know when n variable will change during the connection
for example ,we define $value_cookie ,and it get a value in http_request,we can find this value in http_response - Robert_47833
Altostratus
btw,what is the unit of cycle?
[jucao@srwd00lba001:/S1-green-P:Active] ~ b rule SRWD29-Main-rb1112 show all
RULE SRWD29-Main-rb1112
+-> HTTP_REQUEST 64 total 0 fail 0 abort
| | Cycles (min, avg, max) = (0, 428880, 527337)
+-> HTTP_RESPONSE 64 total 0 fail 0 abort
| Cycles (min, avg, max) = (0, 0, 0)
what does it mean? for example should I notice if it is 500000 ,how about 300000.
do they have big difference? - Robert_47833
Altostratus
hI,Bhattman
In item 2,
when HTTP_REQUEST timing on {
set uri [string tolower [HTTP::uri]]
if {[class match $uri starts_with gen3abURLs_SRWD29] } {
if {[HTTP::cookie value_cookie] ne "" } {
pool cjjlb member $value_cookie
}
else {
pool cjjb
}
}
when LB_SELECTED {
set lbvalue "[LB::server_addr] [LB::server port]"
when HTTP_RESPONSE {
if {$lbvalue ne "" } {
HTTP::cookie insert name value_cookie value $lbvalue path "/"
}
}
first request is ok,IE can get the page and get the cookie(10.80.29.15 80)
However,the second request with the cookie ,it doesn't work,even the value_cookie is ip and port of the node
if I change it to the exact node ,such as, pool cjjlb member 10.80.29.15 80 in irule,it works,
I don't know why
can u try it in your lab
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects