>  기사  >  운영 및 유지보수  >  로드 밸런싱을 위해 nginx를 사용하는 방법

로드 밸런싱을 위해 nginx를 사용하는 방법

WBOY
WBOY앞으로
2023-06-03 08:19:121898검색

4레이어 로드 밸런싱 vs. 7레이어 로드 밸런싱

7레이어 로드 밸런싱이나 4레이어 로드 밸런싱은 실제로 iso osi 네트워크 모델의 레이어 이름을 기준으로 결정된다고 흔히 말합니다. nginx는 http 프로토콜을 사용하고 있습니다. 로드 밸런싱은 애플리케이션 계층에서 수행되므로 7계층 로드 밸런싱이라고 합니다. 예를 들어 TCP 계층에서 로드 밸런싱 작업을 수행하는 lvs를 계층 4 로드 밸런싱이라고 합니다. 일반적으로 다음과 같은 로드 밸런싱 분류가 있습니다.

로드 밸런싱을 위해 nginx를 사용하는 방법

일반적인 소프트웨어 지원

로드 밸런싱을 위해 nginx를 사용하는 방법

일반적인 로드 밸런싱 알고리즘

일반적인 로드 밸런싱 알고리즘에는 다음이 포함됩니다.

로드 밸런싱을 위해 nginx를 사용하는 방법

로드 밸런싱 데모 예: 일반 폴링

다음으로 nginx를 사용하여 일반 폴링을 수행하는 방법을 시연합니다.

로드 밸런싱을 위해 nginx를 사용하는 방법

미리 준비

미리 두 포트 7001/7002에서 두 서비스를 시작합니다. 서로 다른 정보를 표시하는 데 사용됩니다. 시연의 편의를 위해 토네이도를 이용하여 미러를 만들었고, 도커 컨테이너 시작 시 전달되는 다양한 매개변수를 사용하여 서비스의 차이점을 표시했습니다.

[root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "user service 1: 7001"
ddba0abd24524d270a782c3fab907f6a35c0ce514eec3159357bded09022ee57
[root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "user service 1: 7002"
95deadd795e19f675891bfcd44e5ea622c95615a95655d1fd346351eca707951
[root@kong ~]# 
[root@kong ~]# curl http://192.168.163.117:7001
hello, service :user service 1: 7001
[root@kong ~]# 
[root@kong ~]# curl http://192.168.163.117:7002
hello, service :user service 1: 7002
[root@kong ~]#

nginx 시작

[root@kong ~]# docker run -p 9080:80 --name nginx-lb -d nginx 
9d53c7e9a45ef93e7848eb3f4e51c2652a49681e83bda6337c89a3cf2f379c74
[root@kong ~]# docker ps |grep nginx-lb
9d53c7e9a45e    nginx           "nginx -g 'daemon ..."  11 seconds ago   up 10 seconds    0.0.0.0:9080->80/tcp                         nginx-lb
[root@kong ~]#

nginx 코드 조각

다음 nginx 코드 조각을 준비하여 nginx의 /etc/nginx/conf.d/default.conf에 추가합니다.

http {
upstream nginx_lb {
  server 192.168.163.117:7001;
  server 192.168.163.117:7002;
}
server {
  listen    80;
  server_name www.liumiao.cn 192.168.163.117;
  location / {
    proxy_pass http://nginx_lb;
  }
}

default.conf 수정 방법

컨테이너에 vim을 설치하여 효과를 얻을 수 있으며, 로컬에서 수정한 다음 docker cp를 통해 전달하거나 sed로 직접 수정할 수도 있습니다. 컨테이너에 vim을 설치하는 경우 다음 방법을 사용하세요

[root@kong ~]# docker exec -it nginx-lb sh
# apt-get update
...省略
# apt-get install vim
...省略

수정 전

# cat default.conf
server {
  listen    80;
  server_name localhost;
  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;
  location / {
    root  /usr/share/nginx/html;
    index index.html index.htm;
  }
  #error_page 404       /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }
  # proxy the php scripts to apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}
  # pass the php scripts to fastcgi server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #  root      html;
  #  fastcgi_pass  127.0.0.1:9000;
  #  fastcgi_index index.php;
  #  fastcgi_param script_filename /scripts$fastcgi_script_name;
  #  include    fastcgi_params;
  #}
  # deny access to .htaccess files, if apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}
#

수정 후

# cat default.conf
upstream nginx_lb {
  server 192.168.163.117:7001;
  server 192.168.163.117:7002;
}
server {
  listen    80;
  server_name www.liumiao.cn 192.168.163.117;
  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;
  location / {
    #root  /usr/share/nginx/html;
    #index index.html index.htm;
    proxy_pass http://nginx_lb;
  }
  #error_page 404       /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }
  # proxy the php scripts to apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}
  # pass the php scripts to fastcgi server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #  root      html;
  #  fastcgi_pass  127.0.0.1:9000;
  #  fastcgi_index index.php;
  #  fastcgi_param script_filename /scripts$fastcgi_script_name;
  #  include    fastcgi_params;
  #}
  # deny access to .htaccess files, if apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}
#

nginx 컨테이너를 다시 시작

[root@kong ~]# docker restart nginx-lb
nginx-lb
[root@kong ~]#

결과를 확인

보면 확실히 알 수 있습니다 order , 폴링 수행:

[root@kong ~]# cur
hello, service :user service 1: 7001

[root@kong ~]# cur
hello, service :user service 1: 7002
[root@kong ~]# 컬
hello, service :user service 1: 7001
[root@kong ~]# 컬
hello, service :user service 1: 7002
[root@kong ~]#

로드 밸런싱 데모 예: 가중치 폴링

이를 바탕으로 가중치 폴링을 수행하려면 가중치만 추가하면 됩니다

로드 밸런싱을 위해 nginx를 사용하는 방법

default.conf를 수정

default.conf를 다음과 같이 수정

# cp default.conf default.conf.org
# vi default.conf
# diff default.conf default.conf.org
2,3c2,3
<   server 192.168.163.117:7001 weight=100;
<   server 192.168.163.117:7002 weight=200;
---
>   server 192.168.163.117:7001;
>   server 192.168.163.117:7002;
#

nginx 컨테이너를 다시 시작

[root@kong ~]# docker restart nginx-lb
nginx-lb
[root@kong ~]#

결과 확인

폴링 결과가 1/3과 2/3의 비율에 따라 진행되는 것을 확인할 수 있습니다:

[root@kong ~]# 컬
hello, service :user service 1 : 7001

[root@kong ~]# 컬
hello, 서비스 :user 서비스 1: 7002
[root@kong ~]# 컬
hello, 서비스 :user 서비스 1: 7002
[root@kong ~]#

위 내용은 로드 밸런싱을 위해 nginx를 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제