HTTP 헤더 수정 예제
HTTP 헤더 수정 예제 (HTTP Header Modifier Examples)
Gateway는 클라이언트로부터 오는 HTTP 요청의 헤더를 수정할 수 있어요. RequestHeaderModifier 필터를 사용해 인입 요청에 헤더를 추가, 제거, 수정할 수 있어요.
본문
Gateway는 클라이언트로부터 오는 HTTP 요청의 헤더를 수정할 수 있어요.
Echo 앱 배포하기 (Deploy the Echo App)
echo 서버로 이루어진 디플로이먼트를 사용할 거예요.
이 애플리케이션은 클라이언트에 응답하면서, 응답 본문에 원래 요청을 받은 Pod에 대한 정보를 포함해요. 이 정보를 사용해서 Gateway가 트래픽을 어떻게 조작하는지 보여드릴게요.
$ kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/1.20.2/examples/kubernetes/gateway/echo-basic.yaml
파드가 예상대로 실행 중인지 확인해볼게요.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
echo-1-7d88f779b-m6r46 1/1 Running 0 21s
echo-2-5bfb6668b4-n7llh 1/1 Running 0 21s
Cilium Gateway 배포하기 (Deploy the Cilium Gateway)
HTTP 헤더 수정은 인입 요청에서 HTTP 헤더를 추가, 제거, 수정하는 과정이에요. HTTP 헤더 수정을 구성하려면 하나 이상의 HTTP 필터와 함께 Gateway 객체를 정의해요. 각 필터는 커스텀 헤더 추가, 기존 헤더 수정처럼 인입 요청에 가할 특정 수정을 지정해요.
HTTP 요청에 헤더를 추가하려면 add 동작과 함께 RequestHeaderModifier 유형의 필터를 사용하고, 추가할 헤더의 이름과 값을 지정하세요.
request-header.yaml에서 예시 Gateway와 HTTPRoute 정의를 찾을 수 있어요:
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: cilium-gw
spec:
gatewayClassName: cilium
listeners:
- protocol: HTTP
port: 80
name: web-gw-echo
allowedRoutes:
namespaces:
from: Same
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: header-http-echo
spec:
parentRefs:
- name: cilium-gw
rules:
- matches:
- path:
type: PathPrefix
value: /add-a-request-header
filters:
- type: RequestHeaderModifier
requestHeaderModifier:
add:
- name: my-header-name
value: my-header-value
backendRefs:
- name: echo-1
port: 8080
이 예제는 my-header-name이라는 이름의 헤더를 my-header-value 값으로 추가해요.
Gateway와 HTTPRoute를 배포해볼게요:
$ kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/1.20.2/examples/kubernetes/gateway/request-header.yaml
앞선 kubectl 명령은 포트 80에서 수신하는 cilium-gw라는 Gateway를 만들어요.
$ kubectl get gateway cilium-gw
NAME CLASS ADDRESS PROGRAMMED AGE
cilium-gw cilium 172.18.255.200 8s
참고
EKS 같은 일부 프로바이더는 IP 주소 대신 완전한 도메인 이름(FQDN)을 사용해요.
인입 HTTP 요청 수정하기 (Modify incoming HTTP Requests)
이제 Gateway가 준비됐으니 HTTP 요청을 보낼 수 있어요.
$ curl -s http://$GATEWAY/add-a-request-header
{
"path": "/add-a-request-header",
"host": "172.18.255.200",
"method": "GET",
"proto": "HTTP/1.1",
"headers": {
"Accept": [
"*/*"
],
"My-Header-Name": [
"my-header-value"
],
"User-Agent": [
"curl/7.81.0"
],
"X-Envoy-Internal": [
"true"
],
"X-Forwarded-For": [
"172.18.0.1"
],
"X-Forwarded-Proto": [
"http"
],
"X-Request-Id": [
"61a72702-3dfa-4bc3-a21c-7544ef36af7b"
]
},
"httpPort": "3000",
"namespace": "default",
"ingress": "",
"service": "",
"pod": "echo-1-7d88f779b-m6r46"
}
curl이 성공하면 echo 서버가 보낸 응답 본문에서 인입 요청의 HTTP 헤더를 볼 수 있어요. 또한 Gateway가 헤더를 추가했다는 것도 볼 수 있어요.
remove 키워드와 헤더 이름 목록으로 헤더를 제거할 수도 있어요.
filters
- type: RequestHeaderModifier
requestHeaderModifier:
remove: ["x-request-id"]
필터에 remove-a-request-header 접두사 일치를 추가하면 x-request-id 헤더가 제거되는 걸 볼 수 있어요:
$ curl --fail -s http://$GATEWAY/remove-a-request-header
{
"path": "/remove-a-request-header",
"host": "172.18.255.200",
"method": "GET",
"proto": "HTTP/1.1",
"headers": {
"Accept": [
"*/*"
],
"User-Agent": [
"curl/7.81.0"
],
"X-Envoy-Internal": [
"true"
],
"X-Forwarded-For": [
"172.18.0.1"
],
"X-Forwarded-Proto": [
"http"
]
},
"httpPort": "3000",
"namespace": "default",
"ingress": "",
"service": "",
"pod": "echo-1-7d88f779b-m6r46"
}
기존 헤더를 편집하려면 set 동작으로 수정할 헤더의 값과 새로 설정할 헤더 값을 지정해주세요.
filters:
- type: RequestHeaderModifier
requestHeaderModifier:
set:
- name: x-request-id
value: set-cilium-header-value
필터에 edit-a-request-header 접두사 일치를 추가하면 x-request-id 헤더가 변경되는 걸 볼 수 있어요:
$ curl -s http://$GATEWAY/edit-a-request-header
{
"path": "/edit-a-request-header",
"host": "172.18.255.200",
"method": "GET",
"proto": "HTTP/1.1",
"headers": {
"Accept": [
"*/*"
],
"User-Agent": [
"curl/7.81.0"
],
"X-Envoy-Internal": [
"true"
],
"X-Forwarded-For": [
"172.18.0.1"
],
"X-Forwarded-Proto": [
"http"
],
"X-Request-Id": [
"set-cilium-header-value"
]
},
"httpPort": "3000",
"namespace": "default",
"ingress": "",
"service": "",
"pod": "echo-1-7d88f779b-m6r46"
}
더 알아보기 (Learn more)
- Cilium Gateway API — Cilium Gateway API 개요
- Gateway API (HTTP) — HTTP Gateway 예제
- Gateway API (HTTPS) — HTTPS Gateway 예제