Home Server Cara Install HAProxy 2.1-stable di Debian 10

Cara Install HAProxy 2.1-stable di Debian 10

388
0
Cara Install HAProxy 1.9.7 di Debian 9
Cara Install HAProxy 1.9.7 di Debian 9
Cara Install HAProxy 2.1-Stable di Debian 10

HAProxy 2.1-Stable atau High Availability Proxy versi 2.1-Stable adalah program under linux yang digunakan sebagai load balancer di cloud.

HAProxy 2.1-Stable di install untuk membagi beban server ke beberapa server atau vps. Load-balancer server juga dikenal dengan Front End Server.

Secara umum load balancer server digunakan agar server yang digunakan menjadi High Availability sehingga menjadikannya zero down time.

Cara install HAProxy 2.1-Stable di Debian 10 caranya hampir sama dengan Cara Install HAProxy 1.9.7 di Debian 9 yang telah dijelaskan sebelumnya.

Layer 4 Load Balancing
Layer 4 Load Balancing

Sebagai contoh kita mempunyai website dengan trafik yang tinggi:

  • Server A: loadbalancer.content.id dengan IP Address 10.10.1.1
  • Server B: loadbalancer.content.id dengan IP Address 10.10.2.1

Dengan Load Balancer, beban server akan dibagi secara merata ke kedua server diatas. Jadi Load Balancer bekerja membagi trafik yang tinggi pada website di pecah menjadi dua dan dibagi ke masing-masing load balancer.

Jadi beban server yang sebelumnya full jika menggunakan 1 server akan dibagi menjadi 2 dan masing-masing server akan mendapatkan 1/2 dari beban server sebelumnya.

Disamping itu HAProxy bisa juga digunakan sebagai port forwarder. Misalnya jika kita memiliki VPS dibelakang HAProxy dengan IP NAT (tidak memiliki public IP), HAProxy bisa digunakan sebagai port forwarder misalnya port 3888 di forward ke port 22 VPS NAT.

Cara Mudah Install HAProxy 2.1-Stable di Debian 10

Prerequerment:

1. 2 VPS atau lebih. 1 sebagai HAProxy, 1 sebagai Web Server. (Jika belum memiliki VPS baca: cara Mendapatkan Balance $100 Digital Ocean dan Cara Mendapatkan Balance $50 dari Vultr dan Buat VPS Anda Selama 10 Bulan

2. PuTTY untuk akses VPS.

3. Koneksi Internet.

Cara Insatall HAProxy 2.1-Stable di Debian 10

Step 1. Login ke VPS dengan akses root.

Step 2. Ketik perintah berikut di terminal:

curl https://haproxy.debian.net/bernat.debian.org.gpg | \
      apt-key add -
root@sgdoc:~# curl https://haproxy.debian.net/bernat.debian.org.gpg | \
>       apt-key add -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  106k  100  106k    0     0  81722      0  0:00:01  0:00:01 --:--:-- 81736
OK

Step 3. Tambahkan repository HAProxy dengan perintah berikut

echo deb http://haproxy.debian.net stretch-backports-1.9 main | \
      tee /etc/apt/sources.list.d/haproxy.list
root@sgdoc:~# echo deb http://haproxy.debian.net stretch-backports-2.1 main | \
>       tee /etc/apt/sources.list.d/haproxy.list
deb http://haproxy.debian.net stretch-backports-2.1 main

Step 4. Lakukan update dengan perintah

apt-get update
root@sgdoc:~# apt update
Hit:1 http://security.debian.org/debian-security stretch/updates InRelease
Ign:2 http://ftp.us.debian.org/debian stretch InRelease
Get:3 http://haproxy.debian.net stretch-backports-1.9 InRelease [4,593 B]
Hit:4 http://ftp.us.debian.org/debian stretch-updates InRelease
Get:5 http://haproxy.debian.net stretch-backports-1.9/main amd64 Packages [1,454 B]
Hit:6 http://ftp.us.debian.org/debian stretch Release
Fetched 6,047 B in 0s (6,377 B/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.

Step 5. Lakukan instalasi HAProxy

apt-get install haproxy=2.1.\*
root@sgdoc:~# apt-get install haproxy=2.1.\*
Reading package lists... Done
Building dependency tree
Reading state information... Done
Selected version '2.1~bpo9+1' (HAProxy 2.1:stretch-backports-2.1 [amd64]) for 'haproxy'
The following additional packages will be installed:
  liblua5.3-0 libpcre2-8-0
Suggested packages:
  vim-haproxy haproxy-doc
The following NEW packages will be installed:
  haproxy liblua5.3-0 libpcre2-8-0
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,789 kB of archives.
After this operation, 4,070 kB of additional disk space will be used.
Do you want to continue? [Y/n]

Contoh Konfigurasi HAProxy sebagai Load Balancer:

Berikut contoh HAProxy sebagai Load Balancer:

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# 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     2000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
    tune.ssl.default-dh-param 2048
#---------------------------------------------------------------------
# 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                 4000

#####################################################################################
#                                    HAPROXY Status                                 #
#####################################################################################

        stats enable
        stats auth contentid:Content123!
        stats uri /haproxyStats
        option forwardfor
        option http-server-close

#####################################################################################
#                                  HAPROXY Frontend                                 #
#####################################################################################

frontend www-http-https
         bind :80
         bind :443 ssl crt-list /etc/ssl/list/list.txt
         acl letsencrypt-acl path_beg /.well-known/acme-challenge/
         use_backend letsencrypt-backend if letsencrypt-acl

#####################################################################################
#                                 VHOST HAPROXY ACL                                 #
#####################################################################################

acl host_varnish hdr_dom(host) -i content.id ssl crt /etc/ssl/content.id.pem

#####################################################################################
#                             VHOST HAPROXY USE BACKEND                             #
#####################################################################################

use_backend varnish if host_varnish

#--------------------------------------------------------------------
# Haproxy Backend Server
#-------------------------------------------------------------------

backend varnish
        balance leastconn
        option httpclose
        cookie JSESSIONID prefix
        server Rama-01 10.10.1.4:6081 cookie A check #Varnish-01
        server Rama-02 10.10.1.5:6081 cookie A check #Varnish-02

#Backup jika Varnish-01 dan Varnish-02 Down gunakan webserver sebagai backup
        server WebHosting 10.10.1.103:80 check backup 

Contoh HAProxy sebagai Port Forwarder

Tambahkan baris berikut pada bagian Frontend

frontend ssh-rama-01
         bind :33801
         server ssh-rama-01 10.10.1.4:22

frontend ssh-rama-02
         bind :33802
         server ssh-rama-01 10.10.1.5:22

frontend ssh-rama-03
         bind :33803
         server WebHosting 10.10.1.103:22

Terima Kasih

Salam