[F5] iRules 구문 분석
F5의 iRules는 F5 BIG-IP LTM(Local Traffic Manager)에서 트래픽을 세부적으로 제어할 수 있도록 제공하는 TCL 기반의 스크립트 언어입니다. iRules를 사용하면 HTTP 요청을 특정 풀(Pool)로 라우팅하거나, SSL 오프로딩(SSL Offloading), 로깅(logging), 요청 변조(rewriting) 등의 다양한 기능을 구현할 수 있습니다.
iRules는 크게 이벤트(Event), 조건문(Conditional Statements), 액션(Actions) 세 가지 요소로 구성됩니다.
1. 이벤트 (when)
iRules는 특정 이벤트 발생 시 실행됩니다. when 키워드를 사용하여 이벤트를 정의할 수 있습니다.
대표적인 iRules 이벤트는 다음과 같습니다.
when HTTP_REQUEST | 클라이언트가 HTTP 요청을 보낼 때 실행 |
when HTTP_RESPONSE | 서버가 HTTP 응답을 반환할 때 실행 |
when CLIENTSSL_HANDSHAKE | SSL 핸드셰이크 발생 시 실행 |
when LB_SELECTED | 로드밸런서에서 대상 서버를 선택했을 때 실행 |
예제:
when HTTP_REQUEST {
log local0. "HTTP 요청이 감지되었습니다: [HTTP::uri]"
}
2. 조건문 (if / elseif / else)
iRules에서 조건문은 if, elseif, else를 사용합니다.
예제:
when HTTP_REQUEST {
set path [string tolower [HTTP::path]]
if { $path starts_with "/api/v1/" } {
log local0. "API v1 요청입니다."
} elseif { $path starts_with "/api/v2/" } {
log local0. "API v2 요청입니다."
} else {
log local0. "기타 요청입니다."
}
}
위 코드는 클라이언트가 요청한 URL 경로에 따라 로그를 다르게 남기는 예제입니다.
3. 액션 (Action)
액션은 특정 조건을 만족할 때 수행하는 동작을 의미합니다. 예를 들어, 특정 트래픽을 특정 풀(Pool)로 라우팅하거나, SSL을 비활성화하는 등의 작업이 있습니다.
pool <POOL_NAME> | 요청을 특정 풀로 전달 |
SSL::disable serverside | 서버 측 SSL 암호화를 비활성화 |
log local0. "메시지" | 로컬 로그에 메시지를 기록 |
HTTP::redirect "<https://example.com>" | 특정 URL로 리디렉션 |
HTTP::header insert "Header-Name" "Value" | HTTP 헤더 추가 |
예제:
when HTTP_REQUEST {
if { [HTTP::uri] starts_with "/secure" } {
HTTP::redirect "<https://secure.example.com>"
}
}
위 코드는 /secure 경로로 들어오는 요청을 https://secure.example.com으로 리디렉션하는 역할을 합니다.
iRules의 동작 과정 분석
아래 iRules 예제를 통해 F5의 트래픽 처리 흐름을 살펴보겠습니다.
[예제 코드]
when HTTP_REQUEST {
set requested_path [string tolower [HTTP::path]]
# 아래 path와 일치하면 APIM_Kong_Gateway로 LB
if { $requested_path starts_with "/api/v1/"
|| $requested_path starts_with "/api/v2/dev"
|| $requested_path starts_with "/account/api/v1.0"
}
{
pool p_pool-iRules_8080
set log_message ""
if { $requested_path starts_with "/api/v1/" } {
set log_message "/api/v1/"
} elseif { $requested_path starts_with "/api/v2/dev" } {
set log_message "/api/v2/dev"
} elseif { $requested_path starts_with "/account/api/con/v1.0" } {
set log_message "/account/api/con/v1.0"
}
log local0. "Requested Path: $log_message"
}
# 그 외 path는 default pool로 LB 됨
}
when LB_SELECTED {
# LB되는 실제 서버 IP 확인 후 SSL Offload 적용
set server_ip [LB::server addr]
# 10.225.31.21 또는 10.225.31.31로 선택되면 serverside SSL 비활성화 (SSL Offload)
if { $server_ip eq "10.225.31.21" || $server_ip eq "10.225.31.31" } {
SSL::disable serverside
log local0. "$log_message->Traffic forwarded to: [LB::server addr]:[LB::server port] (SSL Offload Disabled)"
}
}
1. 클라이언트가 HTTP 요청을 보냄
- when HTTP_REQUEST 이벤트가 발생
- 요청된 URL 경로를 requested_path 변수에 저장
- 특정 경로와 일치하면 p_pool-iRules_8080 풀로 트래픽 전달
- 요청된 경로를 로그에 기록
2. F5가 로드밸런싱을 수행하여 서버를 선택
- when LB_SELECTED 이벤트가 발생
- LB위해 선택된 서버의 IP를 확인 (LB::server addr) 후 server_ip 변수에 저장
- 특정 IP(10.225.31.21 또는 10.225.31.31)라면 SSL 오프로드 적용
이처럼 F5 iRules는 트래픽을 정교하게 제어할 수 있도록 하는 강력한 기능을 제공합니다.
- 이벤트(when) 를 통해 특정 시점에서 실행되며,
- 조건문(if, elseif, else) 을 이용해 트래픽을 분기하고,
- 액션(pool, log, SSL::disable) 을 사용해 트래픽을 적절히 처리할 수 있습니다.
[참고 사이트]
iRules HTTP : https://clouddocs.f5.com/api/irules/HTTP.html
iRules IP : https://clouddocs.f5.com/api/irules/IP.html
iRules LB : https://clouddocs.f5.com/api/irules/LB.html