solve-my-curiosity

X-Forwarded-For + Ingress(K8S) 본문

K8S

X-Forwarded-For + Ingress(K8S)

curiosity314 2025. 6. 8. 15:15

X-Forwarded-For에 대해 알아보자.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Forwarded-For

 

X-Forwarded-For header - HTTP | MDN

The HTTP X-Forwarded-For (XFF) request header is a de-facto standard header for identifying the originating IP address of a client connecting to a web server through a proxy server.

developer.mozilla.org

MDN의 설명처럼 X-Forwarded-For(줄여서 XFF)는 클라이언트의 원 IP를 추적하기 위해 사용되는 리퀘스트헤더이다. 

문법은 다음과 같다. 

X-Forwarded-For: <client>, <proxy>
X-Forwarded-For: <client>, <proxy>, …, <proxyN>

 

고로 맨 처음 있는 IP가 client의 IP이다. 

 

여러 프록시가 있지만 프록시의 종류마다 XFF헤더를 넣는 프록시가 있고 안 넣는 프록시도 있다.

프록시 종류 기본 동작 설명

nginx ❌ 기본은 안 넣음 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for로 명시해야 함
Apache HTTPD mod_proxy 설정 필요
HAProxy ⭕ 기본적으로 XFF 헤더 추가함  
Envoy ⭕ 기본적으로 넣지만, 커스터마이징 가능  
Ingress Controller (nginx 기반) ⭕ 보통 기본 설정에서 넣어줌 (use-forwarded-headers=true)  
Cloud LB (AWS ELB, GCP LB 등) ⭕ 자동으로 삽입  

 

그러면 이제 K8S에서 IP의 flow를 보도록 하자. (환경 : KIND(kubernetes In Docker)에서 실행했습니다 / Ingress-Nginx)

먼저 쿠버네티스의 IP흐름은 다음과 같다. 

1. 사용자가 도메인을 웹에서 검색 (web.localhost:80)

2. 도커의 portMapping 정보대로 이동 

47794a3c2183   kindest/node:v1.30.0   "/usr/local/bin/entr…"   8 weeks ago   Up 2 days   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 127.0.0.1:64163->6443/tcp   local-cluster-control-plane

3. ingress-controller pod의 hostPort 80으로 매핑 

│     Image ID:      registry.k8s.io/ingress-nginx/controller@sha256:5b161f051d017e55d358435f295f5e9a297e66158f136321d9b04520ec6c48a3
│     Ports:         80/TCP, 443/TCP, 8443/TCP
│     Host Ports:    80/TCP, 443/TCP, 0/TCP

4.ingress-controller Pod 안의 shell 접속 후 /etc/nginx/nginx.conf 확인

## start server web.localhost
        server {
                server_name web.localhost ;

                listen 80  ;
                listen [::]:80  ;
                listen 443  ssl http2 ;
                listen [::]:443  ssl http2 ;

5. Ingress 리소스 참조 (사실 ingress-controller가 계속 ingress 리소스 보면서 reconcile하므로 nginx.conf에 ingress yaml 정보 들어가있음)

spec:
│   rules:
│   - host: web.localhost
│     http:
│       paths:
│       - backend:
│           service:
│             name: web
│             port:
│               number: 80
│         path: /
│         pathType: Prefix

6. web service로 이동(web svc:80 -> pod:80으로 이동)

│   ports:
│   - port: 80
│     protocol: TCP
│     targetPort: 80
│   selector:

7. web pod로 이동(Pod yaml에는 포트 리스닝 정보 안들어가있어서 shell 접속 후 curl 명령어)

root@web-7c56dcdb9b-hsbhh:/# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@web-7c56dcdb9b-hsbhh:/#

로 알 수 있다. 

 

물론 로컬환경이 아니라 Production환경이였다면 아마 Ingress-controller Pod의 hostPort가 아닌 Ingress-controller-svc로 이동했을 것이다. 

 

 

(위 과정을 거치면서 처음으로 k9s를 써봤는데 진짜 편하다)

k9s cli

'K8S' 카테고리의 다른 글

Cert-Manager Mini Project (1)  (2) 2025.06.12
TLS handshake + ServiceAccount  (2) 2025.06.08
Kubernetes의 Log System 를 알아보자  (0) 2025.06.01
인턴 트러블슈팅 후기 2  (2) 2025.01.12
인턴 트러블슈팅 후기 1  (1) 2025.01.08