DevOps Expert | Haproxy – Instalasi & Konfigurasi pada VPS Ubuntu 18.04

haproxy load balancer server

Pada bab sebelumnya kita sudah mengetahui tentang apa, mengapa dan bagaimana cara kerja haproxy. Jika Anda belum mengetahui konsep dan alasan di balik haproxy sebaiknya Anda membacanya terlebih dahulu disini.

Haproxy Installation

Pertama kita create vps terlebih dahulu, bisa dengan digital ocean atau vultr atau yang lain nya. Jika belum mengetahui cara deploy (membuat) VPS dan instalasinya silahkan ikuti step by step disini. Selain VPS bisa juga menggunakan VM (virtual machine) atau bahkan dedicated computer.

Sediakan 3 VPS. Saya asumsikan sudah terinstall ubuntu server 18.04 atau keluarga debian, karena kurang lebih caranya sama kalau sesama keluarga linux debian. beda-beda dikit paling.
Pada VPS 1 (Hostname haproxy.qodrbee.com) lakukan

  1. Setting IP private
    edit file konfigurasi ip address

    root@haproxy:~# nano /etc/netplan/01-netcfg.yaml

    akan muncul isi file seperti ini lalu edit ip address menjadi 192.168.1.1/24

    network:
      version: 2
      renderer: networkd
      ethernets:
        ens3:
          dhcp4: yes
        ens7:
          addresses: [192.168.1.1/24]
          dhcp4: no
          dhcp6: no
          optional: true
          nameservers:
            addresses: [8.8.8.8,8.8.4.4]
    

    ens3 adalah ethernet yg terhubung ke public dan
    ens7 adalah yang terhubung ke private atau ke web server kita nantinya.
    Angka 3 dan 7 pada ens bisa berbeda untuk vps lain. Silahkan disesuaikan
    jika sudah simpan dengan ctrl+x lalu apply settingan tersebut dengan perintah

    root@haproxy:~# netplan apply

    cek ip address ens7 apakah sudah berubah

    root@haproxy:~# ifconfig
    ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.20.30.40  netmask 255.255.254.0  broadcast 10.20.30.255
            inet6 fe80::5400:1ff:fef4:c42e  prefixlen 64  scopeid 0x20<link>
            ether 56:00:01:f4:c4:ce  txqueuelen 1000  (Ethernet)
            RX packets 2008493  bytes 468728324 (468.7 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 1952526  bytes 1666990785 (1.6 GB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    ens7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.1.1  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::5800:1ff:fef4:c48e  prefixlen 64  scopeid 0x20<link>
            ether 5a:00:01:f4:c4:ce  txqueuelen 1000  (Ethernet)
            RX packets 8122671  bytes 2208780819 (2.2 GB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 14258789  bytes 1377041568 (1.3 GB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 104578  bytes 9203032 (9.2 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 104578  bytes 9203032 (9.2 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
  2. Install haproxy
    update repo

    root@haproxy:~# apt update

    install haproxy

    root@haproxy:~# apt install haproxy

    Setelah terinstall, maka edit default config aplikasi haproxynya untuk mengarahkan settingan load balancernya, di /etc/default/haproxy , hilangkan tanda # di awal tulisan CONFIG dan ubah ENABLED menjadi 1.

    root@haproxy:~# nano /etc/default/haproxy
    # Defaults file for HAProxy
    #
    # This is sourced by both, the initscript and the systemd unit file, so do not
    # treat it as a shell script fragment.
    
    # Change the config file location if needed
    CONFIG="/etc/haproxy/haproxy.cfg"
    
    # Add extra flags here, see haproxy(1) for a few options
    #EXTRAOPTS="-de -m 16"
    ENABLED=1

    okey sekarang installasi haproxy sudah selesai, nanti kita lanjutkan untuk konfigurasinya.

Kemudian pada VPS 2 dan 3 (web1.qodrbee.com dan web2.qodrbee.com) kita lakukan

 

Haproxy Configuration

buka file konfigurasi haproxy

root@haproxy:~# nano /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1 local0 notice   # menyimpan log pada 
                                  # syslog server (localhost)
    maxconn 2000          # max koneksi yang ditangani secara bersamaan
    user haproxy          # user yg menangani haproxy (jgn diganti)
    group haproxy         # group yg menangani haproxy (jgn diganti)

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    retries 3             # jml percobaan ketika timeout sebelum failure
    option redispatch     # meng-enable session redistribution 
                          # jika connection fail
    timeout connect  5s   # lama max sampai terhubung ke server 
                          # hingga berhasil
    timeout client  10s   # lama proses pengiriman data client/server
    timeout server  10s   # lama proses pengiriman data client/server

    listen web-load-balanced 0.0.0.0:80 # listen agar dapat di akses 
                                        # dari manapun
    mode http
    balance roundrobin    # algoritma metode load balancing
                          # ada pilihan algoritma roundrobin/static-rr/
                          # leastconn/source/uri/url_param
    option httpclose
    option forwardfor
    server web1 192.168.1.2:80 check # server backend, health check untuk 
    server web2 192.168.1.3:80 check # mengetahui status server backend

Oke konfigurasi standar haproxy sudah selasai. Selanjutnya kita uji coba. Start dulu haproxynya

root@haproxy:~# service haproxy start

buka komputer lain di luar VPS untuk melakukan request ke server load balancer sebut saja komputer user. Misal kita kasih IP address nya 192.168.1.111
kita lakukan dengan browser atau curl. Untuk uji coba kali ini kita pakai curl.
buat script php sebagai berikut untuk mengetahui IP server dan Client dari request.
simpan di /var/www/html/cek_loadbalance.php di server web1 dan web2

<?php
header('Content-Type: text/plain');
echo "Server IP: ".$_SERVER['SERVER_ADDR'];
echo "\nClient IP: ".$_SERVER['REMOTE_ADDR'];
echo "\nX-Forwarded-for: ".$_SERVER['HTTP_X_FORWARDED_FOR'];
?>

lalu dari komputer user lakukan

root@user:~# curl cek_loadbalance.php

seharusnya akan muncul hasil seperti ini

root@user:~$ curl http://192.168.1.1/cek_loadbalance.php
Server IP: 192.168.1.2
Client IP: 192.168.1.1
X-Forwarded-for: 192.168.1.111
root@user:~$ curl http://192.168.1.1/cek_loadbalance.php
Server IP: 192.168.1.3
Client IP: 192.168.1.1
X-Forwarded-for: 192.168.1.111

perhatikan server IP… dia berganti ganti karena kita memakai algoritma round robin. Request yang datang ke load balancer di arahkan bergantian ke 192.168.1.2 (web1) dan 192.168.1.3(web2)

X-Forwarded-for adalah IP komputer user

Incoming search terms: