Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Instalasi dan Konfigurasi HAProxy sebagai Load Balancer MySQL

Posted on February 24, 2012

1. Instalasi HAProxy

Unduh sourcecode haproxy terbaru dari http://haproxy.1wt.eu, ekstraks dan compile source codenya. Silakan lakukan dengan aplikasi installer paket (yum, apt-get, urpmi dll) jika kesulitan.

[sourcecode language=”bash”]
wget -c http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz
tar -xvf haproxy-1.4.15.tar.gz
cd haproxy-1.4.15
make install
[/sourcecode]

2. Konfigurasi HAProxy

Contoh konfigurasi HAProxy:

[sourcecode language=”bash”]
global
log 127.0.0.1 local0
maxconn 4096
user haproxy
group haproxy
daemon

defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 4000
clitimeout 50000
srvtimeout 30000
stats enable
stats scope .

frontend mysql_cluster
bind 111.68.112.42:3306
default_backend mysql_cluster

backend mysql_cluster
mode tcp
balance roundrobin
stats enable
option tcpka
option httpchk
server db01 111.68.112.43:3306 weight 1 check port 9200 inter 5s rise 2 fall 2
server db02 111.68.112.44:3306 weight 1 check port 9200 inter 5s rise 2 fall 2

listen stats 111.68.112.42:31337
mode http
option httpclose
balance roundrobin
stats uri /
stats realm Haproxy\ Statistics
#stats auth user:pass
[/sourcecode]

Uji Coba Konfigurasi HAProxy

1. Pastikan dulu syntax konfigurasi HAProxy yang kita buat sudah benar.
[sourcecode]./haproxy -f ./haproxy.cfg -c[/sourcecode]

2. Jalankan HAProxy
Silakan jalankan HAProxy untuk mencoba syntax yang kita tulis, silakan matikan lagi untuk konfigurasi selanjutnya.
[sourcecode]./haproxy3 -f ./haproxy.cfg -p /var/run/haproxy.pid[/sourcecode]

Konfigurasi Server Master Replikasi MySQL

Implementasikan konfigurasi ini pada server master 1.
a. Kita harus membuat service mysql di xinetd (sebuah pengganti inetd), dimana diwakili oleh sebuah skrip yang akan mengecek keadaan server mysql.

Berikut skripnya:
[sourcecode language=”bash”]
#!/bin/bash
# /opt/mysqlchk
# This script checks if a mysql server is healthy running on localhost. It will
# return:
#
# "HTTP/1.x 200 OK\r" (if mysql is running smoothly)
#
# – OR –
#
# "HTTP/1.x 500 Internal Server Error\r" (else)
#
# The purpose of this script is make haproxy capable of monitoring mysql properly
#
# Author: Unai Rodriguez
#
# It is recommended that a low-privileged-mysql user is created to be used by
# this script. Something like this:
#
# mysql> GRANT SELECT on mysql.* TO ‘mysqlchkusr’@’localhost’ \
# -> IDENTIFIED BY ‘257retfg2uysg218’ WITH GRANT OPTION;
# mysql> flush privileges;

MYSQL_HOST="localhost"
MYSQL_PORT="3306"
MYSQL_USERNAME="mysqlchkusr"
MYSQL_PASSWORD="mysql321"

TMP_FILE="/tmp/mysqlchk.out"
ERR_FILE="/tmp/mysqlchk.err"

#
# We perform a simple query that should return a few results :-p
#
/usr/sbin/mysql –host=$MYSQL_HOST –port=$MYSQL_PORT –user=$MYSQL_USERNAME \
–password=$MYSQL_PASSWORD -e"show databases;" > $TMP_FILE 2> $ERR_FILE

#
# Check the output. If it is not empty then everything is fine and we return
# something. Else, we just do not return anything.
#
if [ "$(/bin/cat $TMP_FILE)" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OK\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is running.\r\n"
/bin/echo -e "\r\n"
else
# mysql is fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is *down*.\r\n"
/bin/echo -e "\r\n"
fi
[/sourcecode]

b. Selain itu kita juga harus membuat skrip tersebut sebagai services. Berikut skripnya:

[sourcecode language=”bash”]
# /etc/xinetd.d/mysqlchk
# default: on
# description: mysqlchk
service mysqlchk
{
flags = REUSE
socket_type = stream
port = 9200
wait = no
user = nobody
server = /opt/mysqlchk
log_on_failure += USERID
disable = no
only_from = 0.0.0.0/0 # recommended to put the IPs that need
# to connect exclusively (security purposes)
per_source = UNLIMITED # Recently added (May 20, 2010)
# Prevents the system from complaining
# about having too many connections open from
# the same IP. More info:
# http://www.linuxfocus.org/English/November2000/article175.shtml
}
[/sourcecode]

c. Kemudian pastikan bahwa skrip kita telah terdapat pada daftar services sistem (/etc/services).
[sourcecode]
root@srv14 src# cat /etc/services | grep 9200
#wap-wsp 9200/tcp # WAP connectionless session service
wap-wsp 9200/udp # WAP connectionless session service
mysqlchk 9200/tcp # mysqlchk
[/sourcecode]

d. Siapkan user pengecek kondisi server (sesuai nama user dalam skrip-skrip diatas, misalnya mysqlchkuser).
[sourcecode language=”sql”]
mysql> create user ‘mysqlchkuser’@’localhost’ identified by ‘mysql321’;
mysql> GRANT SELECT on mysql.* TO ‘mysqlchkusr’@’localhost’ identified by ‘mysql321’ with grant option;
mysql> flush privileges;

mysql> show grants for mysqlchkusr;
+————————————————————————————————————+
| Grants for mysqlchkusr@% |
+————————————————————————————————————+
| GRANT USAGE ON *.* TO ‘mysqlchkusr’@’%’ IDENTIFIED BY PASSWORD ‘*4ADE5E38BA4BB05808B2EBDF16E4175E9EA590D5’ |
| GRANT SELECT ON `mysql`.* TO ‘mysqlchkusr’@’%’ WITH GRANT OPTION |
+————————————————————————————————————+
2 rows in set (0.00 sec)

mysql>
[/sourcecode]

Implementasikan pada Server master 2
a. Kita harus membuat service mysql di xinetd (sebuah pengganti inetd), dimana diwakili oleh sebuah skrip yang akan mengecek keadaan server mysql.
[sourcecode language=”bash”]
#!/bin/bash
# /opt/mysqlchk
# This script checks if a mysql server is healthy running on localhost. It will
# return:
#
# "HTTP/1.x 200 OK\r" (if mysql is running smoothly)
#
# – OR –
#
# "HTTP/1.x 500 Internal Server Error\r" (else)
#
# The purpose of this script is make haproxy capable of monitoring mysql properly
#
# Author: Unai Rodriguez
#
# It is recommended that a low-privileged-mysql user is created to be used by
# this script. Something like this:
#
# mysql> GRANT SELECT on mysql.* TO ‘mysqlchkusr’@’localhost’ \
# -> IDENTIFIED BY ‘257retfg2uysg218’ WITH GRANT OPTION;
# mysql> flush privileges;

MYSQL_HOST="localhost"
MYSQL_PORT="3306"
MYSQL_USERNAME="mysqlchkusr"
MYSQL_PASSWORD="mysql321"

TMP_FILE="/tmp/mysqlchk.out"
ERR_FILE="/tmp/mysqlchk.err"

#
# We perform a simple query that should return a few results :-p
#
/usr/sbin/mysql –host=$MYSQL_HOST –port=$MYSQL_PORT –user=$MYSQL_USERNAME \
–password=$MYSQL_PASSWORD -e"show databases;" > $TMP_FILE 2> $ERR_FILE

#
# Check the output. If it is not empty then everything is fine and we return
# something. Else, we just do not return anything.
#
if [ "$(/bin/cat $TMP_FILE)" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OK\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is running.\r\n"
/bin/echo -e "\r\n"
else
# mysql is fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is *down*.\r\n"
/bin/echo -e "\r\n"
fi
[/sourcecode]

b. Selain itu kita juga harus membuat skrip tersebut sebagai services. Berikut skripnya:
[sourcecode language=”bash”]
# /etc/xinetd.d/mysqlchk
# default: on
# description: mysqlchk
service mysqlchk
{
flags = REUSE
socket_type = stream
port = 9200
wait = no
user = nobody
server = /opt/mysqlchk
log_on_failure += USERID
disable = no
only_from = 0.0.0.0/0 # recommended to put the IPs that need
# to connect exclusively (security purposes)
per_source = UNLIMITED # Recently added (May 20, 2010)
# Prevents the system from complaining
# about having too many connections open from
# the same IP. More info:
# http://www.linuxfocus.org/English/November2000/article175.shtml
}
[/sourcecode]

c. Kemudian pastikan bahwa skrip kita telah terdapat pada daftar services sistem (/etc/services).
[sourcecode]
root@srv15 ~# cat /etc/services | grep 9200
#wap-wsp 9200/tcp # WAP connectionless session service
wap-wsp 9200/udp # WAP connectionless session service
mysqlchk 9200/tcp
[/sourcecode]

d. Siapkan user pengecek kondisi server (sesuai nama user dalam skrip-skrip diatas, misalnya mysqlchkuser).
[sourcecode language=”sql”]
mysql> create user ‘mysqlchkuser’@’localhost’ identified by ‘mysql321’;
mysql> GRANT SELECT on mysql.* TO ‘mysqlchkusr’@’localhost’ identified by ‘mysql321’ with grant option;
mysql> flush privileges;

mysql> show grants for mysqlchkusr;
+————————————————————————————————————+
| Grants for mysqlchkusr@% |
+————————————————————————————————————+
| GRANT USAGE ON *.* TO ‘mysqlchkusr’@’%’ IDENTIFIED BY PASSWORD ‘*4ADE5E38BA4BB05808B2EBDF16E4175E9EA590D5’ |
| GRANT SELECT ON `mysql`.* TO ‘mysqlchkusr’@’%’ WITH GRANT OPTION |
+————————————————————————————————————+
2 rows in set (0.00 sec)

mysql>
[/sourcecode]

e. Berikan permission 777 untuk folder /tmp/mysqlchk.* (atau sesuaikan menurut skrip anda)
[sourcecode]# chmod 777 /tmp/mysqlchk.*[/sourcecode]

Uji Coba HAProxy

Silakan uji coba HAProxy dengan menjalankannya berikut file konfigurasi yang telah kita buat.
[sourcecode]
# /usr/local/sbin/haproxy -f /usr/local/etc/haproxy.cfg -p /var/run/haproxy.pid
[/sourcecode]

Nah, untuk memonitor service di kedua server, silakan gunakan webbrowser untuk melihatnya di http://ip-address-balancer-anda:31337/.

Diramu dari berbagai artikel HEBAT di :
1. LinuxAdminZone.com (http://linuxadminzone.com/how-to-install-setup-and-config-haproxy-loadbalancer-for-content-switching/)
2. ALinux.web.id (http://alinux.web.id/2011/08/17/load-balancing-mysql-replication-master-to-master-with-haproxy.html)
3. Dan Cryer (http://www.dancryer.com/2010/01/load-balancing-mysql-with-ha-proxy)

Terbaru

  • Samsung Galaxy Z-Fold: Uji Jatuh Bebas yang Mengguncang Keandalan Lipatannya
  • Google One 2026: Apa yang Akan Jadi Fitur Utama dan Harga yang Diharapkan?
  • Apa Itu Error 0x800704f8? Ini Pengertian dan Cara Mengatasinya
  • Android Akhirnya Dapat GPS Darurat di India! Setelah Hampir 10 Tahun
  • Apa Itu GetContact Premium? Ini Pengertian dan Cara Daftarnya
  • Android Maze Figure: Koleksi Baru Google yang Bikin Penggemar Bergairah!
  • Google Update Besar-besaran Desember 2025: Apa yang Akan Memengaruhi Pengalaman Anda?
  • Masih Pusing Hitung Gaji Manual? Waktunya Pakai Aplikasi HR
  • Apa Itu Dustruco? Ini Pengertian dan Cara Pasangnya di HP Kalian
  • Apa Itu Aplikasi Dooie Live? Ini Pengertian dan Cara Pakai Aplikasinya untuk Cari Jodoh
  • Apa Itu Battle Emote Jefri Nichol dan Om Telolet Om di MLBB? Ini Penjelasannya
  • Apa itu Game Luna Mobile dan Bagaimana Cara Menangnya?
  • Apa Itu Kompensasi Sistem Trail Mobile Legends? Ini Penjelasan dan Cara Klaim Hadiahnya
  • Apa Itu Update Google Pixel 2 Desember 2025? Ini Penjelasannya!
  • Ini Cara Reset Desil di Aplikasi Cek Bansos Biar Valid (Update Januari 2026)
  • Apa Itu EZNET Wireless dan Fiber Optic? Ini Perbedaan dan Pengertian Lengkapnya
  • Pengertian Rework Magic Wheel dan Rank Mythic Eternal: Apa itu Perubahan Sistem Baru Mobile Legends?
  • Apa Itu Diamond Combo? Pengertian Game Puzzle Viral yang Katanya Bisa Hasilkan Cuan
  • Apa Itu Showbox? Pengertian, Fungsi, dan Cara Menggunakannya di Android
  • Cara Mengatasi Fitur Monet Facebook Pro Tiba-tiba Hilang
  • Google Bikin Kejutan! Pixel 10 Diskon Gila-gilaan di YouTube Premium
  • Apa Itu Google CC? Ini Pengertian Agen Produktivitas AI Eksperimental Terbaru
  • Apa Itu Ultras Seblak di eSport? Pengertian dan Fenomena Baru Suporter eSport
  • Android 16: Animasi Folder Baru yang Mengubah Cara Kita Berinteraksi!
  • Android 16: Notifikasi Lokasi ‘Blue Dot’ – Fitur Baru yang Perlu Kalian Ketahui!
  • Apa Itu Risiko Auto Click di Event Spongebob Mobile Legends? Ini Penjelasannya
  • Apa Itu Fitur Eksperimental Windows? Ini Pengertian dan Cara Menonaktifkannya
  • Apa Itu Android 16 Beta 1? Ini Pengertian dan Fitur Terbarunya
  • Belum Tahu? Ini Trik Supaya Bisa Dapat Skin Patrick Mobile Legends dengan Harga Murah
  • Pixel Desember 2025: Update Besar Siap Meluncur, Apa yang Baru?
  • Apa itu Cosmic Desktop: Pengertian dan Cara Pasangnya di Ubuntu 26.04?
  • Apa Itu Auvidea X242? Pengertian Carrier Board Jetson T5000 dengan Dual 10Gbe
  • Elementary OS 8.1 Resmi Rilis: Kini Pakai Wayland Secara Standar!
  • Apa Itu Raspberry Pi Imager? Pengertian dan Pembaruan Versi 2.0.3 yang Wajib Kalian Tahu
  • Performa Maksimal! Ini Cara Manual Update Ubuntu ke Linux Kernel 6.18 LTS
  • Apa Itu Elestio VibeCoder? Ini Pengertian dan Penjelasan Lengkapnya
  • Apa Itu Elestio Get A Team? Ini Pengertian Karyawan AI Digital
  • Apa itu RunPod? Ini Pengertian dan Tutorial Cara Deploy Pod Pertamamu
  • Apa Itu Migrasi Pod di RunPod? Ini Pengertian dan Cara Kerjanya
  • Loading Model AI Lama? Coba Fitur Cached Models RunPod Ini, Hemat Waktu & Biaya!
  • Gila! 574 Penjahat Siber Diciduk Interpol di Afrika, Kok Bisa Jaringannya Segede Ini?
  • Apa Itu Regulasi Drone Asing FAA? Ini Pengertian dan Implikasinya
  • Apa Itu Insiden Data Breach Nissan? Ini Kronologi dan Penjelasannya
  • Apa Itu Skandal Instacart? Pengertian Dark Pattern dan Refund 940 Miliar
  • Apa Itu Kerentanan UEFI? Pengertian Celah Keamanan DMA pada Booting Awal
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

©2025 emka.web.id | Design: Newspaperly WordPress Theme