Lua 스크립트로 웨이포인트 확장하기
Lua 스크립트로 웨이포인트 확장하기 (Extend waypoints with Lua scripts)
Istio는 TrafficExtension API를 통해 인라인 Lua 스크립트로 웨이포인트 프록시를 확장하는 기능을 제공해요. 앰비언트 모드에서 TrafficExtension 리소스는 targetRefs를 사용해 웨이포인트 프록시에 부착되어야 해요.
출처: Istio 문서
본문
시작하기 전에 (Before you begin)
- 앰비언트 모드 Getting Started 가이드를 따라 Istio를 설정하세요.
- Bookinfo 샘플 애플리케이션을 배포하세요.
- default 네임스페이스를 앰비언트 메시에 추가하세요.
- 테스트 소스로 curl 샘플 앱을 배포하세요:
$ kubectl apply -f @samples/curl/curl.yaml@
게이트웨이에서 (At a gateway)
게이트웨이 이름을 가져오세요:
$ kubectl get gateway
NAME CLASS ADDRESS PROGRAMMED AGE
bookinfo-gateway istio bookinfo-gateway-istio.default.svc.cluster.local True 42m
Lua 패리티 필터로 bookinfo-gateway를 대상으로 하는 TrafficExtension을 생성하세요. 이 필터는 x-number 요청 헤더를 읽고 값이 odd인지 even인지 나타내는 x-parity 응답 헤더를 추가해요. 값은 요청 처리 중에 동적 메타데이터에 저장되어, 응답 헤더를 쓸 때 사용할 수 있어요:
$ kubectl apply -f - <<EOF
apiVersion: extensions.istio.io/v1alpha1
kind: TrafficExtension
metadata:
name: parity-at-gateway
spec:
targetRefs:
- kind: Gateway
group: gateway.networking.k8s.io
name: bookinfo-gateway
phase: STATS
lua:
inlineCode: |
function envoy_on_request(request_handle)
local number = tonumber(request_handle:headers():get("x-number"))
if number == nil then return end
local parity = number % 2 == 0 and "even" or "odd"
request_handle:streamInfo():dynamicMetadata():set(
"envoy.filters.http.lua", "parity", parity)
end
function envoy_on_response(response_handle)
local meta = response_handle:streamInfo():dynamicMetadata():get(
"envoy.filters.http.lua")
if meta == nil then return end
response_handle:headers():add("x-parity", meta["parity"])
end
EOF
게이트웨이를 통한 트래픽 검증하기 (Verify the traffic via the gateway)
$ kubectl exec deploy/curl -- curl -s -o /dev/null -D - -H "x-number: 4" "http://bookinfo-gateway-istio.default.svc.cluster.local/productpage" | grep x-parity
x-parity: even
네임스페이스의 모든 서비스에 대해 웨이포인트에서 (At a waypoint, for all services in a namespace)
웨이포인트 프록시 배포하기 (Deploy a waypoint proxy)
웨이포인트 배포 지침을 따라 bookinfo 네임스페이스에 웨이포인트 프록시를 배포하세요:
$ istioctl waypoint apply --enroll-namespace --wait
트래픽이 서비스에 도달하는지 확인하세요:
$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null http://productpage:9080/productpage
200
웨이포인트 게이트웨이 이름을 가져오세요:
$ kubectl get gateway
NAME CLASS ADDRESS PROGRAMMED AGE
bookinfo-gateway istio bookinfo-gateway-istio.default.svc.cluster.local True 23h
waypoint istio-waypoint 10.96.202.82 True 21h
웨이포인트를 대상으로 하는 TrafficExtension을 생성하세요:
$ kubectl apply -f - <<EOF
apiVersion: extensions.istio.io/v1alpha1
kind: TrafficExtension
metadata:
name: parity-at-waypoint
spec:
targetRefs:
- kind: Gateway
group: gateway.networking.k8s.io
name: waypoint
phase: STATS
lua:
inlineCode: |
function envoy_on_request(request_handle)
local number = tonumber(request_handle:headers():get("x-number"))
if number == nil then return end
local parity = number % 2 == 0 and "even" or "odd"
request_handle:streamInfo():dynamicMetadata():set(
"envoy.filters.http.lua", "parity", parity)
end
function envoy_on_response(response_handle)
local meta = response_handle:streamInfo():dynamicMetadata():get(
"envoy.filters.http.lua")
if meta == nil then return end
response_handle:headers():add("x-parity", meta["parity"])
end
EOF
웨이포인트 프록시를 통한 트래픽 검증하기 (Verify the traffic via the waypoint proxy)
$ kubectl exec deploy/curl -- curl -s -o /dev/null -D - -H "x-number: 7" http://productpage:9080/productpage | grep x-parity
x-parity: odd
특정 서비스에 대해 웨이포인트에서 (At a waypoint, for a specific service)
네임스페이스 전체 필터를 제거하고 reviews 서비스만 대상으로 하는 필터로 교체하세요:
$ kubectl delete trafficextension parity-at-waypoint
reviews 서비스를 직접 대상으로 하는 TrafficExtension을 생성해서, 필터가 그 서비스로 향하는 트래픽에만 적용되도록 하세요:
$ kubectl apply -f - <<EOF
apiVersion: extensions.istio.io/v1alpha1
kind: TrafficExtension
metadata:
name: parity-for-reviews
spec:
targetRefs:
- kind: Service
group: ""
name: reviews
match:
- mode: SERVER
phase: STATS
lua:
inlineCode: |
function envoy_on_request(request_handle)
local number = tonumber(request_handle:headers():get("x-number"))
if number == nil then return end
local parity = number % 2 == 0 and "even" or "odd"
request_handle:streamInfo():dynamicMetadata():set(
"envoy.filters.http.lua", "parity", parity)
end
function envoy_on_response(response_handle)
local meta = response_handle:streamInfo():dynamicMetadata():get(
"envoy.filters.http.lua")
if meta == nil then return end
response_handle:headers():add("x-parity", meta["parity"])
end
EOF
서비스를 대상으로 하는 트래픽 검증하기 (Verify the traffic targeting the service)
$ kubectl exec deploy/curl -- curl -s -o /dev/null -D - -H "x-number: 3" http://reviews:9080/reviews/1 | grep x-parity
x-parity: odd
정리 (Cleanup)
TrafficExtension리소스를 제거하세요:
$ kubectl delete trafficextension parity-at-gateway parity-for-reviews
- 앰비언트 모드 제거 가이드를 따라 Istio와 샘플 테스트 애플리케이션을 제거하세요.