NGINX를 사용해서 TCP / UDP 트래픽을 프록시하고 로드 밸런싱하는 방법에 대해 설명합니다.
소개
로드 밸런싱은 네트워크 트래픽을 여러 백엔드 서버에 효율적으로 분산하는 것을 말합니다.
Prerequisities
- '--with-stream' 를 포함하여 빌드된 NGINX
Reverse Proxy 구성
먼저, NGINX가 클라이언트의 TCP / UDP 를 프록시된 서버로 전달할 수 있도록 리버스 프록시를 구성해야 합니다.
proxy_pass에는 서버가 트래픽을 전달할 proxy된 서버나 upstream 그룹을 지정합니다.
stream {
server {
listen 12345;
#TCP traffic will be forwarded to the "stream_backend" upstream group
proxy_pass stream_backend;
}
server {
listen 12346;
#TCP traffic will be forwarded to the specified server
proxy_pass backend.example.com:12346;
}
server {
listen 53 udp;
#UDP traffic will be forwarded to the "dns_servers" upstream group
proxy_pass dns_servers;
}
# ...
}
프록시 서버에 여러 네트워크 인터페이스가 있는 경우, NGINX가 upstream 서버에 연결할 때 특정 IP 주소를 사용하도록 선택적으로 구성할 수 있습니다. 이는 프록시된 서버가 특정 IP 의 연결을 허용하도록 구성된 경우 유용할 수 있습니다.
proxy_bind에 네트워크 인터페이스 IP 주소를 지정합니다.
stream {
# ...
server {
listen 127.0.0.1:12345;
proxy_pass backend.example.com:12345;
proxy_bind 127.0.0.1:12345;
}
}
TCP / UDP 로드 밸런싱 구성
트래픽이 로드 밸런싱될 서버나 upstream 그룹을 생성합니다. upstream 그룹의 이름은 앞서 구성한 리버스 프록시를 위해 설정된 proxy_pass 지시어에서 사용됩니다.
upstream 그룹을 upstream 서버들로 채웁니다.
upstream 그룹에서 사용할 로드 밸런싱 방법을 구성할 수 있습니다.
- 라운드 로빈 - NGINX는 기본적으로 라운드 로빈 알고리즘을 사용하여 설정된 상위 그룹 서버들에 순차적으로 트래픽을 분배합니다.
- 최소 연결 - 현재 활성 연결 수가 가장 적은 서버를 선택합니다.
stream {
upstream stream_backend {
server backend1.example.com:12345;
server backend2.example.com:12345;
server backend3.example.com:12346;
# ...
}
upstream dns_servers {
least_conn
server 192.168.136.130:53;
server 192.168.136.131:53;
# ...
}
# ...
}
다른 방법으로는, upstream 그룹 대신 단일 서버로 트래픽을 프록시할 수 있습니다. 호스트네임으로 서버를 식별하고 해당 호스트네임이 여러 IP 주소로 해석되도록 구성하면 NGINX는 라운드 로빈 알고리즘을 사용하여 IP 주소간에 트래픽을 로드밸런싱합니다. 이 경우, proxy_pass에 서버의 포트번호를 지정해야하며, IP 주소나 호스트네임 앞에 프로토콜을 명시해서는 안 됩니다.
stream {
# ...
server {
listen 12345;
proxy_pass backend.example.com:12345;
}
}
-----
TCP / UDP 로드 밸런싱 예제
stream {
upstream stream_backend {
least_conn;
server backend1.example.com:12345 weight=5;
server backend2.example.com:12345 max_fails=2 fail_timeout=30s;
server backend3.example.com:12345 max_conns=3;
}
upstream dns_servers {
least_conn;
server 192.168.136.130:53;
server 192.168.136.131:53;
server 192.168.136.132:53;
}
server {
listen 12345;
proxy_pass stream_backend;
proxy_timeout 3s;
proxy_connect_timeout 1s;
}
server {
listen 53 udp;
proxy_pass dns_servers;
}
server {
listen 12346;
proxy_pass backend4.example.com:12346;
}
}
References
https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/
'데이터 엔지니어링' 카테고리의 다른 글
Flink DataSources 정의 및 구성 요소 (0) | 2024.05.20 |
---|---|
Dataflow Windowing : watermark (0) | 2024.05.17 |
Apache Spark : Join strategy (0) | 2024.03.21 |
Apache Spark : RDD, Transformation, Action, Persist (0) | 2024.02.27 |
Apache Spark : SparkContext vs SparkSession (0) | 2024.02.26 |
댓글