# Set Manual Cookie Peristence

**URL:** <https://community.f5.com/t/set-manual-cookie-peristence/58350>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [July 29, 2013, 5:23pm UTC](https://community.f5.com/t/set-manual-cookie-peristence/58350 "2013-07-29T17:23:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hank\_Stallings](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@Hank\_Stallings](https://community.f5.com/u/Hank_Stallings)\
**Post date:** [July 29, 2013, 5:23pm UTC](https://community.f5.com/t/set-manual-cookie-peristence/58350/1 "2013-07-29T17:23:41Z")

</div>

I’m trying to implement an iRule from wiki article at:

[https://devcentral.f5.com/wiki/irul…tence.ashx](https://devcentral.f5.com/wiki/irules.manual_cookie_persistence.ashx)

I am pretty new at iRules and I’m missing something, so I apologize in advance for my ignorance.

I have the following setup:

Virtual Server vs\_MyTwoServers

Default Pool Pool\_MyTwoServers

Persistence Profile mySiteCookie Parent profile: cookie Cookie Method: Http Cookie Insert Cookie Name: PersistCookie8hrs

Nodes: 10.6.0.68:80 (Server7910) 10.6.0.68:80 (Server7911)

I want to be able to add a uri query of and have that session traffic only go to Server7910

I’ve setup a data group list called my\_forced\_nodes:

Data Group List Name: my\_forced\_nodes

Type: String

String Records:

7910 := 10.6.0.68 80

7911 := 10.6.0.69 80

iRule from wiki article with my information in it:

-----------------------------------------------------------------------------

when RULE\_INIT {

set cookie\_name “my\_node\_forced”

}

when HTTP\_REQUEST {

Get node id from URL ending in ?node=server1, ?node=server2, etc.

set node\_forced [findstr [HTTP::uri] “?node=” 6]

Or from previous cookie

set has\_cookie [HTTP::cookie exists $::cookie\_name]

if { $node\_forced eq “” and $has\_cookie } {

set node\_forced [HTTP::cookie value $::cookie\_name]

}

Map node id to IP and port found in a class

Entries should be strings of the form

if { $node\_forced ne “” } {

set node\_data [findclass $node\_forced $::my\_forced\_nodes " "]

if { $node\_data ne “” } {

set node\_ip [getfield $node\_data " " 1]

set node\_port [getfield $node\_data " " 2]

pool Pool\_MyTwoServers member $node\_ip $node\_port

} else {

set node\_forced “”

}

}

}

when HTTP\_RESPONSE {

if { $node\_forced ne “” } {

Add a cookie to continue forcing node assignment

HTTP::cookie insert name $::cookie\_name value $node\_forced path “/”

}

elseif { $has\_cookie } {

Delete cookie and resume load balancing (with built-in persistence)

HTTP::cookie insert name $::cookie\_name value “” path “/”

HTTP::cookie expires $::cookie\_name 0 absolute

}

}

---

<div class="post-metadata">

**Author:** ![Kevin\_Stewart](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/kevin_stewart/32/15267_2.png) [@Kevin\_Stewart](https://community.f5.com/u/Kevin_Stewart)\
**Post date:** [July 29, 2013, 7:14pm UTC](https://community.f5.com/t/set-manual-cookie-peristence/58350/2 "2013-07-29T19:14:20Z")

</div>

Updated for v10/11 with minor changes:

```tcl
when RULE_INIT {
set static::cookie_name "my_node_forced"
}
when HTTP_REQUEST {
 Get node id from URL ending in ?node=server1, ?node=server2, etc.
set node_forced [findstr [HTTP::query] "node=" 5]

 Or from previous cookie
set has_cookie [HTTP::cookie exists $static::cookie_name]

 If node_forced (from query) isn't there but cookie exists - set node_forced = cookie value
if { $node_forced eq "" and $has_cookie } {
set node_forced [HTTP::cookie value $static::cookie_name]
}

 If node_forced (from query) is there but cookie is not - set cookie flag
if { $node_forced ne "" and !$has_cookie } {
set do_cookie 1
}

 Map node id to IP and port found in a class
if { $node_forced ne "" } {
set node_data [class match -value $node_forced equals my_forced_nodes]
if { $node_data ne "" } {
set node_ip [getfield $node_data " " 1]
set node_port [getfield $node_data " " 2]
pool local-pool member $node_ip $node_port
} 
} 
}
when HTTP_RESPONSE {
if { [info exists do_cookie] } {
unset do_cookie
 Add a cookie to continue forcing node assignment
HTTP::cookie insert name $static::cookie_name value $node_forced path "/"
} 
}

```

---

<div class="post-metadata">

**Author:** ![Hank\_Stallings](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@Hank\_Stallings](https://community.f5.com/u/Hank_Stallings)\
**Post date:** [July 29, 2013, 7:38pm UTC](https://community.f5.com/t/set-manual-cookie-peristence/58350/3 "2013-07-29T19:38:55Z")

</div>

Thanks Kevin!! It appears to be consistently working.

Appreciate your help!

Hank

---

<div class="post-metadata">

**Author:** ![Hank\_Stallings](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@Hank\_Stallings](https://community.f5.com/u/Hank_Stallings)\
**Post date:** [July 30, 2013, 6:48pm UTC](https://community.f5.com/t/set-manual-cookie-peristence/58350/4 "2013-07-30T18:48:37Z")

</div>

Add on to this question:

Can you dynamically build a data group list by querying a pool for a list of nodes? For instance in the example above, can an iRule determine the number of servers in the pool and assign a numeric value to the node member? Then instead of using uri query of node=7910, it could be node=1?

What I’m trying to get at is, can the iRule be generic that it would work for any VIP/Pool to be enable app owners to test each individual node without having to create a data group list and an iRule for each application.

Thanks,

Hank

---

<div class="post-metadata">

**Author:** ![Kevin\_Stewart](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/kevin_stewart/32/15267_2.png) [@Kevin\_Stewart](https://community.f5.com/u/Kevin_Stewart)\
**Post date:** [July 30, 2013, 8:07pm UTC](https://community.f5.com/t/set-manual-cookie-peristence/58350/5 "2013-07-30T20:07:34Z")

</div>

Here’s a minor modification of the above using a dynamically created list on CLIENT\_ACCEPTED:

```tcl
when RULE_INIT {
set static::cookie_name "my_node_forced"
}
when CLIENT_ACCEPTED {
 create simple pool member list
set my_nodes [members -list local-pool]
}
when HTTP_REQUEST {
 Get node id from URL ending in ?node=server1, ?node=server2, etc.
set node_forced [findstr [HTTP::query] "node=" 5]

 Or from previous cookie
set has_cookie [HTTP::cookie exists $static::cookie_name]

 If node_forced (from query) isn't there but cookie exists - set node_forced = cookie value
if { $node_forced eq "" and $has_cookie } {
set node_forced [HTTP::cookie value $static::cookie_name]
}

 If node_forced (from query) is there but cookie is not - set cookie flag
if { $node_forced ne "" and !$has_cookie } {
set do_cookie 1
}

 Map node id to IP and port found in a class
if { $node_forced ne "" } {
set node_data [lindex $my_nodes $node_forced]
if { $node_data ne "" } {
set node_ip [getfield $node_data " " 1]
set node_port [getfield $node_data " " 2]
pool local-pool member $node_ip $node_port
} 
} 
}
when HTTP_RESPONSE {
if { [info exists do_cookie] } {
unset do_cookie
 Add a cookie to continue forcing node assignment
HTTP::cookie insert name $static::cookie_name value $node_forced path "/"
} 
}

```

Essentially the same as before, but this time a list is created dynamically of all of the pool members (**set my\_nodes [members -list local-pool]**). Use **set my\_nodes [active\_members -list local-pool]** to get a list of only the available nodes. Then later just read that list entry to get the desired node (**set node\_data [lindex $my\_nodes $node\_forced]**).

An alternative method would be to modify the data group via shell script and TMSH commands on some specified interval. Create an external monitor that polls the pool members, rewrites the data group, then updates it in the config. Add this monitor to a “phantom pool” - a pool not tied to a virtual server. This method allows you to create a scheduled job that is backed up as part of the config.
