Halo, kalau kamu merasa tulisan saya ngebantu, kamu bisa ucapkan terima kasih lewat saweria .

If you feel this website help you, you can donate at saweria .



Artikel ini adalah versi update dari artikel blog DO How To Set Up Highly Available Web Servers with Keepalived and Reserved IPs on Ubuntu 14.04 yang sudah lama tidak di update

Tools yang Digunakan

  1. 2 Server Ubuntu 22.04.2 LTS (1 sebagai primary HA, 1 lagi backup HA)

  2. Nginx v1.22.1

  3. Keepalived v2.2.4

  4. 1 DigitalOcean reserved IP

  5. DigitalOcean Personal Access Token (PAT)

    Buat PAT di DigitalOcean

    Buat PAT di DigitalOcean

Catatan:

  1. Nginx dan keepalived di install menggunakan apt install

How To

  1. Karena kita bakal pake virtual block default, di server primary HA, ganti isi file /var/www/html/index.nginx-debian.html dengan hanya

    <h1>primary</h1>
    
  2. Pada server backup HA, ganti isi file /var/www/html/index.nginx-debian.html dengan hanya

    <h1>secondary</h1>
    
  3. Buat file /etc/keepalived/keepalived.conf di server primary HA

    vrrp_script chk_nginx {
    script "/usr/bin/pgrep nginx"
    interval 2
    user root
    weight 2
    }
    
    vrrp_instance VI_1 {
        interface eth1
        state PRIMARY
        priority 101
    
        virtual_router_id 33
        unicast_src_ip isi_dengan_ip_private_primary_HA
        unicast_peer {
            isi_dengan_ip_private_backup_HA
        }
    
        authentication {
            auth_type PASS
            auth_pass isi_sama_password
        }
    
        track_script {
            chk_nginx
        }
    
        notify /etc/keepalived/change_ip.sh root
    
    }
    
  4. Buat file /etc/keepalived/keepalived.conf di server backup HA

    vrrp_script chk_nginx {
    script "/usr/bin/pgrep nginx"
    interval 2
    user root
    weight 2
    }
    
    vrrp_instance VI_1 {
        interface eth1
        state PRIMARY
        priority 100
    
        virtual_router_id 33
        unicast_src_ip isi_dengan_ip_private_backup_HA
        unicast_peer {
            isi_dengan_ip_private_primary_HA
        }
    
        authentication {
            auth_type PASS
            auth_pass isi_sama_password
        }
    
        track_script {
            chk_nginx
        }
    
        notify /etc/keepalived/change_ip.sh root
    
    }
    

    Perhatikan,

    a. priority di backup lebih rendah dari setup di primary

    b. Jika nginx mati (tidak ditemnukan PID nya via /usr/bin/pgrep nginx), maka keepalived akan menjalankan script /etc/keepalived/change_ip.sh

    c. script "/usr/bin/pgrep nginx", command untuk mendapatkan PID nginx di server

  5. Download file untuk assign-ip otomatis via DO API

    cd /usr/local/bin
    sudo curl -LO http://do.co/assign-ip
    
  6. Buat 1 file baru etc/keepalived/change_ip.sh, file ini akan mengganti reserved IP ke primary/backup

    #!/bin/sh
    export DO_TOKEN='dop_v1'
    IP='Your Reserved IP'
    ID=$(curl -s http://169.254.169.254/metadata/v1/id)
    HAS_RESERVED_IP=$(curl -s http://169.254.169.254/metadata/v1/reserved_ip/ipv4/active)
    
    if [ $HAS_RESERVED_IP = "false" ]; then
        n=0
        while [ $n -lt 10 ]
        do
            python3 /usr/local/bin/assign-ip $IP $ID && break
            n=$((n+1))
            sleep 3
        done
    fi
    

    Buat file itu executable sudo chmod +x /etc/keepalived/change_ip.sh

  7. Test dengan mematikan service nginx di server primary

    Perhatikan, page berubah dari primary menjadi default ubuntu page (sorry, lupa ganti ke secondary) saat nginx di server primary dimatikan


Sumber:

  1. How To Set Up Highly Available Web Servers with Keepalived and Reserved IPs on Ubuntu 14.04