SYS-CON MEDIA Authors: Yeshim Deniz, Elizabeth White, Pat Romanski, Liz McMillan, Courtney Abud

Blog Feed Post

20 Lines or Less # 54: Pools, Geo-IP, and Mobile

What could you do with your code in 20 Lines or Less? That's the question I ask (sometimes?) every week for the DevCentral community, and every week I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.

This week the forums provide us with more examples of iRules wizardry (or at least apprentice awesomeness) in a scant 20 lines or less each. The credit goes to the awesome community for providing such frequent and awesome examples. This week's installation of iRules goodness in particular is brought to you by hoolio, who despite the snow storm (perhaps because of it?) was iRuling away like the mad man he is. Showing off how to simplify geographical based redirection, how to smoothly access a particular pool given the right configuration, and how to do some fancy matching to search for strings of digits.

Mobile Redirects

http://bit.ly/xRJ6Ig

In this example user Ruchir is looking to do some matching based on some mobile device needs. They have a somewhat complex set of requirements to strip out a set of 4 or 6 digits from a URI that could take multiple forms. Making clever use of the URI::path depth command and some intelligence built into switch, Aaron shows that this can be near trivial if you know what knobs to turn.

 

   1: when HTTP_REQUEST {
   2:     # Get the index of the last URI directory
   3:     set depth [URI::path [HTTP::uri] depth]
   4:  
   5:     # Parse the last directory in the path
   6:     set last_dir [URI::path [HTTP::uri] $depth $depth]
   7:  
   8:     # Parse everything after the last hyphen in the last directory
   9:     set digits [string trimleft [string range $last_dir [expr {[string last - $last_dir]}] end-1] -/]
  10:  
  11:     log local0. "URI=[HTTP::uri], \$depth=$depth, \$last_dir=$last_dir, \$digits=$digits"
  12:  
  13:     # Check if we parsed 4 or 6 digits
  14:     switch $digits {
  15:         [0-9][0-9][0-9][0-9] -
  16:         [0-9][0-9][0-9][0-9][0-9][0-9] {
  17:             # Found 4 or 6 digits, send a redirect
  18:             HTTP::redirect "http://m.site.com/test/?gid=$digits"
  19:         }
  20:     }
  21: }

 

Geo-IP Redirection

http://bit.ly/yGw81z

Geolocation is not a new concept in our products, but it is new to many users out there, and it's fantastic to see people bringing it up in the forums. In this example, the desire was to separate out several countries into their own landing pages. Aaron came through and made a much simpler version using switch and some cleaned up matching logic that shows this can be pretty easy indeed. I removed some of the country cases for brevity, but the idea remains intact.

 

   1: when HTTP_REQUEST {
   2:     if { [string tolower [HTTP::host]] equals "www.example.com" && [HTTP::path] eq "/" }{
   3:         # Parse the client IP from the CDN header
   4:         set client_ip [HTTP::header value "Client-IP"]
   5:         if { $client_ip eq "" }{
   6:             # The header was empty/did not exist, so use the actual client IP
   7:             set client_ip [IP::client_addr]
   8:         }
   9:         set country [string tolower [whereis $client_ip abbrev]]
  10:         switch $country {
  11:             "af" -
  12:             "bh" -
  13:             "ye" { HTTP::redirect "http://www.example.com/home-${country}" } 
  14:             default {
  15:                 # Redirect all others
  16:                 HTTP::redirect "http://www.example.com/home"
  17:             }
  18:         }
  19:     } else {
  20:         pool example_web_pool
  21:     }
  22: }

 

Pool Based on Inbound Port

http://bit.ly/yxSIJa

Every so often we get a request from a user that wants to select a pool directly based off of something within the request. I.E. they want to add "/pool1" to the URI or they want to, as in this case, use the port number and append that to a pre-defined pool name and automatically direct traffic to a specific pool. In this case it is a way to specifically select a given node, as the user has one member per pool. That being said, we can do this, but not without one inherent issue in particular. If the pool doesn't exist, the connection will, understandably, fail. So what is a good way around this? The catch command! Aaron demonstrates how this works and gives a way for a backup plan in this snippet.

 

   1: when CLIENT_ACCPEPTED {
   2:  
   3:     # Try assigning the pool based on the client destination port
   4:     # If the pool assignment fails, use the VS default pool
   5:     if {[catch {pool pool_[TCP::local_port]} result]}{
   6:         # Pool did not exist, so log the value for testing
   7:         # The VS default pool will be used
   8:         log local0. "pool_[TCP::local_port] doe not exist"
   9:     } else {
  10:         # Pool assignment succeeded
  11:     }
  12: }

 

Check back again next week, or better yet subscribe to the feed, for more examples of iRules less than 21 lines that you can add to your bag of tricks.

#Colin

 

Read the original blog entry...

More Stories By Colin Walker

Coming from a *Nix Software Engineering background, Colin is no stranger to long hours of coding, testing and deployment. His personal experiences such as on-stage performance and the like have helped to foster the evangelist in him. These days he splits his time between coding, technical writing and evangalism. He can be found on the road to just about anywhere to preach the good word about ADCs, Application Aware networking, Network Side Scripting and geekery in general to anyone that will listen.

Colin currently helps manage and maintain DevCentral (http://devcentral.f5.com). He is also a contributor in many ways, from Articles to Videos to numerous forum posts, to iRules coding and whatever else he can get his hands on that might benefit the community and allow it to continue to grow.

Latest Stories
Take advantage of autoscaling, and high availability for Kubernetes with no worry about infrastructure. Be the Rockstar and avoid all the hurdles of deploying Kubernetes. So Why not take Heat and automate the setup of your Kubernetes cluster? Why not give project owners a Heat Stack to deploy Kubernetes whenever they want to? Hoping to share how anyone can use Heat to deploy Kubernetes on OpenStack and customize to their liking. This is a tried and true method that I've used on my OpenSta...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
10ZiG Technology is a leading provider of endpoints for a Virtual Desktop Infrastructure environment. Our fast and reliable hardware is VMware, Citrix and Microsoft ready and designed to handle all ranges of usage - from task-based to sophisticated CAD/CAM users. 10ZiG prides itself in being one of the only companies whose sole focus is in Thin Clients and Zero Clients for VDI. This focus allows us to provide a truly unique level of personal service and customization that is a rare find in th...
Kubernetes is a new and revolutionary open-sourced system for managing containers across multiple hosts in a cluster. Ansible is a simple IT automation tool for just about any requirement for reproducible environments. In his session at @DevOpsSummit at 18th Cloud Expo, Patrick Galbraith, a principal engineer at HPE, will discuss how to build a fully functional Kubernetes cluster on a number of virtual machines or bare-metal hosts. Also included will be a brief demonstration of running a Galer...
Emil Sayegh is an early pioneer of cloud computing and is recognized as one of the industry's true veterans. A cloud visionary, he is credited with launching and leading the cloud computing and hosting businesses for HP, Rackspace, and Codero. Emil built the Rackspace cloud business while serving as the company's GM of the Cloud Computing Division. Earlier at Rackspace he served as VP of the Product Group and launched the company's private cloud and hosted exchange services. He later moved o...
92% of enterprises are using the public cloud today. As a result, simply being in the cloud is no longer enough to remain competitive. The benefit of reduced costs has normalized while the market forces are demanding more innovation at faster release cycles. Enter Cloud Native! Cloud Native enables a microservices driven architecture. The shift from monolithic to microservices yields a lot of benefits - but if not done right - can quickly outweigh the benefits. The effort required in monitoring,...
As you know, enterprise IT conversation over the past year have often centered upon the open-source Kubernetes container orchestration system. In fact, Kubernetes has emerged as the key technology -- and even primary platform -- of cloud migrations for a wide variety of organizations. Kubernetes is critical to forward-looking enterprises that continue to push their IT infrastructures toward maximum functionality, scalability, and flexibility. As they do so, IT professionals are also embr...
Signs of a shift in the usage of public clouds are everywhere. Previously, as organizations outgrew old IT methods, the natural answer was to try the public cloud approach; however, the public platform alone is not a complete solution. Complaints include unpredictable/escalating costs and mounting security concerns in the public cloud. Ultimately, public cloud adoption can ultimately mean a shift of IT pains instead of a resolution. That's why the move to hybrid, custom, and multi-cloud will ...
The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get tailored market studies; and more.
Docker is sweeping across startups and enterprises alike, changing the way we build and ship applications. It's the most prominent and widely known software container platform, and it's particularly useful for eliminating common challenges when collaborating on code (like the "it works on my machine" phenomenon that most devs know all too well). With Docker, you can run and manage apps side-by-side - in isolated containers - resulting in better compute density. It's something that many developer...
DevOps is under attack because developers don’t want to mess with infrastructure. They will happily own their code into production, but want to use platforms instead of raw automation. That’s changing the landscape that we understand as DevOps with both architecture concepts (CloudNative) and process redefinition (SRE). Rob Hirschfeld’s recent work in Kubernetes operations has led to the conclusion that containers and related platforms have changed the way we should be thinking about DevOps and...
The KCSP program is a pre-qualified tier of vetted service providers that offer Kubernetes support, consulting, professional services and training for organizations embarking on their Kubernetes journey. The KCSP program ensures that enterprises get the support they're looking for to roll out new applications more quickly and more efficiently than before, while feeling secure that there's a trusted and vetted partner that's available to support their production and operational needs.
In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It's clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. Th...
xMatters helps enterprises prevent, manage and resolve IT incidents. xMatters industry-leading Service Availability platform prevents IT issues from becoming big business problems. Large enterprises, small workgroups, and innovative DevOps teams rely on its proactive issue resolution service to maintain operational visibility and control in today's highly-fragmented IT environment. xMatters provides toolchain integrations to hundreds of IT management, security and DevOps tools. xMatters is the ...
Kubernetes is an open source system for automating deployment, scaling, and management of containerized applications. Kubernetes was originally built by Google, leveraging years of experience with managing container workloads, and is now a Cloud Native Compute Foundation (CNCF) project. Kubernetes has been widely adopted by the community, supported on all major public and private cloud providers, and is gaining rapid adoption in enterprises. However, Kubernetes may seem intimidating and complex ...