Load Balancer HAProxy Ubuntu 24.04
Load Balancer HAProxy Ubuntu 24.04

Setup Load Balancer dengan HAProxy Ubuntu 24.04 LTS

Menjaga agar aplikasi web tetap cepat dan tersedia secara terus-menerus adalah tantangan utama.

Ketika lalu lintas pengguna melonjak, satu server saja sering kali tidak cukup untuk menangani beban, yang berisiko menyebabkan Downtime atau Lambatnya respon server.

HAProxy (High Availability Proxy) hadir sebagai solusi. Ini adalah open-source load balancer andalan yang digunakan oleh perusahaan besar seperti GitHub, Reddit, dan Stack Overflow.

HAProxy bertindak seperti “manajer lalu lintas”, mendistribusikan setiap permintaan pengguna ke beberapa server backend sehingga tidak ada satu server pun yang bekerja sendirian.

Service Local

Install Node Js dan NPM

sudo apt install nodejs -y && sudo apt install npm -y

Cek version Node Js

node -v

Output:

v18.19.1

Cek version NPM

npm -v

Output:

9.2.0

Install PM2

PM2 manager proses untuk aplikasi berbasis Node Js. Fungsi untuk menjaga aplikasi tetap menyala (online) 24/7, merestart otomatis saat crash, dan menyeimbangkan beban (load balancing).

sudo npm install -g pm2

Website Sederhana Framework Express Js

APP 1

  • Title : APP 1
  • H2 : Website APP 1
  • Port: 8181
  • Direktori : app-1

APP 2

  • Title : APP 2
  • H2 : Website APP 2
  • Port : 8282
  • Direktori : app-2

Buat direktori website app-1

mkdir app-1
cd app-1

Init app-1

npm init -y

Install Framework Express Js

npm install express

Buat file app-1.js

nano app-1.js

Masukan source code website sederhana dibawah.

const express = require('express');
const app = express();
const port = 8181;

app.get('/', (req, res) => {
    res.send(`
        <!DOCTYPE html>
        <html>
        <head>
            <title>APP 1</title>
            <style>
                body {
                    margin: 0;
                    height: 100vh;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    font-family: Arial, sans-serif;
                }
                h1 {
                    color: white;
                    font-size: 4rem;
                    text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
                }
            </style>
        </head>
        <body>
            <h1>Website APP 1</h1>
        </body>
        </html>
    `);
});

app.listen(port, () => {
    console.log(`Server running at http://127.0.0.1:${port}`);
});

Jalankan app-1.js dengan pm2 agar berjalan di background.

sudo pm2 start app-1.js

Untuk APP 2 ulangi seperti langkah-langkah diatas.

Install HAProxy

Jalankan perintah dibawah untuk install package HAProxy.

sudo apt update -y && sudo apt upgrade -y
sudo apt install haproxy -y

Aktikan dan jalankan service HAProxy

sudo systemctl enable haproxy
sudo systemctl start haproxy

Check HAProxy Version

Setelah terinstall, check version HAProxy.

sudo haproxy -v

Secara default ubuntu 24.04 menggunakan version 2.8.16 LTS.

Output:

HAProxy version 2.8.16-0ubuntu0.24.04.2 2026/04/15 - https://haproxy.org/

Load Balancer HAProxy

Backup konfigurasi default HAProxy

sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

Edit file konfigurasi HAProxy

sudo nano /etc/haproxy/haproxy.cfg

Ganti dengan script dibawah.

frontend haproxy-main
    bind *:80
    option forwardfor
    default_backend local_service

backend local_service
    balance roundrobin
    server app1    127.0.0.1:8181 check
    server app2    127.0.0.1:8282 check


global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

Cek konfigurasi HAProxy

sudo haproxy -f /etc/haproxy/haproxy.cfg -c
Output:

Configuration file is valid

Restart service HAProxy untuk menjalankan ulang dan update konfigurasi.

sudo systemctl restart haproxy

HAProxy Statistic Report

Buka file konfigurasi HAProxy.

sudo nano /etc/haproxy/haproxy.cfg

Tambahkan script dibawah, bagian paling bawah.

listen stats
    bind *:9000
    mode http
    stats enable
    stats hide-version
    stats refresh 30s
    stats uri /haproxy-stats
    stats auth admin:password123

Penjelasan parameter:

  • bind *:9000 : HAProxy akan mendengarkan port 9000 untuk memonitor statistik.
  • stats enable : Mengaktifkan halaman statistik.
  • stats hide-version : Menyembunyikan informasi versi HAProxy dari halaman dasbor demi keamanan.
  • stats refresh 30s : Halaman dasbor akan otomatis memuat ulang (refresh) setiap 30 detik.
  • stats uri /haproxy-stats : URL untuk mengakses statistik (contoh: http://IP_Public:9000/haproxy-stats).
  • stats auth admin:password123 : Autentikasi dasar menggunakan username admin dan password password123.

Validasi configuration HAProxy

sudo haproxy -f /etc/haproxy/haproxy.cfg -c

Restart service HAProxy

sudo systemctl restart haproxy

Akses http://IP_Public:9000/haproxy-stats untuk melihat dashboard stats haproxy. Akan muncuk from login username dan password isikan dengan admin/password123.

Dashboard HAProxy Statistic Report
Dashboard HAProxy Statistic Report

Pengujian Load Balancing HAProxy

Aksess url http://IP_Public di browser. Lakukan reload dengan menekan keyboard ctrl + r, pastikan berubah dari “Website APP 1” menjadi “Website APP 2“.

Pengujian HAProxy Load Balancing
Pengujian HAProxy Load Balancing

Kesimpulan

Kamu telah berhasil membangun infrastruktur web yang tangguh. Dengan Load Balancer HAProxy di Ubuntu 24.04, tidak hanya meningkatkan kapasitas penanganan lalu lintas, tetapi juga menciptakan sistem High Availability (HA).

Jika salah satu server backend mati (down), HAProxy secara otomatis akan mengalihkan semua pengguna ke server yang masih hidup, memastikan layanan tetap online tanpa gangguan .

Untuk kebutuhan production tingkat lanjut, dapat mulai mengeksplorasi fitur seperti HTTPS/SSL termination (mengamankan traffic), Least Connections (mengirim ke server dengan sibuk paling sedikit), atau bahkan menyiapkan Keepalived untuk membuat Load Balancer yang ganda (master-slave) agar HAProxy itu sendiri tidak menjadi single point of failure.