Load Balance HAProxy in Centos | Roundrobin, Lastconn, Source

Selamat datang kembali di website asepit yang selalu memberikan informasi mengenai dunia Teknologi khususnya pada bidang Komputer, Pemrograman, dan juga Jaringan. Bagaimana kabar kalian semua mudah - mudahan dalam keadaan sehat wal afiat dan selalu dilancarkan rezekinya, Pada kesempatan kali ini saya akan membahas mengenai cara konfigurasi Loadbalance pada server Centos. Loadbalance ini merupakan suatu metode yang dapat membagi sebuah traffic data ke beberapa server yang bertujuan agar tidak membebani satu server saja sehingga server akan menjadi lebih seimbang dan mengurangi resiko overload. Algoritma yang sering digunakan yaitu Roundrobin, Lastconn, dan Source. Berikut langkah - langkah untuk konfigurasi dan menginstalnya

Persiapan

- Memiliki 3 VPS beserta domainnya (bisa menggunakan virtualbox)
- Memiliki akses root ke VPS tersebut

1. Instal system operasi Centos 7 ke VPS (skip jika sudah) dan update system tersebut dengan perintah berikut

yum update -y

2. Login ke VPS yang akan dijadikan sebgai loadbalance & tambahkan hostname Server 1 & 2 ke dalamnya sebagai contoh seperti ini

vim /etc/hosts
192.168.10.23 loadbalance
192.168.10.20 srv1.loadbalance
192.168.10.22 srv2.loadbalance

3. Masukkan juga hostname di atas ke Server 1 & 2

4. Lalu instal HAProxy dan salin konfigurasi default di server Loadbalance dengan perintah berikut

yum install haproxy -y
cd /etc/haproxy/
cp haproxy.cfg haproxy.cfg.orig

5. Tambahkan beberapa syntak di haproxy.cfg tadi kurang lebih seperti ini

vim haproxy.cfg

Sebelum

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check

Sesudah

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# Konfigurasi Monitoring HAProxy
#---------------------------------------------------------------------
listen haproxy-monitoring *:8080         #Untuk Monitoring HAProxy pada port 8080
    mode http
    option forwardfor
    option httpclose
    stats enable
    stats show-legends
    stats refresh 5s
    stats uri /stats
    stats realm Haproxy\ Statistics
    stats auth admin:rahasia
    stats admin if TRUE
    default_backend app-main

#---------------------------------------------------------------------
# Konfigurasi FrontEnd
#---------------------------------------------------------------------
frontend main
    bind *:80
    option http-server-close
    option forwardfor
    default_backend app-main

#---------------------------------------------------------------------
# Konfigurasi Algoritma yg digunakan
#---------------------------------------------------------------------
backend app-main
    balance roundrobin                                # Algoritma yg berjalan
    option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost# Pengecekan Server & App apakah berjalan / tidak
    server srv1 192.168.10.20:80 check
    server srv2 192.168.10.22:80 check

6. Ubah pada bagian konfigurasi Rsyslog dengan menghilangkan komen (tanda #) pada bagian ini

vim /etc/rsyslog.conf
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
$UDPServerAddress 127.0.0.1

7. Buat konfigurasi HAProxy untuk Rsyslog

cd /etc/rsyslog.d/
vim haproxy.conf
local2.=info    /var/log/haproxy-access.log
local2.notice   /var/log/haproxy-info.log

8. Jalankan & restart HAProxy dan Rsyslog

systemctl restart haproxy
systemctl enable haproxy
systemctl status haproxy
systemctl restart rsyslog
systemctl enable rsyslog
systemctl status rsyslog

9. Kemudian lanjut pindah ke Server 1 & 2 untuk penginstalan Nginx nya

yum install epel-release -y
yum install nginx -y

10. Dan coba buat syntak sederhana untuk memastikan loadbalance sudah berjalan / belumnya

echo “<h1>Server 1 > Loadbalance</h1>” > index.html     #untuk server 1
echo “<h1>Server 2 > Loadbalance</h1>” > index.html     #untuk server 2

 

11. Dan Jalankan Nginx untuk melihat perubahan yang telah di masukkan

systemctl restart nginx
systemctl enable nginx
systemctl status nginx

12. Lanjut kita coba pada browser dengan mengakses IP / Domain Loadbalance

Dan untuk melihat statistik nya kurang lebih seperti ini

13. Jika tidak dapat muncul kita bisa mendisable Firewall / mendaftarkan port yang berjalan ke Firewall tersebut (terapkan disemua server)

systemctl stop firewalld

Atau seperti ini

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

 

 

 

Mungkin hanya itu untuk pembahasan kali ini, saya mohon ma'af bila dalam penyampaian serta penulisan terdapat sebuah kesalahan. Jangan lupa ikuti juga platform ASEP IT lainnya dibawah ini, saya cukupkan sekian dan Terima Kasih...

 

 

Website klik disini
Youtube klik disini
Fans Page klik disini

Related Articles

Comments