728x90

Load Balance Setting in NginX

 

로드밸런서 셋팅

  1. NginX install check

Setting in Ubuntu

# Debian and Ubuntu sudo apt-get update # Then install the Nginx Open Source edition sudo apt-get install nginx

 

Setting in CentOS

# CentOS # Install the extra packages repository sudo yum install epel-release # Update the repositories and install Nginx sudo yum update sudo yum install nginx

yum install epel-release 로 repository 를 최신 상태로 업데이트한다. 일반적으로 repo 의 경우는 초기상태에서는 최신 버전을 가르키진 않기 때문에, 해당 방식으로 업데이트 해준다.

[ Remi 와 Epel 은 Dependency Library 관계를 가지고 있다.]

 

CentOS 사용자의 경우 host 설정 파일 (/etc/nginx/conf.d/) 아래에 있는 .conf 파일들이 virtual host 상태로 로드된다.

 

2. 데몬을 재시작한다.

sudo systemctl restart nginx

 

 

3. 기본적으로 로딩 페이지에서 에러 발생시, 방화벽에 의해서 연결이 차단된 상태일 가능성이 있다. CentOS 7 의 기본 방화벽 설정은 HTTP 트래픽을 허용하지 않으므로 허용시킨다.

sudo firewall-cmd --add-service=http --permanent sudo firewall-cmd --reload

 

4. 브라우저 리로드

 

nano 명령어로 해당 conf 파일을 생성시킨다. nano 가 익숙하지 않다면 vi 로 해결할 것

. For example with nano:

sudo nano /etc/nginx/conf.d/load-balancer.conf

 

해당 파일 안에서 2개의 segment 로 구분하게 되는데, upstream 이 로드밸런서에 해당하고, server 에 관한 설정이 nginx 에서 설정하는 가상호스트의 설정을 따르게 된다.

In the load-balancer.conf you’ll need to define the following two segments, upstream and server, see the examples below.

# Define which servers to include in the load balancing scheme. # It's best to use the servers' private IPs for better performance and security. # You can find the private IPs at yourUpCloud control panel Network section. http { upstream backend { server 10.1.0.101; server 10.1.0.102; server 10.1.0.103; } # This server accepts all traffic to port 80 and passes it to the upstream. # Notice that the upstream name and the proxy_pass need to match. server { listen 80; location / { proxy_pass http://backend; } } }

Then save the file and exit the editor.

 

CentOS 에서는 같은 이름의 파일으로 심볼릭 링킹 처리가 안된다. 따라서 아래처럼 default.conf 파일을 다른 이름으로 바꿔서 처리해준다.

sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled sudo systemctl restart nginx # nginx daemon 재시작

 

로드 밸런싱 유형 (심화버전)

 

최소 활성화 연결방식 (내부 Threading 사용)

라운드 로빈 방식이 완료하는데에 비교적 오래걸리는 반면에 쓰레드를 사용하여 자원적으로 공평하게 처리한다.

최소 연결 로드밸런싱 방식을 사용하려면, least_conn 을 upstream 영역에 아래와 같이 추가하면 된다.

To enable least connections balancing method, add the parameter least_conn to your upstream section as shown in the example below.

upstream backend { least_conn; server 10.1.0.101; server 10.1.0.102; server 10.1.0.103; }

 

라운드로빈과 최소연결 밸런싱 방식은 평등함을 추구하는 방식이지만 세션을 지속적으로 제공하지는 않는다.

 

ip hashing 방식을 사용하는 경우에 사요자의 IP 주소는 키로 사용된다. 또한 이전 연결에 대하여 같은 서버의 처리시간을 유도한다.

upstream backend { ip_hash; server 10.1.0.101; server 10.1.0.102; server 10.1.0.103; }

 

로드밸런싱 방식에서 가중치를 두어서 로드밸런싱 방식을 처리하고 싶다면 weight 인자를 주어서 처리하면 된다.

upstream backend { server 10.1.0.101 weight=4; server 10.1.0.102 weight=2; server 10.1.0.103; }

 

 

Health Check [ Advanced Method ]

max_fails 와 fail_timeout 을 사용하여 relay 를 유도하는 방식이다.

upstream backend { server 10.1.0.101 weight=5; server 10.1.0.102 max_fails=3 fail_timeout=30s; server 10.1.0.103; }

 

 

 

https://upcloud.com/community/tutorials/configure-load-balancing-nginx/

 

How to configure load balancing using Nginx

Load balancing is an excellent way to scale out your application to increase its performance and redundancy. Here's how to set up load balancing with Nginx.

upcloud.com

 

 

 

+ Recent posts