https://kubby72.tistory.com/entry/nginx-upstream-module 에서 upstram 에 대한 load-balacing방법에 대해 알아 보있다.
이중upstream에 아래와 같이 dns (org)을 입력하고
upstream org {
hash $host$uri consistent;
server org;
}
/etc/hosts를 아래와 같이 수정해 dnsmasq로 dns 를 통해 전파하게 되면
https://kubby72.tistory.com/entry/simple-DNS-server-dnsmasq
192.168.1.100 org
192.168.1.101 org
192.168.1.102 org
nginx는 아래와 같이 upstream 블락에 server ip_addr를 3개 입력한 것 처럼 동작한다.
for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.101:80
하지만 dns lookup는 nginx가 start될 때 실행한 IP정보를 바탕으로 한 것이여서 중간에 dns설정이 변경된 것을 nginx는 알지 못하고, 초기 nginx start시 dns lookup한 정보로 지속적으로 요청 중임을 알 수 있다.
$ cat /etc/hosts
192.168.1.100 org
#192.168.1.101 org
#192.168.1.102 org
$ systemctl restart dnsmasq
$ dig org +short
192.168.1.100 >> dns lookup이 3대에서 1대로 줄어들었음
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80
nginx 는 여전히 nginx 실행 시 lookup 된 IP정보롤 upstream에 요청
이러한 dns lookup을 실시간으로 반영하기 위해서는 nginx를 재가동해야지만 가능하다.
물론 상용버전을 사용하면 upstream 블락 내에도 resolver와 resolver_timeout을 사용할 수 있다.
이를 해결하기 위한 방법으로는 다음과 같은 방법으로 해결이 가능하다.
1) proxy_pass 에 도메인 직접입력이 아니라 변수로 할당하는 방법
server {
location / {
resolver 127.0.0.1 valid=10s;
set $backend "org";
proxy_pass http://$backend;
}
}
위와 같이 설정 후 /etc/hosts파일을 변경 후 dnsmasq를 restart 한 후 테스트해 보면 dns설정에 따라 upstream이 변경되는 것을 확인 할 수 있다.
$ dig org +short
192.168.1.100
$ for i in {1..10}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
2) upstream dynamic server 모듈 사용방법
https://github.com/GUI/nginx-upstream-dynamic-servers
GitHub - GUI/nginx-upstream-dynamic-servers: An nginx module to resolve domain names inside upstreams and keep them up to date.
An nginx module to resolve domain names inside upstreams and keep them up to date. - GitHub - GUI/nginx-upstream-dynamic-servers: An nginx module to resolve domain names inside upstreams and keep t...
github.com
기존 설정(nginx -V로 확인가능)에 아래와 같이 upsteream-dynamic-server 모듈을 추가하여 complie
--add-module=../nginx-upstream-dynamic-servers --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
아래와 같이 upstream블록 내 이제 resolve라는 매개변수를 추가해 dns resolving을 할 수 있다.
http {
resolver 8.8.8.8;
upstream org {
hash $host$uri consistent;
server org resolve;
}
}
3) 기타 사용가능한 추가 모듈
https://github.com/nicholaschiasson/ngx_upstream_jdomain
GitHub - nicholaschiasson/ngx_upstream_jdomain: An asynchronous domain name resolution module for nginx upstream.
An asynchronous domain name resolution module for nginx upstream. - GitHub - nicholaschiasson/ngx_upstream_jdomain: An asynchronous domain name resolution module for nginx upstream.
github.com
https://github.com/alibaba/tengine/blob/master/docs/modules/ngx_http_upstream_dynamic.md
GitHub - alibaba/tengine: A distribution of Nginx with some advanced features
A distribution of Nginx with some advanced features - GitHub - alibaba/tengine: A distribution of Nginx with some advanced features
github.com
'nginx' 카테고리의 다른 글
How to purge the Nginx cache (0) | 2023.06.19 |
---|---|
nginx proxy hierarchy (0) | 2023.06.19 |
nginx upstream module - part2 (0) | 2023.06.19 |
Consistant hash (0) | 2023.06.16 |
nginx upstream module - part1 (load balancing) (0) | 2023.06.15 |