Load Balancing bằng nginx
Load balancing trên các đối tượng ứng dụng là một kỹ thuật thường được sử dụng để tối ưu hóa việc sử dụng tài nguyên, tối đa hóa lưu lượng, giảm độ trễ.
Chúng ta có thể sử dụng nginx như một máy chủ cân bằng tải HTTP để phân phối lưu lượng truy cập với một số máy chủ ứng dụng. Nó giúp cải thiện hiệu suất, khả năng mở rộng và độ tin cậy của các ứng dụng web với nginx.
Phương pháp load balancing
The following load balancing mechanisms (or methods) are supported in nginx:
round-robin — requests to the application servers are distributed in a round-robin fashion, least-connected — next request is assigned to the server with the least number of active connections, ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address). nginx có các phương pháp cân bằng tải sau:
- round-robin - request gửi tới máy chủ ứng dụng được phân phối bởi round-robin
- least-connected - các request tiếp theo được gán cho server dựa trên số lược connection.
- ip-hash - một hàm hash (hash-function) được sử dụng để xác định máy chủ nào có thể được chọn cho request kế tiếp (dựa trên IP của máy khách)
Cấu hình load balancing mặc định
Dưới đây là 1 cấu hình đơn giản cho nginx:
http {
upstream loadblancing-test {
server server1.demo.com;
server server2.demo.com;
server server2.demo.com;
}
server {
listen 80;
location / {
proxy_pass http://loadblancing-test;
}
}
}
Trong ví dụ trên, chúng ta có 3 máy chủ ứng dụng server1-server3. Khi phương pháp cân bằng tải được cấu hình một cách cụ thể, nó sẽ mặc định chọn round-robin. Tất cả các request sẽ được ủy nhiệm cho nhóm loadblancing-test, và nginx sẽ có nhiệm vụ phân phối các request.
Reverse proxy được kế thừa trong nginx bao gồm load balancing cho cả HTTP, HTTPS, FastCGI, uwsgi, SCGI, và memcached.
Để cấu hình cân bằng tải cho HTTPS thay vì HTTP, chúng ta chỉ cần chỉ cần sử dụng "https" trong giao thức protocol.
Cân bằng tải least-connected
Another load balancing discipline is least-connected. Least-connected allows controlling the load on application instances more fairly in a situation when some of the requests take longer to complete.
With the least-connected load balancing, nginx will try not to overload a busy application server with excessive requests, distributing the new requests to a less busy server instead.
Least-connected load balancing in nginx is activated when the least_conn directive is used as part of the server group configuration:
Một phương pháp cân bằng tải có quy tắc khác đó là least-connected. least-connected cho phép quản lý số lượng máy chủ một cách có kiểm soát khi request mất quá nhiều thời gian để hoàn thành.
Với least-connected, nginx sẽ cố gắng không để quá tải máy chủ với quá nhiều request, nó sẽ phân phối request tới máy chủ đang ít bận rộn hơn.
least-connected trong nginx sẽ được kích hoạt khi least_con được sử dụng trong nhóm config:
upstream loadblancing-test {
least_conn;
server server1.demo.com;
server server2.demo.com;
server server2.demo.com;
}
Duy trì phiên làm việc (session)
Please note that with round-robin or least-connected load balancing, each subsequent client’s request can be potentially distributed to a different server. There is no guarantee that the same client will be always directed to the same server.
If there is the need to tie a client to a particular application server — in other words, make the client’s session “sticky” or “persistent” in terms of always trying to select a particular server — the ip-hash load balancing mechanism can be used.
With ip-hash, the client’s IP address is used as a hashing key to determine what server in a server group should be selected for the client’s requests. This method ensures that the requests from the same client will always be directed to the same server except when this server is unavailable.
To configure ip-hash load balancing, just add the ip_hash directive to the server (upstream) group configuration:
Xin lưu ý rằng với phương pháp round-robin hay least-connected, mỗi request của từng client có thể được phân phối vào các máy chủ khác nhau. Không có ràng buộc đảm bảo nào rằng cùng một client sẽ luôn luôn được hướng tới cùng một máy chủ.
Nếu có yêu cầu để buộc một client luôn đến một máy chủ - nói cách khác, luôn làm cho phiên làm việc của client luôn dính đến một máy chủ được lựa chọn - thì ip-hash sẽ cần được sử dụng.
Với ip-hash, địa chỉ IP của client được sử dụng như một chìa khóa để xác định những máy chủ trong một nhóm máy chủ nên được lựa chọn. Phương pháp này đảm bảo rằng các yêu cầu từ client sẽ luôn được hướng đến cùng một máy chủ trừ khi máy chủ này là không còn tồn tại.
Để cấu hình cân bằng tải ip-hash, chỉ cần thêm các chỉ thị ip_hash:
upstream loadblancing-test {
ip_hash;
server server1.demo.com;
server server2.demo.com;
server server2.demo.com;
}
All rights reserved