Konfigurasi HAProxy Menggunakan Ansible

Setup Load Balancing menggunakan HAProxy dengan 2 Web Server Nginx, dan Bantuan Vagrant untuk Provision VM

Bahan-bahan

Siapkan file template terlebih dahulu, saya sudah membuatnya. https://github.com/ryanrzky/ansible-haproxy/tree/master/template

Langkah 1 – Buat Vagrantfile

Buat Vagrantfile, untuk Provision 3 VM, 1 untuk HAProxy dan 2 Untuk Web Server (Nginx), dengan OS Ubuntu 18. Di dalam Vagrantfile, saya menambahkan script untuk ganti password root, update repository dan install aplikasi sshpas.

IMAGE_NAME = "ubuntu/bionic64"
N = 3

$script = <<-SCRIPT
echo -e "root123\nroot123" | sudo passwd root
sudo apt update
sudo apt install sshpass -y
SCRIPT

Vagrant.configure("2") do |config|
    config.ssh.insert_key = false

    config.vm.provision "shell", inline: $script

    config.vm.provider "virtualbox" do |v|
        v.memory = 512
        v.cpus = 1
    end
      
    (1..N).each do |i|
        config.vm.define "node-#{i}" do |node|
            node.vm.box = IMAGE_NAME
            node.vm.network "private_network", ip: "192.168.99.#{i + 10}"
            node.vm.hostname = "node-#{i}"
        end
    end
end

Langkah 2 – Siapkan file host.yaml

Untuk IP dan password Server, sesuaikan dengan kondisi.

all:
    vars:
        ansible_connection: ssh
        ansible_user: root
        ansible_ssh_pass: root123
        ansible_ssh_extra_args: '-o StrictHostKeyChecking=no'
    children:
        haproxy:
            hosts:
              192.168.99.11:
        webserver:
            children:
                webserver1:
                    hosts:
                      192.168.99.12:
                webserver2:
                    hosts:
                      192.168.99.13:

Langkah 3 – Siapkan file Playbook Ansible

---
    - hosts: all
      tasks:
        - name: update system
          apt:
           update_cache: yes

    - hosts: haproxy
      tasks:
        - name: install haproxy
          apt:
            name: haproxy
            state: latest
        - name: copy file configuration haproxy
          template:
            src: ./template/haproxy/haproxy.cfg.j2
            dest: /etc/haproxy/haproxy.cfg
            backup: yes
            owner: root
            group: root
            mode: '0644'
        - name: start haproxy
          service:
            name: haproxy
            state: restarted

    - hosts: webserver
      tasks:
        - name: install nginx
          apt:
            name: nginx
            state: latest

    - hosts: webserver1
      tasks:
        - name: copy file default page webserver1
          template:
            src: ./template/webserver1/index.html.j2
            dest: /var/www/html/index.nginx-debian.html
            owner: www-data
            group: www-data
            mode: '0755'

    - hosts: webserver2
      tasks:
        - name: copy file default page webserver2
          template:
            src: ./template/webserver2/index.html.j2
            dest: /var/www/html/index.nginx-debian.html
            owner: www-data
            group: www-data
            mode: '0755'
...

Langkah 4 – Jalankan Playbook Ansible

ansible-playbook -i hosts.yml haproxy-nginx-install.yml (sesuai nama playbook.yml)

Semua sudah saya upload di repository Github saya.

https://github.com/ryanrzky/ansible-haproxy

Selamat mencoba.

Default image
Ryan Rizky Diantoro
Saya seorang Linux System Administrator yang berasal dari Jepara

Leave a Reply