본문 바로가기
클라우드/Azure

[Kubernetes] Kubernetes Forward Proxy를 통한 Outbound 통제

by worldcenter 2025. 1. 30.

Nginx Forward Proxy를 사용하여 Kubernetes Pod의 아웃바운드를 통제합니다.

 

테스트 환경

  • Azure Kubernetes 1.27.3
  • Network Plugin : Kubenet
  • 아키텍처 : Backend 클러스터에 있는 Pod에서 Frontend 클러스터에 있는 Nginx Forward Proxy를 거쳐 허용한 도메인만 외부로 나갈 수 있도록 구성해야 함

 

 

Network Flow

  1. Backend Pod에서 curl -vk https://google.com 을 실행합니다.
  2. Backend Pod는 Private DNS에서 Proxy Server 주소를 resolve 합니다.
  3. 이 때 Proxy Server의 IP는 Proxy Server Service IP(Type: Load Balancer) 입니다.
  4. Frontend Forward Proxy Pod에서 도메인을 필터링하여 외부와 통신합니다.

 

 

Frontend Cluster 상에서 Nginx Forward Proxy 구성

1. Nginx Forward Proxy Deployment를 배포합니다.

Nginx Forward Proxy를 사용하기 위해서는 ngx_http_proxy_connect_module 모듈이 설치되어 있어야 합니다.
금번 테스트에서는 해당 모듈이 설치된 Nginx Forward Proxy Docker Image를 사용해야 합니다.
# https://hub.docker.com/r/reiz/nginx_proxy
kubectl create deploy nginx-forward --image=reiz/nginx_proxy:latest --port=8888

 

2. Nginx Forward Proxy Service를 배포합니다.

apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/azure-load-balancer-internal: "true"
    service.beta.kubernetes.io/azure-load-balancer-internal-subnet: "<SubnetName>"
  labels:
    app: nginx-forward
  name: nginx-forward-svc
spec:
  ports:
  - port: 8888
    protocol: TCP
    targetPort: 8888
  selector:
    app: nginx-forward
  type: LoadBalancer

 

3. Nginx Forward Proxy Conf 파일을 수정합니다.

vi /usr/local/nginx/conf/nginx.conf
user www-data;
worker_processes auto;
daemon off; # Don't run Nginx as daemon, as we run it in Docker we need a foreground process.
events { }

http {
    server_names_hash_bucket_size 128;

    access_log /var/log/nginx_access.log;
    error_log /var/log/nginx_errors.log;

    # Whitelist
    server {
        listen       8888;
        server_name  google.com;
        server_name  *.google.com;
        proxy_connect;
        proxy_max_temp_file_size 0;
        resolver 8.8.8.8;
        location / {
           proxy_pass $scheme://$http_host;
           proxy_set_header Host $http_host;
        }
    }

    # Everything else is denied
    server {
        listen       8888;
        server_name ~.+;
        return 404;
    }

}

 

4. Private DNS에 Proxy Server DNS 추가합니다.(LoadBalancer Frontend IP)

 

 

Backend Cluster 상에서 Client Pod 구성

1. Client로 사용할 Deployment를 배포합니다.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: apache
  name: apache
spec:
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - image: httpd:latest
        name: httpd
        ports:
        - containerPort: 80
          protocol: TCP
        env:
        - name: http_proxy
          value: "forward.gaonnuri.site:8888"
        - name: https_proxy
          value: "forward.gaonnuri.site:8888"

 

 

참고 링크

https://learn.microsoft.com/ko-kr/azure/aks/http-proxy

 

HTTP 프록시를 사용하여 AKS(Azure Kubernetes Service) 노드 구성 - Azure Kubernetes Service

AKS(Azure Kubernetes Service) 노드에 HTTP 프록시 구성 기능을 사용합니다.

learn.microsoft.com

https://medium.com/humanscape-tech/nginx%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-forward-proxy-%EA%B5%AC%ED%98%84-91f3555549be

 

Nginx를 이용한 forward proxy 구현

안녕하세요. Humanscape Software Engineer David 입니다.

medium.com

https://github.com/chobits/ngx_http_proxy_connect_module

 

GitHub - chobits/ngx_http_proxy_connect_module: A forward proxy module for CONNECT request handling

A forward proxy module for CONNECT request handling - chobits/ngx_http_proxy_connect_module

github.com