Load Balancing dengan Nginx dan Puma
Tutorial ini adalah sangat ringkas hanya untuk menunjukkan kebolehan nginx sebagai load balancer yang dapat menjadi 'orang tengah' antara banyak server di belakangnya
Keperluan
1. Paling kurang 2 server aplikasi2. Satu server diinstall nginx
Contoh Persekitaran yang sekarang
1. server1:
os: debian
application: ruby on rails
web server : puma
dns : myaplication1.yusdirman.com
2. server2: myaplication2.yusdirman.com
os:debian
application: ruby on rails
web server : puma
dns : myaplication1.yusdirman.com
3. nginx diinstall di server1
/ets/nginx/nginx.conf setup (default):
worker_processes 2;worker_processes auto;
worker_rlimit_nofile 65536;
error_log logs/error.log notice;
error_log logs/error.log info;
events {
worker_connections 65536;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/sites-enabled/*;
}
/etx/nginx/sites-available/myapplication.yusdirman.com.conf
upstream myapplication {
server myapplication1.yusdirman.com fail_timeout=0; #localhost
server myapplication2.yusdirman.com fail_timeout=0; #remote1
#server myapplication3.yusdirman.com fail_timeout=0; #remote2
#server myapplication4.yusdirman.com fail_timeout=0; #remote3
server {
listen myapplication.yusdirman.com:80;
server_name myapplication.yusdirman.com;
location /{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://myapplication;
# Default is HTTP/1, keepalive is only enabled in HTTP/1.1
proxy_http_version 1.1;
proxy_set_header Connection "";
}
access_log /var/log/nginx/myapplication.access.log;
error_log /var/log/nginx/myapplication.error.log debug;
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
bina 'soft-link' antara /etc/nginx/sites-available ke /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-avaliable/myapplication.yusdirman.com.conf /etc/nginx/sites-enabled/
Larikan aplikasi di setiap server
server myapplication1.yusdirman.com
cd /home/diman/myapplication
#puma telah disetkan sebagai default webserver
rails server -e production -b myapplication1.yusdirman.com -d
server myapplication2.yusdirman.com
cd /home/diman/myapplication
#puma telah disetkan sebagai default webserver
rails server -e production -b myapplication2.yusdirman.com -d
Mulakan nginx di server1 (tempat diinstall)
sudo service nginx start
atau
sudo service nginx restart
Nginx Load Balancing
Teknik-teknik Load Balancing dengan nginxdefault: round robin
keterangan: bergilir-gilir. Capaian pertama akan ke server pertama. Capaian kedua akan ke server seterusnya
1. least_conn
keterangan: capaian akan ke server yang paling kurang akses
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
}
nota: tandakan 'down' untuk server yang tidak digunakan/dibaiki.
2. ip_hash
keterangan: ditentukan oleh alamat ip klien. Akses dari IP yang sama akan ke server dengan IP yang berdekatan.
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}
3. hash
keterangan: bergantung kepada kod kunci daripada klien
upstream backend {
hash $request_uri consistent;
server backend1.example.com;
server backend2.example.com;
}
4. Pemberat
keterangan: tetapkan pemberat untuk server. Jika ada 7 akses, 5 akses akan ke server backend1 dan 1 ke backend2 dan 1 ke backend3.
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
server backend3.example.com;
}
Sekian dahulu.
Info tambahan di : https://www.nginx.com/resources/admin-guide/load-balancer/
Ulasan
Catat Ulasan
Comment